2019-12-18 21:10:41 +08:00
|
|
|
# frozen_string_literal: true
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2022-02-11 17:24:29 +08:00
|
|
|
module RepositoryFilters
|
|
|
|
class ColumnNotFoundException < StandardError; end
|
|
|
|
|
|
|
|
class ValueNotFoundException < StandardError; end
|
|
|
|
end
|
|
|
|
|
2019-12-18 21:10:41 +08:00
|
|
|
class RepositoryDatatableService
|
2022-06-21 16:49:03 +08:00
|
|
|
include MyModulesHelper
|
2020-01-13 23:31:42 +08:00
|
|
|
attr_reader :repository_rows, :all_count, :mappings
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2022-02-16 01:02:08 +08:00
|
|
|
PREDEFINED_COLUMNS = %w(row_id row_name added_on added_by archived_on archived_by assigned).freeze
|
2021-12-21 19:38:52 +08:00
|
|
|
|
2018-03-09 21:22:00 +08:00
|
|
|
def initialize(repository, params, user, my_module = nil)
|
2018-03-06 23:32:39 +08:00
|
|
|
@repository = repository
|
2018-03-09 00:36:10 +08:00
|
|
|
@user = user
|
|
|
|
@my_module = my_module
|
|
|
|
@params = params
|
2022-06-21 16:49:03 +08:00
|
|
|
@assigned_view = @params[:assigned].in?(%w(assigned assigned_simple))
|
2020-01-13 23:09:07 +08:00
|
|
|
@sortable_columns = build_sortable_columns
|
2018-03-09 21:22:00 +08:00
|
|
|
create_columns_mappings
|
2021-02-22 17:34:06 +08:00
|
|
|
@repository_rows = process_query
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-09 21:22:00 +08:00
|
|
|
def create_columns_mappings
|
2018-03-09 21:43:12 +08:00
|
|
|
# Make mappings of custom columns, so we have same id for every
|
|
|
|
# column
|
2020-04-21 20:49:36 +08:00
|
|
|
index = @repository.default_columns_count
|
2018-03-09 21:22:00 +08:00
|
|
|
@mappings = {}
|
|
|
|
@repository.repository_columns.order(:id).each do |column|
|
2018-03-20 00:49:09 +08:00
|
|
|
@mappings[column.id] = index.to_s
|
|
|
|
index += 1
|
2018-03-09 21:22:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def process_query
|
2021-12-21 19:38:52 +08:00
|
|
|
search_value = @params[:search][:value]
|
|
|
|
order_params = @params[:order].first
|
|
|
|
order_by_column = { column: order_params[:column].to_i, dir: order_params[:dir] }
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2019-03-01 03:39:10 +08:00
|
|
|
repository_rows = fetch_rows(search_value)
|
2020-01-13 23:09:07 +08:00
|
|
|
|
2022-07-06 17:53:26 +08:00
|
|
|
# Aliased my_module_repository_rows join for consistent assigned counts
|
|
|
|
repository_rows =
|
|
|
|
repository_rows.joins(
|
|
|
|
'LEFT OUTER JOIN "my_module_repository_rows" AS "all_my_module_repository_rows" ON '\
|
|
|
|
'"all_my_module_repository_rows"."repository_row_id" = "repository_rows"."id"'
|
|
|
|
)
|
|
|
|
|
2020-01-13 23:09:07 +08:00
|
|
|
# Adding assigned counters
|
2018-03-09 00:36:10 +08:00
|
|
|
if @my_module
|
2022-06-21 16:49:03 +08:00
|
|
|
if @assigned_view
|
2020-01-13 23:09:07 +08:00
|
|
|
repository_rows = repository_rows.joins(:my_module_repository_rows)
|
|
|
|
.where(my_module_repository_rows: { my_module_id: @my_module })
|
2022-01-25 19:12:55 +08:00
|
|
|
if @repository.has_stock_management?
|
|
|
|
repository_rows = repository_rows
|
2022-07-12 22:53:07 +08:00
|
|
|
.select('SUM(DISTINCT my_module_repository_rows.stock_consumption) AS "consumed_stock"')
|
2022-01-25 19:12:55 +08:00
|
|
|
end
|
2020-01-13 23:09:07 +08:00
|
|
|
else
|
2020-06-24 18:06:53 +08:00
|
|
|
repository_rows = repository_rows
|
|
|
|
.joins(:repository)
|
2020-07-07 20:40:19 +08:00
|
|
|
.joins('LEFT OUTER JOIN "my_module_repository_rows" "current_my_module_repository_rows"'\
|
|
|
|
'ON "current_my_module_repository_rows"."repository_row_id" = "repository_rows"."id" '\
|
|
|
|
'AND "current_my_module_repository_rows"."my_module_id" = ' + @my_module.id.to_s)
|
|
|
|
.where('current_my_module_repository_rows.id IS NOT NULL '\
|
|
|
|
'OR (repository_rows.archived = FALSE AND repositories.archived = FALSE)')
|
|
|
|
.select('CASE WHEN current_my_module_repository_rows.id IS NOT NULL '\
|
|
|
|
'THEN true ELSE false END as row_assigned')
|
|
|
|
.group('current_my_module_repository_rows.id')
|
2020-01-13 23:09:07 +08:00
|
|
|
end
|
2018-03-09 00:36:10 +08:00
|
|
|
end
|
2020-04-08 03:02:16 +08:00
|
|
|
repository_rows = repository_rows
|
|
|
|
.left_outer_joins(my_module_repository_rows: { my_module: :experiment })
|
2022-07-06 17:53:26 +08:00
|
|
|
.select('COUNT(DISTINCT all_my_module_repository_rows.id) AS "assigned_my_modules_count"')
|
2020-04-08 03:02:16 +08:00
|
|
|
.select('COUNT(DISTINCT my_modules.experiment_id) AS "assigned_experiments_count"')
|
|
|
|
.select('COUNT(DISTINCT experiments.project_id) AS "assigned_projects_count"')
|
2020-01-25 16:43:05 +08:00
|
|
|
repository_rows = repository_rows.preload(Extends::REPOSITORY_ROWS_PRELOAD_RELATIONS)
|
2019-03-01 03:39:10 +08:00
|
|
|
|
2021-12-21 19:38:52 +08:00
|
|
|
sort_rows(order_by_column, repository_rows)
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2019-03-01 03:39:10 +08:00
|
|
|
def fetch_rows(search_value)
|
2020-07-10 17:43:06 +08:00
|
|
|
repository_rows = @repository.repository_rows
|
|
|
|
if @params[:archived] && !@repository.archived?
|
|
|
|
repository_rows = repository_rows.where(archived: @params[:archived])
|
|
|
|
end
|
2020-01-13 23:09:07 +08:00
|
|
|
|
|
|
|
@all_count =
|
2022-06-21 16:49:03 +08:00
|
|
|
if @my_module && @assigned_view
|
2020-01-13 23:09:07 +08:00
|
|
|
repository_rows.joins(:my_module_repository_rows)
|
|
|
|
.where(my_module_repository_rows: { my_module_id: @my_module })
|
|
|
|
.count
|
|
|
|
else
|
|
|
|
repository_rows.count
|
|
|
|
end
|
2019-03-01 03:39:10 +08:00
|
|
|
|
2021-08-27 22:02:11 +08:00
|
|
|
repository_rows = repository_rows.where(external_id: @params[:external_ids]) if @params[:external_ids]
|
2020-01-13 23:09:07 +08:00
|
|
|
|
2019-03-01 03:39:10 +08:00
|
|
|
if search_value.present?
|
2021-08-20 19:14:18 +08:00
|
|
|
if @repository.default_search_fileds.include?('users.full_name')
|
|
|
|
repository_rows = repository_rows.joins(:created_by)
|
|
|
|
end
|
|
|
|
repository_row_matches = repository_rows.where_attributes_like(@repository.default_search_fileds, search_value)
|
2020-01-13 23:09:07 +08:00
|
|
|
results = repository_rows.where(id: repository_row_matches)
|
|
|
|
|
2021-05-20 04:48:52 +08:00
|
|
|
data_types = @repository.repository_columns.pluck(:data_type).uniq
|
|
|
|
|
|
|
|
Extends::REPOSITORY_EXTRA_SEARCH_ATTR.each do |data_type, config|
|
|
|
|
next unless data_types.include?(data_type.to_s)
|
|
|
|
|
2021-07-07 23:43:51 +08:00
|
|
|
custom_cell_matches = repository_rows.joins(config[:includes])
|
2021-05-20 04:48:52 +08:00
|
|
|
.where_attributes_like(config[:field], search_value)
|
2020-01-13 23:09:07 +08:00
|
|
|
results = results.or(repository_rows.where(id: custom_cell_matches))
|
|
|
|
end
|
|
|
|
|
|
|
|
repository_rows = results
|
2019-03-01 03:39:10 +08:00
|
|
|
end
|
|
|
|
|
2021-12-21 19:38:52 +08:00
|
|
|
repository_rows = repository_rows.where(id: advanced_search(repository_rows)) if @params[:advanced_search].present?
|
|
|
|
|
2020-06-11 18:48:31 +08:00
|
|
|
repository_rows.left_outer_joins(:created_by, :archived_by)
|
2020-01-14 23:55:10 +08:00
|
|
|
.select('repository_rows.*')
|
|
|
|
.select('COUNT("repository_rows"."id") OVER() AS filtered_count')
|
|
|
|
.group('repository_rows.id')
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2021-12-21 19:38:52 +08:00
|
|
|
def advanced_search(repository_rows)
|
|
|
|
adv_search_params = @params[:advanced_search]
|
|
|
|
filter = @repository.repository_table_filters.new
|
2022-02-16 01:01:53 +08:00
|
|
|
adv_search_params[:filter_elements].each_with_index do |filter_element_params, index|
|
2021-12-21 19:38:52 +08:00
|
|
|
repository_rows =
|
|
|
|
if PREDEFINED_COLUMNS.include?(filter_element_params[:repository_column_id])
|
|
|
|
add_predefined_column_filter_condition(repository_rows, filter_element_params)
|
|
|
|
else
|
2022-02-16 01:01:53 +08:00
|
|
|
add_custom_column_filter_condition(repository_rows, filter, index, filter_element_params)
|
2021-12-21 19:38:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
repository_rows
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_predefined_column_filter_condition(repository_rows, filter_element_params)
|
2022-01-05 02:20:02 +08:00
|
|
|
case filter_element_params[:repository_column_id]
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'row_id'
|
|
|
|
build_row_id_filter_condition(repository_rows, filter_element_params)
|
2022-01-28 05:50:04 +08:00
|
|
|
when 'row_name'
|
2021-12-21 19:38:52 +08:00
|
|
|
build_name_filter_condition(repository_rows, filter_element_params)
|
|
|
|
when 'added_on'
|
|
|
|
build_added_on_filter_condition(repository_rows, filter_element_params)
|
|
|
|
when 'added_by'
|
|
|
|
build_added_by_filter_condition(repository_rows, filter_element_params)
|
|
|
|
when 'archived_by'
|
|
|
|
build_archived_by_filter_condition(repository_rows, filter_element_params)
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'archived_on'
|
|
|
|
build_archived_on_filter_condition(repository_rows, filter_element_params)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'assigned'
|
|
|
|
build_assigned_filter_condition(repository_rows, filter_element_params)
|
|
|
|
else
|
|
|
|
repository_rows
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_row_id_filter_condition(repository_rows, filter_element_params)
|
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'contains'
|
|
|
|
repository_rows
|
|
|
|
.where("(#{RepositoryRow::PREFIXED_ID_SQL})::text ILIKE ?",
|
|
|
|
"%#{ActiveRecord::Base.sanitize_sql_like(filter_element_params.dig(:parameters, :text))}%")
|
|
|
|
when 'doesnt_contain'
|
|
|
|
repository_rows
|
|
|
|
.where.not("(#{RepositoryRow::PREFIXED_ID_SQL})::text ILIKE ?",
|
|
|
|
"%#{ActiveRecord::Base.sanitize_sql_like(filter_element_params.dig(:parameters, :text))}%")
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow ID!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_name_filter_condition(repository_rows, filter_element_params)
|
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'contains'
|
|
|
|
repository_rows.where('repository_rows.name ILIKE ?',
|
|
|
|
"%#{ActiveRecord::Base.sanitize_sql_like(filter_element_params.dig(:parameters, :text))}%")
|
|
|
|
when 'doesnt_contain'
|
|
|
|
repository_rows
|
|
|
|
.where.not('repository_rows.name ILIKE ?',
|
|
|
|
"%#{ActiveRecord::Base.sanitize_sql_like(filter_element_params.dig(:parameters, :text))}%")
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow Name!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_added_on_filter_condition(repository_rows, filter_element_params)
|
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'today'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
2022-02-22 21:14:16 +08:00
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") >= ? AND " \
|
2022-02-25 21:30:37 +08:00
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") < ?",
|
2022-02-22 21:14:16 +08:00
|
|
|
Time.zone.now.beginning_of_day,
|
|
|
|
Time.zone.now.end_of_day
|
2022-02-14 22:03:50 +08:00
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'yesterday'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") >= ? AND "\
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") < ?",
|
|
|
|
Time.zone.now.beginning_of_day - 1.day, Time.zone.now.beginning_of_day
|
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'last_week'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") >= ? AND "\
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") < ?",
|
|
|
|
Time.zone.now.beginning_of_week - 1.week, Time.zone.now.beginning_of_week
|
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'this_month'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
2022-02-28 18:16:29 +08:00
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") >= ? AND date_trunc('minute', \"repository_rows\".\"created_at\") <= ?",
|
2022-02-22 21:14:16 +08:00
|
|
|
Time.zone.now.beginning_of_month,
|
|
|
|
Time.zone.now.end_of_month
|
2022-02-14 22:03:50 +08:00
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'last_year'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") >= ? AND "\
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") < ?",
|
|
|
|
Time.zone.now.beginning_of_year - 1.year, Time.zone.now.beginning_of_year
|
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'this_year'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
2022-02-22 21:14:16 +08:00
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") >= ? AND "\
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") <= ?",
|
|
|
|
Time.zone.now.beginning_of_year,
|
|
|
|
Time.zone.now.end_of_year
|
2022-02-14 22:03:50 +08:00
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'equal_to'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"created_at\") = ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'unequal_to'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where.not("date_trunc('minute', \"repository_rows\".\"created_at\") = ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'greater_than'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"created_at\") > ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'greater_than_or_equal_to'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"created_at\") >= ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'less_than'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"created_at\") < ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'less_than_or_equal_to'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"created_at\") <= ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'between'
|
2022-02-14 22:03:50 +08:00
|
|
|
repository_rows.where(
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") > ? AND "\
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"created_at\") < ?",
|
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :start_datetime)),
|
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :end_datetime))
|
|
|
|
)
|
2021-12-21 19:38:52 +08:00
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow Added On!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-03 20:18:15 +08:00
|
|
|
def build_archived_on_filter_condition(repository_rows, filter_element_params)
|
2022-02-25 21:29:27 +08:00
|
|
|
return repository_rows unless @params[:archived]
|
|
|
|
|
2022-02-03 20:18:15 +08:00
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'today'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") >= ? AND date_trunc('minute', \"repository_rows\".\"archived_on\") <= ?",
|
2022-02-22 21:14:16 +08:00
|
|
|
Time.zone.now.beginning_of_day, Time.zone.now.end_of_day)
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'yesterday'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") >= ? AND date_trunc('minute', \"repository_rows\".\"archived_on\") < ?",
|
2022-02-03 20:18:15 +08:00
|
|
|
Time.zone.now.beginning_of_day - 1.day, Time.zone.now.beginning_of_day)
|
|
|
|
when 'last_week'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") >= ? AND date_trunc('minute', \"repository_rows\".\"archived_on\") < ?",
|
2022-02-03 20:18:15 +08:00
|
|
|
Time.zone.now.beginning_of_week - 1.week, Time.zone.now.beginning_of_week)
|
|
|
|
when 'this_month'
|
2022-02-22 21:14:16 +08:00
|
|
|
repository_rows.where(
|
2022-02-28 18:16:29 +08:00
|
|
|
"date_trunc('minute', \"repository_rows\".\"archived_on\") >= ? AND date_trunc('minute', \"repository_rows\".\"archived_on\") <= ?",
|
2022-02-22 21:14:16 +08:00
|
|
|
Time.zone.now.beginning_of_month,
|
|
|
|
Time.zone.now.end_of_month
|
|
|
|
)
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'last_year'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") >= ? AND date_trunc('minute', \"repository_rows\".\"archived_on\") < ?",
|
2022-02-03 20:18:15 +08:00
|
|
|
Time.zone.now.beginning_of_year - 1.year, Time.zone.now.beginning_of_year)
|
|
|
|
when 'this_year'
|
2022-02-22 21:14:16 +08:00
|
|
|
repository_rows.where(
|
2022-02-25 21:29:27 +08:00
|
|
|
"date_trunc('minute', \"repository_rows\".\"archived_on\") >= ? AND "\
|
|
|
|
"date_trunc('minute', \"repository_rows\".\"archived_on\") <= ?",
|
2022-02-22 21:14:16 +08:00
|
|
|
Time.zone.now.beginning_of_year,
|
|
|
|
Time.zone.now.end_of_year
|
|
|
|
)
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'equal_to'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") = ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'unequal_to'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where.not("date_trunc('minute', \"repository_rows\".\"archived_on\") = ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'greater_than'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") > ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'greater_than_or_equal_to'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") >= ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'less_than'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") < ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'less_than_or_equal_to'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") <= ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
when 'between'
|
2022-02-28 18:16:29 +08:00
|
|
|
repository_rows.where("date_trunc('minute', \"repository_rows\".\"archived_on\") > ? AND date_trunc('minute', \"repository_rows\".\"archived_on\") < ?",
|
2022-02-11 17:45:21 +08:00
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :start_datetime)),
|
|
|
|
Time.zone.parse(filter_element_params.dig(:parameters, :end_datetime)))
|
2022-02-03 20:18:15 +08:00
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow Archived On!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-21 19:38:52 +08:00
|
|
|
def build_added_by_filter_condition(repository_rows, filter_element_params)
|
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'any_of'
|
|
|
|
repository_rows.joins(:created_by)
|
|
|
|
.where(created_by: { id: filter_element_params.dig(:parameters, :user_ids) })
|
|
|
|
when 'none_of'
|
|
|
|
repository_rows.joins(:created_by)
|
|
|
|
.where.not(created_by: { id: filter_element_params.dig(:parameters, :user_ids) })
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow Added By!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_archived_by_filter_condition(repository_rows, filter_element_params)
|
2022-02-25 21:29:27 +08:00
|
|
|
return repository_rows unless @params[:archived]
|
|
|
|
|
2021-12-21 19:38:52 +08:00
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'any_of'
|
|
|
|
repository_rows.joins(:archived_by)
|
|
|
|
.where(archived_by: { id: filter_element_params.dig(:parameters, :user_ids) })
|
|
|
|
when 'none_of'
|
|
|
|
repository_rows.joins(:archived_by)
|
|
|
|
.where.not(archived_by: { id: filter_element_params.dig(:parameters, :user_ids) })
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow Archived By!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_assigned_filter_condition(repository_rows, filter_element_params)
|
|
|
|
case filter_element_params[:operator]
|
|
|
|
when 'any_of'
|
|
|
|
repository_rows.joins(:my_modules)
|
|
|
|
.where(my_modules: { id: filter_element_params.dig(:parameters, :my_module_ids) })
|
|
|
|
when 'none_of'
|
2022-02-23 17:05:24 +08:00
|
|
|
repository_rows.where('NOT EXISTS (SELECT NULL FROM my_module_repository_rows
|
|
|
|
WHERE my_module_repository_rows.repository_row_id = repository_rows.id AND
|
|
|
|
my_module_repository_rows.my_module_id IN (?))', filter_element_params.dig(:parameters, :my_module_ids))
|
2021-12-21 19:38:52 +08:00
|
|
|
when 'all_of'
|
|
|
|
repository_rows
|
|
|
|
.joins(:my_modules)
|
|
|
|
.where(my_modules: { id: filter_element_params.dig(:parameters, :my_module_ids) })
|
|
|
|
.having('COUNT(my_modules.id) = ?', filter_element_params.dig(:parameters, :my_module_ids).count)
|
|
|
|
.group(:id)
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Wrong operator for RepositoryRow Assigned To!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-16 01:01:53 +08:00
|
|
|
def add_custom_column_filter_condition(repository_rows, filter, index, filter_element_params)
|
2022-02-11 17:24:29 +08:00
|
|
|
repository_column = @repository.repository_columns.find_by(id: filter_element_params['repository_column_id'])
|
|
|
|
raise RepositoryFilters::ColumnNotFoundException unless repository_column
|
|
|
|
|
2021-12-21 19:38:52 +08:00
|
|
|
filter_element = filter.repository_table_filter_elements.new(
|
2022-02-11 17:24:29 +08:00
|
|
|
repository_column: repository_column,
|
2021-12-21 19:38:52 +08:00
|
|
|
operator: filter_element_params[:operator],
|
|
|
|
parameters: filter_element_params[:parameters]
|
|
|
|
)
|
2022-01-18 04:53:13 +08:00
|
|
|
config = Extends::REPOSITORY_ADVANCED_SEARCH_ATTR[filter_element.repository_column.data_type.to_sym]
|
2022-02-16 01:01:53 +08:00
|
|
|
join_cells_alias = "repository_cells_#{index}"
|
|
|
|
join_values_alias = "repository_values_#{index}"
|
2021-12-21 19:38:52 +08:00
|
|
|
|
2022-02-16 01:01:53 +08:00
|
|
|
enforce_referenced_value_existence!(filter_element)
|
2022-02-11 17:24:29 +08:00
|
|
|
|
2022-02-16 01:01:53 +08:00
|
|
|
repository_rows =
|
|
|
|
repository_rows
|
|
|
|
.joins(
|
|
|
|
"LEFT OUTER JOIN \"repository_cells\" AS \"#{join_cells_alias}\"" \
|
|
|
|
" ON \"repository_rows\".\"id\" = \"#{join_cells_alias}\".\"repository_row_id\"" \
|
|
|
|
" AND \"#{join_cells_alias}\".\"repository_column_id\" = '#{filter_element.repository_column.id}'"
|
|
|
|
).joins(
|
|
|
|
"LEFT OUTER JOIN \"#{config[:table_name]}\" AS \"#{join_values_alias}\"" \
|
|
|
|
" ON \"#{join_values_alias}\".\"id\" = \"#{join_cells_alias}\".\"value_id\""
|
|
|
|
)
|
2022-02-11 17:24:29 +08:00
|
|
|
|
2022-02-16 01:01:53 +08:00
|
|
|
if %w(empty file_not_attached).include?(filter_element_params[:operator])
|
|
|
|
repository_rows.where(join_values_alias => { id: nil })
|
|
|
|
else
|
2021-12-21 19:38:52 +08:00
|
|
|
value_klass = filter_element.repository_column.data_type.constantize
|
2022-01-31 18:10:39 +08:00
|
|
|
value_klass.add_filter_condition(repository_rows, join_values_alias, filter_element)
|
2021-12-21 19:38:52 +08:00
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2022-02-11 17:24:29 +08:00
|
|
|
def enforce_referenced_value_existence!(filter_element)
|
|
|
|
relation_method =
|
|
|
|
Extends::REPOSITORY_ADVANCED_SEARCH_REFERENCED_VALUE_RELATIONS[
|
|
|
|
filter_element.repository_column.data_type.to_sym
|
|
|
|
]
|
|
|
|
|
|
|
|
return unless relation_method
|
|
|
|
|
|
|
|
relation = filter_element.repository_column.public_send(relation_method)
|
|
|
|
value_item_ids = filter_element.parameters['item_ids']
|
|
|
|
raise RepositoryFilters::ValueNotFoundException if relation.where(id: value_item_ids).count != value_item_ids.length
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
|
2020-01-13 23:09:07 +08:00
|
|
|
def build_sortable_columns
|
2021-08-20 19:14:18 +08:00
|
|
|
array = @repository.default_sortable_columns
|
2018-03-09 00:36:10 +08:00
|
|
|
@repository.repository_columns.count.times do
|
|
|
|
array << 'repository_cell.value'
|
|
|
|
end
|
2022-06-21 16:49:03 +08:00
|
|
|
|
|
|
|
array << 'consumed_stock' if @repository.has_stock_management? && @assigned_view
|
2018-03-09 00:36:10 +08:00
|
|
|
array
|
|
|
|
end
|
|
|
|
|
2022-06-21 16:49:03 +08:00
|
|
|
def map_simple_view_column(index)
|
|
|
|
return assigned_repository_simple_view_name_column_id(@repository) unless @repository.has_stock_management?
|
|
|
|
|
|
|
|
case index
|
|
|
|
when 0
|
|
|
|
assigned_repository_simple_view_name_column_id(@repository)
|
|
|
|
when 1
|
|
|
|
@mappings[@repository.repository_stock_column.id]
|
|
|
|
when 2
|
|
|
|
@sortable_columns.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
def sort_rows(column_obj, records)
|
2018-03-16 19:27:19 +08:00
|
|
|
dir = %w(DESC ASC).find do |direction|
|
|
|
|
direction == column_obj[:dir].upcase
|
2018-03-09 21:43:12 +08:00
|
|
|
end || 'ASC'
|
2020-01-13 23:09:07 +08:00
|
|
|
|
2018-03-09 00:36:10 +08:00
|
|
|
column_index = column_obj[:column]
|
2018-04-18 21:53:48 +08:00
|
|
|
service = RepositoryTableStateService.new(@user, @repository)
|
|
|
|
col_order = service.load_state.state['ColReorder']
|
2022-06-21 16:49:03 +08:00
|
|
|
column_id = case @params[:assigned]
|
|
|
|
when 'assigned_simple'
|
|
|
|
map_simple_view_column(column_index).to_i
|
|
|
|
else
|
|
|
|
@params[:draw].to_i == 1 ? column_index.to_i : col_order[column_index].to_i
|
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
|
2021-07-07 23:43:51 +08:00
|
|
|
case @sortable_columns[column_id - 1]
|
|
|
|
when 'assigned'
|
2022-06-21 16:49:03 +08:00
|
|
|
return records if @my_module && @assigned_view
|
2019-12-18 21:10:41 +08:00
|
|
|
|
2020-01-13 23:09:07 +08:00
|
|
|
records.order("assigned_my_modules_count #{dir}")
|
2021-07-07 23:43:51 +08:00
|
|
|
when 'repository_cell.value'
|
2018-03-09 00:36:10 +08:00
|
|
|
id = @mappings.key(column_id.to_s)
|
2021-07-07 23:43:51 +08:00
|
|
|
sorting_column = @repository.repository_columns.find_by(id: id)
|
2019-06-06 23:28:59 +08:00
|
|
|
return records unless sorting_column
|
|
|
|
|
|
|
|
sorting_data_type = sorting_column.data_type.constantize
|
2022-03-28 22:48:05 +08:00
|
|
|
cells = sorting_data_type.joins(
|
|
|
|
"INNER JOIN repository_cells AS repository_sort_cells "\
|
|
|
|
"ON repository_sort_cells.value_id = #{sorting_data_type.table_name}.id "\
|
2022-04-08 17:08:51 +08:00
|
|
|
"AND repository_sort_cells.value_type = '#{sorting_data_type.base_class.name}'"
|
2022-03-28 22:48:05 +08:00
|
|
|
).where('repository_sort_cells.repository_column_id': sorting_column.id)
|
|
|
|
|
2021-07-07 23:43:51 +08:00
|
|
|
if sorting_data_type.const_defined?('EXTRA_SORTABLE_VALUE_INCLUDE')
|
|
|
|
cells = cells.joins(sorting_data_type::EXTRA_SORTABLE_VALUE_INCLUDE)
|
|
|
|
end
|
2019-06-06 23:28:59 +08:00
|
|
|
|
2020-04-08 03:02:16 +08:00
|
|
|
cells = if sorting_column.repository_checklist_value?
|
2021-07-07 23:43:51 +08:00
|
|
|
cells
|
2022-03-28 22:48:05 +08:00
|
|
|
.select("repository_sort_cells.repository_row_id, \
|
2021-07-07 23:43:51 +08:00
|
|
|
STRING_AGG(#{sorting_data_type::SORTABLE_COLUMN_NAME}, ' ' \
|
|
|
|
ORDER BY #{sorting_data_type::SORTABLE_COLUMN_NAME}) AS value")
|
2022-03-28 22:48:05 +08:00
|
|
|
.group('repository_sort_cells.repository_row_id')
|
2020-04-08 03:02:16 +08:00
|
|
|
else
|
2021-07-07 23:43:51 +08:00
|
|
|
cells
|
2022-03-28 22:48:05 +08:00
|
|
|
.select("repository_sort_cells.repository_row_id, #{sorting_data_type::SORTABLE_COLUMN_NAME} AS value")
|
2020-04-08 03:02:16 +08:00
|
|
|
end
|
2019-06-06 23:28:59 +08:00
|
|
|
|
|
|
|
records.joins("LEFT OUTER JOIN (#{cells.to_sql}) AS values ON values.repository_row_id = repository_rows.id")
|
2020-01-13 23:09:07 +08:00
|
|
|
.group('values.value')
|
2019-06-06 23:28:59 +08:00
|
|
|
.order("values.value #{dir}")
|
2021-07-07 23:43:51 +08:00
|
|
|
when 'users.full_name'
|
2020-01-13 23:09:07 +08:00
|
|
|
records.group('users.full_name').order("users.full_name #{dir}")
|
2022-06-21 16:49:03 +08:00
|
|
|
when 'consumed_stock'
|
|
|
|
records.order("#{@sortable_columns[column_id - 1]} #{dir}")
|
2018-03-09 00:36:10 +08:00
|
|
|
else
|
2020-01-13 23:09:07 +08:00
|
|
|
records.group(@sortable_columns[column_id - 1]).order("#{@sortable_columns[column_id - 1]} #{dir}")
|
2018-03-09 00:36:10 +08:00
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
end
|