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
37 changes: 30 additions & 7 deletions app/jobs/send_submission_batch_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,47 @@ def perform(delivery:)
end
end

message_id = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, date: batch_begin_date, mode:).send_batch
batch_service = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, mode:)

message_id = send_email(batch_service, delivery, batch_begin_date)

delivery.new_attempt!
delivery.update!(
delivery_reference: message_id,
)

CurrentJobLoggingAttributes.delivery_reference = delivery.delivery_reference
EventLogger.log_form_event("daily_batch_email_sent", {
log_batch_sent(delivery, batch_begin_date, mode)
log_submissions_included_in_batch(delivery, batch_begin_date)
end

private

def send_email(batch_service, delivery, batch_begin_date)
if delivery.daily?
batch_service.send_daily_batch(date: batch_begin_date)
elsif delivery.weekly?
batch_service.send_weekly_batch(begin_date: batch_begin_date, end_date: batch_begin_date + 6.days)
else
raise StandardError, "Unexpected delivery schedule: #{delivery.delivery_schedule}"
end
end

def log_batch_sent(delivery, batch_begin_date, mode)
event_name = delivery.daily? ? "daily_batch_email_sent" : "weekly_batch_email_sent"
EventLogger.log_form_event(event_name, {
mode: mode.to_s,
batch_date: batch_begin_date,
number_of_submissions: submissions.count,
batch_begin_date: batch_begin_date,
number_of_submissions: delivery.submissions.count,
})
end

submissions.each do |submission|
EventLogger.log_form_event("included_in_daily_batch_email", {
def log_submissions_included_in_batch(delivery, batch_begin_date)
event_name = delivery.daily? ? "included_in_daily_batch_email" : "included_in_weekly_batch_email"
delivery.submissions.each do |submission|
EventLogger.log_form_event(event_name, {
submission_reference: submission.reference,
batch_date: batch_begin_date,
batch_begin_date: batch_begin_date,
})
end
end
Expand Down
36 changes: 31 additions & 5 deletions app/mailers/aws_ses_submission_batch_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
reply_to: Settings.ses_submission_email.reply_to_email_address,
delivery_method: Rails.configuration.x.aws_ses_form_submission_mailer["delivery_method"]

def batch_submission_email(form:, date:, mode:, files:)
def daily_submission_batch_email(form:, date:, mode:, files:)
@form_name = form.name
@date = date.strftime("%-d %B %Y")

Check failure on line 8 in app/mailers/aws_ses_submission_batch_mailer.rb

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "%-d %B %Y" 3 times.

See more on https://sonarcloud.io/project/issues?id=alphagov_forms-runner&issues=AZ0AWySnOvebFd6oAU3T&open=AZ0AWySnOvebFd6oAU3T&pullRequest=1975
@mode = mode
@filenames = files.keys
@subject = subject
@subject = daily_batch_subject

files.each do |name, file|
attachments[name] = {
encoding: "base64",
content: Base64.encode64(file),
}
end

mail(to: form.submission_email, subject: @subject)
end

def weekly_submission_batch_email(form:, begin_date:, end_date:, mode:, files:)
@form_name = form.name
@begin_date = begin_date.strftime("%-d %B %Y")
@end_date = end_date.strftime("%-d %B %Y")
@mode = mode
@filenames = files.keys
@subject = weekly_batch_subject

files.each do |name, file|
attachments[name] = {
Expand All @@ -22,11 +40,19 @@

private

def subject
def daily_batch_subject
if @mode.preview?
I18n.t("mailer.submission_batch.daily.subject_preview", form_name: @form_name, date: @date)
else
I18n.t("mailer.submission_batch.daily.subject", form_name: @form_name, date: @date)
end
end

def weekly_batch_subject
if @mode.preview?
I18n.t("mailer.submission_batch.subject_preview", form_name: @form_name, date: @date)
I18n.t("mailer.submission_batch.weekly.subject_preview", form_name: @form_name, begin_date: @begin_date, end_date: @end_date)
else
I18n.t("mailer.submission_batch.subject", form_name: @form_name, date: @date)
I18n.t("mailer.submission_batch.weekly.subject", form_name: @form_name, begin_date: @begin_date, end_date: @end_date)
end
end
end
1 change: 1 addition & 0 deletions app/models/delivery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Delivery < ApplicationRecord
enum :delivery_schedule, {
immediate: "immediate",
daily: "daily",
weekly: "weekly",
}

def status
Expand Down
44 changes: 36 additions & 8 deletions app/services/aws_ses_submission_batch_service.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
class AwsSesSubmissionBatchService
def initialize(submissions_query:, form:, date:, mode:)
def initialize(submissions_query:, form:, mode:)
@submissions_query = submissions_query
@form = form
@date = date
@mode = mode
end

def send_batch
def send_daily_batch(date:)
files = csv_attachments(date)

mail = AwsSesSubmissionBatchMailer.daily_submission_batch_email(form: @form, date:, mode: @mode, files:).deliver_now

CurrentJobLoggingAttributes.delivery_reference = mail.message_id
mail.message_id
end

def send_weekly_batch(begin_date:, end_date:)
files = csv_attachments(end_date)

mail = AwsSesSubmissionBatchMailer.weekly_submission_batch_email(
form: @form,
begin_date:,
end_date:,
mode: @mode,
files:,
).deliver_now

CurrentJobLoggingAttributes.delivery_reference = mail.message_id
mail.message_id
end

private

def csv_attachments(date_for_filename)
files = {}

csvs = CsvGenerator.generate_batched_submissions(submissions_query: @submissions_query, is_s3_submission: false)
csvs.each.with_index(1) do |csv, index|
csv_version = csvs.size > 1 ? index : nil
filename = SubmissionFilenameGenerator.batch_csv_filename(form_name: @form.name, date: @date, mode: @mode, form_version: csv_version)
filename = SubmissionFilenameGenerator.batch_csv_filename(
form_name: @form.name,
date: date_for_filename,
mode: @mode,
form_version: csv_version,
)

files[filename] = csv
end

mail = AwsSesSubmissionBatchMailer.batch_submission_email(form: @form, date: @date, mode: @mode, files:).deliver_now

CurrentJobLoggingAttributes.delivery_reference = mail.message_id
mail.message_id
files
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<p><%= I18n.t("mailer.form_name", form_name: @form_name) %></p>

<% if @filenames.size > 1 %>
<p><%= I18n.t("mailer.submission_batch.multiple_files_explainer", date: @date) %></p>
<p><%= I18n.t("mailer.submission_batch.daily.multiple_files_explainer", date: @date) %></p>
<% else %>
<p><%= I18n.t("mailer.submission_batch.single_file_explainer", date: @date) %></p>
<p><%= I18n.t("mailer.submission_batch.daily.single_file_explainer", date: @date) %></p>
<% end %>

<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<%= I18n.t("mailer.form_name", form_name: @form_name) %>

<% if @filenames.size > 1 %>
<%= I18n.t("mailer.submission_batch.multiple_files_explainer", date: @date) %>
<%= I18n.t("mailer.submission_batch.daily.multiple_files_explainer", date: @date) %>
<% else %>
<%= I18n.t("mailer.submission_batch.single_file_explainer", date: @date) %>
<%= I18n.t("mailer.submission_batch.daily.single_file_explainer", date: @date) %>
<% end %>

<% @filenames.each do |filename| %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<% if @mode.preview? %>
<p>
<%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %>
</p>
<% end %>

<p><%= I18n.t("mailer.form_name", form_name: @form_name) %></p>

<% if @filenames.size > 1 %>
<p><%= I18n.t("mailer.submission_batch.weekly.multiple_files_explainer", begin_date: @begin_date, end_date: @end_date) %></p>
<% else %>
<p><%= I18n.t("mailer.submission_batch.weekly.single_file_explainer", begin_date: @begin_date, end_date: @end_date) %></p>
<% end %>

<ul>
<% @filenames.each do |filename| %>
<li><%= filename %></li>
<% end %>
</ul>

<p style="margin: 0 0 20px 0; border-left: 10px solid #B1B4B6; padding: 15px;">
<%= I18n.t("mailer.check_before_using") %>
</p>

<p><%= I18n.t("mailer.submission_batch.submissions_are_split") %></p>

<p><%= I18n.t("mailer.submission_batch.file_upload_questions") %></p>

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

<div
style="margin: 0 0 20px 0; border-left: 10px solid #B1B4B6;
padding: 15px 0 0.1px 15px; font-size: 19px; line-height: 25px;"
>
<h2><%= I18n.t("mailer.cannot_reply.heading") %></h2>
<%= I18n.t("mailer.cannot_reply.contact_forms_team_html").html_safe %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% if @mode.preview? %>
<%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %>
<% end %>

<%= I18n.t("mailer.form_name", form_name: @form_name) %>

<% if @filenames.size > 1 %>
<%= I18n.t("mailer.submission_batch.weekly.multiple_files_explainer", begin_date: @begin_date, end_date: @end_date) %>
<% else %>
<%= I18n.t("mailer.submission_batch.weekly.single_file_explainer", begin_date: @begin_date, end_date: @end_date) %>
<% end %>

<% @filenames.each do |filename| %>
• <%= filename %>
<% end %>

<%= I18n.t("mailer.check_before_using") %>

<%= I18n.t("mailer.submission_batch.submissions_are_split") %>

<%= I18n.t("mailer.submission_batch.file_upload_questions") %>

---

<%= I18n.t("mailer.cannot_reply.heading") %>
<%= I18n.t("mailer.cannot_reply.contact_forms_team_plain") %>
14 changes: 10 additions & 4 deletions config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,22 @@ cy:
time: 'Submitted at: %{time} on %{date}'
welsh_submission: At least one of the questions was answered in Welsh
submission_batch:
daily:
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
file_upload_questions: For forms with file upload questions, the uploaded files will only be attached to the individual completed form emails.
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
preview:
archived: These are test submissions to a preview of an archived form.
draft: These are test submissions to a preview of a draft form.
live: These are test submissions to a preview of a live form.
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
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.
weekly:
multiple_files_explainer: 'All submissions made for this form between %{begin_date} and %{end_date} are attached to this email in CSV files named:'
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:'
subject: 'Weekly form submissions: ‘%{form_name}’ - %{begin_date} to %{end_date}'
subject_preview: 'TEST WEEKLY FORM SUBMISSIONS: ‘%{form_name}’ - %{begin_date} to %{end_date}'
submission_confirmation:
default_support_contact_details: The form’s contact details for support will appear here once they’ve been added.
default_what_happens_next: The form’s information about what happens next will appear here once it has been added.
Expand Down
14 changes: 10 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,16 +450,22 @@ en:
time: 'Submitted at: %{time} on %{date}'
welsh_submission: At least one of the questions was answered in Welsh
submission_batch:
daily:
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
file_upload_questions: For forms with file upload questions, the uploaded files will only be attached to the individual completed form emails.
multiple_files_explainer: 'All submissions to this form from %{date} are attached to this email in CSV files named:'
preview:
archived: These are test submissions to a preview of an archived form.
draft: These are test submissions to a preview of a draft form.
live: These are test submissions to a preview of a live form.
single_file_explainer: 'All submissions to this form from %{date} are attached to this email in a CSV file named:'
subject: 'Daily form submissions: ‘%{form_name}’ - %{date}'
subject_preview: 'TEST DAILY FORM SUBMISSIONS: ‘%{form_name}’ - %{date}'
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.
weekly:
multiple_files_explainer: 'All submissions made for this form between %{begin_date} and %{end_date} are attached to this email in CSV files named:'
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:'
subject: 'Weekly form submissions: ‘%{form_name}’ - %{begin_date} to %{end_date}'
subject_preview: 'TEST WEEKLY FORM SUBMISSIONS: ‘%{form_name}’ - %{begin_date} to %{end_date}'
submission_confirmation:
default_support_contact_details: The form’s contact details for support will appear here once they’ve been added.
default_what_happens_next: The form’s information about what happens next will appear here once it has been added.
Expand Down
Loading
Loading