11class 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 )
10+ }
11+
12+ scope :ordered_by_form_version_and_date , lambda {
13+ order ( Arel . sql ( "(form_document->>'updated_at')::timestamptz ASC, created_at ASC" ) )
14+ }
15+
716 delegate :preview? , to : :mode_object
817
918 encrypts :answers
@@ -17,7 +26,7 @@ def form
1726 end
1827
1928 def submission_time
20- created_at . in_time_zone ( submission_time_zone )
29+ created_at . in_time_zone ( TimeZoneUtils . submission_time_zone )
2130 end
2231
2332 def payment_url
@@ -33,25 +42,6 @@ def self.sent?(reference)
3342 submission &.single_submission_delivery &.delivery_reference &.present?
3443 end
3544
36- def self . group_by_form_version ( submissions )
37- submission_by_version = { }
38- last_version = nil
39-
40- # For forms that have the same updated_at timestamp, we know they will be identical. If two forms have different
41- # updated_at timestamps, we check to see if their steps are the same. If they are, we group those forms' submissions
42- # together.
43- submissions . group_by { |submission | submission . form . updated_at } . sort . to_h . each do |updated_at , submissions |
44- if last_version && last_version . steps == submissions . first . form . steps
45- submission_by_version [ last_version . updated_at ] . push ( *submissions )
46- else
47- submission_by_version [ updated_at ] = submissions
48- last_version = submissions . first . form
49- end
50- end
51-
52- submission_by_version
53- end
54-
5545private
5646
5747 def mode_object
0 commit comments