Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions app/jobs/send_confirmation_email_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ class SendConfirmationEmailJob < ApplicationJob
def perform(submission:, notify_response_id:, confirmation_email_address:)
set_submission_logging_attributes(submission:)

I18n.with_locale(submission.submission_locale || I18n.default_locale) do
form = submission.form
mail = FormSubmissionConfirmationMailer.send_confirmation_email(
what_happens_next_markdown: form.what_happens_next_markdown,
support_contact_details: form.support_details,
notify_response_id:,
confirmation_email_address:,
mailer_options: mailer_options_for(submission:, form:),
)
form = submission.form
welsh_form = fetch_welsh_form(submission:, form:)
mail = FormSubmissionConfirmationMailer.send_confirmation_email(
what_happens_next_markdown: form.what_happens_next_markdown,
what_happens_next_markdown_cy: welsh_form&.what_happens_next_markdown,
support_contact_details: form.support_details,
support_contact_details_cy: welsh_form&.support_details,
notify_response_id:,
confirmation_email_address:,
mailer_options: mailer_options_for(submission:, form:),
submission_locale: submission.submission_locale,
)

mail.deliver_now
CurrentJobLoggingAttributes.confirmation_email_id = mail.govuk_notify_response.id
end
mail.deliver_now
CurrentJobLoggingAttributes.confirmation_email_id = mail.govuk_notify_response.id
rescue StandardError
CloudWatchService.record_job_failure_metric(self.class.name)
raise
Expand All @@ -34,4 +36,15 @@ def mailer_options_for(submission:, form:)
payment_url: submission.payment_url,
)
end

def fetch_welsh_form(submission:, form:)
return nil unless submission.submission_locale.to_sym == :cy

form_document = Api::V2::FormDocumentRepository.find_with_mode(
form_id: form.id,
mode: submission.mode_object,
language: :cy,
)
Form.new(form_document) if form_document
end
end
14 changes: 9 additions & 5 deletions app/mailers/form_submission_confirmation_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
class FormSubmissionConfirmationMailer < GovukNotifyRails::Mailer
def send_confirmation_email(what_happens_next_markdown:, support_contact_details:, notify_response_id:, confirmation_email_address:, mailer_options:)
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"),
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!
Expand All @@ -24,7 +28,7 @@ def send_confirmation_email(what_happens_next_markdown:, support_contact_details
mail(to: confirmation_email_address)
end

def format_support_details(support_details)
def format_support_details(support_details, locale: :en)
phone = support_details.phone
call_charges_url = support_details.call_charges_url
email = support_details.email
Expand All @@ -33,7 +37,7 @@ def format_support_details(support_details)

support_details = []
support_details << normalize_whitespace(phone) if phone.present?
support_details << "[#{I18n.t('support_details.call_charges')}](#{call_charges_url})" 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?

Expand All @@ -55,7 +59,7 @@ def make_notify_boolean(bool)
end

def template_id
return Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id if I18n.locale == :cy
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
Expand Down
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ govuk_notify:
form_submission_email_reply_to_id: fab9373b-fb7c-483f-ae25-5a9852bfcc04
form_submission_email_template_id: 427eb8bc-ce0d-40a3-bf54-d76e8c3ec916
form_filler_confirmation_email_template_id: 2d1f36dc-9799-43dd-8673-b631f9e0b4a5
form_filler_confirmation_email_welsh_template_id: 4c5c75df-3a48-48ec-80d9-df9cabcdc9fc
form_filler_confirmation_email_welsh_template_id: b297c8f1-419e-4af4-b067-b8cad6364daf

# Configuration for Sentry
# Sentry will only initialise if dsn is set to some other value
Expand Down
2 changes: 1 addition & 1 deletion spec/config/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
include_examples expected_value_test, :form_submission_email_reply_to_id, govuk_notify, "fab9373b-fb7c-483f-ae25-5a9852bfcc04"
include_examples expected_value_test, :form_submission_email_template_id, govuk_notify, "427eb8bc-ce0d-40a3-bf54-d76e8c3ec916"
include_examples expected_value_test, :form_filler_confirmation_email_template_id, govuk_notify, "2d1f36dc-9799-43dd-8673-b631f9e0b4a5"
include_examples expected_value_test, :form_filler_confirmation_email_welsh_template_id, govuk_notify, "4c5c75df-3a48-48ec-80d9-df9cabcdc9fc"
include_examples expected_value_test, :form_filler_confirmation_email_welsh_template_id, govuk_notify, "b297c8f1-419e-4af4-b067-b8cad6364daf"
end

describe "sentry" do
Expand Down
54 changes: 52 additions & 2 deletions spec/jobs/send_confirmation_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,41 @@

expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
what_happens_next_markdown: "Please wait for a response",
what_happens_next_markdown_cy: nil,
support_contact_details: have_attributes(
phone: "0203 222 2222",
call_charges_url: "https://www.gov.uk/call-charges",
email: "[email protected]",
url: "https://example.gov.uk/help",
url_text: "Get help",
),
support_contact_details_cy: nil,
notify_response_id: "confirmation-ref",
confirmation_email_address: "[email protected]",
mailer_options: an_instance_of(SendConfirmationEmailJob::MailerOptions),
submission_locale: "en",
)
end

context "when locale is Welsh" do
it "uses the Welsh template" do
context "when submission locale is Welsh" do
let(:welsh_form_document) do
build(:v2_form_document,
what_happens_next_markdown: "Arhoswch am ymateb",
support_phone: "0291 111 1111",
support_email: "[email protected]")
end

