From 8313752663178860d2df463e5737d51a190c7a83 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 22 Jul 2019 13:21:05 +0000 Subject: [PATCH] mailers: add CTA to templated emails --- app/mailers/notification_mailer.rb | 2 ++ app/models/concerns/mail_template_concern.rb | 10 ++++++++++ app/models/mails/refused_mail.rb | 4 ++++ app/views/notification_mailer/_actions.html.haml | 13 +++++++++++++ .../notification_mailer/send_notification.html.haml | 3 +++ spec/mailers/notification_mailer_spec.rb | 5 +++++ 6 files changed, 37 insertions(+) create mode 100644 app/views/notification_mailer/_actions.html.haml diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 816ff2c7c..5ec6cc596 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -9,6 +9,7 @@ class NotificationMailer < ApplicationMailer include ActionView::Helpers::SanitizeHelper helper ServiceHelper + helper MailerHelper layout 'mailers/notifications_layout' @@ -46,6 +47,7 @@ class NotificationMailer < ApplicationMailer @service = dossier.procedure.service @logo_url = attach_logo(dossier.procedure) @rendered_template = sanitize(body) + @actions = mail_template.actions_for_dossier(dossier) mail(subject: subject, to: email, template_name: 'send_notification') end diff --git a/app/models/concerns/mail_template_concern.rb b/app/models/concerns/mail_template_concern.rb index 3b613eb2b..f720c8297 100644 --- a/app/models/concerns/mail_template_concern.rb +++ b/app/models/concerns/mail_template_concern.rb @@ -3,6 +3,12 @@ module MailTemplateConcern include TagsSubstitutionConcern + module Actions + SHOW = :show + ASK_QUESTION = :ask_question + REPLY = :reply + end + def subject_for_dossier(dossier) replace_tags(subject, dossier) end @@ -11,6 +17,10 @@ module MailTemplateConcern replace_tags(body, dossier) end + def actions_for_dossier(dossier) + [MailTemplateConcern::Actions::SHOW, MailTemplateConcern::Actions::ASK_QUESTION] + end + def update_rich_body self.rich_body = self.body end diff --git a/app/models/mails/refused_mail.rb b/app/models/mails/refused_mail.rb index ad598d859..375b94326 100644 --- a/app/models/mails/refused_mail.rb +++ b/app/models/mails/refused_mail.rb @@ -9,5 +9,9 @@ module Mails DISPLAYED_NAME = 'Accusé de rejet du dossier' DEFAULT_SUBJECT = 'Votre dossier nº --numéro du dossier-- a été refusé (--libellé démarche--)' DOSSIER_STATE = Dossier.states.fetch(:refuse) + + def actions_for_dossier(dossier) + [MailTemplateConcern::Actions::REPLY, MailTemplateConcern::Actions::SHOW] + end end end diff --git a/app/views/notification_mailer/_actions.html.haml b/app/views/notification_mailer/_actions.html.haml new file mode 100644 index 000000000..f6ccd9130 --- /dev/null +++ b/app/views/notification_mailer/_actions.html.haml @@ -0,0 +1,13 @@ += vertical_margin(15) + +- actions.each_with_index do |action, index| + - variant = (index == 0 ? :primary : :secondary) + - case action + - when MailTemplateConcern::Actions::SHOW + = round_button('Consulter mon dossier', dossier_url(@dossier), variant) + - when MailTemplateConcern::Actions::ASK_QUESTION + = round_button('J’ai une question', messagerie_dossier_url(@dossier), variant) + - when MailTemplateConcern::Actions::REPLY + = round_button('Répondre à ce message', messagerie_dossier_url(@dossier), variant) + += vertical_margin(8) diff --git a/app/views/notification_mailer/send_notification.html.haml b/app/views/notification_mailer/send_notification.html.haml index 875806f48..6ce7aa565 100644 --- a/app/views/notification_mailer/send_notification.html.haml +++ b/app/views/notification_mailer/send_notification.html.haml @@ -3,5 +3,8 @@ = @rendered_template +- if @actions.present? + = render 'notification_mailer/actions', actions: @actions, dossier: @dossier + - content_for :footer do = render 'layouts/mailers/service_footer', service: @service, dossier: @dossier diff --git a/spec/mailers/notification_mailer_spec.rb b/spec/mailers/notification_mailer_spec.rb index 4d28508b9..da2c13a7f 100644 --- a/spec/mailers/notification_mailer_spec.rb +++ b/spec/mailers/notification_mailer_spec.rb @@ -28,6 +28,11 @@ RSpec.describe NotificationMailer, type: :mailer do expect(mail.body).to have_link('messagerie') end + it 'renders the actions' do + expect(mail.body).to have_link('Consulter mon dossier', href: dossier_url(dossier)) + expect(mail.body).to have_link('J’ai une question', href: messagerie_dossier_url(dossier)) + end + context 'when the template body contains tags' do let(:email_template) { create(:received_mail, subject: 'Email subject', body: 'Hello --nom--, your dossier --lien dossier-- was processed.') }