From e77f75907ee41d7d9cf631075efe2aa5d864b75d Mon Sep 17 00:00:00 2001 From: ncounter Date: Wed, 18 Dec 2024 14:42:45 +0100 Subject: [PATCH] Filter requests by created_at range --- .../concerns/webui/requests_filter.rb | 13 +++++++- .../app/models/bs_request/find_for/query.rb | 3 ++ src/api/app/views/webui/shared/_input.haml | 3 +- .../bs_requests/_requests_filter.html.haml | 17 +++++++++- .../beta/webui/requests_index_spec.rb | 31 +++++++++++++++++++ 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/api/app/controllers/concerns/webui/requests_filter.rb b/src/api/app/controllers/concerns/webui/requests_filter.rb index 492cfafdfac..78f8a5630e5 100644 --- a/src/api/app/controllers/concerns/webui/requests_filter.rb +++ b/src/api/app/controllers/concerns/webui/requests_filter.rb @@ -18,10 +18,15 @@ def filter_requests params[:states] = @filter_state if @filter_state.present? params[:types] = @filter_action_type if @filter_action_type.present? + params[:created_at_from] = @filter_created_at_from if @filter_created_at_from.present? + params[:created_at_to] = @filter_created_at_to if @filter_created_at_to.present? + @bs_requests = BsRequest::FindFor::Query.new(params, initial_bs_requests).all set_selected_filter end + # rubocop:disable Metrics/PerceivedComplexity + # rubocop:disable Metrics/CyclomaticComplexity def set_filters @filter_direction = params[:direction].presence || 'all' @filter_direction = 'all' if ALLOWED_DIRECTIONS.exclude?(@filter_direction) @@ -33,11 +38,17 @@ def set_filters @filter_action_type = @filter_action_type.intersection(BsRequestAction::TYPES) @filter_creators = params[:creators].present? ? params[:creators].compact_blank! : [] + + @filter_created_at_from = params[:created_at_from].presence || '' + @filter_created_at_to = params[:created_at_to].presence || '' end + # rubocop:enable Metrics/PerceivedComplexity + # rubocop:enable Metrics/CyclomaticComplexity def set_selected_filter @selected_filter = { direction: @filter_direction, action_type: @filter_action_type, search_text: params[:requests_search_text], - state: @filter_state, creators: @filter_creators } + state: @filter_state, creators: @filter_creators, created_at_from: @filter_created_at_from, + created_at_to: @filter_created_at_to } end def filter_by_text(text) diff --git a/src/api/app/models/bs_request/find_for/query.rb b/src/api/app/models/bs_request/find_for/query.rb index 817de272950..0756eb92dfb 100644 --- a/src/api/app/models/bs_request/find_for/query.rb +++ b/src/api/app/models/bs_request/find_for/query.rb @@ -10,6 +10,9 @@ def all @relation = BsRequest::FindFor::Project.new(@parameters, @relation).all @relation = BsRequest::FindFor::User.new(@parameters, @relation).all if user_login.present? @relation = BsRequest::FindFor::Group.new(@parameters, @relation).all if group_title.present? + created_at_from = Date.parse(@parameters['created_at_from']) if @parameters['created_at_from'].present? + created_at_to = Date.parse(@parameters['created_at_to']) if @parameters['created_at_to'].present? + @relation = @relation.where(created_at: (created_at_from&.beginning_of_day..created_at_to&.end_of_day)) @relation = @relation.where(id: ids) if @parameters.key?('ids') @relation = @relation.do_search(search) if search.present? @relation diff --git a/src/api/app/views/webui/shared/_input.haml b/src/api/app/views/webui/shared/_input.haml index 995b9bc931d..a3b7bcfa50d 100644 --- a/src/api/app/views/webui/shared/_input.haml +++ b/src/api/app/views/webui/shared/_input.haml @@ -4,4 +4,5 @@ .col-9.d-flex = text_field_tag(filter_item, '', class: 'form-control form-control-sm', - placeholder: placeholder, value: selected_input_value) + placeholder: placeholder, value: selected_input_value, + type: defined?(type) ? type : 'text') diff --git a/src/api/app/views/webui/shared/bs_requests/_requests_filter.html.haml b/src/api/app/views/webui/shared/bs_requests/_requests_filter.html.haml index 010ec7fd36a..34b8dfc5fd7 100644 --- a/src/api/app/views/webui/shared/bs_requests/_requests_filter.html.haml +++ b/src/api/app/views/webui/shared/bs_requests/_requests_filter.html.haml @@ -1,3 +1,4 @@ +-# haml-lint:disable ViewLength .accordion.accordion-flush .mt-2.mb-2.accordion-item.border-0 .px-3.py-2.accordion-button.no-style{ data: { 'bs-toggle': 'collapse', 'bs-target': '#request-filter-direction' }, @@ -73,7 +74,7 @@ checked: selected_filter[:action_type]&.include?('submit')} .mt-2.mb-2.accordion-item.border-0 - %h6.px-3.py-2.accordion-button.no-style{ data: { 'bs-toggle': 'collapse', 'bs-target': '#request-filter-creator' }, + .px-3.py-2.accordion-button.no-style{ data: { 'bs-toggle': 'collapse', 'bs-target': '#request-filter-creator' }, aria: { expanded: 'true', controls: 'request-filter-creator' } } %b Creator .selected-content.small.ms-1 @@ -96,5 +97,19 @@ key: "creators[#{creator}]", name: 'creators[]', value: creator, checked: selected_filter[:creators]&.include?(creator) } + + .mt-2.mb-2.accordion-item.border-0 + .px-3.py-2.accordion-button.no-style{ data: { 'bs-toggle': 'collapse', 'bs-target': '#request-filter-created-at' }, + aria: { expanded: 'true', controls: 'request-filter-created-at' } } + %b Created at + .selected-content.small.ms-1 + .px-4.pb-2.accordion-collapse.collapse.show#request-filter-created-at + = render partial: 'webui/shared/input', locals: { label: 'From', filter_item: 'created_at_from', type: 'date', + placeholder: 'mm/dd/yyyy', selected_input_value: selected_filter[:created_at_from] } + = render partial: 'webui/shared/input', locals: { label: 'To', filter_item: 'created_at_to', type: 'date', + placeholder: 'mm/dd/yyyy', selected_input_value: selected_filter[:created_at_to] } + .text-center.mt-4.mb-4 = link_to('Clear', url, class: 'btn btn-light border') + +-# haml-lint:enable ViewLength diff --git a/src/api/spec/features/beta/webui/requests_index_spec.rb b/src/api/spec/features/beta/webui/requests_index_spec.rb index e069f32294c..2a2440e2138 100644 --- a/src/api/spec/features/beta/webui/requests_index_spec.rb +++ b/src/api/spec/features/beta/webui/requests_index_spec.rb @@ -166,4 +166,35 @@ end # rubocop:enable RSpec/ExampleLength end + + describe 'filters request by created_at' do + let!(:request_in_review) do + create(:bs_request_with_submit_action, + description: 'This is in review state', + creator: receiver, + source_package: source_project.packages.first, + target_project: other_source_project, + state: :review, + review_by_user: receiver) + end + + # rubocop:disable RSpec/ExampleLength + it 'shows only requests within the selected date range' do + request_in_review.created_at = DateTime.now - 10.days # set request to be older + request_in_review.save # update and store the new value + find_by_id('requests-dropdown-trigger').click if mobile? # open the filter dropdown + within('#filters') do + # filter from yesterday to tomorrow + fill_in 'created_at_from', with: DateTime.now - 1.day + fill_in 'created_at_to', with: DateTime.now + 1.day + sleep 2 # there is a timeout before firing the filtering + end + + within('#requests') do + expect(page).to have_no_link(href: "/request/show/#{request_in_review.number}") + expect(page).to have_link(href: "/request/show/#{outgoing_request.number}") + end + end + # rubocop:enable RSpec/ExampleLength + end end