From e509ff17175d9665d44f7555bba86768641418af Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 20 Jun 2023 17:14:34 +0100 Subject: [PATCH] fix(dossier): do not send notifcations to deleted users --- app/mailers/dossier_mailer.rb | 11 ++++++++--- app/models/commentaire.rb | 2 +- .../instructeurs/dossiers_controller_spec.rb | 15 ++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 24ebeab43..854fae5e6 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -6,7 +6,7 @@ class DossierMailer < ApplicationMailer layout 'mailers/layout' default from: NO_REPLY_EMAIL - after_action :prevent_perform_deliveries, only: [:notify_new_draft, :notify_new_answer] + after_action :prevent_perform_deliveries, only: [:notify_new_draft, :notify_new_answer, :notify_pending_correction] def notify_new_draft @dossier = params[:dossier] @@ -46,7 +46,9 @@ class DossierMailer < ApplicationMailer end end - def notify_pending_correction(dossier) + def notify_pending_correction + commentaire = params[:commentaire] + dossier = commentaire.dossier I18n.with_locale(dossier.user_locale) do @dossier = dossier @service = dossier.procedure.service @@ -183,7 +185,10 @@ class DossierMailer < ApplicationMailer protected def prevent_perform_deliveries - if params[:commentaire]&.discarded? || params[:dossier]&.skip_user_notification_email? + commentaire = params[:commentaire] + dossier = commentaire&.dossier || params[:dossier] + + if commentaire&.discarded? || dossier&.skip_user_notification_email? mail.perform_deliveries = false end end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index a34c477c2..99e086747 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -114,7 +114,7 @@ class Commentaire < ApplicationRecord def notify_user(job_options = {}) if flagged_pending_correction? - DossierMailer.notify_pending_correction(dossier).deliver_later(job_options) + DossierMailer.with(commentaire: self).notify_pending_correction.deliver_later(job_options) else DossierMailer.with(commentaire: self).notify_new_answer.deliver_later(job_options) end diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index 74eeccb38..f359fb76e 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -509,23 +509,20 @@ describe Instructeurs::DossiersController, type: :controller do before do sign_in(instructeur.user) - - allow(DossierMailer).to receive(:notify_pending_correction) - .and_return(double(deliver_later: nil)) - expect(controller.current_instructeur).to receive(:mark_tab_as_seen).with(dossier, :messagerie) end + context "dossier en instruction sends an email to user" do + let(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure:) } + + it { expect { subject }.to have_enqueued_mail(DossierMailer, :notify_pending_correction) } + end + context "dossier en instruction" do let(:dossier) { create(:dossier, :en_instruction, :with_individual, procedure: procedure) } before { subject } - it 'sends an email to user' do - expect(DossierMailer).to have_received(:notify_pending_correction).once - expect(DossierMailer).to have_received(:notify_pending_correction).with(dossier) - end - it 'pass en_construction and create a pending correction' do expect(response).to have_http_status(:ok) expect(response.body).to include('en attente de correction')