adjust filters

This commit is contained in:
Pete Matsyburka
2024-11-23 01:45:10 +02:00
parent b86aac86d1
commit 9beaaa0851
28 changed files with 410 additions and 304 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ module Submissions
arel = arel.or(Template.arel_table[:name].lower.matches("%#{keyword.downcase}%"))
end
submissions.joins(:submitters).where(arel).distinct
submissions.joins(:submitters).where(arel).group(:id)
end
def update_template_fields!(submission)
+64
View File
@@ -0,0 +1,64 @@
# frozen_string_literal: true
module Submissions
module Filter
ALLOWED_PARAMS = %w[
author
completed_at_from
completed_at_to
created_at_from
created_at_to
].freeze
DATE_PARAMS = %w[
completed_at_from
completed_at_to
created_at_from
created_at_to
].freeze
module_function
def call(submissions, current_user, params)
filters = normalize_filter_params(params, current_user)
if filters[:author].present?
user = current_user.account.users.find_by(email: filters[:author])
submissions = submissions.where(created_by_user_id: user&.id || -1)
end
submissions = submissions.where(created_at: filters[:created_at_from]..) if filters[:created_at_from].present?
if filters[:created_at_to].present?
submissions = submissions.where(created_at: ..filters[:created_at_to].end_of_day)
end
if filters[:completed_at_from].present? || filters[:completed_at_to].present?
completed_arel = Submitter.arel_table[:completed_at].maximum
submissions = submissions.completed.joins(:submitters).group(:id)
if filters[:completed_at_from].present?
submissions = submissions.having(completed_arel.gteq(filters[:completed_at_from]))
end
if filters[:completed_at_to].present?
submissions = submissions.having(completed_arel.lteq(filters[:completed_at_to].end_of_day))
end
end
submissions
end
def normalize_filter_params(params, current_user)
tz = ActiveSupport::TimeZone[current_user.account.timezone] || Time.zone
ALLOWED_PARAMS.each_with_object({}) do |key, acc|
next if params[key].blank?
value = DATE_PARAMS.include?(key) ? tz.parse(params[key]) : params[key]
acc[key.to_sym] = value
end
end
end
end
+1 -1
View File
@@ -23,7 +23,7 @@ module Submissions
RTL_REGEXP = TextUtils::RTL_REGEXP
MAX_IMAGE_HEIGHT = 100
US_TIMEZONES = %w[EST CST MST PST HST AKDT].freeze
US_TIMEZONES = TimeUtils::US_TIMEZONES
module_function
+2
View File
@@ -22,6 +22,8 @@ module TimeUtils
DEFAULT_DATE_FORMAT_US = 'MM/DD/YYYY'
DEFAULT_DATE_FORMAT = 'DD/MM/YYYY'
US_TIMEZONES = %w[EST CST MST PST HST AKDT].freeze
module_function
def timezone_abbr(timezone, time = Time.current)