diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 88e1930a3..0b70194ef 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -314,11 +314,7 @@ module Users if @commentaire.errors.empty? @commentaire.dossier.update!(last_commentaire_updated_at: Time.zone.now) - dossier.followers_instructeurs - .with_instant_email_message_notifications - .each do |instructeur| - DossierMailer.notify_new_commentaire_to_instructeur(dossier, instructeur.email).deliver_later - end + flash.notice = t('.message_send') redirect_to messagerie_dossier_path(dossier) else diff --git a/app/mailers/avis_mailer.rb b/app/mailers/avis_mailer.rb index 49a8b14e0..1efa8116f 100644 --- a/app/mailers/avis_mailer.rb +++ b/app/mailers/avis_mailer.rb @@ -20,6 +20,17 @@ class AvisMailer < ApplicationMailer end end + # i18n-tasks-use t("avis_mailer.#{action}.subject") + def notify_new_commentaire_to_expert(dossier, avis, expert) + I18n.with_locale(dossier.user_locale) do + @dossier = dossier + @avis = avis + @subject = default_i18n_subject(dossier_id: dossier.id, libelle_demarche: dossier.procedure.libelle) + + mail(to: expert.email, subject: @subject) + end + end + def self.critical_email?(action_name) false end diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index b461170dc..72c25e0d6 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -97,6 +97,8 @@ class Commentaire < ApplicationRecord # - Otherwise, a instructeur posted a commentaire, we need to notify the user if sent_by_instructeur? || sent_by_expert? notify_user(wait: 5.minutes) + elsif !sent_by_system? + notify_administration end end @@ -108,6 +110,26 @@ class Commentaire < ApplicationRecord end end + def notify_administration + dossier.followers_instructeurs + .with_instant_email_message_notifications + .find_each do |instructeur| + DossierMailer.notify_new_commentaire_to_instructeur(dossier, instructeur.email).deliver_later + end + + experts_contactes = Set.new + + dossier.avis.includes(:expert).find_each do |avis| + if avis.expert.present? + expert_id = avis.expert.id + if !experts_contactes.include?(expert_id) + AvisMailer.notify_new_commentaire_to_expert(dossier, avis, avis.expert).deliver_later + experts_contactes.add(expert_id) + end + end + end + end + def messagerie_available? return if sent_by_system? if dossier.present? && !dossier.messagerie_available? diff --git a/app/views/avis_mailer/notify_new_commentaire_to_expert.html.haml b/app/views/avis_mailer/notify_new_commentaire_to_expert.html.haml new file mode 100644 index 000000000..d3630adc7 --- /dev/null +++ b/app/views/avis_mailer/notify_new_commentaire_to_expert.html.haml @@ -0,0 +1,9 @@ +- content_for(:title, "#{@subject}") + +%p= t(:hello, scope: [:views, :shared, :greetings]) + +%p + = t('.body', dossier_id: @dossier.id, libelle_demarche: @dossier.procedure.libelle) +%p= link_to("Messagerie du dossier n°#{@dossier.id}", messagerie_expert_avis_url(procedure_id: @dossier.procedure.id, id: @avis.id)) + += render partial: "layouts/mailers/signature" diff --git a/config/locales/views/avis_mailer/notify_new_commentaire_to_expert/en.yml b/config/locales/views/avis_mailer/notify_new_commentaire_to_expert/en.yml new file mode 100644 index 000000000..ab5cdacfc --- /dev/null +++ b/config/locales/views/avis_mailer/notify_new_commentaire_to_expert/en.yml @@ -0,0 +1,5 @@ +en: + avis_mailer: + notify_new_commentaire_to_expert: + subject: New comment posted on the file n°%{dossier_id} + body: A new comment has been submitted by the user on file number %{dossier_id} on the file %{libelle_demarche} diff --git a/config/locales/views/avis_mailer/notify_new_commentaire_to_expert/fr.yml b/config/locales/views/avis_mailer/notify_new_commentaire_to_expert/fr.yml new file mode 100644 index 000000000..83bc1105f --- /dev/null +++ b/config/locales/views/avis_mailer/notify_new_commentaire_to_expert/fr.yml @@ -0,0 +1,5 @@ +fr: + avis_mailer: + notify_new_commentaire_to_expert: + subject: Nouveau commentaire déposé sur le dossier n°%{dossier_id} + body: Un nouveau commentaire a été déposé par l’usager sur le dossier n° %{dossier_id} de la démarche %{libelle_demarche} diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 196507157..b582dbba3 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -1179,6 +1179,24 @@ describe Users::DossiersController, type: :controller do end end + context 'email notification to experts' do + let(:expert) { create(:expert) } + let(:experts_procedure) { create(:experts_procedure, expert: expert, procedure: procedure) } + let(:avis) { create(:avis, dossier: dossier, claimant: instructeur_with_instant_message, experts_procedure: experts_procedure) } + let(:avis2) { create(:avis, dossier: dossier, claimant: instructeur_with_instant_message, experts_procedure: experts_procedure) } + + before do + allow(AvisMailer).to receive(:notify_new_commentaire_to_expert).and_return(double(deliver_later: nil)) + avis + avis2 + subject + end + + it 'sends just one email to the expert linked to several avis on the same dossier' do + expect(AvisMailer).to have_received(:notify_new_commentaire_to_expert).with(dossier, avis, expert).once + end + end + context 'notification' do before 'instructeurs have no notification before the message' do expect(instructeur_with_instant_message.followed_dossiers.with_notifications).to eq([])