Skip to content

Commit ac8af80

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 ac8af80

File tree

3 files changed

+132
-39
lines changed

3 files changed

+132
-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: 102 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,118 @@
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
111+
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
110116

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)
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
132+
133+
it "logs each submission included in the batch" do
134+
expect(log_lines).to include(
135+
hash_including(
136+
"event" => "form_included_in_daily_batch_email",
137+
"batch_begin_date" => date.to_s,
138+
"submission_reference" => submissions.first.reference,
139+
"delivery_reference" => mail.message_id,
140+
),
141+
hash_including(
142+
"event" => "form_included_in_daily_batch_email",
143+
"batch_begin_date" => date.to_s,
144+
"submission_reference" => submissions.second.reference,
145+
"delivery_reference" => mail.message_id,
146+
),
147+
)
148+
end
114149
end
115150

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-
)
151+
context "when the delivery is for a weekly batch" do
152+
let(:delivery) { create(:delivery, delivery_schedule: "weekly", submissions:, batch_begin_at:) }
153+
154+
it "sends an email" do
155+
expect(ActionMailer::Base.deliveries.count).to eq(1)
156+
expect(mail.to).to include(form_document.submission_email)
157+
end
158+
159+
it "includes the date range in London time in the subject" do
160+
expect(mail.subject).to include("10 April 2025 to 16 April 2025")
161+
end
162+
163+
it "attaches a csv containing header plus one line per submission" do
164+
csv_content = mail.attachments.first.decoded
165+
expect(csv_content.lines.count).to eq(submissions.count + 1)
166+
end
167+
168+
it "logs that the email was sent" do
169+
expect(log_lines).to include(
170+
hash_including(
171+
"event" => "form_weekly_batch_email_sent",
172+
"form_id" => form_id,
173+
"mode" => mode_string,
174+
"preview" => "false",
175+
"batch_begin_date" => date.to_s,
176+
"number_of_submissions" => submissions.count,
177+
"delivery_reference" => mail.message_id,
178+
"delivery_id" => delivery.id,
179+
"job_id" => @job_id,
180+
),
181+
)
182+
end
183+
184+
it "logs each submission included in the batch" do
185+
expect(log_lines).to include(
186+
hash_including(
187+
"event" => "form_included_in_weekly_batch_email",
188+
"batch_begin_date" => date.to_s,
189+
"submission_reference" => submissions.first.reference,
190+
"delivery_reference" => mail.message_id,
191+
),
192+
hash_including(
193+
"event" => "form_included_in_weekly_batch_email",
194+
"batch_begin_date" => date.to_s,
195+
"submission_reference" => submissions.second.reference,
196+
"delivery_reference" => mail.message_id,
197+
),
198+
)
199+
end
130200
end
131201
end
132202

0 commit comments

Comments
 (0)