Skip to content

Commit fa3707f

Browse files
committed
Add method to get all weekly batches for a given date
Add a method, that given a date, will return batches of submissions per form and mode where the form has `send_weekly_submission_batch` set to true for the final submission in the week. Include all submissions for the week in the batch, even if they were submitted for an older version of the form that did not have `send_weekly_submission_batch` set to true.
1 parent ca3a901 commit fa3707f

File tree

2 files changed

+159
-11
lines changed

2 files changed

+159
-11
lines changed

app/lib/batch_submissions_selector.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ def daily_batches(date)
1616
end
1717
end
1818

19+
def weekly_batches(time_in_week)
20+
Enumerator.new do |yielder|
21+
form_ids_and_modes_with_send_weekly_submission_batch(time_in_week).each do |form_id, mode|
22+
submissions = Submission.for_form_and_mode(form_id, mode).in_week(time_in_week).order(created_at: :desc)
23+
24+
# If the send_weekly_submission_batch is true for the most recent submission, include all submissions in that
25+
# week in the batch. If it is false do not return a batch for any of the submissions in that week.
26+
next unless submissions.any? && submissions.first.form_document["send_weekly_submission_batch"] == true
27+
28+
yielder << Batch.new(form_id, mode, submissions)
29+
end
30+
end
31+
end
32+
1933
private
2034

2135
def form_ids_and_modes_with_send_daily_submission_batch(date)
@@ -26,5 +40,14 @@ def form_ids_and_modes_with_send_daily_submission_batch(date)
2640
.distinct
2741
.pluck(:form_id, :mode)
2842
end
43+
44+
def form_ids_and_modes_with_send_weekly_submission_batch(begin_at)
45+
# Get all form_ids and modes that have at least one submission in the week with send_weekly_submission_batch
46+
# set to true.
47+
Submission.in_week(begin_at)
48+
.where("(form_document->>'send_weekly_submission_batch')::boolean = true")
49+
.distinct
50+
.pluck(:form_id, :mode)
51+
end
2952
end
3053
end

spec/lib/batch_submissions_selector_spec.rb

Lines changed: 136 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
require "rails_helper"
22

33
RSpec.describe BatchSubmissionsSelector do
4-
let(:form_document_with_batch_enabled) { create(:v2_form_document, send_daily_submission_batch: true) }
5-
let(:form_document_with_batch_disabled) { create(:v2_form_document, send_daily_submission_batch: false) }
6-
7-
let(:date) { Time.zone.local(2022, 12, 1) }
84
let(:form_id) { 101 }
95

106
describe ".daily_batches" do
117
subject(:daily_batches) { described_class.daily_batches(date) }
128

9+
let(:date) { Time.zone.local(2022, 12, 1) }
10+
let(:form_document_with_batch_enabled) { create(:v2_form_document, send_daily_submission_batch: true) }
11+
let(:form_document_with_batch_disabled) { create(:v2_form_document, send_daily_submission_batch: false) }
12+
1313
it "returns an enumerator" do
1414
expect(daily_batches).to be_an(Enumerator)
1515
end
1616

17-
context "when send_daily_submissions_batch is enabled for the form document" do
17+
context "when send_daily_submission_batch is enabled for the form document" do
1818
context "when the date is during BST" do
1919
let(:date) { Time.zone.local(2022, 6, 1) }
2020

@@ -35,7 +35,7 @@
3535
create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2022, 6, 1, 23, 0, 0), form_document: form_document_with_batch_enabled)
3636
end
3737

38-
it "includes only forms/modes with submissions on the date and their submissions" do
38+
it "includes only forms/modes with submissions on the date" do
3939
expect(daily_batches.map(&:to_h)).to contain_exactly(
4040
a_hash_including(form_id: form_id, mode: "form"),
4141
a_hash_including(form_id: form_id, mode: "preview-draft"),
@@ -82,9 +82,7 @@
8282
end
8383
end
8484

85-
context "when send_daily_submissions_batch is enabled part-way through the day for the form document" do
86-
let(:other_form_id) { 102 }
87-
85+
context "when send_daily_submission_batch is enabled part-way through the day for the form document" do
8886
let!(:latest_submission) do
8987
create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_enabled)
9088
end
@@ -104,7 +102,7 @@
104102
end
105103
end
106104

107-
context "when send_daily_submissions_batch is disabled for the form document" do
105+
context "when send_daily_submission_batch is disabled for the form document" do
108106
before do
109107
create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_disabled)
110108
end
@@ -114,7 +112,7 @@
114112
end
115113
end
116114

