Skip to content

Commit ab73a04

Browse files
committed
Modify SendSubmissionBatchJob to also send weekly deliveries
Check the delivery_schedule for the delivery, and call the appropriate method to send the email. Update the logging to log different events for daily and weekly deliveries.
1 parent 47ed9b0 commit ab73a04

File tree

3 files changed

+98
-39
lines changed

3 files changed

+98
-39
lines changed

app/jobs/send_submission_batch_job.rb

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,46 @@ def perform(delivery:)
3232
end
3333

3434
batch_service = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, mode:)
35-
message_id = batch_service.send_daily_batch(date: batch_begin_date)
35+
36+
message_id = send_email(batch_service, delivery, batch_begin_date)
3637

3738
delivery.new_attempt!
3839
delivery.update!(
3940
delivery_reference: message_id,
4041
)
4142

4243
CurrentJobLoggingAttributes.delivery_reference = delivery.delivery_reference
43-
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, {
4463
mode: mode.to_s,
45-
batch_date: batch_begin_date,
46-
number_of_submissions: submissions.count,
64+
batch_begin_date: batch_begin_date,
65+
number_of_submissions: delivery.submissions.count,
4766
})
67+
end
4868

49-
submissions.each do |submission|
50-
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, {
5173
submission_reference: submission.reference,
52-
batch_date: batch_begin_date,
74+
batch_begin_date: batch_begin_date,
5375
})
5476
end
5577
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

spec/jobs/send_submission_batch_job_spec.rb

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@
6969
perform_enqueued_jobs
7070
end
7171

72-
it "sends an email" do
73-
expect(ActionMailer::Base.deliveries.count).to eq(1)
74-
expect(mail.to).to include(form_document.submission_email)
75-
end
76-
77-
it "includes the date in London time in the subject" do
78-
expect(mail.subject).to include("10 April 2025")
79-
end
80-
8172
it "updates the delivery" do
8273
expect(delivery.reload.delivery_reference).to eq(mail.message_id)
8374
expect(delivery.reload.last_attempt_at).to be_within(1.second).of(@job_ran_at)
@@ -94,39 +85,84 @@
9485
failure_reason: "bounced")
9586
end
9687

97-
it "updates the resets the delivery details" do
88+
it "resets the delivery details" do
9889
expect(delivery.reload.delivered_at).to be_nil
9990
expect(delivery.reload.failed_at).to be_nil
10091
expect(delivery.reload.failure_reason).to be_nil
10192
end
10293
end
10394

104-
it "attaches a csv with the expected filename" do
105-
expect(mail.attachments).not_to be_empty
95+
context "when the delivery is for a daily batch" do
96+
it "sends an email" do
97+
expect(ActionMailer::Base.deliveries.count).to eq(1)
98+
expect(mail.to).to include(form_document.submission_email)
99+
end
106100

107-
filenames = mail.attachments.map(&:filename)
108-
expect(filenames).to contain_exactly("govuk_forms_my_form_2025-04-10.csv")
109-
end
101+
it "includes the date in London time in the subject" do
102+
expect(mail.subject).to include("10 April 2025")
103+
end
104+
105+
it "attaches a csv with the expected filename" do
106+
expect(mail.attachments).not_to be_empty
107+
108+
filenames = mail.attachments.map(&:filename)
109+
expect(filenames).to contain_exactly("govuk_forms_my_form_2025-04-10.csv")
110+
end
110111

111-
it "attaches a csv containing header plus one line per submission" do
112-
csv_content = mail.attachments.first.decoded
113-
expect(csv_content.lines.count).to eq(submissions.count + 1)
112+
it "attaches a csv containing header plus one line per submission" do
113+
csv_content = mail.attachments.first.decoded
114+
expect(csv_content.lines.count).to eq(submissions.count + 1)
115+
end
116+
117+
it "logs that the email was sent" do
118+
expect(log_lines).to include(
119+
hash_including(
120+
"event" => "form_daily_batch_email_sent",
121+
"form_id" => form_id,
122+
"mode" => mode_string,
123+
"preview" => "false",
124+
"batch_begin_date" => date.to_s,
125+
"number_of_submissions" => submissions.count,
126+
"delivery_reference" => mail.message_id,
127+
"delivery_id" => delivery.id,
128+
"job_id" => @job_id,
129+
),
130+
)
131+
end
114132
end
115133

116-
it "logs that the email was sent" do
117-
expect(log_lines).to include(
118-
hash_including(
119-
"event" => "form_daily_batch_email_sent",
120-
"form_id" => form_id,
121-
"mode" => mode_string,
122-
"preview" => "false",
123-
"batch_date" => date.to_s,
124-
"number_of_submissions" => submissions.count,
125-
"delivery_reference" => mail.message_id,
126-
"delivery_id" => delivery.id,
127-
"job_id" => @job_id,
128-
),
129-
)
134+
context "when the delivery is for a weekly batch" do
135+
let(:delivery) { create(:delivery, delivery_schedule: "weekly", submissions:, batch_begin_at:) }
136+
137+
it "sends an email" do
138+
expect(ActionMailer::Base.deliveries.count).to eq(1)
139+
expect(mail.to).to include(form_document.submission_email)
140+
end
141+
142+
it "includes the date range in London time in the subject" do
143+
expect(mail.subject).to include("10 April 2025 to 16 April 2025")
144+
end
145+
146+
it "attaches a csv containing header plus one line per submission" do
147+
csv_content = mail.attachments.first.decoded
148+
expect(csv_content.lines.count).to eq(submissions.count + 1)
149+
end
150+
151+
it "logs that the email was sent" do
152+
expect(log_lines).to include(
153+
hash_including(
154+
"event" => "form_weekly_batch_email_sent",
155+
"form_id" => form_id,
156+
"mode" => mode_string,
157+
"preview" => "false",
158+
"batch_begin_date" => date.to_s,
159+
"number_of_submissions" => submissions.count,
160+
"delivery_reference" => mail.message_id,
161+
"delivery_id" => delivery.id,
162+
"job_id" => @job_id,
163+
),
164+
)
165+
end
130166
end
131167
end
132168

0 commit comments

Comments
 (0)