Skip to content

Commit d3cae96

Browse files
authored
Merge pull request #2083 from govuk-forms/welsh-confirmation
Make confirmation email for Welsh bi-lingual
2 parents ed5b502 + 7d37989 commit d3cae96

7 files changed

Lines changed: 150 additions & 54 deletions

app/jobs/send_confirmation_email_job.rb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ class SendConfirmationEmailJob < ApplicationJob
55
def perform(submission:, notify_response_id:, confirmation_email_address:)
66
set_submission_logging_attributes(submission:)
77

8-
I18n.with_locale(submission.submission_locale || I18n.default_locale) do
9-
form = submission.form
10-
mail = FormSubmissionConfirmationMailer.send_confirmation_email(
11-
what_happens_next_markdown: form.what_happens_next_markdown,
12-
support_contact_details: form.support_details,
13-
notify_response_id:,
14-
confirmation_email_address:,
15-
mailer_options: mailer_options_for(submission:, form:),
16-
)
8+
form = submission.form
9+
welsh_form = fetch_welsh_form(submission:, form:)
10+
mail = FormSubmissionConfirmationMailer.send_confirmation_email(
11+
what_happens_next_markdown: form.what_happens_next_markdown,
12+
what_happens_next_markdown_cy: welsh_form&.what_happens_next_markdown,
13+
support_contact_details: form.support_details,
14+
support_contact_details_cy: welsh_form&.support_details,
15+
notify_response_id:,
16+
confirmation_email_address:,
17+
mailer_options: mailer_options_for(submission:, form:),
18+
submission_locale: submission.submission_locale,
19+
)
1720

18-
mail.deliver_now
19-
CurrentJobLoggingAttributes.confirmation_email_id = mail.govuk_notify_response.id
20-
end
21+
mail.deliver_now
22+
CurrentJobLoggingAttributes.confirmation_email_id = mail.govuk_notify_response.id
2123
rescue StandardError
2224
CloudWatchService.record_job_failure_metric(self.class.name)
2325
raise
@@ -34,4 +36,15 @@ def mailer_options_for(submission:, form:)
3436
payment_url: submission.payment_url,
3537
)
3638
end
39+
40+
def fetch_welsh_form(submission:, form:)
41+
return nil unless submission.submission_locale.to_sym == :cy
42+
43+
form_document = Api::V2::FormDocumentRepository.find_with_mode(
44+
form_id: form.id,
45+
mode: submission.mode_object,
46+
language: :cy,
47+
)
48+
Form.new(form_document) if form_document
49+
end
3750
end

app/mailers/form_submission_confirmation_mailer.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
class FormSubmissionConfirmationMailer < GovukNotifyRails::Mailer
2-
def send_confirmation_email(what_happens_next_markdown:, support_contact_details:, notify_response_id:, confirmation_email_address:, mailer_options:)
2+
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)
3+
@submission_locale = submission_locale.to_sym
34
set_template(template_id)
45

