Skip to content

Commit

Permalink
send email to tiers if notification by email is set
Browse files Browse the repository at this point in the history
  • Loading branch information
lisa-durand authored and kara22 committed Nov 27, 2023
1 parent 832cb34 commit de599a7
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/mailers/notification_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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(".","&#8288;.").html_safe}
57 changes: 57 additions & 0 deletions spec/controllers/instructeurs/dossiers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
34 changes: 34 additions & 0 deletions spec/factories/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/individual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@
prenom { nil }
birthdate { nil }
end

trait :with_notification do
notification_method { :email }
email { '[email protected]' }
end

trait :without_notification do
notification_method { :no_notification }
end
end
end
4 changes: 4 additions & 0 deletions spec/mailers/previews/notification_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit de599a7

Please sign in to comment.