Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions app/lib/batch_submissions_selector.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
class BatchSubmissionsSelector
Batch = Data.define(:form_id, :mode, :submissions)

DAILY_BATCH_FORM_DOCUMENT_CONDITION = <<~SQL.squish
form_document->'delivery_configurations' @> '[{"delivery_schedule":"daily"}]'::jsonb
SQL
WEEKLY_BATCH_FORM_DOCUMENT_CONDITION = <<~SQL.squish
form_document->'delivery_configurations' @> '[{"delivery_schedule":"weekly"}]'::jsonb
SQL

class << self
def daily_batches(date)
Enumerator.new do |yielder|
form_ids_and_modes_with_send_daily_submission_batch(date).each do |form_id, mode|
submissions = Submission.for_form_and_mode(form_id, mode).on_day(date).order(created_at: :desc)

# If the most recent submission is configured for daily batching, include all submissions on that day in the
# batch. If it is not, do not return a batch for any of the submissions on that day.
next unless submissions.any? && daily_delivery_configuration?(submissions.first.form_document)

yielder << Batch.new(form_id, mode, submissions)
end
end
Expand All @@ -28,10 +17,6 @@ def weekly_batches(time_in_week)
form_ids_and_modes_with_send_weekly_submission_batch(time_in_week).each do |form_id, mode|
submissions = Submission.for_form_and_mode(form_id, mode).in_week(time_in_week).order(created_at: :desc)

# If the most recent submission is configured for weekly batching, include all submissions in that week in
# the batch. If it is not, do not return a batch for any of the submissions in that week.
next unless submissions.any? && weekly_delivery_configuration?(submissions.first.form_document)

yielder << Batch.new(form_id, mode, submissions)
end
end
Expand All @@ -40,27 +25,31 @@ def weekly_batches(time_in_week)
private

def form_ids_and_modes_with_send_daily_submission_batch(date)
# Get all form_ids and modes that have at least one submission on the date with daily batches enabled
Submission.on_day(date)
.where(DAILY_BATCH_FORM_DOCUMENT_CONDITION)
.distinct
.pluck(:form_id, :mode)
.filter { |form_id, mode| form_has_delivery_schedule?(form_id, mode, form_ids_and_states_with_daily_submission) }
end

def form_ids_and_modes_with_send_weekly_submission_batch(begin_at)
# Get all form_ids and modes that have at least one submission in the week with weekly batches enabled
Submission.in_week(begin_at)
.where(WEEKLY_BATCH_FORM_DOCUMENT_CONDITION)
.distinct
.pluck(:form_id, :mode)
.filter { |form_id, mode| form_has_delivery_schedule?(form_id, mode, form_ids_and_states_with_weekly_submission) }
end

def form_has_delivery_schedule?(form_id, mode, forms_with_delivery_schedule)
state = Mode.new(mode).preview_draft? ? "draft" : "current"

{ form_id:, state: }.in?(forms_with_delivery_schedule)
end

def daily_delivery_configuration?(form_document)
form_document["delivery_configurations"]&.any? { |configuration| configuration["delivery_schedule"] == "daily" }
def form_ids_and_states_with_daily_submission
Api::V3::FormsResource.with_delivery_schedule("daily")
end

def weekly_delivery_configuration?(form_document)
form_document["delivery_configurations"]&.any? { |configuration| configuration["delivery_schedule"] == "weekly" }
def form_ids_and_states_with_weekly_submission
Api::V3::FormsResource.with_delivery_schedule("weekly")
end
end
end
16 changes: 16 additions & 0 deletions app/resources/api/v3/forms_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Api::V3::FormsResource < ActiveResource::Base
self.element_name = "form"
self.site = Settings.forms_api.base_url
self.prefix = "/api/v3/"
self.include_format_in_path = false

class << self
def with_delivery_schedule(delivery_schedule, **options)
forms_with_delivery_schedule = where(delivery_schedule:, **options)

forms_with_delivery_schedule.map do |form|
{ form_id: form.form_id, state: form.state }
end
end
end
end
Loading
Loading