56
set_personalisation(
67
title: mailer_options.title,
78
what_happens_next_text: what_happens_next_markdown.presence || default_what_happens_next_text,
9+
what_happens_next_text_cy: what_happens_next_markdown_cy.presence || what_happens_next_markdown.presence || default_what_happens_next_text,
810
support_contact_details: format_support_details(support_contact_details).presence || default_support_contact_details_text,
11+
support_contact_details_cy: format_support_details(support_contact_details_cy || support_contact_details, locale: :cy).presence || default_support_contact_details_text,
912
submission_time: mailer_options.timestamp.strftime("%l:%M%P").strip,
10-
submission_date: I18n.l(mailer_options.timestamp, format: "%-d %B %Y"),
13+
submission_date: I18n.l(mailer_options.timestamp, format: "%-d %B %Y", locale: :en),
14+
submission_date_cy: I18n.l(mailer_options.timestamp, format: "%-d %B %Y", locale: :cy),
1115
# GOV.UK Notify's templates have conditionals, but only positive
1216
# conditionals, so to simulate negative conditionals we add two boolean
1317
# flags; but they must always have opposite values!
@@ -24,7 +28,7 @@ def send_confirmation_email(what_happens_next_markdown:, support_contact_details
2428
mail(to: confirmation_email_address)
2529
end
2630

27-
def format_support_details(support_details)
31+
def format_support_details(support_details, locale: :en)
2832
phone = support_details.phone
2933
call_charges_url = support_details.call_charges_url
3034
email = support_details.email
@@ -33,7 +37,7 @@ def format_support_details(support_details)
3337

3438
support_details = []
3539
support_details << normalize_whitespace(phone) if phone.present?
36-
support_details << "[#{I18n.t('support_details.call_charges')}](#{call_charges_url})" if phone.present?
40+
support_details << "[#{I18n.t('support_details.call_charges', locale: locale)}](#{call_charges_url})" if phone.present?
3741
support_details << "[#{email}](mailto:#{email})" if email.present?
3842
support_details << "[#{url_text}](#{url})" if url.present? && url_text.present?
3943

@@ -55,7 +59,7 @@ def make_notify_boolean(bool)
5559
end
5660

5761
def template_id
58-
return Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id if I18n.locale == :cy
62+
return Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id if @submission_locale == :cy
5963

6064
Settings.govuk_notify.form_filler_confirmation_email_template_id
6165
end

config/settings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ govuk_notify:
2626
form_submission_email_reply_to_id: fab9373b-fb7c-483f-ae25-5a9852bfcc04
2727
form_submission_email_template_id: 427eb8bc-ce0d-40a3-bf54-d76e8c3ec916
2828
form_filler_confirmation_email_template_id: 2d1f36dc-9799-43dd-8673-b631f9e0b4a5
29-
form_filler_confirmation_email_welsh_template_id: 4c5c75df-3a48-48ec-80d9-df9cabcdc9fc
29+
form_filler_confirmation_email_welsh_template_id: b297c8f1-419e-4af4-b067-b8cad6364daf
3030

3131
# Configuration for Sentry
3232
# Sentry will only initialise if dsn is set to some other value

spec/config/settings_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
include_examples expected_value_test, :form_submission_email_reply_to_id, govuk_notify, "fab9373b-fb7c-483f-ae25-5a9852bfcc04"
4646
include_examples expected_value_test, :form_submission_email_template_id, govuk_notify, "427eb8bc-ce0d-40a3-bf54-d76e8c3ec916"
4747
include_examples expected_value_test, :form_filler_confirmation_email_template_id, govuk_notify, "2d1f36dc-9799-43dd-8673-b631f9e0b4a5"
48-
include_examples expected_value_test, :form_filler_confirmation_email_welsh_template_id, govuk_notify, "4c5c75df-3a48-48ec-80d9-df9cabcdc9fc"
48+
include_examples expected_value_test, :form_filler_confirmation_email_welsh_template_id, govuk_notify, "b297c8f1-419e-4af4-b067-b8cad6364daf"
4949
end
5050

5151
describe "sentry" do

spec/jobs/send_confirmation_email_job_spec.rb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,41 @@
5555

5656
expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
5757
what_happens_next_markdown: "Please wait for a response",
58+
what_happens_next_markdown_cy: nil,
5859
support_contact_details: have_attributes(
5960
phone: "0203 222 2222",
6061
call_charges_url: "https://www.gov.uk/call-charges",
6162
email: "help@example.gov.uk",
6263
url: "https://example.gov.uk/help",
6364
url_text: "Get help",
6465
),
66+
support_contact_details_cy: nil,
6567
notify_response_id: "confirmation-ref",
6668
confirmation_email_address: "testing@gov.uk",
6769
mailer_options: an_instance_of(SendConfirmationEmailJob::MailerOptions),
70+
submission_locale: "en",
6871
)
6972
end
7073

71-
context "when locale is Welsh" do
72-
it "uses the Welsh template" do
74+
context "when submission locale is Welsh" do
75+
let(:welsh_form_document) do
76+
build(:v2_form_document,
77+
what_happens_next_markdown: "Arhoswch am ymateb",
78+
support_phone: "0291 111 1111",
79+
support_email: "cymraeg@example.gov.uk")
80+
end
81+
82+
before do
7383
submission.update!(submission_locale: "cy")
84+
allow(Api::V2::FormDocumentRepository).to receive(:find_with_mode).and_call_original
85+
allow(Api::V2::FormDocumentRepository).to receive(:find_with_mode).with(
86+
form_id: anything,
87+
mode: anything,
88+
language: :cy,
89+
).and_return(welsh_form_document)
90+
end
91+
92+
it "uses the bilingual template" do
7493
described_class.perform_now(
7594
submission:,
7695
notify_response_id:,
@@ -80,6 +99,37 @@
8099
mail = ActionMailer::Base.deliveries.last
81100
expect(mail.govuk_notify_template).to eq("7891011")
82101
end
102+
103+
it "passes the Welsh what happens next to the mailer" do
104+
allow(FormSubmissionConfirmationMailer).to receive(:send_confirmation_email).and_call_original
105+
106+
described_class.perform_now(
107+
submission:,
108+
notify_response_id:,
109+
confirmation_email_address:,
110+
)
111+
112+
expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
113+
hash_including(what_happens_next_markdown_cy: "Arhoswch am ymateb"),
114+
)
115+
end
116+
117+
it "passes the Welsh support details" do
118+
allow(FormSubmissionConfirmationMailer).to receive(:send_confirmation_email).and_call_original
119+
120+
described_class.perform_now(
121+
submission:,
122+
notify_response_id:,
123+
confirmation_email_address:,
124+
)
125+
126+
expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
127+
hash_including(support_contact_details_cy: have_attributes(
128+
phone: "0291 111 1111",
129+
email: "cymraeg@example.gov.uk",
130+
)),
131+
)
132+
end
83133
end
84134

85135
context "when there is an error during processing" do

spec/mailers/form_submission_confirmation_mailer_spec.rb

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
require "rails_helper"
22

