Skip to content

Commit

Permalink
Filter requests by created_at range
Browse files Browse the repository at this point in the history
  • Loading branch information
ncounter committed Jan 27, 2025
1 parent c83b58f commit c2bb929
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/api/app/controllers/concerns/webui/requests_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Webui::RequestsFilter
ALLOWED_DIRECTIONS = %w[all incoming outgoing].freeze
TEXT_SEARCH_MAX_RESULTS = 10_000

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def filter_requests
set_filters

Expand All @@ -20,9 +22,14 @@ def filter_requests
params[:types] = @filter_action_type if @filter_action_type.present?
params[:staging_project] = @filter_staging_project if @filter_staging_project.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:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
Expand All @@ -42,13 +49,17 @@ def set_filters
@filter_creators = params[:creators].present? ? params[:creators].compact_blank! : []

@filter_staging_project = params[:staging_project].presence || []

@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
# rubocop:enable Metrics/PerceivedComplexity

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, staging_project: @filter_staging_project, priority: @filter_priority }
state: @filter_state, creators: @filter_creators, staging_project: @filter_staging_project, priority: @filter_priority,
created_at_from: @filter_created_at_from, created_at_to: @filter_created_at_to }
end

def filter_by_text(text)
Expand Down
4 changes: 4 additions & 0 deletions src/api/app/models/bs_request/find_for/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ 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 = DateTime.parse(@parameters['created_at_from']) if @parameters['created_at_from'].present?
# [see below] `created_at_to + 1.minute` is a workaround to include the upper limit of the date time range in the filter result set
created_at_to = DateTime.parse(@parameters['created_at_to']) + 1.minute if @parameters['created_at_to'].present?
@relation = @relation.where(created_at: (created_at_from..created_at_to))
@relation = @relation.where(id: ids) if @parameters.key?('ids')
@relation = @relation.do_search(search) if search.present?
@relation
Expand Down
3 changes: 2 additions & 1 deletion src/api/app/views/webui/shared/_input.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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: defined?(placeholder) ? placeholder : '', value: selected_input_value,
type: defined?(type) ? type : 'text')
Original file line number Diff line number Diff line change
@@ -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' },
Expand Down Expand Up @@ -85,7 +86,7 @@
checked: selected_filter[:priority]&.include?(priority.to_s) }

.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
Expand All @@ -94,7 +95,6 @@
= render partial: 'webui/shared/autocomplete', locals: { html_id: 'creators_search', label: 'Creators:',
html_name: 'creators[]', required: false, with_label: false,
data: { source: autocomplete_users_path } }

.scroll-list-wrapper
- if selected_filter[:creators].include?(User.session.login)
.dropdown-item-text
Expand Down Expand Up @@ -126,7 +126,21 @@
key: "staging_project[#{sp.id}]", name: 'staging_project[]',
value: sp.name,
checked: checked }

.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 datetime
.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: 'datetime-local',
selected_input_value: selected_filter[:created_at_from] }
= render partial: 'webui/shared/input', locals: { label: 'To', filter_item: 'created_at_to', type: 'datetime-local',
selected_input_value: selected_filter[:created_at_to] }

.text-center.mt-4.mb-4
= link_to('Clear', url, class: 'btn btn-light border')

- content_for(:content_for_head, javascript_include_tag('webui/content-selector-filters'))

-# haml-lint:enable ViewLength
31 changes: 31 additions & 0 deletions src/api/spec/features/beta/webui/requests_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('#content-selector-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

0 comments on commit c2bb929

Please sign in to comment.