Skip to content

Commit 3c60a22

Browse files
Add daily batch scope to Submission
This scopes submission based on their created_at, form_id and mode. The given date is converted to the local time before the comparison to the created_at value. Co-authored-by: Stephen Daly <stephen.daly@digital.cabinet-office.gov.uk>
1 parent 72f905b commit 3c60a22

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

app/models/submission.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class Submission < ApplicationRecord
2-
include TimeZoneUtils
3-
42
has_many :submission_deliveries, dependent: :destroy
53
has_many :deliveries, through: :submission_deliveries
64

5+
scope :for_daily_batch, lambda { |form_id, date, mode|
6+
start_time = date.in_time_zone(TimeZoneUtils.submission_time_zone).beginning_of_day
7+
end_time = start_time.end_of_day
8+
9+
where(form_id:, created_at: start_time..end_time, mode: mode).order(created_at: :desc)
10+
}
11+
712
delegate :preview?, to: :mode_object
813

914
encrypts :answers
@@ -17,7 +22,7 @@ def form
1722
end
1823

1924
def submission_time
20-
created_at.in_time_zone(submission_time_zone)
25+
created_at.in_time_zone(TimeZoneUtils.submission_time_zone)
2126
end
2227

2328
def payment_url

lib/time_zone_utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module TimeZoneUtils
2-
def submission_time_zone
2+
def self.submission_time_zone
33
Rails.configuration.x.submission.time_zone || "UTC"
44
end
55
end

spec/models/submission_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,52 @@
11
require "rails_helper"
22

33
RSpec.describe Submission, type: :model do
4+
describe "scopes" do
5+
describe ".for_daily_batch" do
6+
context "when the date is during BST" do
7+
let(:form_id) { 101 }
8+
let!(:start_of_day_submission) { create(:submission, form_id:, created_at: Time.utc(2022, 5, 31, 23, 0, 0), mode:) }
9+
let!(:end_of_day_submission) { create(:submission, form_id:, created_at: Time.utc(2022, 6, 1, 22, 59, 59), mode:) }
10+
let(:mode) { "form" }
11+
let(:date) { Date.new(2022, 6, 1) }
12+
13+
before do
14+
create(:submission, form_id: 5, created_at: Time.utc(2022, 6, 1, 12, 0, 0), mode:)
15+
create(:submission, form_id:, created_at: Time.utc(2022, 5, 31, 22, 59, 59), mode:)
16+
create(:submission, form_id:, created_at: Time.utc(2022, 6, 1, 23, 0, 0), mode:)
17+
create(:submission, form_id:, created_at: Time.utc(2022, 6, 2, 12, 0, 0), mode: "preview-live")
18+
end
19+
20+
it "returns only submissions for the given form, date and mode" do
21+
submissions = described_class.for_daily_batch(form_id, date, mode)
22+
expect(submissions.size).to eq(2)
23+
expect(submissions).to contain_exactly(start_of_day_submission, end_of_day_submission)
24+
end
25+
end
26+
27+
context "when the date is not during BST" do
28+
let(:form_id) { 101 }
29+
let!(:start_of_day_submission) { create(:submission, form_id:, created_at: Time.utc(2022, 12, 1, 0, 0, 0), mode:) }
30+
let!(:end_of_day_submission) { create(:submission, form_id:, created_at: Time.utc(2022, 12, 1, 23, 59, 59), mode:) }
31+
let(:mode) { "form" }
32+
let(:date) { Date.new(2022, 12, 1) }
33+
34+
before do
35+
create(:submission, form_id: 5, created_at: Time.utc(2022, 12, 1, 12, 0, 0), mode:)
36+
create(:submission, form_id:, created_at: Time.utc(2022, 11, 30, 23, 59, 59), mode:)
37+
create(:submission, form_id:, created_at: Time.utc(2022, 12, 2, 0, 0, 0), mode:)
38+
create(:submission, form_id:, created_at: Time.utc(2022, 12, 1, 12, 0, 0), mode: "preview-live")
39+
end
40+
41+
it "returns only submissions for the given form, date and mode" do
42+
submissions = described_class.for_daily_batch(form_id, date, mode)
43+
expect(submissions.size).to eq(2)
44+
expect(submissions).to contain_exactly(start_of_day_submission, end_of_day_submission)
45+
end
46+
end
47+
end
48+
end
49+
450
describe "#sent?" do
551
context "when the submission is sent" do
652
let(:submission) { create :submission, :sent }

0 commit comments

Comments
 (0)