Merge pull request #6825 from betagouv/fix-dossier-deletion

fix dossier deletion mailers
This commit is contained in:
Kara Diaby 2022-01-25 12:05:45 +01:00 committed by GitHub
commit edc8c9c8a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 14 additions and 74 deletions

View file

@ -84,22 +84,11 @@ class DossierMailer < ApplicationMailer
mail(to: to_email, subject: @subject)
end
def notify_deletion_to_user(deleted_dossier, to_email)
I18n.with_locale(deleted_dossier.user_locale) do
@subject = default_i18n_subject(dossier_id: deleted_dossier.dossier_id)
@deleted_dossier = deleted_dossier
def notify_en_construction_deletion_to_administration(dossier, to_email)
@subject = default_i18n_subject(dossier_id: dossier.id)
@dossier = dossier
mail(to: to_email, subject: @subject)
end
end
def notify_instructeur_deletion_to_user(deleted_dossier, to_email)
I18n.with_locale(deleted_dossier.user_locale) do
@subject = default_i18n_subject(libelle_demarche: deleted_dossier.procedure.libelle)
@deleted_dossier = deleted_dossier
mail(to: to_email, subject: @subject)
end
mail(to: to_email, subject: @subject)
end
def notify_deletion_to_administration(deleted_dossier, to_email)

View file

@ -774,7 +774,6 @@ class Dossier < ApplicationRecord
update(hidden_by_user_at: Time.zone.now, dossier_transfer_id: nil)
end
user_email = user_deleted? ? nil : user_email_for(:notification)
deleted_dossier = nil
transaction do
@ -797,14 +796,6 @@ class Dossier < ApplicationRecord
DossierMailer.notify_deletion_to_administration(deleted_dossier, email).deliver_later
end
end
if user_email.present?
if reason == :user_request
DossierMailer.notify_deletion_to_user(deleted_dossier, user_email).deliver_later
else
DossierMailer.notify_instructeur_deletion_to_user(deleted_dossier, user_email).deliver_later
end
end
end
end

View file

@ -3,6 +3,6 @@
%p= t(:hello, scope: [:views, :shared, :greetings])
%p
= t('.body', dossier_id: @deleted_dossier.dossier_id, procedure: @deleted_dossier.procedure.libelle)
= t('.body', dossier_id: @dossier.id, procedure: @dossier.procedure.libelle)
= render partial: "layouts/mailers/signature"

View file

@ -1,8 +0,0 @@
- content_for(:title, "#{@subject}")
%p= t(:hello, scope: [:views, :shared, :greetings])
%p
= t('.body_html', dossier_id: @deleted_dossier.dossier_id, libelle_demarche: @deleted_dossier.procedure.libelle, deleted_dossiers_link: dossiers_url(statut: 'dossiers-supprimes'))
= render partial: "layouts/mailers/signature"

View file

@ -1,11 +0,0 @@
fr:
dossier_mailer:
notify_deletion_to_user:
subject: Votre dossier nº %{dossier_id} a bien été supprimé
body: Votre dossier n° %{dossier_id} (%{procedure}) a bien été supprimé. Une trace de ce traitement sera conservée pour ladministration.
notify_instructeur_deletion_to_user:
subject: Votre dossier sur la démarche « %{libelle_demarche} » est supprimé
body_html: |
Afin de limiter la conservation de vos données personnelles, votre dossier n° %{dossier_id} concernant la démarche <b>« %{libelle_demarche} »</b> est <b>supprimé</b>.<br><br>
Cette suppression ne modifie pas le statut final (accepté, refusé ou sans suite) de votre dossier.<br><br>
Une trace de ce dossier est visible dans votre interface : <a href='%{deleted_dossiers_link}'>%{deleted_dossiers_link}</a>.

View file

@ -0,0 +1,5 @@
fr:
dossier_mailer:
notify_en_construction_deletion_to_administration:
subject: Le dossier nº %{dossier_id} a été supprimé à la demande de lusager
body: À la demande de lusager, le dossier n° %{dossier_id} (%{procedure}) a été supprimé.

View file

