fix(instructeurs/avis#revive): ArgumentError on AvisMailer.avis_invitation

This commit is contained in:
Martin 2022-06-13 13:44:06 +02:00 committed by mfo
parent 011fcd40d8
commit 8b487c847b
4 changed files with 10 additions and 7 deletions

View file

@ -48,8 +48,7 @@ module CreateAvisConcern
persisted.each do |avis|
avis.dossier.demander_un_avis!(avis)
if avis.dossier == dossier
targeted_link = TargetedUserLink.create!(target_context: 'avis', target_model_type: Avis.name, target_model_id: avis.id, user: avis.expert.user)
AvisMailer.avis_invitation(avis, targeted_link).deliver_later
AvisMailer.avis_invitation(avis).deliver_later
sent_emails_addresses << avis.expert.email
# the email format is already verified, we update value to nil
avis.update_column(:email, nil)

View file

@ -4,8 +4,13 @@ class AvisMailer < ApplicationMailer
layout 'mailers/layout'
def avis_invitation(avis, targeted_user_link)
def avis_invitation(avis, targeted_user_link = nil) # ensure re-entrance if existing AvisMailer.avis_invitation in queue
if avis.dossier.visible_by_administration?
targeted_user_link = avis.targeted_user_links
.find_or_create_by(target_context: 'avis',
target_model_type: Avis.name,
target_model_id: avis.id,
user: avis.expert.user)
@avis = avis
email = @avis.expert&.email
@url = targeted_user_link_url(targeted_user_link)

View file

@ -285,7 +285,6 @@ describe Experts::AvisController, type: :controller do
it { expect(flash.alert).to eq(["toto.fr : Email n'est pas valide"]) }
it { expect(flash.notice).to eq("Une demande davis a été envoyée à titi@titimail.com") }
it { expect(Avis.count).to eq(old_avis_count + 1) }
it { expect(TargetedUserLink.where(target_model: Avis.joins(expert: :user).where(user: { email: 'titi@titimail.com' })).count).to eq(1) }
end
context 'when the previous avis is public' do

View file

@ -5,14 +5,14 @@ RSpec.describe AvisMailer, type: :mailer do
let(:dossier) { create(:dossier, :en_construction) }
let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: dossier.procedure) }
let(:avis) { create(:avis, dossier: dossier, claimant: claimant, experts_procedure: experts_procedure, introduction: 'intro') }
let(:targeted_user_link) { create(:targeted_user_link, target_context: :avis, target_model: avis, user: expert) }
subject { described_class.avis_invitation(avis.reload, targeted_user_link) }
subject { described_class.avis_invitation(avis.reload) }
it { expect(subject.subject).to eq("Donnez votre avis sur le dossier nº #{avis.dossier.id} (#{avis.dossier.procedure.libelle})") }
it { expect(subject.body).to have_text("Vous avez été invité par\r\n#{avis.claimant.email}\r\nà donner votre avis sur le dossier nº #{avis.dossier.id} de la démarche :\r\n#{avis.dossier.procedure.libelle}") }
it { expect(subject.body).to include(avis.introduction) }
it { expect(subject.body).to include(targeted_user_link_url(targeted_user_link)) }
it { expect(subject.body).to include(targeted_user_link_url(TargetedUserLink.where(target_model: avis).first)) }
it { expect { subject.body }.to change { TargetedUserLink.where(target_model: avis).count }.from(0).to(1) }
context 'when the dossier has been deleted before the avis was sent' do
before { dossier.update(hidden_by_user_at: 1.hour.ago) }