From 8b487c847b5d7a2322616316d073c753b5572fb6 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 13 Jun 2022 13:44:06 +0200 Subject: [PATCH] fix(instructeurs/avis#revive): ArgumentError on AvisMailer.avis_invitation --- app/controllers/concerns/create_avis_concern.rb | 3 +-- app/mailers/avis_mailer.rb | 7 ++++++- spec/controllers/experts/avis_controller_spec.rb | 1 - spec/mailers/avis_mailer_spec.rb | 6 +++--- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index a3c1a006f..44d7249ef 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -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) diff --git a/app/mailers/avis_mailer.rb b/app/mailers/avis_mailer.rb index 02e00f28b..c24cd7ede 100644 --- a/app/mailers/avis_mailer.rb +++ b/app/mailers/avis_mailer.rb @@ -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) diff --git a/spec/controllers/experts/avis_controller_spec.rb b/spec/controllers/experts/avis_controller_spec.rb index c702f027a..655d21b63 100644 --- a/spec/controllers/experts/avis_controller_spec.rb +++ b/spec/controllers/experts/avis_controller_spec.rb @@ -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 d’avis 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 diff --git a/spec/mailers/avis_mailer_spec.rb b/spec/mailers/avis_mailer_spec.rb index 0e82e1234..88ce2beb3 100644 --- a/spec/mailers/avis_mailer_spec.rb +++ b/spec/mailers/avis_mailer_spec.rb @@ -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) }