before do
submission.update!(submission_locale: "cy")
allow(Api::V2::FormDocumentRepository).to receive(:find_with_mode).and_call_original
allow(Api::V2::FormDocumentRepository).to receive(:find_with_mode).with(
form_id: anything,
mode: anything,
language: :cy,
).and_return(welsh_form_document)
end

it "uses the bilingual template" do
described_class.perform_now(
submission:,
notify_response_id:,
Expand All @@ -80,6 +99,37 @@
mail = ActionMailer::Base.deliveries.last
expect(mail.govuk_notify_template).to eq("7891011")
end

it "passes the Welsh what happens next to the mailer" do
allow(FormSubmissionConfirmationMailer).to receive(:send_confirmation_email).and_call_original

described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
)

expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
Comment thread
chao-xian marked this conversation as resolved.
hash_including(what_happens_next_markdown_cy: "Arhoswch am ymateb"),
)
end

it "passes the Welsh support details" do
allow(FormSubmissionConfirmationMailer).to receive(:send_confirmation_email).and_call_original

described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
)

expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
hash_including(support_contact_details_cy: have_attributes(
phone: "0291 111 1111",
email: "[email protected]",
)),
)
end
end

context "when there is an error during processing" do
Expand Down
92 changes: 59 additions & 33 deletions spec/mailers/form_submission_confirmation_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require "rails_helper"

describe FormSubmissionConfirmationMailer, type: :mailer do
let(:submission_locale) { :en }
let(:mail) do
described_class.send_confirmation_email(what_happens_next_markdown:,
support_contact_details:,
notify_response_id: "for-my-ref",
confirmation_email_address:,
mailer_options:)
mailer_options:,
submission_locale:)
end
let(:mailer_options) do
FormSubmissionService::MailerOptions.new(title:,
Expand All @@ -30,24 +32,18 @@
Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id = "7891011"
end

context "when the request locale is not set" do
it "uses the English language template" do
expect(mail.govuk_notify_template).to eq("123456")
end
end

context "when the request locale is set to :en" do
include_context "with locale set to :en"
context "when submission_locale is :en" do
let(:submission_locale) { :en }

it "uses the English language template" do
expect(mail.govuk_notify_template).to eq("123456")
end
end

context "when the request locale is set to :cy" do
include_context "with locale set to :cy"
context "when submission_locale is :cy" do
let(:submission_locale) { :cy }

it "uses the Welsh language template" do
it "uses the bilingual template" do
expect(mail.govuk_notify_template).to eq("7891011")
end
end
Expand All @@ -64,10 +60,55 @@
expect(mail.govuk_notify_personalisation[:what_happens_next_text]).to eq("Please wait for a response")
end

context "when what_happens_next_markdown_cy is provided" do
let(:mail) do
described_class.send_confirmation_email(what_happens_next_markdown:,
support_contact_details:,
notify_response_id: "for-my-ref",
confirmation_email_address:,
mailer_options:,
submission_locale:,
what_happens_next_markdown_cy: "Arhoswch am ymateb")
end

it "includes the Welsh what happens next" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text_cy]).to eq("Arhoswch am ymateb")
end
end

context "when what_happens_next_markdown_cy is not provided" do
it "falls back to the English what happens next" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text_cy]).to eq("Please wait for a response")
end
end

it "includes the forms support contact details" do
expect(mail.govuk_notify_personalisation[:support_contact_details]).to eq("0203 222 2222\n\n[Find out about call charges](https://www.gov.uk/call-charges)")
end

context "when support_contact_details_cy is not provided" do
it "falls back to support_contact_details formatted with Welsh locale" do
expect(mail.govuk_notify_personalisation[:support_contact_details_cy]).to eq("0203 222 2222\n\n[Darganfyddwch am gostau galwadau](https://www.gov.uk/call-charges)")
end
end

context "when support_contact_details_cy is provided" do
let(:welsh_support) { OpenStruct.new(phone: "0291 111 1111", email: nil, url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
let(:mail) do
described_class.send_confirmation_email(what_happens_next_markdown:,
support_contact_details:,
support_contact_details_cy: welsh_support,
notify_response_id: "for-my-ref",
confirmation_email_address:,
mailer_options:,
submission_locale:)
end

it "uses the Welsh support details formatted with Welsh locale" do
expect(mail.govuk_notify_personalisation[:support_contact_details_cy]).to eq("0291 111 1111\n\n[Darganfyddwch am gostau galwadau](https://www.gov.uk/call-charges)")
end
end

context "when what happens next is missing" do
let(:what_happens_next_markdown) { nil }

Expand Down Expand Up @@ -143,30 +184,15 @@
end
end

context "when the request locale is not set" do
it "includes the date user submitted the form in English" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
end
end
end

context "when the request locale is set to :en" do
include_context "with locale set to :en"
it "includes the date user submitted the form in English" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
end
it "includes the date user submitted the form in English" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
end
end

context "when the request locale is set to :cy" do
include_context "with locale set to :cy"

it "includes the date user submitted the form in Welsh" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 Medi 2022")
end
it "includes the date user submitted the form in Welsh" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date_cy]).to eq("14 Medi 2022")
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/requests/forms/check_your_answers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,12 @@
expected_personalisation = {
title: form_data.name,
what_happens_next_text: form_data.what_happens_next_markdown,
what_happens_next_text_cy: form_data.what_happens_next_markdown,
support_contact_details: contact_support_details_format,
support_contact_details_cy: I18n.with_locale(:cy) { contact_support_details_format },
submission_time: "10:00am",
submission_date: "14 December 2022",
submission_date_cy: "14 Rhagfyr 2022",
test: "no",
submission_reference: reference,
include_payment_link: "no",
Expand Down