Skip to content

Commit 4841b0f

Browse files
authored
Merge pull request #1976 from alphagov/add-batch-begin-at
Add batch_begin_at to deliveries
2 parents 58a5a96 + 659d7e5 commit 4841b0f

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

app/jobs/schedule_daily_batch_deliveries_job.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def perform
88
CurrentJobLoggingAttributes.job_id = job_id
99

1010
date = Time.zone.yesterday
11+
batch_begin_at = date.in_time_zone(TimeZoneUtils.submission_time_zone).beginning_of_day
1112

1213
DailySubmissionBatchSelector.batches(date).each do |batch|
1314
existing_deliveries = batch.submissions.first.deliveries.daily
@@ -18,7 +19,11 @@ def perform
1819
next
1920
end
2021

21-
delivery = Delivery.create!(delivery_schedule: :daily, submissions: batch.submissions)
22+
delivery = Delivery.create!(
23+
delivery_schedule: :daily,
24+
submissions: batch.submissions,
25+
batch_begin_at:,
26+
)
2227

2328
send_batch_job = SendSubmissionBatchJob.perform_later(delivery:)
2429

app/jobs/send_submission_batch_job.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def perform(delivery:)
1717
latest_submission = submissions.order(created_at: :desc).first
1818
form = latest_submission.form
1919
mode = latest_submission.mode_object
20-
date = latest_submission.submission_time.to_date
20+
21+
batch_begin_date = delivery.batch_begin_at.in_time_zone(TimeZoneUtils.submission_time_zone).to_date
2122

2223
set_submission_batch_logging_attributes(form:, mode:, delivery:)
2324

@@ -30,7 +31,7 @@ def perform(delivery:)
3031
end
3132
end
3233

33-
message_id = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, date:, mode:).send_batch
34+
message_id = AwsSesSubmissionBatchService.new(submissions_query: submissions, form:, date: batch_begin_date, mode:).send_batch
3435

3536
delivery.new_attempt!
3637
delivery.update!(
@@ -40,14 +41,14 @@ def perform(delivery:)
4041
CurrentJobLoggingAttributes.delivery_reference = delivery.delivery_reference
4142
EventLogger.log_form_event("daily_batch_email_sent", {
4243
mode: mode.to_s,
43-
batch_date: date,
44+
batch_date: batch_begin_date,
4445
number_of_submissions: submissions.count,
4546
})
4647

4748
submissions.each do |submission|
4849
EventLogger.log_form_event("included_in_daily_batch_email", {
4950
submission_reference: submission.reference,
50-
batch_date: date,
51+
batch_date: batch_begin_date,
5152
})
5253
end
5354
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddBatchBeginAtToDeliveries < ActiveRecord::Migration[8.1]
2+
def change
3+
add_column :deliveries, :batch_begin_at, :datetime
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/jobs/schedule_daily_batch_deliveries_job_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@
5353
expect(enqueued_args.second).to include("delivery" => hash_including("_aj_globalid"))
5454
expect(locate_delivery(enqueued_args.second)).to eq(Delivery.second)
5555
end
56+
57+
describe "setting batch_begin_at" do
58+
context "when the date for the batch is the day the clocks go forwards" do
59+
let(:travel_time) { Time.zone.local(2025, 3, 31) }
60+
61+
it "sets the batch_begin_at to the beginning of the day in BST" do
62+
expect(Delivery.first.batch_begin_at).to eq(Time.utc(2025, 3, 30, 0, 0, 0))
63+
end
64+
end
65+
66+
context "when the date for the batch is after the clocks have gone forwards" do
67+
let(:travel_time) { Time.zone.local(2025, 4, 1) }
68+
69+
it "sets the batch_begin_at to the beginning of the day in BST" do
70+
expect(Delivery.first.batch_begin_at).to eq(Time.utc(2025, 3, 30, 23, 0, 0))
71+
end
72+
end
73+
74+
context "when the date for the batch is the day the clocks go back" do
75+
let(:travel_time) { Time.zone.local(2025, 10, 27) }
76+
77+
it "sets the batch_begin_at to the beginning of the day in GMT" do
78+
expect(Delivery.first.batch_begin_at).to eq(Time.utc(2025, 10, 25, 23, 0, 0))
79+
end
80+
end
81+
82+
context "when the date for the batch is the day after the clocks have gone back" do
83+
let(:travel_time) { Time.zone.local(2025, 10, 28) }
84+
85+
it "sets the batch_begin_at to the beginning of the day in GMT" do
86+
expect(Delivery.first.batch_begin_at).to eq(Time.utc(2025, 10, 27, 0, 0, 0))
87+
end
88+
end
89+
end
5690
end
5791

5892
context "when a Delivery already exists for a batch" do

spec/jobs/send_submission_batch_job_spec.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
include ActiveJob::TestHelper
66

77
let(:mode_string) { "form" }
8-
let(:date) { Date.new(2022, 12, 14) }
9-
let(:delivery) { create(:delivery, delivery_schedule: "daily", submissions:) }
8+
let(:date) { Date.new(2025, 4, 10) }
9+
let(:batch_begin_at) { Time.utc(2025, 4, 9, 23, 0, 0) }
10+
let(:delivery) { create(:delivery, delivery_schedule: "daily", submissions:, batch_begin_at:) }
1011

1112
let(:form_document) { create(:v2_form_document, :with_steps, name: "My Form", submission_email:) }
1213
let(:submission_email) { "to@example.com" }
@@ -73,13 +74,25 @@
7374
expect(mail.to).to include(form_document.submission_email)
7475
end
7576

77+
it "includes the date in London time in the subject" do
78+
expect(mail.subject).to include("10 April 2025")
79+
end
80+
7681
it "updates the delivery" do
7782
expect(delivery.reload.delivery_reference).to eq(mail.message_id)
7883
expect(delivery.reload.last_attempt_at).to be_within(1.second).of(@job_ran_at)
7984
end
8085

8186
context "when the delivery has already been attempted" do
82-
let(:delivery) { create(:delivery, delivery_schedule: "daily", submissions:, delivered_at: Time.zone.now - 2.hours, failed_at: Time.zone.now - 1.hour, failure_reason: "bounced") }
87+
let(:delivery) do
88+
create(:delivery,
89+
delivery_schedule: "daily",
90+
submissions:,
91+
batch_begin_at:,
92+
delivered_at: Time.zone.now - 2.hours,
93+
failed_at: Time.zone.now - 1.hour,
94+
failure_reason: "bounced")
95+
end
8396

8497
it "updates the resets the delivery details" do
8598
expect(delivery.reload.delivered_at).to be_nil
@@ -92,7 +105,7 @@
92105
expect(mail.attachments).not_to be_empty
93106

94107
filenames = mail.attachments.map(&:filename)
95-
expect(filenames).to contain_exactly("govuk_forms_my_form_2022-12-14.csv")
108+
expect(filenames).to contain_exactly("govuk_forms_my_form_2025-04-10.csv")
96109
end
97110

98111
it "attaches a csv containing header plus one line per submission" do

0 commit comments

Comments
 (0)