Skip to content

Commit 4f3bc95

Browse files
committed
Add JSON file attached message to submission emails
1 parent 2053c77 commit 4f3bc95

File tree

9 files changed

+52
-3
lines changed

9 files changed

+52
-3
lines changed

app/mailers/aws_ses_form_submission_mailer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ class AwsSesFormSubmissionMailer < ApplicationMailer
33
reply_to: Settings.ses_submission_email.reply_to_email_address,
44
delivery_method: Rails.configuration.x.aws_ses_form_submission_mailer["delivery_method"]
55

6-
def submission_email(answer_content_html:, answer_content_plain_text:, submission:, files:, csv_filename: nil)
6+
def submission_email(answer_content_html:, answer_content_plain_text:, submission:, files:, csv_filename: nil, json_filename: nil)
77
@answer_content_html = answer_content_html
88
@answer_content_plain_text = answer_content_plain_text
99
@submission = submission
1010
@subject = email_subject
1111
@csv_filename = csv_filename
12+
@json_filename = json_filename
1213

1314
files.each do |name, file|
1415
attachments[name] = {

app/services/aws_ses_submission_service.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def deliver_submission_email
3939
answer_content_plain_text:,
4040
submission: @submission,
4141
files:,
42-
csv_filename:).deliver_now
42+
csv_filename:,
43+
json_filename:).deliver_now
4344

4445
CurrentJobLoggingAttributes.mail_message_id = mail.message_id
4546
mail.message_id

app/views/aws_ses_form_submission_mailer/submission_email.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<p><%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %></p>
3232
<% end %>
3333

34+
<% if @json_filename.present? %>
35+
<p><%= I18n.t("mailer.submission.json_file", filename: @json_filename) %></p>
36+
<% end %>
37+
3438
<%= @answer_content_html.html_safe %>
3539

3640
<hr style="border: 0; height: 1px; background: #B1B4B6; margin: 30px 0 30px 0;">

app/views/aws_ses_form_submission_mailer/submission_email.text.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %>
2222
<% end %>
2323

24+
<% if @json_filename.present? %>
25+
<%= I18n.t("mailer.submission.json_file", filename: @json_filename) %>
26+
<% end %>
27+
2428
<%= @answer_content_plain_text %>
2529

2630
---

config/locales/cy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ cy:
433433
csv_file: 'A CSV file of these answers is attached to this email in a file named: %{filename}'
434434
file_attached: "%{filename} (attached to this email)"
435435
from: GOV.UK Forms <%{email_address}>
436+
json_file: 'A JSON file of these answers is attached to this email in a file named: %{filename}'
436437
payment: You can check that a payment has been made by using this reference number to search transactions within GOV.​UK Pay.
437438
preview: This is a test submission from a preview of this form.
438439
question_skipped: This question was skipped

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ en:
433433
csv_file: 'A CSV file of these answers is attached to this email in a file named: %{filename}'
434434
file_attached: "%{filename} (attached to this email)"
435435
from: GOV.UK Forms <%{email_address}>
436+
json_file: 'A JSON file of these answers is attached to this email in a file named: %{filename}'
436437
payment: You can check that a payment has been made by using this reference number to search transactions within GOV.​UK Pay.
437438
preview: This is a test submission from a preview of this form.
438439
question_skipped: This question was skipped

spec/mailers/aws_ses_form_submission_mailer_preview.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ def submission_email_with_csv
3737
files: {},
3838
csv_filename: "my_answers.csv")
3939
end
40+
41+
def submission_email_with_json
42+
form_document = build(:v2_form_document, submission_email: "testing@gov.uk", payment_url: "https://www.gov.uk/payments/your-payment-link")
43+
submission = build(:submission, form_document:, is_preview: false)
44+
AwsSesFormSubmissionMailer.submission_email(answer_content_html: "<h3>What's your email address?</h3><p>forms@example.gov.uk</p>",
45+
answer_content_plain_text: "What's your email address?\n\nforms@example.gov.uk",
46+
submission:,
47+
files: {},
48+
json_filename: "my_answers.json")
49+
end
4050
end

spec/mailers/aws_ses_form_submission_mailer_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "rails_helper"
22

33
describe AwsSesFormSubmissionMailer, type: :mailer do
4-
subject(:mail) { described_class.submission_email(answer_content_html:, answer_content_plain_text:, submission:, files:, csv_filename:) }
4+
subject(:mail) { described_class.submission_email(answer_content_html:, answer_content_plain_text:, submission:, files:, csv_filename:, json_filename:) }
55

66
let(:submission) do
77
build(:submission, form_document: form_document, created_at: submission_timestamp,
@@ -17,6 +17,7 @@
1717
let(:submission_reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
1818
let(:payment_url) { nil }
1919
let(:csv_filename) { nil }
20+
let(:json_filename) { nil }
2021
let(:submission_timestamp) { Time.utc(2022, 12, 14, 13, 0o0, 0o0) }
2122

2223
context "when form filler submits a completed form" do
@@ -256,6 +257,26 @@
256257
expect(part.body).not_to have_text(I18n.t("mailer.submission.csv_file", filename: csv_filename))
257258
end
258259
end
260+
261+
context "when the json file of answers is attached" do
262+
let(:json_filename) { "my_answers.json" }
263+
264+
describe "the html part" do
265+
let(:part) { mail.html_part }
266+
267+
it "includes text about the JSON filename" do
268+
expect(part.body).to have_css("p", text: I18n.t("mailer.submission.json_file", filename: json_filename))
269+
end
270+
end
271+
272+
describe "the plaintext part" do
273+
let(:part) { mail.text_part }
274+
275+
it "includes text about the JSON filename" do
276+
expect(part.body).to have_text(I18n.t("mailer.submission.json_file", filename: json_filename))
277+
end
278+
end
279+
end
259280
end
260281
end
261282

spec/services/aws_ses_submission_service_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
submission:,
6868
files: {},
6969
csv_filename: nil,
70+
json_filename: nil,
7071
).once
7172
end
7273
end
@@ -100,6 +101,7 @@
100101
submission:,
101102
files: { question.email_filename => file_content },
102103
csv_filename: nil,
104+
json_filename: nil,
103105
).once
104106
end
105107

@@ -165,6 +167,7 @@
165167
submission: submission,
166168
files: { "govuk_forms_a_great_form_#{submission_reference}.csv" => expected_csv_content },
167169
csv_filename: "govuk_forms_a_great_form_#{submission_reference}.csv",
170+
json_filename: nil,
168171
).once
169172
end
170173

@@ -196,6 +199,7 @@
196199
question.email_filename => file_content,
197200
},
198201
csv_filename: "govuk_forms_a_great_form_#{submission_reference}.csv",
202+
json_filename: nil,
199203
).once
200204
end
201205
end
@@ -214,6 +218,7 @@
214218
JSON.parse(json)["form_name"] == "A great form"
215219
end,
216220
},
221+
json_filename: "govuk_forms_a_great_form_#{submission_reference}.json",
217222
),
218223
).and_call_original
219224

@@ -254,6 +259,7 @@
254259
submission:,
255260
files: {},
256261
csv_filename: nil,
262+
json_filename: nil,
257263
).once
258264
end
259265
end

0 commit comments

Comments
 (0)