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
20 changes: 3 additions & 17 deletions app/jobs/send_confirmation_email_job.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
class SendConfirmationEmailJob < ApplicationJob
queue_as :confirmation_emails
MailerOptions = Data.define(:title, :is_preview, :timestamp, :submission_reference, :payment_url)

def perform(submission:, notify_response_id:, confirmation_email_address:)
set_submission_logging_attributes(submission:)

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,
form:,
welsh_form:,
submission:,
notify_response_id:,
confirmation_email_address:,
mailer_options: mailer_options_for(submission:, form:),
submission_locale: submission.submission_locale,
)

mail.deliver_now
Expand All @@ -27,16 +23,6 @@ def perform(submission:, notify_response_id:, confirmation_email_address:)

private

def mailer_options_for(submission:, form:)
MailerOptions.new(
title: form.name,
is_preview: submission.preview?,
timestamp: submission.submission_time,
submission_reference: submission.reference,
payment_url: submission.payment_url,
)
end

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

Expand Down
48 changes: 26 additions & 22 deletions app/mailers/form_submission_confirmation_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
class FormSubmissionConfirmationMailer < GovukNotifyRails::Mailer
include NotifyUtils

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
def send_confirmation_email(form:, welsh_form:, submission:, notify_response_id:, confirmation_email_address:)
@submission_locale = submission.submission_locale.to_sym
set_template(template_id)

what_happens_next_text = form.what_happens_next_markdown.presence || default_what_happens_next_text
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 || "",
title: 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: submission.payment_url || "",
)

set_reference(notify_response_id)
Expand All @@ -31,11 +29,11 @@ def send_confirmation_email(what_happens_next_markdown:, support_contact_details
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
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?
Expand All @@ -48,6 +46,12 @@ def format_support_details(support_details, locale: :en)

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
Expand Down
2 changes: 0 additions & 2 deletions app/services/form_submission_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def call(**args)
end
end

MailerOptions = Data.define(:title, :is_preview, :timestamp, :submission_reference, :payment_url)

def initialize(current_context:, email_confirmation_input:, mode:)
@current_context = current_context
@form = current_context.form
Expand Down
43 changes: 6 additions & 37 deletions spec/jobs/send_confirmation_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,16 @@
)

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: "help@example.gov.uk",
url: "https://example.gov.uk/help",
url_text: "Get help",
),
support_contact_details_cy: nil,
form: submission.form,
welsh_form: nil,
submission:,
notify_response_id: "confirmation-ref",
confirmation_email_address: "testing@gov.uk",
mailer_options: an_instance_of(SendConfirmationEmailJob::MailerOptions),
submission_locale: "en",
)
end

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: "cymraeg@example.gov.uk")
end
let(:welsh_form_document) { build(:v2_form_document, name: "Welsh Form") }

before do
submission.update!(submission_locale: "cy")
Expand All @@ -100,21 +86,7 @@
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(
hash_including(what_happens_next_markdown_cy: "Arhoswch am ymateb"),
)
end

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

described_class.perform_now(
Expand All @@ -124,10 +96,7 @@
)

expect(FormSubmissionConfirmationMailer).to have_received(:send_confirmation_email).with(
hash_including(support_contact_details_cy: have_attributes(
phone: "0291 111 1111",
email: "cymraeg@example.gov.uk",
)),
hash_including(welsh_form: have_attributes(name: welsh_form_document.name)),
)
end
end
Expand Down
Loading