33
describe FormSubmissionConfirmationMailer, type: :mailer do
4+
let(:submission_locale) { :en }
45
let(:mail) do
56
described_class.send_confirmation_email(what_happens_next_markdown:,
67
support_contact_details:,
78
notify_response_id: "for-my-ref",
89
confirmation_email_address:,
9-
mailer_options:)
10+
mailer_options:,
11+
submission_locale:)
1012
end
1113
let(:mailer_options) do
1214
FormSubmissionService::MailerOptions.new(title:,
@@ -30,24 +32,18 @@
3032
Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id = "7891011"
3133
end
3234

33-
context "when the request locale is not set" do
34-
it "uses the English language template" do
35-
expect(mail.govuk_notify_template).to eq("123456")
36-
end
37-
end
38-
39-
context "when the request locale is set to :en" do
40-
include_context "with locale set to :en"
35+
context "when submission_locale is :en" do
36+
let(:submission_locale) { :en }
4137

4238
it "uses the English language template" do
4339
expect(mail.govuk_notify_template).to eq("123456")
4440
end
4541
end
4642

47-
context "when the request locale is set to :cy" do
48-
include_context "with locale set to :cy"
43+
context "when submission_locale is :cy" do
44+
let(:submission_locale) { :cy }
4945

50-
it "uses the Welsh language template" do
46+
it "uses the bilingual template" do
5147
expect(mail.govuk_notify_template).to eq("7891011")
5248
end
5349
end
@@ -64,10 +60,55 @@
6460
expect(mail.govuk_notify_personalisation[:what_happens_next_text]).to eq("Please wait for a response")
6561
end
6662

63+
context "when what_happens_next_markdown_cy is provided" do
64+
let(:mail) do
65+
described_class.send_confirmation_email(what_happens_next_markdown:,
66+
support_contact_details:,
67+
notify_response_id: "for-my-ref",
68+
confirmation_email_address:,
69+
mailer_options:,
70+
submission_locale:,
71+
what_happens_next_markdown_cy: "Arhoswch am ymateb")
72+
end
73+
74+
it "includes the Welsh what happens next" do
75+
expect(mail.govuk_notify_personalisation[:what_happens_next_text_cy]).to eq("Arhoswch am ymateb")
76+
end
77+
end
78+
79+
context "when what_happens_next_markdown_cy is not provided" do
80+
it "falls back to the English what happens next" do
81+
expect(mail.govuk_notify_personalisation[:what_happens_next_text_cy]).to eq("Please wait for a response")
82+
end
83+
end
84+
6785
it "includes the forms support contact details" do
6886
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)")
6987
end
7088

89+
context "when support_contact_details_cy is not provided" do
90+
it "falls back to support_contact_details formatted with Welsh locale" do
91+
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)")
92+
end
93+
end
94+
95+
context "when support_contact_details_cy is provided" do
96+
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") }
97+
let(:mail) do
98+
described_class.send_confirmation_email(what_happens_next_markdown:,
99+
support_contact_details:,
100+
support_contact_details_cy: welsh_support,
101+
notify_response_id: "for-my-ref",
102+
confirmation_email_address:,
103+
mailer_options:,
104+
submission_locale:)
105+
end
106+
107+
it "uses the Welsh support details formatted with Welsh locale" do
108+
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)")
109+
end
110+
end
111+
71112
context "when what happens next is missing" do
72113
let(:what_happens_next_markdown) { nil }
73114

@@ -143,30 +184,15 @@
143184
end
144185
end
145186

146-
context "when the request locale is not set" do
147-
it "includes the date user submitted the form in English" do
148-
travel_to timestamp do
149-
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
150-
end
151-
end
152-
end
153-
154-
context "when the request locale is set to :en" do
155-
include_context "with locale set to :en"
156-
it "includes the date user submitted the form in English" do
157-
travel_to timestamp do
158-
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
159-
end
187+
it "includes the date user submitted the form in English" do
188+
travel_to timestamp do
189+
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
160190
end
161191
end
162192

163-
context "when the request locale is set to :cy" do
164-
include_context "with locale set to :cy"
165-
166-
it "includes the date user submitted the form in Welsh" do
167-
travel_to timestamp do
168-
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 Medi 2022")
169-
end
193+
it "includes the date user submitted the form in Welsh" do
194+
travel_to timestamp do
195+
expect(mail.govuk_notify_personalisation[:submission_date_cy]).to eq("14 Medi 2022")
170196
end
171197
end
172198
end

spec/requests/forms/check_your_answers_controller_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,12 @@
497497
expected_personalisation = {
498498
title: form_data.name,
499499
what_happens_next_text: form_data.what_happens_next_markdown,
500+
what_happens_next_text_cy: form_data.what_happens_next_markdown,
500501
support_contact_details: contact_support_details_format,
502+
support_contact_details_cy: I18n.with_locale(:cy) { contact_support_details_format },
501503
submission_time: "10:00am",
502504
submission_date: "14 December 2022",
505+
submission_date_cy: "14 Rhagfyr 2022",
503506
test: "no",
504507
submission_reference: reference,
505508
include_payment_link: "no",

0 commit comments

Comments
 (0)