From 5493d543b0ef8c32ab28f31b039bdce5d319f732 Mon Sep 17 00:00:00 2001 From: Andrej Date: Thu, 30 May 2024 15:50:16 +0200 Subject: [PATCH] Add advanced repository filter for updated on and updated by [SCI-10751] --- app/javascript/packs/vue/repository_filter.js | 22 +++++ app/services/repository_datatable_service.rb | 91 ++++++++++++++++++- 2 files changed, 112 insertions(+), 1 deletion(-) diff --git a/app/javascript/packs/vue/repository_filter.js b/app/javascript/packs/vue/repository_filter.js index f130d82fd..5cd5ad142 100644 --- a/app/javascript/packs/vue/repository_filter.js +++ b/app/javascript/packs/vue/repository_filter.js @@ -64,6 +64,26 @@ const DEFAULT_FILTERS = [ }, data: { operator: 'any_of' }, isBlank: true + }, + { + id: 7, + column: { + data_type: 'RepositoryDateTimeValue', + id: 'updated_on', + name: I18n.t('repositories.table.updated_on') + }, + data: { operator: 'equal_to' }, + isBlank: true + }, + { + id: 8, + column: { + data_type: 'RepositoryUserValue', + id: 'updated_by', + name: I18n.t('repositories.table.updated_by') + }, + data: { operator: 'any_of' }, + isBlank: true } ]; @@ -76,6 +96,8 @@ window.initRepositoryFilter = () => { { id: 'relationships', name: I18n.t('repositories.table.relationships'), data_type: 'RepositoryRelationshipValue' }, { id: 'added_on', name: I18n.t('repositories.table.added_on'), data_type: 'RepositoryDateTimeValue' }, { id: 'added_by', name: I18n.t('repositories.table.added_by'), data_type: 'RepositoryUserValue' }, + { id: 'updated_on', name: I18n.t('repositories.table.updated_on'), data_type: 'RepositoryDateTimeValue' }, + { id: 'updated_by', name: I18n.t('repositories.table.updated_by'), data_type: 'RepositoryUserValue' }, { id: 'archived_by', name: I18n.t('repositories.table.archived_by'), data_type: 'RepositoryUserValue' }, { id: 'archived_on', name: I18n.t('repositories.table.archived_on'), data_type: 'RepositoryDateTimeValue' } ]; diff --git a/app/services/repository_datatable_service.rb b/app/services/repository_datatable_service.rb index 027f5186d..44b0d58f6 100644 --- a/app/services/repository_datatable_service.rb +++ b/app/services/repository_datatable_service.rb @@ -10,7 +10,8 @@ class RepositoryDatatableService include MyModulesHelper attr_reader :repository_rows, :all_count, :mappings - PREDEFINED_COLUMNS = %w(row_id row_name added_on added_by archived_on archived_by assigned relationships).freeze + PREDEFINED_COLUMNS = %w(row_id row_name added_on added_by archived_on archived_by + assigned relationships updated_on updated_by).freeze def initialize(repository, params, user, my_module = nil) @repository = repository @@ -160,6 +161,10 @@ class RepositoryDatatableService build_added_on_filter_condition(repository_rows, filter_element_params) when 'added_by' build_added_by_filter_condition(repository_rows, filter_element_params) + when 'updated_on' + build_updated_on_filter_condition(repository_rows, filter_element_params) + when 'updated_by' + build_updated_by_filter_condition(repository_rows, filter_element_params) when 'archived_by' build_archived_by_filter_condition(repository_rows, filter_element_params) when 'archived_on' @@ -356,6 +361,77 @@ class RepositoryDatatableService end end + def build_updated_on_filter_condition(repository_rows, filter_element_params) + case filter_element_params[:operator] + when 'today' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") >= ? AND " \ + "date_trunc('minute', \"repository_rows\".\"updated_at\") < ?", + Time.zone.now.beginning_of_day, + Time.zone.now.end_of_day + ) + when 'yesterday' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") >= ? AND "\ + "date_trunc('minute', \"repository_rows\".\"updated_at\") < ?", + Time.zone.now.beginning_of_day - 1.day, Time.zone.now.beginning_of_day + ) + when 'last_week' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") >= ? AND "\ + "date_trunc('minute', \"repository_rows\".\"updated_at\") < ?", + Time.zone.now.beginning_of_week - 1.week, Time.zone.now.beginning_of_week + ) + when 'this_month' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") >= ? AND "\ + "date_trunc('minute', \"repository_rows\".\"updated_at\") <= ?", + Time.zone.now.beginning_of_month, + Time.zone.now.end_of_month + ) + when 'last_year' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") >= ? AND "\ + "date_trunc('minute', \"repository_rows\".\"updated_at\") < ?", + Time.zone.now.beginning_of_year - 1.year, Time.zone.now.beginning_of_year + ) + when 'this_year' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") >= ? AND "\ + "date_trunc('minute', \"repository_rows\".\"updated_at\") <= ?", + Time.zone.now.beginning_of_year, + Time.zone.now.end_of_year + ) + when 'equal_to' + repository_rows.where("date_trunc('minute', \"repository_rows\".\"updated_at\") = ?", + Time.zone.parse(filter_element_params.dig(:parameters, :datetime))) + when 'unequal_to' + repository_rows.where.not("date_trunc('minute', \"repository_rows\".\"updated_at\") = ?", + Time.zone.parse(filter_element_params.dig(:parameters, :datetime))) + when 'greater_than' + repository_rows.where("date_trunc('minute', \"repository_rows\".\"updated_at\") > ?", + Time.zone.parse(filter_element_params.dig(:parameters, :datetime))) + when 'greater_than_or_equal_to' + repository_rows.where("date_trunc('minute', \"repository_rows\".\"updated_at\") >= ?", + Time.zone.parse(filter_element_params.dig(:parameters, :datetime))) + when 'less_than' + repository_rows.where("date_trunc('minute', \"repository_rows\".\"updated_at\") < ?", + Time.zone.parse(filter_element_params.dig(:parameters, :datetime))) + when 'less_than_or_equal_to' + repository_rows.where("date_trunc('minute', \"repository_rows\".\"updated_at\") <= ?", + Time.zone.parse(filter_element_params.dig(:parameters, :datetime))) + when 'between' + repository_rows.where( + "date_trunc('minute', \"repository_rows\".\"updated_at\") > ? AND "\ + "date_trunc('minute', \"repository_rows\".\"updated_at\") < ?", + Time.zone.parse(filter_element_params.dig(:parameters, :start_datetime)), + Time.zone.parse(filter_element_params.dig(:parameters, :end_datetime)) + ) + else + raise ArgumentError, 'Wrong operator for RepositoryRow Updated on!' + end + end + def build_added_by_filter_condition(repository_rows, filter_element_params) case filter_element_params[:operator] when 'any_of' @@ -384,6 +460,19 @@ class RepositoryDatatableService end end + def build_updated_by_filter_condition(repository_rows, filter_element_params) + case filter_element_params[:operator] + when 'any_of' + repository_rows.joins(:last_modified_by) + .where(last_modified_by: { id: filter_element_params.dig(:parameters, :user_ids) }) + when 'none_of' + repository_rows.joins(:last_modified_by) + .where.not(last_modified_by: { id: filter_element_params.dig(:parameters, :user_ids) }) + else + raise ArgumentError, 'Wrong operator for RepositoryRow Updated By!' + end + end + def build_assigned_filter_condition(repository_rows, filter_element_params) return repository_rows if filter_element_params.dig(:parameters, :my_module_ids).blank?