From e5303fd9862142df6978dff789666d4e3693e53b Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 20 Nov 2018 10:50:25 +0000 Subject: [PATCH 1/4] mailers: streamline the NotificationMailer Notifications are now only for demarche-templated emails. --- app/mailers/dossier_mailer.rb | 14 +++++++++++ app/mailers/notification_mailer.rb | 23 ++++--------------- app/models/commentaire.rb | 2 +- app/models/dossier.rb | 2 +- .../notify_new_answer.html.haml} | 0 .../notify_new_draft.html.haml} | 0 spec/mailers/dossier_mailer_spec.rb | 21 +++++++++++++++++ spec/mailers/notification_mailer_spec.rb | 8 ------- .../previews/dossier_mailer_preview.rb | 8 +++++++ .../previews/notification_mailer_preview.rb | 4 ---- 10 files changed, 49 insertions(+), 33 deletions(-) rename app/views/{notification_mailer/new_answer.html.haml => dossier_mailer/notify_new_answer.html.haml} (100%) rename app/views/{notification_mailer/send_draft_notification.html.haml => dossier_mailer/notify_new_draft.html.haml} (100%) diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index 65d7e68e6..f9b27e3de 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -1,6 +1,20 @@ class DossierMailer < ApplicationMailer layout 'mailers/layout' + def notify_new_draft(dossier) + @dossier = dossier + subject = "Retrouvez votre brouillon pour la démarche \"#{dossier.procedure.libelle}\"" + + mail(to: dossier.user.email, subject: subject) + end + + def notify_new_answer(dossier) + @dossier = dossier + subject = "Nouveau message pour votre dossier nº #{dossier.id}" + + mail(to: dossier.user.email, subject: subject) + end + def notify_deletion_to_user(deleted_dossier, to_email) @deleted_dossier = deleted_dossier subject = "Votre dossier n° #{@deleted_dossier.dossier_id} a bien été supprimé" diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 0f4cd3ea9..d3f9252a9 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -1,16 +1,8 @@ +# A Notification is attached as a Comment to the relevant discussion, +# then sent by email to the user. +# +# The subject and body of a Notification can be customized by each demarche. class NotificationMailer < ApplicationMailer - def new_answer(dossier) - subject = "Nouveau message pour votre dossier nº #{dossier.id}" - - send_mail(dossier, subject) - end - - def send_draft_notification(dossier) - subject = "Retrouvez votre brouillon pour la démarche \"#{dossier.procedure.libelle}\"" - - send_mail(dossier, subject) - end - def send_dossier_received(dossier) send_notification(dossier, dossier.procedure.received_mail_template) end @@ -33,13 +25,6 @@ class NotificationMailer < ApplicationMailer private - def send_mail(dossier, subject) - @dossier = dossier - email = dossier.user.email - - mail(subject: subject, to: email) - end - def send_notification(dossier, mail_template) email = dossier.user.email diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 8b510b095..86869f455 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -48,7 +48,7 @@ class Commentaire < ApplicationRecord end def notify_user - NotificationMailer.new_answer(dossier).deliver_later + DossierMailer.notify_new_answer(dossier).deliver_later end def is_virus_free? diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fa3d11d7d..22f3cfe4c 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -356,7 +356,7 @@ class Dossier < ApplicationRecord def send_draft_notification_email if brouillon? - NotificationMailer.send_draft_notification(self).deliver_later + DossierMailer.notify_new_draft(self).deliver_later end end diff --git a/app/views/notification_mailer/new_answer.html.haml b/app/views/dossier_mailer/notify_new_answer.html.haml similarity index 100% rename from app/views/notification_mailer/new_answer.html.haml rename to app/views/dossier_mailer/notify_new_answer.html.haml diff --git a/app/views/notification_mailer/send_draft_notification.html.haml b/app/views/dossier_mailer/notify_new_draft.html.haml similarity index 100% rename from app/views/notification_mailer/send_draft_notification.html.haml rename to app/views/dossier_mailer/notify_new_draft.html.haml diff --git a/spec/mailers/dossier_mailer_spec.rb b/spec/mailers/dossier_mailer_spec.rb index 3ef6b018f..d98dfb4ea 100644 --- a/spec/mailers/dossier_mailer_spec.rb +++ b/spec/mailers/dossier_mailer_spec.rb @@ -3,6 +3,27 @@ require "rails_helper" RSpec.describe DossierMailer, type: :mailer do let(:to_email) { 'gestionnaire@exemple.gouv.fr' } + describe '.notify_new_draft' do + let(:dossier) { create(:dossier, procedure: build(:simple_procedure)) } + + subject { described_class.notify_new_draft(dossier) } + + it { expect(subject.subject).to include("brouillon") } + it { expect(subject.subject).to include(dossier.id.to_s) } + it { expect(subject.body).to include(dossier.procedure.libelle) } + it { expect(subject.body).to include(dossier_url(dossier)) } + end + + describe '.notify_new_answer' do + let(:dossier) { create(:dossier, procedure: build(:simple_procedure)) } + + subject { described_class.notify_new_answer(dossier) } + + it { expect(subject.subject).to include("Nouveau message") } + it { expect(subject.subject).to include(dossier.id.to_s) } + it { expect(subject.body).to include(messagerie_dossier_url(dossier)) } + end + describe '.notify_deletion_to_user' do let(:deleted_dossier) { build(:deleted_dossier) } diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 54af8f987..cbf919603 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -55,12 +55,4 @@ RSpec.describe NotificationMailer, type: :mailer do it_behaves_like "create a commentaire not notified" end - - describe ".new_answer" do - subject(:subject) { described_class.new_answer(dossier) } - - it { expect(subject.body).to match('Un nouveau message est disponible dans votre espace demarches-simplifiees.fr.') } - it { expect(subject.body).to include(messagerie_dossier_url(dossier)) } - it { expect(subject.subject).to eq("Nouveau message pour votre dossier nº #{dossier.id}") } - end end diff --git a/spec/mailers/previews/dossier_mailer_preview.rb b/spec/mailers/previews/dossier_mailer_preview.rb index c74b23ae2..de22920f6 100644 --- a/spec/mailers/previews/dossier_mailer_preview.rb +++ b/spec/mailers/previews/dossier_mailer_preview.rb @@ -1,5 +1,13 @@ # Preview all emails at http://localhost:3000/rails/mailers/dossier_mailer class DossierMailerPreview < ActionMailer::Preview + def notify_new_draft + DossierMailer.notify_new_draft(Dossier.last) + end + + def notify_new_answer + DossierMailer.notify_new_answer(Dossier.last) + end + def notify_deletion_to_user DossierMailer.notify_deletion_to_user(DeletedDossier.last, "user@ds.fr") end diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index a5349a255..cec2f0020 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -2,8 +2,4 @@ class NotificationMailerPreview < ActionMailer::Preview def send_notification NotificationMailer.send_initiated_notification(Dossier.last) end - - def send_draft_notification - NotificationMailer.send_draft_notification(Dossier.last) - end end From f2eaf66a9a7475c2a96f4bc15743afcadd5f2dfc Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 19 Nov 2018 18:14:35 +0100 Subject: [PATCH 2/4] mailers: add missing previews for notification emails --- .../previews/notification_mailer_preview.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index cec2f0020..af96ed00e 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -1,5 +1,21 @@ class NotificationMailerPreview < ActionMailer::Preview - def send_notification + def send_dossier_received + NotificationMailer.send_dossier_received(Dossier.last) + end + + def send_initiated_notification NotificationMailer.send_initiated_notification(Dossier.last) end + + def send_closed_notification + NotificationMailer.send_closed_notification(Dossier.last) + end + + def send_refused_notification + NotificationMailer.send_refused_notification(Dossier.last) + end + + def send_without_continuation_notification + NotificationMailer.send_without_continuation_notification(Dossier.last) + end end From a5f9be00e00f6f734d4d9e6f3cdc9f1edf250387 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 20 Nov 2018 10:57:51 +0000 Subject: [PATCH 3/4] mailers: add preview URL to all previewable mailers --- app/mailers/administrateur_mailer.rb | 1 + app/mailers/administration_mailer.rb | 1 + app/mailers/avis_mailer.rb | 1 + app/mailers/devise_user_mailer.rb | 1 + app/mailers/dossier_mailer.rb | 1 + app/mailers/gestionnaire_mailer.rb | 1 + app/mailers/invite_mailer.rb | 1 + app/mailers/new_attestation_mailer.rb | 1 + app/mailers/notification_mailer.rb | 3 +++ app/mailers/user_mailer.rb | 1 + spec/mailers/previews/user_mailer_preview.rb | 2 +- 11 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/mailers/administrateur_mailer.rb b/app/mailers/administrateur_mailer.rb index b31ab2633..55855db14 100644 --- a/app/mailers/administrateur_mailer.rb +++ b/app/mailers/administrateur_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/administrateur_mailer class AdministrateurMailer < ApplicationMailer layout 'mailers/layout' diff --git a/app/mailers/administration_mailer.rb b/app/mailers/administration_mailer.rb index 36bb5d477..fde506f40 100644 --- a/app/mailers/administration_mailer.rb +++ b/app/mailers/administration_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/administration_mailer class AdministrationMailer < ApplicationMailer layout 'mailers/layout' diff --git a/app/mailers/avis_mailer.rb b/app/mailers/avis_mailer.rb index 2580f4be6..b4ca982c1 100644 --- a/app/mailers/avis_mailer.rb +++ b/app/mailers/avis_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/avis_mailer class AvisMailer < ApplicationMailer def avis_invitation(avis) @avis = avis diff --git a/app/mailers/devise_user_mailer.rb b/app/mailers/devise_user_mailer.rb index faea78ec5..030c528c9 100644 --- a/app/mailers/devise_user_mailer.rb +++ b/app/mailers/devise_user_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/devise_user_mailer class DeviseUserMailer < Devise::Mailer helper :application # gives access to all helpers defined within `application_helper`. include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index f9b27e3de..a00e31d8e 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/dossier_mailer class DossierMailer < ApplicationMailer layout 'mailers/layout' diff --git a/app/mailers/gestionnaire_mailer.rb b/app/mailers/gestionnaire_mailer.rb index a3473f65f..09f7af238 100644 --- a/app/mailers/gestionnaire_mailer.rb +++ b/app/mailers/gestionnaire_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/gestionnaire_mailer class GestionnaireMailer < ApplicationMailer layout 'mailers/layout' diff --git a/app/mailers/invite_mailer.rb b/app/mailers/invite_mailer.rb index 0cf3752cb..7d0be51e6 100644 --- a/app/mailers/invite_mailer.rb +++ b/app/mailers/invite_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/invite_mailer class InviteMailer < ApplicationMailer def invite_user(invite) subject = "Participez à l'élaboration d'un dossier" diff --git a/app/mailers/new_attestation_mailer.rb b/app/mailers/new_attestation_mailer.rb index d8531cffe..b6ea9b78c 100644 --- a/app/mailers/new_attestation_mailer.rb +++ b/app/mailers/new_attestation_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/new_attestation_mailer class NewAttestationMailer < ApplicationMailer include Rails.application.routes.url_helpers diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index d3f9252a9..503bd4bf0 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -1,7 +1,10 @@ +# Preview all emails at http://localhost:3000/rails/mailers/notification_mailer + # A Notification is attached as a Comment to the relevant discussion, # then sent by email to the user. # # The subject and body of a Notification can be customized by each demarche. +# class NotificationMailer < ApplicationMailer def send_dossier_received(dossier) send_notification(dossier, dossier.procedure.received_mail_template) diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index f4cdab83d..2304d41fd 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,3 +1,4 @@ +# Preview all emails at http://localhost:3000/rails/mailers/user_mailer class UserMailer < ApplicationMailer layout 'mailers/layout' diff --git a/spec/mailers/previews/user_mailer_preview.rb b/spec/mailers/previews/user_mailer_preview.rb index 001799662..d2dfa3e70 100644 --- a/spec/mailers/previews/user_mailer_preview.rb +++ b/spec/mailers/previews/user_mailer_preview.rb @@ -1,4 +1,4 @@ -class UserPreview < ActionMailer::Preview +class UserMailerPreview < ActionMailer::Preview def new_account_warning UserMailer.new_account_warning(User.first) end From 38b5c77ad3e6c4b1eec5ef06dcfa3fcb1e9a8f4c Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 20 Nov 2018 11:07:39 +0000 Subject: [PATCH 4/4] mailers: use notification layout for 'new answer' email Fix #2224 --- app/mailers/dossier_mailer.rb | 4 +++- app/views/dossier_mailer/notify_new_answer.html.haml | 6 ------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/app/mailers/dossier_mailer.rb b/app/mailers/dossier_mailer.rb index a00e31d8e..2a9129e83 100644 --- a/app/mailers/dossier_mailer.rb +++ b/app/mailers/dossier_mailer.rb @@ -13,7 +13,9 @@ class DossierMailer < ApplicationMailer @dossier = dossier subject = "Nouveau message pour votre dossier nº #{dossier.id}" - mail(to: dossier.user.email, subject: subject) + mail(to: dossier.user.email, subject: subject) do |format| + format.html { render layout: 'mailers/notification' } + end end def notify_deletion_to_user(deleted_dossier, to_email) diff --git a/app/views/dossier_mailer/notify_new_answer.html.haml b/app/views/dossier_mailer/notify_new_answer.html.haml index 0fa7b860e..36b9d1071 100644 --- a/app/views/dossier_mailer/notify_new_answer.html.haml +++ b/app/views/dossier_mailer/notify_new_answer.html.haml @@ -13,9 +13,3 @@ %p L'équipe demarches-simplifiees.fr - -%p - — - -%p - Merci de ne pas répondre à cet email. Postez directement vos questions dans votre dossier sur la plateforme.