Skip to content

Commit 2dcf2a9

Browse files
authored
Merge pull request #1975 from alphagov/add-job-to-send-weekly-csv
Send emails for weekly submission batches
2 parents ed97c38 + 48d30e4 commit 2dcf2a9

14 files changed

+606
-192
lines changed

app/jobs/send_submission_batch_job.rb

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,47 @@ def perform(delivery:)
3131
end
3232
end
3333

34-
message_id = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, date: batch_begin_date, mode:).send_batch
34+
batch_service = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, mode:)
35+
36+
message_id = send_email(batch_service, delivery, batch_begin_date)
3537

3638
delivery.new_attempt!
3739
delivery.update!(
3840
delivery_reference: message_id,
3941
)
4042

4143
CurrentJobLoggingAttributes.delivery_reference = delivery.delivery_reference
42-
EventLogger.log_form_event("daily_batch_email_sent", {
44+
log_batch_sent(delivery, batch_begin_date, mode)
45+
log_submissions_included_in_batch(delivery, batch_begin_date)
46+
end
47+
48+
private
49+
50+
def send_email(batch_service, delivery, batch_begin_date)
51+
if delivery.daily?
52+
batch_service.send_daily_batch(date: batch_begin_date)
53+
elsif delivery.weekly?
54+
batch_service.send_weekly_batch(begin_date: batch_begin_date, end_date: batch_begin_date + 6.days)
55+
else
56+
raise StandardError, "Unexpected delivery schedule: #{delivery.delivery_schedule}"
57+
end
58+
end
59+
60+
def log_batch_sent(delivery, batch_begin_date, mode)
61+
event_name = delivery.daily? ? "daily_batch_email_sent" : "weekly_batch_email_sent"
62+
EventLogger.log_form_event(event_name, {
4363
mode: mode.to_s,
44-
batch_date: batch_begin_date,
45-
number_of_submissions: submissions.count,
64+
batch_begin_date: batch_begin_date,
65+
number_of_submissions: delivery.submissions.count,
4666
})
67+
end
4768

48-
submissions.each do |submission|
49-
EventLogger.log_form_event("included_in_daily_batch_email", {
69+
def log_submissions_included_in_batch(delivery, batch_begin_date)
70+
event_name = delivery.daily? ? "included_in_daily_batch_email" : "included_in_weekly_batch_email"
71+
delivery.submissions.each do |submission|
72+
EventLogger.log_form_event(event_name, {
5073
submission_reference: submission.reference,
51-
batch_date: batch_begin_date,
74+
batch_begin_date: batch_begin_date,
5275
})
5376
end
5477
end

app/mailers/aws_ses_submission_batch_mailer.rb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,30 @@ class AwsSesSubmissionBatchMailer < 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 batch_submission_email(form:, date:, mode:, files:)
6+
def daily_submission_batch_email(form:, date:, mode:, files:)
77
@form_name = form.name
88
@date = date.strftime("%-d %B %Y")
99
@mode = mode
1010
@filenames = files.keys
11-
@subject = subject
11+
@subject = daily_batch_subject
12+
13+
files.each do |name, file|
14+
attachments[name] = {
15+
encoding: "base64",
16+
content: Base64.encode64(file),
17+
}
18+
end
19+
20+
mail(to: form.submission_email, subject: @subject)
21+
end
22+
23+
def weekly_submission_batch_email(form:, begin_date:, end_date:, mode:, files:)
24+
@form_name = form.name
25+
@begin_date = begin_date.strftime("%-d %B %Y")
26+
@end_date = end_date.strftime("%-d %B %Y")
27+
@mode = mode
28+
@filenames = files.keys
29+
@subject = weekly_batch_subject
1230

1331
files.each do |name, file|
1432
attachments[name] = {
@@ -22,11 +40,19 @@ def batch_submission_email(form:, date:, mode:, files:)
2240

2341
private
2442

25-
def subject
43+
def daily_batch_subject
44+
if @mode.preview?
45+
I18n.t("mailer.submission_batch.daily.subject_preview", form_name: @form_name, date: @date)
46+
else
47+
I18n.t("mailer.submission_batch.daily.subject", form_name: @form_name, date: @date)
48+
end
49+
end
50+
51+
def weekly_batch_subject
2652
if @mode.preview?
27-
I18n.t("mailer.submission_batch.subject_preview", form_name: @form_name, date: @date)
53+
I18n.t("mailer.submission_batch.weekly.subject_preview", form_name: @form_name, begin_date: @begin_date, end_date: @end_date)
2854
else
29-
I18n.t("mailer.submission_batch.subject", form_name: @form_name, date: @date)
55+
I18n.t("mailer.submission_batch.weekly.subject", form_name: @form_name, begin_date: @begin_date, end_date: @end_date)
3056
end
3157
end
3258
end

app/models/delivery.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Delivery < ApplicationRecord
99
enum :delivery_schedule, {
1010
immediate: "immediate",
1111
daily: "daily",
12+
weekly: "weekly",
1213
}
1314

1415
def status
Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
class AwsSesSubmissionBatchService
2-
def initialize(submissions_query:, form:, date:, mode:)
2+
def initialize(submissions_query:, form:, mode:)
33
@submissions_query = submissions_query
44
@form = form
5-
@date = date
65
@mode = mode
76
end
87

9-
def send_batch
8+
def send_daily_batch(date:)
9+
files = csv_attachments(date)
10+
11+
mail = AwsSesSubmissionBatchMailer.daily_submission_batch_email(form: @form, date:, mode: @mode, files:).deliver_now
12+
13+
CurrentJobLoggingAttributes.delivery_reference = mail.message_id
14+
mail.message_id
15+
end
16+
17+
def send_weekly_batch(begin_date:, end_date:)
18+
files = csv_attachments(end_date)
19+
20+
mail = AwsSesSubmissionBatchMailer.weekly_submission_batch_email(
21+
form: @form,
22+
begin_date:,
23+
end_date:,
24+
mode: @mode,
25+
files:,
26+
).deliver_now
27+
28+
CurrentJobLoggingAttributes.delivery_reference = mail.message_id
29+
mail.message_id
30+
end
31+
32+
private
33+
34+
def csv_attachments(date_for_filename)
1035
files = {}
1136

1237
csvs = CsvGenerator.generate_batched_submissions(submissions_query: @submissions_query, is_s3_submission: false)
1338
csvs.each.with_index(1) do |csv, index|
1439
csv_version = csvs.size > 1 ? index : nil
15-
filename = SubmissionFilenameGenerator.batch_csv_filename(form_name: @form.name, date: @date, mode: @mode, form_version: csv_version)
40+
filename = SubmissionFilenameGenerator.batch_csv_filename(
41+
form_name: @form.name,
42+
date: date_for_filename,
43+
mode: @mode,
44+
form_version: csv_version,
45+
)
46+
1647
files[filename] = csv
1748
end
1849

19-
mail = AwsSesSubmissionBatchMailer.batch_submission_email(form: @form, date: @date, mode: @mode, files:).deliver_now
20-
21-
CurrentJobLoggingAttributes.delivery_reference = mail.message_id
22-
mail.message_id
50+
files
2351
end
2452
end

app/views/aws_ses_submission_batch_mailer/batch_submission_email.html.erb renamed to app/views/aws_ses_submission_batch_mailer/daily_submission_batch_email.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<p><%= I18n.t("mailer.form_name", form_name: @form_name) %></p>
88

99
<% if @filenames.size > 1 %>
10-
<p><%= I18n.t("mailer.submission_batch.multiple_files_explainer", date: @date) %></p>
10+
<p><%= I18n.t("mailer.submission_batch.daily.multiple_files_explainer", date: @date) %></p>
1111
<% else %>
12-
<p><%= I18n.t("mailer.submission_batch.single_file_explainer", date: @date) %></p>
12+
<p><%= I18n.t("mailer.submission_batch.daily.single_file_explainer", date: @date) %></p>
1313
<% end %>
1414

1515
<ul>

app/views/aws_ses_submission_batch_mailer/batch_submission_email.text.erb renamed to app/views/aws_ses_submission_batch_mailer/daily_submission_batch_email.text.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<%= I18n.t("mailer.form_name", form_name: @form_name) %>
66

77
<% if @filenames.size > 1 %>
8-
<%= I18n.t("mailer.submission_batch.multiple_files_explainer", date: @date) %>
8+
<%= I18n.t("mailer.submission_batch.daily.multiple_files_explainer", date: @date) %>
99
<% else %>
10-
<%= I18n.t("mailer.submission_batch.single_file_explainer", date: @date) %>
10+
<%= I18n.t("mailer.submission_batch.daily.single_file_explainer", date: @date) %>
1111
<% end %>
1212

1313
<% @filenames.each do |filename| %>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<% if @mode.preview? %>
2+
<p>
3+
<%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %>
4+
</p>
5+
<% end %>
6+
7+
<p><%= I18n.t("mailer.form_name", form_name: @form_name) %></p>
8+
9+
<% if @filenames.size > 1 %>
10+
<p><%= I18n.t("mailer.submission_batch.weekly.multiple_files_explainer", begin_date: @begin_date, end_date: @end_date) %></p>
11+
<% else %>
12+
<p><%= I18n.t("mailer.submission_batch.weekly.single_file_explainer", begin_date: @begin_date, end_date: @end_date) %></p>
13+
<% end %>
14+
15+
<ul>
16+
<% @filenames.each do |filename| %>
17+
<li><%= filename %></li>
18+
<% end %>
19+
</ul>
20+
21+
<p style="margin: 0 0 20px 0; border-left: 10px solid #B1B4B6; padding: 15px;">
22+
<%= I18n.t("mailer.check_before_using") %>
23+
</p>
24+
25+
<p><%= I18n.t("mailer.submission_batch.submissions_are_split") %></p>
26+
27+
<p><%= I18n.t("mailer.submission_batch.file_upload_questions") %></p>
28+
29+
<hr style="border: 0; height: 1px; background: #B1B4B6; margin: 30px 0 30px 0;">
30+
31+
<div
32+
style="margin: 0 0 20px 0; border-left: 10px solid #B1B4B6;
33+
padding: 15px 0 0.1px 15px; font-size: 19px; line-height: 25px;"
34+
>
35+
<h2><%= I18n.t("mailer.cannot_reply.heading") %></h2>
36+
<%= I18n.t("mailer.cannot_reply.contact_forms_team_html").html_safe %>
37+
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<% if @mode.preview? %>
2+
<%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %>
3+
<% end %>
4+
5+
<%= I18n.t("mailer.form_name", form_name: @form_name) %>
6+
7+
<% if @filenames.size > 1 %>
8+
<%= I18n.t("mailer.submission_batch.weekly.multiple_files_explainer", begin_date: @begin_date, end_date: @end_date) %>
9+
<% else %>
10+
<%= I18n.t("mailer.submission_batch.weekly.single_file_explainer", begin_date: @begin_date, end_date: @end_date) %>
11+
<% end %>
12+
13+
<% @filenames.each do |filename| %>
14+
<%= filename %>
15+
<% end %>
16+
17+
<%= I18n.t("mailer.check_before_using") %>
18+
19+
<%= I18n.t("mailer.submission_batch.submissions_are_split") %>
20+
21+
<%= I18n.t("mailer.submission_batch.file_upload_questions") %>
22+
23+
---
24+
25+
<%= I18n.t("mailer.cannot_reply.heading") %>
26+
<%= I18n.t("mailer.cannot_reply.contact_forms_team_plain") %>

config/locales/cy.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,22 @@ cy:
468468
time: 'Submitted at: %{time} on %{date}'
469469
welsh_submission: At least one of the questions was answered in Welsh
470470
submission_batch:
471+
daily:
472+
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
473+
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
474+
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
475+
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
471476
file_upload_questions: For forms with file upload questions, the uploaded files will only be attached to the individual completed form emails.
472-
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
473477
preview:
474478
archived: These are test submissions to a preview of an archived form.
475479
draft: These are test submissions to a preview of a draft form.
476480
live: These are test submissions to a preview of a live form.
477-
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
478-
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
479-
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
480481
submissions_are_split: Submissions are split into separate CSV files if the form's been changed in a way that affects the structure of the CSV.
482+
weekly:
483+
multiple_files_explainer: 'All submissions made for this form between %{begin_date} and %{end_date} are attached to this email in CSV files named:'
484+
single_file_explainer: 'All submissions made for this form between %{begin_date} and %{end_date} are attached to this email in a CSV file named:'
485+
subject: 'Weekly form submissions: ‘%{form_name}’ - %{begin_date} to %{end_date}'
486+
subject_preview: 'TEST WEEKLY FORM SUBMISSIONS: ‘%{form_name}’ - %{begin_date} to %{end_date}'
481487
submission_confirmation:
482488
default_support_contact_details: The form’s contact details for support will appear here once they’ve been added.
483489
default_what_happens_next: The form’s information about what happens next will appear here once it has been added.

config/locales/en.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,22 @@ en:
468468
time: 'Submitted at: %{time} on %{date}'
469469
welsh_submission: At least one of the questions was answered in Welsh
470470
submission_batch:
471+
daily:
472+
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
473+
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
474+
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
475+
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
471476
file_upload_questions: For forms with file upload questions, the uploaded files will only be attached to the individual completed form emails.
472-
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
473477
preview:
474478
archived: These are test submissions to a preview of an archived form.
475479
draft: These are test submissions to a preview of a draft form.
476480
live: These are test submissions to a preview of a live form.
477-
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
478-
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
479-
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
480481
submissions_are_split: Submissions are split into separate CSV files if the form's been changed in a way that affects the structure of the CSV.
482+
weekly:
483+
multiple_files_explainer: 'All submissions made for this form between %{begin_date} and %{end_date} are attached to this email in CSV files named:'
484+
single_file_explainer: 'All submissions made for this form between %{begin_date} and %{end_date} are attached to this email in a CSV file named:'
485+
subject: 'Weekly form submissions: ‘%{form_name}’ - %{begin_date} to %{end_date}'
486+
subject_preview: 'TEST WEEKLY FORM SUBMISSIONS: ‘%{form_name}’ - %{begin_date} to %{end_date}'
481487
submission_confirmation:
482488
default_support_contact_details: The form’s contact details for support will appear here once they’ve been added.
483489
default_what_happens_next: The form’s information about what happens next will appear here once it has been added.

0 commit comments

Comments
 (0)