Skip to content
Closed
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ You can enable Redis sessions by providing the Redis connection URL in the envir

### Configuring GOV.UK Notify

We use [GOV.UK Notify] to send bounce notifications to group and organisation admins.
We use [GOV.UK Notify] to send the following emails:
- confirmation emails to users who have submitted a form, if they have not asked for a copy of their answers
- bounce notifications to group and organisation admins

If you want to test the Notify functionality locally, you will need to get a test API key from the Notify service. Add it as an environment variable under `SETTINGS__GOVUK_NOTIFY__API_KEY` or add it to a local config file:

Expand Down
21 changes: 12 additions & 9 deletions app/jobs/send_confirmation_email_job.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
class SendConfirmationEmailJob < ApplicationJob
queue_as :confirmation_emails

# TODO: remove notify_response_id once deployed.
# we are passing this is so as not to break any job already serialised in solid_queue at deploy time.
# rubocop:disable Lint/UnusedMethodArgument
def perform(submission:, confirmation_email_address:, notify_response_id: nil, include_copy_of_answers: false)
def perform(submission:, notify_response_id:, confirmation_email_address:, include_copy_of_answers: false)
set_submission_logging_attributes(submission:)
# rubocop:enable Lint/UnusedMethodArgument

# The job will use the locale at the time it was created. Force it to be "en" as we send multilingual emails for
# forms submitted in Welsh.
I18n.with_locale("en") do
mail = AwsSesSubmissionConfirmationMailer.submission_confirmation_email(
submission:, confirmation_email_address:, include_copy_of_answers:,
)
mail = if include_copy_of_answers
AwsSesSubmissionConfirmationMailer.submission_confirmation_email(
submission:, confirmation_email_address:, include_copy_of_answers:,
)
else
FormSubmissionConfirmationMailer.send_confirmation_email(
submission:, notify_response_id:, confirmation_email_address:,
)
end

mail.deliver_now
CurrentJobLoggingAttributes.confirmation_email_id = mail.message_id
CurrentJobLoggingAttributes.confirmation_email_id = mail.govuk_notify_response&.id.presence || mail.message_id
end
rescue StandardError
CloudWatchService.record_job_failure_metric(self.class.name)
Expand Down
73 changes: 73 additions & 0 deletions app/mailers/form_submission_confirmation_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class FormSubmissionConfirmationMailer < GovukNotifyRails::Mailer
include NotifyUtils
include EmailFormatHelper

def send_confirmation_email(submission:, notify_response_id:, confirmation_email_address:)
@submission_locale = submission.submission_locale.to_sym
set_template(template_id)

form = submission.form
welsh_form = submission.welsh_form
what_happens_next_text = form.what_happens_next_markdown.presence || default_what_happens_next_text
set_personalisation(
title: form.name,
title_cy: welsh_form&.name || form.name,
what_happens_next_text:,
what_happens_next_text_cy: welsh_form&.what_happens_next_markdown.presence || what_happens_next_text,
support_contact_details: format_support_details(form.support_details).presence || default_support_contact_details_text,
support_contact_details_cy: welsh_support_details(form, welsh_form),
submission_time: submission.submission_time.strftime("%l:%M%P").strip,
submission_date: I18n.l(submission.submission_time, format: "%-d %B %Y", locale: :en),
submission_date_cy: I18n.l(submission.submission_time, format: "%-d %B %Y", locale: :cy),
test: make_notify_boolean(submission.preview?),
submission_reference: submission.reference,
include_payment_link: make_notify_boolean(submission.payment_url.present?),
payment_link: form.payment_url_with_reference(submission.reference) || "",
payment_link_cy: welsh_form&.payment_url_with_reference(submission.reference) || "",
)

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 welsh_support_details(form, welsh_form)
format_support_details(welsh_form&.support_details, locale: :cy).presence ||
format_support_details(form.support_details, locale: :cy).presence ||
default_support_contact_details_text
end

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 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
end
1 change: 1 addition & 0 deletions app/services/form_submission_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def validate_confirmation_email_address
def enqueue_send_confirmation_email_job(submission:)
SendConfirmationEmailJob.perform_later(
submission:,
notify_response_id: email_confirmation_input.confirmation_email_reference,
confirmation_email_address: confirmation_email_address,
include_copy_of_answers: send_copy_of_answers?,
) do |job|
Expand Down
41 changes: 37 additions & 4 deletions spec/jobs/send_confirmation_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
submission_locale: "en",
)
end
let(:notify_response_id) { "confirmation-ref" }
let(:confirmation_email_address) { "testing@gov.uk" }

context "when include_copy_of_answers is false" do
Expand All @@ -42,6 +43,7 @@
expect {
described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
)
}.to change(ActionMailer::Base.deliveries, :count).by(1)
Expand All @@ -51,26 +53,53 @@
end

it "builds mailer arguments from the submission" do
allow(AwsSesSubmissionConfirmationMailer).to receive(:submission_confirmation_email).and_call_original
allow(FormSubmissionConfirmationMailer).to receive(:send_confirmation_email).and_call_original

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

expect(AwsSesSubmissionConfirmationMailer).to have_received(:submission_confirmation_email).with(
expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
submission:,
notify_response_id: "confirmation-ref",
confirmation_email_address: "testing@gov.uk",
include_copy_of_answers: false,
)
end

context "when submission locale is Welsh" do
let(:welsh_form_document) { build(:v2_form_document, name: "Welsh Form") }

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:,
confirmation_email_address:,
)

mail = ActionMailer::Base.deliveries.last
expect(mail.govuk_notify_template).to eq("7891011")
end
end
end

context "when include_copy_of_answers is true" do
it "sends the confirmation email including the answers" do
expect {
described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
include_copy_of_answers: true,
)
Expand All @@ -86,6 +115,7 @@
I18n.with_locale(:cy) do
described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
include_copy_of_answers: true,
)
Expand Down Expand Up @@ -118,6 +148,7 @@
it "passes the confirmation email configuration set name to SES" do
described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
include_copy_of_answers: true,
)
Expand All @@ -131,14 +162,15 @@

context "when there is an error during processing" do
before do
allow(AwsSesSubmissionConfirmationMailer).to receive(:submission_confirmation_email).and_raise(StandardError, "Test error")
allow(FormSubmissionConfirmationMailer).to receive(:send_confirmation_email).and_raise(StandardError, "Test error")
allow(CloudWatchService).to receive(:record_job_failure_metric)
end

it "raises an error" do
expect {
described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
)
}.to raise_error(StandardError, "Test error")
Expand All @@ -147,6 +179,7 @@
it "sends cloudwatch metric for failure" do
described_class.perform_now(
submission:,
notify_response_id:,
confirmation_email_address:,
)
expect(CloudWatchService).to have_received(:record_job_failure_metric).with("SendConfirmationEmailJob")
Expand Down
Loading