diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 6ebe4cec262..61be19fe366 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -6,7 +6,7 @@ # The subject and body of a Notification can be customized by each demarche. # class NotificationMailer < ApplicationMailer - before_action :set_dossier + before_action :set_dossier, except: [:send_notification_for_tiers] before_action :set_services_publics_plus, only: :send_notification helper ServiceHelper @@ -24,6 +24,21 @@ def send_notification end end + def send_notification_for_tiers(dossier) + @dossier = dossier + + if @dossier.individual.no_notification? + mail.perform_deliveries = false + return + end + + @subject = "Votre dossier rempli par le mandataire #{@dossier.mandataire_first_name} #{@dossier.mandataire_last_name} a été mis à jour" + @email = @dossier.individual.email + @logo_url = procedure_logo_url(@dossier.procedure) + + mail(subject: @subject, to: @email, template_name: 'send_notification_for_tiers') + end + def self.send_en_construction_notification(dossier) with(dossier: dossier, state: Dossier.states.fetch(:en_construction)).send_notification end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 8e062a62a5c..66158267992 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -884,6 +884,7 @@ def after_passer_en_construction save! MailTemplatePresenterService.create_commentaire_for_state(self) NotificationMailer.send_en_construction_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? procedure.compute_dossiers_count RoutingEngine.compute(self) end @@ -918,6 +919,7 @@ def after_passer_en_instruction(h) MailTemplatePresenterService.create_commentaire_for_state(self) if !disable_notification NotificationMailer.send_en_instruction_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end log_dossier_operation(instructeur, :passer_en_instruction) end @@ -934,6 +936,7 @@ def after_passer_automatiquement_en_instruction save! MailTemplatePresenterService.create_commentaire_for_state(self) NotificationMailer.send_en_instruction_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? if procedure.sva_svr_enabled? # TODO: handle serialization errors when SIRET demandeur was not completed @@ -1011,6 +1014,7 @@ def after_accepter(h) MailTemplatePresenterService.create_commentaire_for_state(self) if !disable_notification NotificationMailer.send_accepte_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end send_dossier_decision_to_experts(self) log_dossier_operation(instructeur, :accepter, self) @@ -1036,6 +1040,7 @@ def after_accepter_automatiquement remove_titres_identite! MailTemplatePresenterService.create_commentaire_for_state(self) NotificationMailer.send_accepte_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? log_automatic_dossier_operation(:accepter, self) end @@ -1061,6 +1066,7 @@ def after_refuser(h) if !disable_notification NotificationMailer.send_refuse_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end send_dossier_decision_to_experts(self) log_dossier_operation(instructeur, :refuser, self) @@ -1080,6 +1086,7 @@ def after_refuser_automatiquement remove_titres_identite! MailTemplatePresenterService.create_commentaire_for_state(self) NotificationMailer.send_refuse_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? log_automatic_dossier_operation(:refuser, self) end @@ -1105,6 +1112,7 @@ def after_classer_sans_suite(h) if !disable_notification NotificationMailer.send_sans_suite_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? end send_dossier_decision_to_experts(self) log_dossier_operation(instructeur, :classer_sans_suite, self) diff --git a/app/views/notification_mailer/send_notification_for_tiers.html.haml b/app/views/notification_mailer/send_notification_for_tiers.html.haml new file mode 100644 index 00000000000..aa839fa2ab4 --- /dev/null +++ b/app/views/notification_mailer/send_notification_for_tiers.html.haml @@ -0,0 +1,17 @@ +- content_for :procedure_logo do + = render 'layouts/mailers/logo', url: @logo_url + +%p + Bonjour, + +%p= "#{ t("instructeurs.dossiers.decisions_rendues_block.without_email.#{@dossier.state}", processed_at: l(@dossier.updated_at.to_date))}." + +%p= "Le dossier nº #{@dossier.id} est rempli en votre nom par le mandataire #{@dossier.mandataire_first_name} #{@dossier.mandataire_last_name} sur la procédure #{@dossier.procedure.libelle}." + +%p= "Pour en savoir plus, veuillez-vous rapprocher du mandataire #{@dossier.user.email}." + +%p + = t(:best_regards, scope: [:views, :shared, :greetings]) + %br + = t('layouts.mailers.signature.team') + #{APPLICATION_NAME.gsub(".","⁠.").html_safe} diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index f76083a1981..406e6253e84 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -7,6 +7,8 @@ let(:instructeurs) { [instructeur] } let(:procedure) { create(:procedure, :published, :for_individual, instructeurs: instructeurs) } let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure: procedure) } + let(:dossier_for_tiers) { create(:dossier, :en_construction, :for_tiers_with_notification, procedure: procedure) } + let(:dossier_for_tiers_without_notif) { create(:dossier, :en_construction, :for_tiers_without_notification, procedure: procedure) } let(:fake_justificatif) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } before { sign_in(instructeur.user) } @@ -333,6 +335,58 @@ end end + context "with for_tiers" do + before do + dossier_for_tiers.passer_en_instruction!(instructeur: instructeur) + sign_in(instructeur.user) + end + context 'without continuation' do + subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure.id, dossier_id: dossier_for_tiers.id }, format: :turbo_stream } + + it 'Notification email is sent' do + expect(NotificationMailer).to receive(:send_sans_suite_notification) + .with(dossier_for_tiers).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_later) + + expect(NotificationMailer).to receive(:send_notification_for_tiers) + .with(dossier_for_tiers).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_later) + + subject + end + + it '2 emails are sent' do + expect { perform_enqueued_jobs { subject } }.to change { ActionMailer::Base.deliveries.count }.by(2) + end + end + end + + context "with for_tiers_without_notif" do + before do + dossier_for_tiers_without_notif.passer_en_instruction!(instructeur: instructeur) + sign_in(instructeur.user) + end + context 'without continuation' do + subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure.id, dossier_id: dossier_for_tiers_without_notif.id }, format: :turbo_stream } + + it 'Notification email is sent' do + expect(NotificationMailer).to receive(:send_sans_suite_notification) + .with(dossier_for_tiers_without_notif).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_later) + + expect(NotificationMailer).to receive(:send_notification_for_tiers) + .with(dossier_for_tiers_without_notif).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_later) + + subject + end + + it 'only one email is sent' do + expect { perform_enqueued_jobs { subject } }.to change { ActionMailer::Base.deliveries.count }.by(1) + end + end + end + context "with classer_sans_suite" do before do dossier.passer_en_instruction!(instructeur: instructeur) @@ -354,6 +408,9 @@ .with(dossier).and_return(NotificationMailer) expect(NotificationMailer).to receive(:deliver_later) + expect(NotificationMailer).not_to receive(:send_notification_for_tiers) + .with(dossier) + subject end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index f6bf07516b8..f934364bf93 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -43,6 +43,40 @@ end end + trait :for_tiers_with_notification do + for_tiers { true } + mandataire_first_name { 'John' } + mandataire_last_name { 'Doe' } + + transient do + for_individual? { true } + end + + after(:build) do |dossier, _evaluator| + if !dossier.procedure.for_individual? + raise 'Inconsistent factory: attempting to create a dossier :with_individual on a procedure that is not `for_individual?`' + end + dossier.individual = build(:individual, :with_notification, dossier: dossier) + end + end + + trait :for_tiers_without_notification do + for_tiers { true } + mandataire_first_name { 'John' } + mandataire_last_name { 'Doe' } + + transient do + for_individual? { true } + end + + after(:build) do |dossier, _evaluator| + if !dossier.procedure.for_individual? + raise 'Inconsistent factory: attempting to create a dossier :with_individual on a procedure that is not `for_individual?`' + end + dossier.individual = build(:individual, :without_notification, dossier: dossier) + end + end + trait :with_individual do transient do for_individual? { true } diff --git a/spec/factories/individual.rb b/spec/factories/individual.rb index c1aa66c5ddd..df00d814744 100644 --- a/spec/factories/individual.rb +++ b/spec/factories/individual.rb @@ -12,5 +12,14 @@ prenom { nil } birthdate { nil } end + + trait :with_notification do + notification_method { :email } + email { 'julien.xavier@test.com' } + end + + trait :without_notification do + notification_method { :no_notification } + end end end diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index 04fdd078303..24b05836d25 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -19,6 +19,10 @@ def send_sans_suite_notification NotificationMailer.send_sans_suite_notification(dossier) end + def send_notification_for_tiers + NotificationMailer.send_notification_for_tiers(dossier) + end + private def dossier