2023-12-12 19:17:38 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Lists
|
|
|
|
class MyModulesService < BaseService
|
|
|
|
PRELOAD = {
|
|
|
|
results: {},
|
|
|
|
my_module_status: {},
|
|
|
|
tags: {},
|
|
|
|
task_comments: { user: :teams },
|
|
|
|
user_assignments: :user,
|
|
|
|
designated_users: {},
|
2024-02-22 20:38:24 +08:00
|
|
|
experiment: { project: :team }
|
2023-12-12 19:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_records
|
|
|
|
@records = @raw_data.includes(PRELOAD)
|
|
|
|
.select('my_modules.*')
|
|
|
|
.group('my_modules.id')
|
|
|
|
|
|
|
|
view_mode = if @params[:experiment].archived_branch?
|
|
|
|
'archived'
|
|
|
|
else
|
|
|
|
@params[:view_mode] || 'active'
|
|
|
|
end
|
2024-03-15 17:36:47 +08:00
|
|
|
@records = @records.archived if view_mode == 'archived' && !@params[:experiment].archived_branch?
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.active if view_mode == 'active'
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_records
|
2024-01-26 16:29:39 +08:00
|
|
|
if @params[:search].present?
|
|
|
|
@records = @records.where_attributes_like(['my_modules.name', MyModule::PREFIXED_ID_SQL], @params[:search])
|
|
|
|
end
|
|
|
|
|
2023-12-12 19:17:38 +08:00
|
|
|
@filters.each do |name, value|
|
|
|
|
__send__("#{name}_filter", value) if value.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sortable_columns
|
|
|
|
@sortable_columns ||= {
|
|
|
|
due_date: 'due_date',
|
|
|
|
name: 'name',
|
2024-03-06 23:04:41 +08:00
|
|
|
code: 'id',
|
2024-02-27 20:34:55 +08:00
|
|
|
archived_on: 'archived_on',
|
|
|
|
age: 'age',
|
|
|
|
status: 'status',
|
|
|
|
designated: 'designated',
|
|
|
|
results: 'results',
|
|
|
|
tags: 'tags',
|
2024-05-09 18:08:33 +08:00
|
|
|
signatures: 'signatures',
|
|
|
|
comments: 'comments'
|
2023-12-12 19:17:38 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_records
|
|
|
|
return unless @params[:order]
|
|
|
|
|
|
|
|
sort = "#{sortable_columns[order_params[:column].to_sym]}_#{sort_direction(order_params)}"
|
|
|
|
|
|
|
|
case sort
|
|
|
|
when 'due_date_ASC'
|
|
|
|
@records = @records.order(:due_date, :name)
|
|
|
|
when 'due_date_DESC'
|
|
|
|
@records = @records.order(Arel.sql("COALESCE(due_date, DATE '2100-01-01') DESC"), :name)
|
|
|
|
when 'name_ASC'
|
|
|
|
@records = @records.order(:name)
|
|
|
|
when 'name_DESC'
|
|
|
|
@records = @records.order(name: :desc)
|
|
|
|
when 'id_ASC'
|
|
|
|
@records = @records.order(:id)
|
|
|
|
when 'id_DESC'
|
|
|
|
@records = @records.order(id: :desc)
|
|
|
|
when 'archived_on_ASC'
|
|
|
|
@records = @records.order(Arel.sql('COALESCE(my_modules.archived_on, my_modules.archived_on) ASC'))
|
|
|
|
when 'archived_on_DESC'
|
|
|
|
@records = @records.order(Arel.sql('COALESCE(my_modules.archived_on, my_modules.archived_on) DESC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'age_ASC'
|
|
|
|
@records = @records.order(:created_at)
|
|
|
|
when 'age_DESC'
|
|
|
|
@records = @records.order(created_at: :desc)
|
|
|
|
when 'status_ASC'
|
|
|
|
@records = @records.order(:my_module_status_id)
|
|
|
|
when 'status_DESC'
|
|
|
|
@records = @records.order(my_module_status_id: :desc)
|
2024-05-09 18:08:33 +08:00
|
|
|
when 'comments_ASC'
|
|
|
|
@records = @records.left_joins(:task_comments)
|
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT comments.id) ASC'))
|
|
|
|
when 'comments_DESC'
|
|
|
|
@records = @records.left_joins(:task_comments)
|
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT comments.id) DESC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'designated_ASC'
|
|
|
|
@records = @records.left_joins(:user_my_modules)
|
2024-02-27 20:41:20 +08:00
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT user_my_modules.id) ASC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'designated_DESC'
|
|
|
|
@records = @records.left_joins(:user_my_modules)
|
2024-02-27 20:41:20 +08:00
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT user_my_modules.id) DESC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'results_ASC'
|
|
|
|
@records = @records.left_joins(:results)
|
2024-02-27 20:41:20 +08:00
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT results.id) ASC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'results_DESC'
|
|
|
|
@records = @records.left_joins(:results)
|
2024-02-27 20:41:20 +08:00
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT results.id) DESC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'tags_ASC'
|
|
|
|
@records = @records.left_joins(:tags)
|
2024-02-27 20:41:20 +08:00
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT tags.id) ASC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'tags_DESC'
|
|
|
|
@records = @records.left_joins(:tags)
|
2024-02-27 20:41:20 +08:00
|
|
|
.group('my_modules.id')
|
|
|
|
.order(Arel.sql('COUNT(DISTINCT tags.id) DESC'))
|
2024-02-27 20:34:55 +08:00
|
|
|
else
|
|
|
|
__send__("#{sortable_columns[order_params[:column].to_sym]}_sort", sort_direction(order_params))
|
2023-12-12 19:17:38 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def query_filter(value)
|
|
|
|
@records = @records.where_attributes_like('my_modules.name', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_from_filter(value)
|
|
|
|
@records = @records.where('my_modules.due_date >= ?', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def due_date_to_filter(value)
|
|
|
|
@records = @records.where('my_modules.due_date <= ?', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_on_from_filter(value)
|
|
|
|
@records = @records.where('my_modules.archived_on >= ?', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_on_to_filter(value)
|
|
|
|
@records = @records.where('my_modules.archived_on <= ?', value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def designated_users_filter(users)
|
|
|
|
@records = @records.joins(:user_my_modules).where(user_my_modules: { user_id: users.values })
|
|
|
|
end
|
|
|
|
|
|
|
|
def statuses_filter(statuses)
|
|
|
|
@records = @records.where(my_module_status_id: statuses.values)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|