@ -766,7 +766,6 @@ describe Instructeurs::DossiersController, type: :controller do
before do
dossier.accepter!(instructeur: instructeur, motivation: "le dossier est correct")
dossier.update!(hidden_by_user_at: Time.zone.now.beginning_of_day.utc)
allow(DossierMailer).to receive(:notify_instructeur_deletion_to_user).and_return(double(deliver_later: nil))
subject
end
@ -775,10 +774,6 @@ describe Instructeurs::DossiersController, type: :controller do
expect(DossierOperationLog.where(dossier_id: dossier.id).last.operation).to eq('supprimer')
end
it 'send an email to the user' do
expect(DossierMailer).to have_received(:notify_instructeur_deletion_to_user).with(DeletedDossier.where(dossier_id: dossier.id).first, dossier.user.email)
end
it 'add a record into deleted_dossiers table' do
expect(DeletedDossier.where(dossier_id: dossier.id).count).to eq(1)
expect(DeletedDossier.where(dossier_id: dossier.id).first.revision_id).to eq(dossier.revision_id)
@ -794,7 +789,6 @@ describe Instructeurs::DossiersController, type: :controller do
context 'when the instructeur want to delete a dossier with a decision and not hidden by user' do
before do
dossier.accepter!(instructeur: instructeur, motivation: "le dossier est correct")
allow(DossierMailer).to receive(:notify_instructeur_deletion_to_user).and_return(double(deliver_later: nil))
subject
end
@ -803,10 +797,6 @@ describe Instructeurs::DossiersController, type: :controller do
expect(DossierOperationLog.where(dossier_id: dossier.id).last.operation).not_to eq('supprimer')
end
it 'does not send an email to the user' do
expect(DossierMailer).not_to have_received(:notify_instructeur_deletion_to_user).with(DeletedDossier.where(dossier_id: dossier.id).first, dossier.user.email)
end
it 'add a record into deleted_dossiers table' do
expect(DeletedDossier.where(dossier_id: dossier.id).count).to eq(0)
end

View file

@ -1008,7 +1008,6 @@ describe Users::DossiersController, type: :controller do
shared_examples_for "the dossier can not be deleted" do
it "doesnt notify the deletion" do
expect(DossierMailer).not_to receive(:notify_deletion_to_administration)
expect(DossierMailer).not_to receive(:notify_deletion_to_user)
subject
end
@ -1024,7 +1023,6 @@ describe Users::DossiersController, type: :controller do
it "notifies the user and the admin of the deletion" do
expect(DossierMailer).to receive(:notify_deletion_to_administration).with(kind_of(DeletedDossier), dossier.procedure.administrateurs.first.email).and_return(double(deliver_later: nil))
expect(DossierMailer).to receive(:notify_deletion_to_user).with(kind_of(DeletedDossier), dossier.user.email).and_return(double(deliver_later: nil))
subject
end

View file

@ -60,16 +60,11 @@ RSpec.describe DossierMailer, type: :mailer do
it { expect(subject.perform_deliveries).to be_falsy }
end
describe '.notify_deletion_to_user' do
let(:deleted_dossier) { build(:deleted_dossier) }
def notify_deletion_to_administration(deleted_dossier, to_email)
@subject = default_i18n_subject(dossier_id: deleted_dossier.dossier_id)
@deleted_dossier = deleted_dossier
subject { described_class.notify_deletion_to_user(deleted_dossier, to_email) }
it { expect(subject.subject).to eq("Votre dossier nº #{deleted_dossier.dossier_id} a bien été supprimé") }
it { expect(subject.body).to include("Votre dossier") }
it { expect(subject.body).to include(deleted_dossier.dossier_id) }
it { expect(subject.body).to include("a bien été supprimé") }
it { expect(subject.body).to include(deleted_dossier.procedure.libelle) }
mail(to: to_email, subject: @subject)
end
describe '.notify_deletion_to_administration' do

View file

@ -45,10 +45,6 @@ class DossierMailerPreview < ActionMailer::Preview
DossierMailer.notify_brouillon_deletion(dossier_hashes, usager_email)
end
def notify_deletion_to_user
DossierMailer.notify_deletion_to_user(deleted_dossier, usager_email)
end
def notify_instructeur_deletion_to_user
DossierMailer.notify_instructeur_deletion_to_user(deleted_dossier, usager_email)
end

View file

@ -799,7 +799,6 @@ describe Dossier do
let(:reason) { :user_request }
before do
allow(DossierMailer).to receive(:notify_deletion_to_user).and_return(double(deliver_later: nil))
allow(DossierMailer).to receive(:notify_deletion_to_administration).and_return(double(deliver_later: nil))
end
@ -835,10 +834,6 @@ describe Dossier do
expect(deleted_dossier.deleted_at).to be_present
end
it 'notifies the user' do
expect(DossierMailer).to have_received(:notify_deletion_to_user).with(deleted_dossier, dossier.user.email)
end
it 'records the operation in the log' do
expect(last_operation.operation).to eq("supprimer")
expect(last_operation.automatic_operation?).to be_falsey