Skip to content

Commit 4821c04

Browse files
Add scope to Submission by version and date
This scope orders a list of submissions by their form's udpated_at, which is used as a proxy for its version, and then the submission date. The intention is to use this in the CSV generation for submission batches, where we'll need to maintain the order that submissions were made, but keep them in version clusters Co-authored-by: Stephen Daly <stephen.daly@digital.cabinet-office.gov.uk>
1 parent f031fdb commit 4821c04

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

app/models/submission.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ class Submission < ApplicationRecord
66
start_time = date.in_time_zone(TimeZoneUtils.submission_time_zone).beginning_of_day
77
end_time = start_time.end_of_day
88

9-
where(form_id:, created_at: start_time..end_time, mode: mode).order(created_at: :desc)
9+
where(form_id:, created_at: start_time..end_time, mode: mode)
10+
}
11+
12+
scope :ordered_by_form_version_and_date, lambda {
13+
order(Arel.sql("(form_document->>'updated_at')::timestamptz ASC, created_at ASC"))
1014
}
1115

1216
delegate :preview?, to: :mode_object

spec/models/submission_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@
4545
end
4646
end
4747
end
48+
49+
describe ".ordered_by_form_version_and_date" do
50+
let(:first_form_version) { create :v2_form_document, updated_at: Time.utc(2022, 6, 1, 12, 0, 0) }
51+
let(:second_form_version) { create :v2_form_document, updated_at: Time.utc(2022, 12, 1, 12, 0, 0) }
52+
53+
before do
54+
create :submission, form_document: second_form_version, created_at: Time.utc(2022, 12, 1, 21, 0, 0), reference: "fourth_submission"
55+
create :submission, form_document: first_form_version, created_at: Time.utc(2022, 12, 1, 15, 0, 0), reference: "third_submission"
56+
create :submission, form_document: second_form_version, created_at: Time.utc(2022, 12, 1, 13, 0, 0), reference: "second_submission"
57+
create :submission, form_document: first_form_version, created_at: Time.utc(2022, 12, 1, 9, 0, 0), reference: "first_submission"
58+
end
59+
60+
it "returns the submissions in order" do
61+
expect(described_class.all.ordered_by_form_version_and_date.pluck(:reference)).to eq(%w[first_submission third_submission second_submission fourth_submission])
62+
end
63+
end
4864
end
4965

5066
describe "#sent?" do

0 commit comments

Comments
 (0)