From ae08044ff247d73b4e3e0d9a9d71000ff23ead79 Mon Sep 17 00:00:00 2001 From: Lisa Durand Date: Thu, 21 Mar 2024 15:03:20 +0100 Subject: [PATCH] create AR email and send it at the right time --- app/mailers/notification_mailer.rb | 12 +++++- app/models/concerns/dossier_state_concern.rb | 40 ++++++++++++++----- ...send_accuse_lecture_notification.html.haml | 23 +++++++++++ config/locales/views/layouts/mailers/fr.yml | 6 +++ .../instructeurs/dossiers_controller_spec.rb | 25 ++++++++++++ spec/factories/procedure.rb | 4 ++ spec/mailers/notification_mailer_spec.rb | 9 +++++ .../previews/notification_mailer_preview.rb | 4 ++ 8 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 app/views/notification_mailer/send_accuse_lecture_notification.html.haml diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 70f5b4091..4459e741d 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, except: [:send_notification_for_tiers] + before_action :set_dossier, except: [:send_notification_for_tiers, :send_accuse_lecture_notification] before_action :set_services_publics_plus, only: :send_notification helper ServiceHelper @@ -42,6 +42,16 @@ class NotificationMailer < ApplicationMailer mail(subject: @subject, to: @email, template_name: 'send_notification_for_tiers') end + def send_accuse_lecture_notification(dossier) + @dossier = dossier + @subject = "La décision a été rendue pour votre démarche #{@dossier.procedure.libelle.truncate_words(50)}" + @email = @dossier.user_email_for(:notification) + + @logo_url = procedure_logo_url(@dossier.procedure) + + mail(subject: @subject, to: @email, template_name: 'send_accuse_lecture_notification') + 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/concerns/dossier_state_concern.rb b/app/models/concerns/dossier_state_concern.rb index 908f2bbd6..2f400e4e3 100644 --- a/app/models/concerns/dossier_state_concern.rb +++ b/app/models/concerns/dossier_state_concern.rb @@ -120,8 +120,12 @@ module DossierStateConcern disable_notification = h.fetch(:disable_notification, false) if !disable_notification - NotificationMailer.send_accepte_notification(self).deliver_later - NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + if procedure.accuse_lecture? + NotificationMailer.send_accuse_lecture_notification(self).deliver_later + else + NotificationMailer.send_accepte_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + end end send_dossier_decision_to_experts(self) @@ -152,8 +156,12 @@ module DossierStateConcern end def after_commit_accepter_automatiquement - NotificationMailer.send_accepte_notification(self).deliver_later - NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + if procedure.accuse_lecture? + NotificationMailer.send_accuse_lecture_notification(self).deliver_later + else + 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) remove_titres_identite! @@ -184,8 +192,12 @@ module DossierStateConcern disable_notification = h.fetch(:disable_notification, false) if !disable_notification - NotificationMailer.send_refuse_notification(self).deliver_later - NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + if procedure.accuse_lecture? + NotificationMailer.send_accuse_lecture_notification(self).deliver_later + else + NotificationMailer.send_refuse_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + end end send_dossier_decision_to_experts(self) @@ -209,8 +221,12 @@ module DossierStateConcern end def after_commit_refuser_automatiquement - NotificationMailer.send_refuse_notification(self).deliver_later - NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + if procedure.accuse_lecture? + NotificationMailer.send_accuse_lecture_notification(self).deliver_later + else + 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) remove_titres_identite! @@ -241,8 +257,12 @@ module DossierStateConcern disable_notification = h.fetch(:disable_notification, false) if !disable_notification - NotificationMailer.send_sans_suite_notification(self).deliver_later - NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + if procedure.accuse_lecture? + NotificationMailer.send_accuse_lecture_notification(self).deliver_later + else + NotificationMailer.send_sans_suite_notification(self).deliver_later + NotificationMailer.send_notification_for_tiers(self).deliver_later if self.for_tiers? + end end send_dossier_decision_to_experts(self) diff --git a/app/views/notification_mailer/send_accuse_lecture_notification.html.haml b/app/views/notification_mailer/send_accuse_lecture_notification.html.haml new file mode 100644 index 000000000..58f1f4ba1 --- /dev/null +++ b/app/views/notification_mailer/send_accuse_lecture_notification.html.haml @@ -0,0 +1,23 @@ +- content_for :procedure_logo do + = render 'layouts/mailers/logo', url: @logo_url + +%p + = t("layouts.mailers.accuse_lecture.good_morning") + +%p + = t("layouts.mailers.accuse_lecture.first_part", + dossier_id: number_with_delimiter(@dossier.id)) + + %span{ :style => "font-weight: bold;" } + = @dossier.procedure.libelle + + = t("layouts.mailers.accuse_lecture.second_part") +%p + = t("layouts.mailers.accuse_lecture.third_part") + = link_to 'Démarches Simplifiées.', dossier_url(@dossier) + +%p + = t(:best_regards, scope: [:views, :shared, :greetings]) + %br + = t('layouts.mailers.signature.team') + #{APPLICATION_NAME.gsub(".","⁠.").html_safe} diff --git a/config/locales/views/layouts/mailers/fr.yml b/config/locales/views/layouts/mailers/fr.yml index 314d426f9..03d65b696 100644 --- a/config/locales/views/layouts/mailers/fr.yml +++ b/config/locales/views/layouts/mailers/fr.yml @@ -25,5 +25,11 @@ fr: accepte: a été accepté le %{processed_at}. refuse: a été refusé le %{processed_at}. sans_suite: a été classé sans suite le %{processed_at}. + accuse_lecture: + good_morning: Bonjour, + first_part: Nous vous informons qu'une décision sur le dossier nº %{dossier_id} de la démarche + second_part: a été rendue. + third_part: Pour en connaitre la nature, veuillez vous connecter à votre compte + commentaire_groupe_gestionnaire_footer: do_not_reply_html: Merci de ne pas répondre à cet email. Consultez votre message sur %{application_name} ou contactez votre expéditeur par mail diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index d64142354..9e3a99d76 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -6,7 +6,9 @@ describe Instructeurs::DossiersController, type: :controller do let(:administration) { create(:administration) } let(:instructeurs) { [instructeur] } let(:procedure) { create(:procedure, :published, :for_individual, instructeurs: instructeurs) } + let(:procedure_accuse_lecture) { create(:procedure, :published, :for_individual, :accuse_lecture, instructeurs: instructeurs) } let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure: procedure) } + let(:dossier_accuse_lecture) { create(:dossier, :en_construction, :with_individual, procedure: procedure_accuse_lecture) } 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') } @@ -376,6 +378,29 @@ describe Instructeurs::DossiersController, type: :controller do end end + context "with accuse de lecture procedure" do + before do + dossier_accuse_lecture.passer_en_instruction!(instructeur: instructeur) + sign_in(instructeur.user) + end + context 'with classer_sans_suite' do + subject { post :terminer, params: { process_action: "classer_sans_suite", procedure_id: procedure_accuse_lecture.id, dossier_id: dossier_accuse_lecture.id }, format: :turbo_stream } + + it 'Notification accuse de lecture email is sent and not the others' do + expect(NotificationMailer).to receive(:send_accuse_lecture_notification) + .with(dossier_accuse_lecture).and_return(NotificationMailer) + expect(NotificationMailer).to receive(:deliver_later) + + expect(NotificationMailer).not_to receive(:send_sans_suite_notification) + .with(dossier_accuse_lecture) + + subject + end + + it { expect(subject.body).to include('header-top') } + end + end + context "with classer_sans_suite" do before do dossier.passer_en_instruction!(instructeur: instructeur) diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 74785ebfa..6799dbeda 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -283,6 +283,10 @@ FactoryBot.define do referentiel_de_programmation: { c: 3 }) end end + + trait :accuse_lecture do + accuse_lecture { true } + end end end diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 831df8985..aa61447aa 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -29,6 +29,15 @@ RSpec.describe NotificationMailer, type: :mailer do end end + describe 'send_accuse_lecture_notification' do + let(:dossier) { create(:dossier, :en_construction, procedure: create(:procedure, :accuse_lecture)) } + + subject { described_class.send_accuse_lecture_notification(dossier) } + + it { expect(subject.subject).to include("La décision a été rendue pour votre démarche #{dossier.procedure.libelle}") } + it { expect(subject.body).to include("Pour en connaitre la nature, veuillez vous connecter à votre compte\r\ndemarches-simplifiees.fr") } + end + describe 'send_en_construction_notification' do let(:dossier) { create(:dossier, :en_construction, :with_individual, user: user, procedure:) } diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index 24b05836d..f16838ba5 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -23,6 +23,10 @@ class NotificationMailerPreview < ActionMailer::Preview NotificationMailer.send_notification_for_tiers(dossier) end + def send_accuse_lecture_notification + NotificationMailer.send_accuse_lecture_notification(dossier) + end + private def dossier