117-
context "when send_daily_submissions_batch is disabled part-way through the day for the form document" do
115+
context "when send_daily_submission_batch is disabled part-way through the day for the form document" do
118116
before do
119117
create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_disabled)
120118
create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 9, 0, 0), form_document: form_document_with_batch_enabled)
@@ -125,4 +123,131 @@
125123
end
126124
end
127125
end
126+
127+
describe ".weekly_batches" do
128+
subject(:weekly_batches) { described_class.weekly_batches(date) }
129+
130+
let(:date) { Time.zone.local(2025, 5, 19) }
131+
let(:form_document_with_batch_enabled) { create(:v2_form_document, send_weekly_submission_batch: true) }
132+
let(:form_document_with_batch_disabled) { create(:v2_form_document, send_weekly_submission_batch: false) }
133+
134+
it "returns an enumerator" do
135+
expect(weekly_batches).to be_an(Enumerator)
136+
end
137+
138+
context "when send_weekly_submission_batch is enabled for the form document" do
139+
context "when the week is during BST" do
140+
let(:date) { Time.zone.local(2025, 5, 19) }
141+
142+
let!(:form_submission) do
143+
create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 5, 18, 23, 0, 0), form_document: form_document_with_batch_enabled)
144+
end
145+
let!(:preview_draft_submission) do
146+
create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 5, 25, 22, 59, 59), form_document: form_document_with_batch_enabled)
147+
end
148+
149+
before do
150+
# create form/mode combinations that only have submissions outside the BST week
151+
create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document: form_document_with_batch_enabled)
152+
create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document: form_document_with_batch_enabled)
153+
154+
# create submissions for the form/mode included in a batch outside the BST week to ensure they are excluded
155+
create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document: form_document_with_batch_enabled)
156+
create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document: form_document_with_batch_enabled)
157+
end
158+
159+
it "includes only forms/modes with submissions in the week" do
160+
expect(weekly_batches.map(&:to_h)).to contain_exactly(
161+
a_hash_including(form_id: form_id, mode: "form"),
162+
a_hash_including(form_id: form_id, mode: "preview-draft"),
163+
)
164+
end
165+
166+
it "includes only submissions in the week in the batches" do
167+
expect(weekly_batches.to_a[0].submissions.pluck(:reference)).to contain_exactly(form_submission.reference)
168+
expect(weekly_batches.to_a[1].submissions.pluck(:reference)).to contain_exactly(preview_draft_submission.reference)
169+
end
170+
end
171+
172+
context "when the week is not in BST" do
173+
let(:date) { Time.zone.local(2025, 11, 3) }
174+
175+
let!(:form_submission) do
176+
create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 3, 0, 0, 0), form_document: form_document_with_batch_enabled)
177+
end
178+
let!(:preview_draft_submission) do
179+
create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 9, 23, 59, 59), form_document: form_document_with_batch_enabled)
180+
end
181+
182+
before do
183+
# create form/mode combinations that only have submissions outside the week
184+
create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document: form_document_with_batch_enabled)
185+
create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document: form_document_with_batch_enabled)
186+
187+
# create submissions for the form/mode included in a batch outside the week to ensure they are excluded
188+
create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document: form_document_with_batch_enabled)
189+
create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document: form_document_with_batch_enabled)
190+
end
191+
192+
it "includes only forms/modes with submissions in the week" do
193+
expect(weekly_batches.map(&:to_h)).to contain_exactly(
194+
a_hash_including(form_id: form_id, mode: "form"),
195+
a_hash_including(form_id: form_id, mode: "preview-draft"),
196+
)
197+
end
198+
199+
it "includes only submissions in the week in the batches" do
200+
expect(weekly_batches.to_a[0].submissions.pluck(:reference)).to contain_exactly(form_submission.reference)
201+
expect(weekly_batches.to_a[1].submissions.pluck(:reference)).to contain_exactly(preview_draft_submission.reference)
202+
end
203+
end
204+
end
205+
206+
context "when send_weekly_submission_batch is enabled part-way through the week for the form document" do
207+
let(:date) { Time.zone.local(2025, 11, 3) }
208+
209+
let!(:latest_submission) do
210+
create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document: form_document_with_batch_enabled)
211+
end
212+
let!(:earlier_submission) do
213+
create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document: form_document_with_batch_disabled)
214+
end
215+
216+
it "includes a batch for the form and mode" do
217+
expect(weekly_batches.map(&:to_h)).to contain_exactly(
218+
a_hash_including(form_id: form_id, mode: "form"),
219+
)
220+
end
221+
222+
it "includes all the submissions in the batch" do
223+
submissions = weekly_batches.first.submissions
224+
expect(submissions.pluck(:reference)).to contain_exactly(latest_submission.reference, earlier_submission.reference)
225+
end
226+
end
227+
228+
context "when send_weekly_submission_batch is disabled for the form document" do
229+
let(:date) { Time.zone.local(2025, 11, 3) }
230+
231+
before do
232+
create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 10, 0, 0), form_document: form_document_with_batch_disabled)
233+
end
234+
235+
it "does not include a batch for the form and mode" do
236+
expect(weekly_batches.to_a).to be_empty
237+
end
238+
end
239+
240+
context "when send_weekly_submission_batch is disabled part-way through the week for the form document" do
241+
let(:date) { Time.zone.local(2025, 11, 3) }
242+
243+
before do
244+
create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document: form_document_with_batch_disabled)
245+
create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document: form_document_with_batch_enabled)
246+
end
247+
248+
it "does not include a batch for the form and mode" do
249+
expect(weekly_batches.to_a).to be_empty
250+
end
251+
end
252+
end
128253
end

0 commit comments

Comments
 (0)