-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathform_submission_confirmation_mailer.rb
More file actions
70 lines (56 loc) · 3.16 KB
/
form_submission_confirmation_mailer.rb
File metadata and controls
70 lines (56 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class FormSubmissionConfirmationMailer < GovukNotifyRails::Mailer
def send_confirmation_email(what_happens_next_markdown:, support_contact_details:, notify_response_id:, confirmation_email_address:, mailer_options:, submission_locale: :en, what_happens_next_markdown_cy: nil, support_contact_details_cy: nil)
@submission_locale = submission_locale.to_sym
set_template(template_id)
set_personalisation(
title: mailer_options.title,
what_happens_next_text: what_happens_next_markdown.presence || default_what_happens_next_text,
what_happens_next_text_cy: what_happens_next_markdown_cy.presence || what_happens_next_markdown.presence || default_what_happens_next_text,
support_contact_details: format_support_details(support_contact_details).presence || default_support_contact_details_text,
support_contact_details_cy: format_support_details(support_contact_details_cy || support_contact_details, locale: :cy).presence || default_support_contact_details_text,
submission_time: mailer_options.timestamp.strftime("%l:%M%P").strip,
submission_date: I18n.l(mailer_options.timestamp, format: "%-d %B %Y", locale: :en),
submission_date_cy: I18n.l(mailer_options.timestamp, format: "%-d %B %Y", locale: :cy),
# GOV.UK Notify's templates have conditionals, but only positive
# conditionals, so to simulate negative conditionals we add two boolean
# flags; but they must always have opposite values!
test: make_notify_boolean(mailer_options.is_preview),
submission_reference: mailer_options.submission_reference,
include_payment_link: make_notify_boolean(mailer_options.payment_url.present?),
payment_link: mailer_options.payment_url || "",
)
set_reference(notify_response_id)
set_email_reply_to(Settings.govuk_notify.form_submission_email_reply_to_id)
mail(to: confirmation_email_address)
end
def format_support_details(support_details, locale: :en)
phone = support_details.phone
call_charges_url = support_details.call_charges_url
email = support_details.email
url = support_details.url
url_text = support_details.url_text
support_details = []
support_details << normalize_whitespace(phone) if phone.present?
support_details << "[#{I18n.t('support_details.call_charges', locale: locale)}](#{call_charges_url})" if phone.present?
support_details << "[#{email}](mailto:#{email})" if email.present?
support_details << "[#{url_text}](#{url})" if url.present? && url_text.present?
support_details.compact_blank.join("\n\n")
end
private
def default_what_happens_next_text
I18n.t("mailer.submission_confirmation.default_what_happens_next")
end
def default_support_contact_details_text
I18n.t("mailer.submission_confirmation.default_support_contact_details")
end
def make_notify_boolean(bool)
bool ? "yes" : "no"
end
def template_id
return Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id if @submission_locale == :cy
Settings.govuk_notify.form_filler_confirmation_email_template_id
end
def normalize_whitespace(text)
text.strip.gsub(/\r\n?/, "\n").split(/\n\n+/).map(&:strip).join("\n\n")
end
end