From e91b412254b807c945b2879badce52aa200e251a Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 14 Mar 2019 10:39:38 +0100 Subject: [PATCH 1/9] [Fix #3064] Use a different layout for state notifications emails --- .../layouts/mailers/notification.html.haml | 2 +- .../mailers/notifications_layout.html.erb | 180 ++++++++++++++++++ .../previews/notification_mailer_preview.rb | 3 +- 3 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 app/views/layouts/mailers/notifications_layout.html.erb diff --git a/app/views/layouts/mailers/notification.html.haml b/app/views/layouts/mailers/notification.html.haml index c71be0360..65d748cd1 100644 --- a/app/views/layouts/mailers/notification.html.haml +++ b/app/views/layouts/mailers/notification.html.haml @@ -4,4 +4,4 @@ = succeed '.' do = link_to 'messagerie', messagerie_dossier_url(@dossier), target: '_blank', rel: 'noopener' -= render template: 'layouts/mailers/layout' += render template: 'layouts/mailers/notifications_layout' diff --git a/app/views/layouts/mailers/notifications_layout.html.erb b/app/views/layouts/mailers/notifications_layout.html.erb new file mode 100644 index 000000000..0fe399670 --- /dev/null +++ b/app/views/layouts/mailers/notifications_layout.html.erb @@ -0,0 +1,180 @@ + + + + + <%= yield(:title) %> + + + + + + + + + + + + + + +
+ +
+ + + + + + +
+
+ + + + + + +
+
+

+

+
+
+
+ +
+
+ + +
+ + + + + + +
+ +
+ + + + + + <% if content_for?(:footer) %> + + + + <% end %> + +
+
+

<%= yield(:title) %>

+ <%= yield %> +
+
+

+ — +

+

+ <%= yield(:footer) %> +

+
+
+ +
+
+ + +
+ + + + + + +
+ +
+ + + + + + + + + +
+
+ demarches-simplifiees.fr est un service fourni par la DINSIC et incubé par beta.gouv.fr +
+
+
+

+

+
+
+
+ +
+
+ +
+ + diff --git a/spec/mailers/previews/notification_mailer_preview.rb b/spec/mailers/previews/notification_mailer_preview.rb index af96ed00e..064d5471d 100644 --- a/spec/mailers/previews/notification_mailer_preview.rb +++ b/spec/mailers/previews/notification_mailer_preview.rb @@ -4,7 +4,8 @@ class NotificationMailerPreview < ActionMailer::Preview end def send_initiated_notification - NotificationMailer.send_initiated_notification(Dossier.last) + p = Procedure.where(id: Mails::InitiatedMail.where("body like ?", "% Date: Thu, 14 Mar 2019 14:58:01 +0100 Subject: [PATCH 2/9] [Fix #3064] Add a preview button for state notifications emails --- .../mail_templates_controller.rb | 33 +++++++++++++++++++ app/views/admin/mail_templates/edit.html.haml | 1 + config/routes.rb | 4 +++ .../mail_templates/edit.html.haml_spec.rb | 1 + 4 files changed, 39 insertions(+) create mode 100644 app/controllers/new_administrateur/mail_templates_controller.rb diff --git a/app/controllers/new_administrateur/mail_templates_controller.rb b/app/controllers/new_administrateur/mail_templates_controller.rb new file mode 100644 index 000000000..73d5ed18c --- /dev/null +++ b/app/controllers/new_administrateur/mail_templates_controller.rb @@ -0,0 +1,33 @@ +module NewAdministrateur + class MailTemplatesController < AdministrateurController + include ActionView::Helpers::SanitizeHelper + + def preview + @procedure = procedure + mail_template = find_mail_template_by_slug(params[:id]) + @dossier = Dossier.new(id: 0) + + render(html: sanitize(mail_template.body), layout: 'mailers/notification') + end + + private + + def procedure + @procedure = current_administrateur.procedures.find(params[:procedure_id]) + end + + def mail_templates + [ + @procedure.initiated_mail_template, + @procedure.received_mail_template, + @procedure.closed_mail_template, + @procedure.refused_mail_template, + @procedure.without_continuation_mail_template + ] + end + + def find_mail_template_by_slug(slug) + mail_templates.find { |template| template.class.const_get(:SLUG) == slug } + end + end +end diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml index a00c116f6..c423fe418 100644 --- a/app/views/admin/mail_templates/edit.html.haml +++ b/app/views/admin/mail_templates/edit.html.haml @@ -15,6 +15,7 @@ .text-right = link_to "Annuler", admin_procedure_mail_templates_path(@procedure), class: "btn btn-default" = f.button :submit, 'Mettre à jour', class: "btn-success" + = link_to "Prévisualiser", preview_procedure_mail_template_path(@procedure, @mail_template.class.const_get(:SLUG)), class: "btn btn-primary", target: "_blank" .row .col-md-12 diff --git a/config/routes.rb b/config/routes.rb index 409795dce..20cd28d8a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -378,6 +378,10 @@ Rails.application.routes.draw do patch :move end end + + resources :mail_templates, only: [] do + get 'preview', on: :member + end end resources :services, except: [:show] do diff --git a/spec/views/admin/mail_templates/edit.html.haml_spec.rb b/spec/views/admin/mail_templates/edit.html.haml_spec.rb index f41f6087b..a381a79d9 100644 --- a/spec/views/admin/mail_templates/edit.html.haml_spec.rb +++ b/spec/views/admin/mail_templates/edit.html.haml_spec.rb @@ -10,6 +10,7 @@ describe 'admin/mail_templates/edit.html.haml', type: :view do allow(view).to receive(:admin_procedure_mail_templates_path).and_return("/toto") assign(:mail_template, mail_template) + assign(:procedure, procedure) end context "Champs are listed in the page" do From b3970a5e6f183dacb843bfa10437f1070f20ccbd Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 14 Mar 2019 16:01:09 +0100 Subject: [PATCH 3/9] [Fix #3064] Change default signature --- app/views/notification_mailer/_signature.html.haml | 4 ++++ app/views/notification_mailer/closed_mail.html.haml | 2 +- .../closed_mail_with_attestation.html.haml | 2 +- app/views/notification_mailer/initiated_mail.html.haml | 2 +- app/views/notification_mailer/received_mail.html.haml | 2 +- app/views/notification_mailer/refused_mail.html.haml | 2 +- .../notification_mailer/without_continuation_mail.html.haml | 2 +- 7 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 app/views/notification_mailer/_signature.html.haml diff --git a/app/views/notification_mailer/_signature.html.haml b/app/views/notification_mailer/_signature.html.haml new file mode 100644 index 000000000..132bfafa4 --- /dev/null +++ b/app/views/notification_mailer/_signature.html.haml @@ -0,0 +1,4 @@ +%p + Bonne journée, + +%p --nom du service-- diff --git a/app/views/notification_mailer/closed_mail.html.haml b/app/views/notification_mailer/closed_mail.html.haml index 080b1071a..dcaf366be 100644 --- a/app/views/notification_mailer/closed_mail.html.haml +++ b/app/views/notification_mailer/closed_mail.html.haml @@ -7,4 +7,4 @@ %p À tout moment, vous pouvez consulter votre dossier et les éventuels messages de l'administration à cette adresse : --lien dossier-- -= render partial: "layouts/mailers/signature" += render partial: "notification_mailer/signature" diff --git a/app/views/notification_mailer/closed_mail_with_attestation.html.haml b/app/views/notification_mailer/closed_mail_with_attestation.html.haml index d8b646f57..22dce3d5a 100644 --- a/app/views/notification_mailer/closed_mail_with_attestation.html.haml +++ b/app/views/notification_mailer/closed_mail_with_attestation.html.haml @@ -10,4 +10,4 @@ %p À tout moment, vous pouvez consulter votre dossier et les éventuels messages de l'administration à cette adresse : --lien dossier-- -= render partial: "layouts/mailers/signature" += render partial: "notification_mailer/signature" diff --git a/app/views/notification_mailer/initiated_mail.html.haml b/app/views/notification_mailer/initiated_mail.html.haml index 796695603..91007a340 100644 --- a/app/views/notification_mailer/initiated_mail.html.haml +++ b/app/views/notification_mailer/initiated_mail.html.haml @@ -7,4 +7,4 @@ %p À tout moment, vous pouvez consulter votre dossier et les éventuels messages de l'administration à cette adresse : --lien dossier-- -= render partial: "layouts/mailers/signature" += render partial: "notification_mailer/signature" diff --git a/app/views/notification_mailer/received_mail.html.haml b/app/views/notification_mailer/received_mail.html.haml index a774789c1..d006d6271 100644 --- a/app/views/notification_mailer/received_mail.html.haml +++ b/app/views/notification_mailer/received_mail.html.haml @@ -4,4 +4,4 @@ %p Votre administration vous confirme la bonne réception de votre dossier nº --numéro du dossier--. Celui-ci sera instruit dans le délai légal déclaré par votre interlocuteur. -= render partial: "layouts/mailers/signature" += render partial: "notification_mailer/signature" diff --git a/app/views/notification_mailer/refused_mail.html.haml b/app/views/notification_mailer/refused_mail.html.haml index 1ed19b296..f45054ca2 100644 --- a/app/views/notification_mailer/refused_mail.html.haml +++ b/app/views/notification_mailer/refused_mail.html.haml @@ -10,4 +10,4 @@ %p Pour en savoir plus sur le motif du refus, vous pouvez consulter votre dossier et les éventuels messages de l'administration à cette adresse : --lien dossier-- -= render partial: "layouts/mailers/signature" += render partial: "notification_mailer/signature" diff --git a/app/views/notification_mailer/without_continuation_mail.html.haml b/app/views/notification_mailer/without_continuation_mail.html.haml index 3033f61cc..c397c1adc 100644 --- a/app/views/notification_mailer/without_continuation_mail.html.haml +++ b/app/views/notification_mailer/without_continuation_mail.html.haml @@ -10,4 +10,4 @@ %p Pour en savoir plus sur les raisons de ce classement sans suite, vous pouvez consulter votre dossier et les éventuels messages de l'administration à cette adresse : --lien dossier-- -= render partial: "layouts/mailers/signature" += render partial: "notification_mailer/signature" From 7e551a9d8dbdb0bc2cc87c9e0267237e077ac44f Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 14 Mar 2019 16:01:30 +0100 Subject: [PATCH 4/9] [Fix #3064] If procedure has logo, then display it in emails --- app/mailers/notification_mailer.rb | 5 +++++ app/views/layouts/mailers/notification.html.haml | 7 +++++++ app/views/layouts/mailers/notifications_layout.html.erb | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 8b855662d..939cb9a88 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -38,6 +38,11 @@ class NotificationMailer < ApplicationMailer @dossier = dossier + if dossier.procedure.logo? + @logo_filename = dossier.procedure.logo.filename + attachments.inline[@logo_filename] = dossier.procedure.logo.read + end + mail(subject: subject, to: email) do |format| # rubocop:disable Rails/OutputSafety format.html { render(html: body.html_safe, layout: 'mailers/notification') } diff --git a/app/views/layouts/mailers/notification.html.haml b/app/views/layouts/mailers/notification.html.haml index 65d748cd1..77cffee7a 100644 --- a/app/views/layouts/mailers/notification.html.haml +++ b/app/views/layouts/mailers/notification.html.haml @@ -1,3 +1,10 @@ +- if @logo_filename.present? + - content_for :procedure_logo do + %table{ width: "100%", border: "0", cellspacing: "0", cellpadding: "0" } + %tr + %td{ align: "center" } + = image_tag attachments[@logo_filename].url, style: "height: 150px;" + - content_for :footer do %strong Merci de ne pas répondre à cet email. Pour vous adresser à votre administration, passez directement par votre diff --git a/app/views/layouts/mailers/notifications_layout.html.erb b/app/views/layouts/mailers/notifications_layout.html.erb index 0fe399670..7c9ea03f9 100644 --- a/app/views/layouts/mailers/notifications_layout.html.erb +++ b/app/views/layouts/mailers/notifications_layout.html.erb @@ -86,10 +86,10 @@
+ <%= yield(:procedure_logo) %> From 60d66f0422b54ade06ac9d5ade89265354561c96 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 14 Mar 2019 16:15:55 +0100 Subject: [PATCH 5/9] [Fix #3064] Add logo and cancel link on preview --- .../new_administrateur/mail_templates_controller.rb | 3 ++- app/mailers/notification_mailer.rb | 5 +++-- app/views/layouts/mailers/notification.html.haml | 10 +++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/controllers/new_administrateur/mail_templates_controller.rb b/app/controllers/new_administrateur/mail_templates_controller.rb index 73d5ed18c..515b80435 100644 --- a/app/controllers/new_administrateur/mail_templates_controller.rb +++ b/app/controllers/new_administrateur/mail_templates_controller.rb @@ -4,8 +4,9 @@ module NewAdministrateur def preview @procedure = procedure - mail_template = find_mail_template_by_slug(params[:id]) @dossier = Dossier.new(id: 0) + mail_template = find_mail_template_by_slug(params[:id]) + @logo_url = procedure.logo.url render(html: sanitize(mail_template.body), layout: 'mailers/notification') end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 939cb9a88..aafa91665 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -39,8 +39,9 @@ class NotificationMailer < ApplicationMailer @dossier = dossier if dossier.procedure.logo? - @logo_filename = dossier.procedure.logo.filename - attachments.inline[@logo_filename] = dossier.procedure.logo.read + logo_filename = dossier.procedure.logo.filename + attachments.inline[logo_filename] = dossier.procedure.logo.read + @logo_url = attachments[logo_filename].url end mail(subject: subject, to: email) do |format| diff --git a/app/views/layouts/mailers/notification.html.haml b/app/views/layouts/mailers/notification.html.haml index 77cffee7a..89d3a5c29 100644 --- a/app/views/layouts/mailers/notification.html.haml +++ b/app/views/layouts/mailers/notification.html.haml @@ -1,14 +1,18 @@ -- if @logo_filename.present? +- if @logo_url.present? - content_for :procedure_logo do %table{ width: "100%", border: "0", cellspacing: "0", cellpadding: "0" } %tr %td{ align: "center" } - = image_tag attachments[@logo_filename].url, style: "height: 150px;" + = image_tag @logo_url, style: "height: 150px;" - content_for :footer do + - if @dossier.present? + - messagerie_url = messagerie_dossier_url(@dossier) + - else + - messagerie_url = "#" %strong Merci de ne pas répondre à cet email. Pour vous adresser à votre administration, passez directement par votre = succeed '.' do - = link_to 'messagerie', messagerie_dossier_url(@dossier), target: '_blank', rel: 'noopener' + = link_to 'messagerie', messagerie_url, target: '_blank', rel: 'noopener' = render template: 'layouts/mailers/notifications_layout' From 9c6c11027cc17042d271520bc0a5e7896ff9f499 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 21 Mar 2019 15:39:37 +0100 Subject: [PATCH 6/9] [Fix #3064] Add service footer on notification emails --- .../mail_templates_controller.rb | 5 ++-- app/mailers/notification_mailer.rb | 5 ++-- .../layouts/mailers/notification.html.haml | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/controllers/new_administrateur/mail_templates_controller.rb b/app/controllers/new_administrateur/mail_templates_controller.rb index 515b80435..7bc7dfba6 100644 --- a/app/controllers/new_administrateur/mail_templates_controller.rb +++ b/app/controllers/new_administrateur/mail_templates_controller.rb @@ -4,9 +4,10 @@ module NewAdministrateur def preview @procedure = procedure - @dossier = Dossier.new(id: 0) - mail_template = find_mail_template_by_slug(params[:id]) @logo_url = procedure.logo.url + @service = procedure.service + + mail_template = find_mail_template_by_slug(params[:id]) render(html: sanitize(mail_template.body), layout: 'mailers/notification') end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index aafa91665..767a521b3 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -36,14 +36,15 @@ class NotificationMailer < ApplicationMailer create_commentaire_for_notification(dossier, subject, body) - @dossier = dossier - if dossier.procedure.logo? logo_filename = dossier.procedure.logo.filename attachments.inline[logo_filename] = dossier.procedure.logo.read @logo_url = attachments[logo_filename].url end + @dossier = dossier + @service = dossier.procedure.service + mail(subject: subject, to: email) do |format| # rubocop:disable Rails/OutputSafety format.html { render(html: body.html_safe, layout: 'mailers/notification') } diff --git a/app/views/layouts/mailers/notification.html.haml b/app/views/layouts/mailers/notification.html.haml index 89d3a5c29..16d307003 100644 --- a/app/views/layouts/mailers/notification.html.haml +++ b/app/views/layouts/mailers/notification.html.haml @@ -15,4 +15,28 @@ = succeed '.' do = link_to 'messagerie', messagerie_url, target: '_blank', rel: 'noopener' + - if @service.present? + %table{ width: "100%", border: "0", cellspacing: "0", cellpadding: "0", style: "cursor:auto;color:#55575d;font-family:Helvetica, Arial, sans-serif;font-size:11px;line-height:22px;text-align:left;" } + %tr + %td{ width: "50%", valign: "top" } + %p + %strong Cette démarche est gérée par : + %br + = @service.nom + %br + = @service.organisme + %br + = @service.adresse + %td{ width: "50%", valign: "top" } + %p + %strong Poser une question sur votre dossier : + %br + = link_to 'Par la messagerie', messagerie_url, target: '_blank', rel: 'noopener' + %br + Par téléphone : + = link_to @service.telephone, "tel:#{@service.telephone}" + %br + Horaires : #{ formatted_horaires(@service.horaires) } + + = render template: 'layouts/mailers/notifications_layout' From b3f3106888fb89cff06077a046aa0826b9b2003e Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 21 Mar 2019 15:39:37 +0100 Subject: [PATCH 7/9] [Fix #3064] Change logo size --- app/views/layouts/mailers/notification.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/mailers/notification.html.haml b/app/views/layouts/mailers/notification.html.haml index 16d307003..b267a0fc5 100644 --- a/app/views/layouts/mailers/notification.html.haml +++ b/app/views/layouts/mailers/notification.html.haml @@ -3,7 +3,7 @@ %table{ width: "100%", border: "0", cellspacing: "0", cellpadding: "0" } %tr %td{ align: "center" } - = image_tag @logo_url, style: "height: 150px;" + = image_tag @logo_url, height: "150", style: "display:block; max-height: 150px; max-width: 150px;" - content_for :footer do - if @dossier.present? From 28f9208ce8e2dc61ec5b2758f42dad0a539738fd Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 21 Mar 2019 15:05:30 +0100 Subject: [PATCH 8/9] When mail template is updated, stay on edit page to easily preview it --- app/controllers/admin/mail_templates_controller.rb | 3 ++- spec/controllers/admin/mail_templates_controller_spec.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/mail_templates_controller.rb b/app/controllers/admin/mail_templates_controller.rb index bc7c948ff..3ed7f277d 100644 --- a/app/controllers/admin/mail_templates_controller.rb +++ b/app/controllers/admin/mail_templates_controller.rb @@ -12,7 +12,8 @@ class Admin::MailTemplatesController < AdminController def update mail_template = find_mail_template_by_slug(params[:id]) mail_template.update(update_params) - redirect_to admin_procedure_mail_templates_path + flash.notice = "Email mis à jour" + redirect_to edit_admin_procedure_mail_template_path(mail_template.procedure_id, params[:id]) end private diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb index e9c35576f..d34ef2684 100644 --- a/spec/controllers/admin/mail_templates_controller_spec.rb +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -31,7 +31,7 @@ describe Admin::MailTemplatesController, type: :controller do } end - it { expect(response).to redirect_to admin_procedure_mail_templates_path(procedure) } + it { expect(response).to redirect_to edit_admin_procedure_mail_template_path(procedure, initiated_mail.class.const_get(:SLUG)) } context 'the mail template' do subject { procedure.reload; procedure.initiated_mail_template } From c550d40e3a5bf69db36b80b0cc0823f76762bf12 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 27 Mar 2019 12:32:38 +0100 Subject: [PATCH 9/9] [Fix #3064] Add preview test --- .../mail_templates_controller_spec.rb | 21 +++++++++++++++++++ spec/factories/procedure.rb | 4 ++++ 2 files changed, 25 insertions(+) create mode 100644 spec/controllers/new_administrateur/mail_templates_controller_spec.rb diff --git a/spec/controllers/new_administrateur/mail_templates_controller_spec.rb b/spec/controllers/new_administrateur/mail_templates_controller_spec.rb new file mode 100644 index 000000000..2a8673f77 --- /dev/null +++ b/spec/controllers/new_administrateur/mail_templates_controller_spec.rb @@ -0,0 +1,21 @@ +describe NewAdministrateur::MailTemplatesController, type: :controller do + render_views + + let(:admin) { create(:administrateur) } + + describe '#preview' do + let(:procedure) { create(:procedure, :with_logo, :with_service, administrateur: admin) } + + before do + sign_in admin + get :preview, params: { id: "initiated_mail", procedure_id: procedure.id } + end + + it { expect(response).to have_http_status(:ok) } + + it { expect(response.body).to have_css("img[src*='#{procedure.logo.filename}']") } + + it { expect(response.body).to include(procedure.service.nom) } + it { expect(response.body).to include(procedure.service.telephone) } + end +end diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index e45f1c2aa..98488e884 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -41,6 +41,10 @@ FactoryBot.define do end end + trait :with_logo do + logo { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'application/pdf') } + end + trait :with_path do path { generate(:published_path) } end
-

<%= yield(:title) %>

<%= yield %>