fix(dossier): do not send notifcations to deleted users

This commit is contained in:
Paul Chavard 2023-06-20 17:14:34 +01:00
parent 8d1dab919d
commit e509ff1717
3 changed files with 15 additions and 13 deletions

View file

@ -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

View file

@ -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

View file

@ -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')