From 66081950f05248040927b192160bb683a27e5590 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 4 Jan 2022 19:20:02 +0100 Subject: [PATCH] Fix added_on filter, add specs [SCI-6388] --- .../mixins/filters/date_time_filter.js | 42 +---- app/services/repository_datatable_service.rb | 29 ++-- .../repository_datatable_service_spec.rb | 158 +++++++++++++++--- 3 files changed, 151 insertions(+), 78 deletions(-) diff --git a/app/javascript/vue/repository_filter/mixins/filters/date_time_filter.js b/app/javascript/vue/repository_filter/mixins/filters/date_time_filter.js index e7e081b4f..58b87fbfb 100644 --- a/app/javascript/vue/repository_filter/mixins/filters/date_time_filter.js +++ b/app/javascript/vue/repository_filter/mixins/filters/date_time_filter.js @@ -8,52 +8,16 @@ export default { this.dateTo = null; } - let date = null; - let dateTo = null; - - let today = new Date(); - - switch (this.operator) { - case 'today': - date = today; - dateTo = today; - break; - case 'yesterday': - date = new Date(new Date().setDate(today.getDate() - 1)); - dateTo = date; - break; - case 'last_week': - let monday = new Date(new Date().setDate( - today.getDate() - today.getDay() - (today.getDay() === 0 ? 6 : -1)) - ); - let lastWeekEnd = new Date(new Date().setDate(monday.getDate() - 1)); - let lastWeekStart = new Date(new Date().setDate(monday.getDate() - 7)); - date = lastWeekStart; - dateTo = lastWeekEnd; - break; - case 'this_month': - date = new Date(today.getFullYear(), today.getMonth(), 1); - dateTo = today; - break; - case 'this_year': - date = new Date(new Date().getFullYear(), 0, 1); - dateTo = today; - break; - case 'last_year': - date = new Date(new Date().getFullYear() - 1, 0, 1); - dateTo = new Date(new Date().getFullYear() - 1, 11, 31); - break; + if(this.isPreset) { + this.date = null; + this.dateTo = null; } - - date && this.updateDate(new Date(date.setHours(0, 0, 0))); - dateTo && this.updateDateTo(new Date(dateTo.setHours(23, 59, 59))); } }, computed: { isBlank() { return this.operator === 'equal_to' && !this.value; }, - isPreset() { return [ 'today', diff --git a/app/services/repository_datatable_service.rb b/app/services/repository_datatable_service.rb index e686203b1..88784f53f 100644 --- a/app/services/repository_datatable_service.rb +++ b/app/services/repository_datatable_service.rb @@ -111,7 +111,6 @@ class RepositoryDatatableService def advanced_search(repository_rows) adv_search_params = @params[:advanced_search] filter = @repository.repository_table_filters.new - adv_search_params[:filter_elements].each do |filter_element_params| repository_rows = if PREDEFINED_COLUMNS.include?(filter_element_params[:repository_column_id]) @@ -125,7 +124,7 @@ class RepositoryDatatableService end def add_predefined_column_filter_condition(repository_rows, filter_element_params) - case filter_element_params['repository_column_id'] + case filter_element_params[:repository_column_id] when 'row_id' build_row_id_filter_condition(repository_rows, filter_element_params) when 'name' @@ -179,35 +178,35 @@ class RepositoryDatatableService def build_added_on_filter_condition(repository_rows, filter_element_params) case filter_element_params[:operator] when 'today' - repository_rows.where('repository_date_time_values.data >= ?', Time.zone.now.beginning_of_day) + repository_rows.where('created_at >= ?', Time.zone.now.beginning_of_day) when 'yesterday' - repository_rows.where('repository_date_time_values.data >= ? AND repository_date_time_values.data < ?', + repository_rows.where('created_at >= ? AND created_at < ?', Time.zone.now.beginning_of_day - 1.day, Time.zone.now.beginning_of_day) when 'last_week' - repository_rows.where('repository_date_time_values.data >= ? AND repository_date_time_values.data < ?', + repository_rows.where('created_at >= ? AND created_at < ?', Time.zone.now.beginning_of_week - 1.week, Time.zone.now.beginning_of_week) when 'this_month' - repository_rows.where('repository_date_time_values.data >= ?', Time.zone.now.beginning_of_month) + repository_rows.where('created_at >= ?', Time.zone.now.beginning_of_month) when 'last_year' - repository_rows.where('repository_date_time_values.data >= ? AND repository_date_time_values.data < ?', + repository_rows.where('created_at >= ? AND created_at < ?', Time.zone.now.beginning_of_year - 1.year, Time.zone.now.beginning_of_year) when 'this_year' - repository_rows.where('repository_date_time_values.data >= ?', Time.zone.now.beginning_of_year) + repository_rows.where('created_at >= ?', Time.zone.now.beginning_of_year) when 'equal_to' - repository_rows.where(repository_date_time_values: { data: filter_element_params.dig(:parameters, :datetime) }) + repository_rows.where(created_at: filter_element_params.dig(:parameters, :datetime)) when 'unequal_to' repository_rows - .where.not(repository_date_time_values: { data: filter_element_params.dig(:parameters, :datetime) }) + .where.not(created_at: filter_element_params.dig(:parameters, :datetime)) when 'greater_than' - repository_rows.where('repository_date_time_values.data > ?', filter_element_params.dig(:parameters, :datetime)) + repository_rows.where('created_at > ?', filter_element_params.dig(:parameters, :datetime)) when 'greater_than_or_equal_to' - repository_rows.where('repository_date_time_values.data >= ?', filter_element_params.dig(:parameters, :datetime)) + repository_rows.where('created_at >= ?', filter_element_params.dig(:parameters, :datetime)) when 'less_than' - repository_rows.where('repository_date_time_values.data < ?', filter_element_params.dig(:parameters, :datetime)) + repository_rows.where('created_at < ?', filter_element_params.dig(:parameters, :datetime)) when 'less_than_or_equal_to' - repository_rows.where('repository_date_time_values.data =< ?', filter_element_params.dig(:parameters, :datetime)) + repository_rows.where('created_at =< ?', filter_element_params.dig(:parameters, :datetime)) when 'between' - repository_rows.where('repository_date_time_values.data > ? AND repository_date_time_values.data < ?', + repository_rows.where('created_at > ? AND created_at < ?', filter_element_params.dig(:parameters, :start_datetime), filter_element_params.dig(:parameters, :end_datetime)) else diff --git a/spec/services/repository_datatable_service_spec.rb b/spec/services/repository_datatable_service_spec.rb index a9ff67eaf..a323d7ae9 100644 --- a/spec/services/repository_datatable_service_spec.rb +++ b/spec/services/repository_datatable_service_spec.rb @@ -49,30 +49,9 @@ describe RepositoryDatatableService do } end - context 'object' do - let(:params) do - { order: { 0 => { column: '3', dir: 'asc' } }, - search: { value: 'row' } } - end - - let(:subject) do - RepositoryDatatableService.new(repository, params, user) - end - - describe '#build_conditions/1' do - it 'parsers the contitions' do - contitions = subject.send(:build_conditions, params) - expect(contitions[:search_value]).to eq 'row' - expect(contitions[:order_by_column]).to eq( - column: 3, dir: 'asc' - ) - end - end - end - describe 'ordering' do it 'is ordered by row name asc' do - params = { order: { 0 => { column: '3', dir: 'asc' } }, + params = { order:[{ column: '3', dir: 'asc' }], search: { value: '' } } subject = RepositoryDatatableService.new(repository, params, @@ -82,7 +61,7 @@ describe RepositoryDatatableService do end it 'is ordered by row name desc' do - params = { order: { 0 => { column: '3', dir: 'desc' } }, + params = { order: [{ column: '3', dir: 'desc' }], search: { value: '' } } subject = RepositoryDatatableService.new(repository, params, @@ -101,7 +80,7 @@ describe RepositoryDatatableService do end it 'returns only the searched entity' do - params = { order: { 0 => { column: '4', dir: 'desc' } }, + params = { order: [{ column: '4', dir: 'desc' }], search: { value: 'test' } } subject = RepositoryDatatableService.new(repository, params, @@ -109,5 +88,136 @@ describe RepositoryDatatableService do expect(subject.repository_rows.first.name).to eq 'test' expect(subject.repository_rows.length).to eq 1 end + + context 'when using advanced filter time presets' do + let(:base_params) do + { order: [{ column: '4', dir: 'desc' }], search: { value: '' } } + end + it 'returns the rows matching "today"' do + repository_row.update_column(:created_at, 2.days.ago) + today_repository_row = RepositoryRow.create( + name: "Today", + repository: repository, + created_by: user, + last_modified_by: user + ) + + params = base_params.merge( + advanced_search: { + filter_elements: [{repository_column_id: "added_on", operator: "today"}] + } + ) + + repository_rows = RepositoryDatatableService.new(repository, params, user).repository_rows + expect(repository_rows).to include(today_repository_row) + expect(repository_rows).to_not include(repository_row) + end + + it 'returns the rows matching "yesterday"' do + yesterday_repository_row = RepositoryRow.create( + name: "Yesterday", + repository: repository, + created_by: user, + last_modified_by: user + ) + + yesterday_repository_row.update_column(:created_at, 1.day.ago) + + params = base_params.merge( + advanced_search: { + filter_elements: [{repository_column_id: "added_on", operator: "yesterday"}] + } + ) + + repository_rows = RepositoryDatatableService.new(repository, params, user).repository_rows + expect(repository_rows).to include(yesterday_repository_row) + expect(repository_rows).to_not include(repository_row) + end + + it 'returns the rows matching "last_week"' do + last_week_repository_row = RepositoryRow.create( + name: "Last week", + repository: repository, + created_by: user, + last_modified_by: user + ) + + last_week_repository_row.update_column(:created_at, 1.week.ago) + + params = base_params.merge( + advanced_search: { + filter_elements: [{repository_column_id: "added_on", operator: "last_week"}] + } + ) + + repository_rows = RepositoryDatatableService.new(repository, params, user).repository_rows + expect(repository_rows).to include(last_week_repository_row) + expect(repository_rows).to_not include(repository_row) + end + + it 'returns the rows matching "this_month"' do + repository_row.update_column(:created_at, Time.now.beginning_of_month) + previous_month_repository_row = RepositoryRow.create( + name: "Last week", + repository: repository, + created_by: user, + last_modified_by: user + ) + + previous_month_repository_row.update_column(:created_at, 1.month.ago) + + params = base_params.merge( + advanced_search: { + filter_elements: [{repository_column_id: "added_on", operator: "this_month"}] + } + ) + + repository_rows = RepositoryDatatableService.new(repository, params, user).repository_rows + expect(repository_rows).to include(repository_row) + expect(repository_rows).to_not include(previous_month_repository_row) + end + + it 'returns the rows matching "last_year"' do + last_year_repository_row = RepositoryRow.create( + name: "Yesterday", + repository: repository, + created_by: user, + last_modified_by: user + ) + + last_year_repository_row.update_column(:created_at, 1.year.ago) + + params = base_params.merge( + advanced_search: { + filter_elements: [{repository_column_id: "added_on", operator: "last_year"}] + } + ) + + repository_rows = RepositoryDatatableService.new(repository, params, user).repository_rows + expect(repository_rows).to include(last_year_repository_row) + expect(repository_rows).to_not include(repository_row) + end + + it 'returns the rows matching "this_year"' do + last_year_repository_row = RepositoryRow.create( + name: "Yesterday", + repository: repository, + created_by: user, + last_modified_by: user + ) + + last_year_repository_row.update_column(:created_at, 1.year.ago.end_of_year) + + params = base_params.merge( + advanced_search: { + filter_elements: [{repository_column_id: "added_on", operator: "this_year"}] + } + ) + + repository_rows = RepositoryDatatableService.new(repository, params, user).repository_rows + expect(repository_rows).to_not include(last_year_repository_row) + expect(repository_rows).to include(repository_row) + end + end end end