Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/forms/assessor_interface/filter_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class AssessorInterface::FilterForm
:show_all,
:stage,
:submitted_at_after,
:submitted_at_before
:submitted_at_before,
:fi_request_statuses
end
15 changes: 15 additions & 0 deletions app/lib/filters/statuses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class Filters::Statuses < Filters::Base
def apply
return scope if statuses.empty?

scope.where("statuses && array[?]::varchar[]", statuses)
end

private

def statuses
Array(params[:fi_request_statuses]).compact_blank
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def stage_filter_options
STAGE_FILTER_OPTIONS.map { |name| stage_filter_entry(name) }
end

def fi_request_statuses_filter_options
FI_REQUESTS_FILTER_OPTIONS.map { |name| statuses_filter_entry(name) }
end

def flag_as_unsuitable?(application_form)
suitability_active? &&
suitability_matcher.flag_as_unsuitable?(application_form:)
Expand All @@ -67,6 +71,11 @@ def flag_as_unsuitable?(application_form)
completed
].freeze

FI_REQUESTS_FILTER_OPTIONS = %w[
waiting_on_further_information
received_further_information
].freeze

def filter_params
(session[:filter_params] || {}).with_indifferent_access
end
Expand All @@ -92,10 +101,14 @@ def application_forms_without_counted_filters
def application_forms_with_pagy
@application_forms_with_pagy ||=
pagy(
::Filters::Stage.apply(
::Filters::Statuses.apply(
scope:
::Filters::ActionRequiredBy.apply(
scope: application_forms_without_counted_filters,
::Filters::Stage.apply(
scope:
::Filters::ActionRequiredBy.apply(
scope: application_forms_without_counted_filters,
params: filter_params,
),
params: filter_params,
),
params: filter_params,
Expand All @@ -107,7 +120,11 @@ def action_required_by_filter_counts
@action_required_by_filter_counts ||=
::Filters::Stage
.apply(
scope: application_forms_without_counted_filters,
scope:
::Filters::Statuses.apply(
scope: application_forms_without_counted_filters,
params: filter_params,
),
params: filter_params,
)
.group(:action_required_by)
Expand All @@ -126,7 +143,11 @@ def stage_filter_counts
@stage_filter_counts ||=
::Filters::ActionRequiredBy
.apply(
scope: application_forms_without_counted_filters,
scope:
::Filters::Statuses.apply(
scope: application_forms_without_counted_filters,
params: filter_params,
),
params: filter_params,
)
.group(:stage)
Expand All @@ -147,6 +168,42 @@ def stage_filter_entry(name)
)
end

def statuses_filter_counts
@statuses_filter_counts ||=
::Filters::Statuses
.apply(
scope:
::Filters::Stage.apply(
scope:
::Filters::ActionRequiredBy.apply(
scope: application_forms_without_counted_filters,
params: filter_params,
),
params: filter_params,
),
params: {
},
)
.map(&:statuses)
.flatten
.compact
.tally
end

def statuses_filter_entry(name)
readable_name =
I18n.t(
name,
scope: %i[components status_tag],
default: name.to_s.humanize,
)

OpenStruct.new(
id: name,
label: "#{readable_name} (#{statuses_filter_counts.fetch(name, 0)})",
)
end

attr_reader :params, :session

def suitability_active?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
<%= f.govuk_check_box :show_all, :true, multiple: false, label: { text: "Show applications completed over 90 days ago" }, checked: @view_object.filter_form.show_all == "true" %>
<% end %>
</section>


<section id="app-applications-filters-fi-requests">
<%= f.govuk_check_boxes_fieldset :fi_request_statuses, legend: { size: "s" }, small: true do %>
<% @view_object.fi_request_statuses_filter_options.each do |option| %>
<%= f.govuk_check_box :fi_request_statuses, option.id, label: { text: option.label }, checked: @view_object.filter_form.fi_request_statuses&.include?(option.id) %>
<% end %>
<% end %>
</section>
<% end %>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/locales/helpers.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ en:
submitted_at: Created date
submitted_at_after: Start date
submitted_at_before: End date
fi_request_statuses: FI requests
assessor_interface_select_work_histories_form:
work_history_ids: Schools
assessor_interface_suitability_record_form:
Expand Down
99 changes: 99 additions & 0 deletions spec/lib/filters/statuses_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Filters::Statuses do
subject(:filtered_scope) { described_class.apply(scope:, params:) }

context "the params includes fi_request_statuses" do
let(:params) { { fi_request_statuses: "waiting_on_further_information" } }
let(:scope) { ApplicationForm.all }

let!(:included) do
create(
:application_form,
:submitted,
statuses: ["waiting_on_further_information"],
)
end

before do
create(
:application_form,
:submitted,
statuses: ["received_further_information"],
)
end

it { is_expected.to contain_exactly(included) }

context "with application form having multiple statuses" do
let!(:included) do
create(
:application_form,
:submitted,
statuses: %w[preliminary_check waiting_on_further_information],
)
end

it { is_expected.to contain_exactly(included) }
end
end

context "the params include multiple fi_request_statuses" do
let(:params) do
{
fi_request_statuses: %w[
waiting_on_further_information
received_further_information
],
}
end
let(:scope) { ApplicationForm.all }

let!(:included) do
[
create(
:application_form,
:submitted,
statuses: ["waiting_on_further_information"],
),
create(
:application_form,
:submitted,
statuses: ["received_further_information"],
),
]
end

before { create(:application_form, :submitted) }

it { is_expected.to match_array(included) }

context "with application form having multiple statuses" do
let!(:included) do
[
create(
:application_form,
:submitted,
statuses: %w[preliminary_check waiting_on_further_information],
),
create(
:application_form,
:submitted,
statuses: ["received_further_information"],
),
]
end

it { is_expected.to match_array(included) }
end
end

context "the params don't include fi_request_statuses" do
let(:params) { {} }
let(:scope) { double }

it { is_expected.to eq(scope) }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@
it { is_expected.to be_empty }
end

context "with a fi_request_statuses filter" do
before do
allow_any_instance_of(Filters::Statuses).to receive(
:apply,
).and_return(ApplicationForm.none)
end

it { is_expected.to be_empty }
end

context "with show all filter" do
before do
allow_any_instance_of(Filters::Stage).to receive(:apply).and_return(
Expand Down Expand Up @@ -237,4 +247,59 @@
end
end
end

describe "#fi_request_statuses_filter_options" do
subject(:fi_request_statuses_filter_options) do
view_object.fi_request_statuses_filter_options
end

it do
expect(subject).to eq(
[
OpenStruct.new(
id: "waiting_on_further_information",
label: "Waiting on further information (0)",
),
OpenStruct.new(
id: "received_further_information",
label: "Received further information (0)",
),
],
)
end

context "with application forms" do
before do
create_list(
:application_form,
2,
:submitted,
:assessment_stage,
statuses: [:waiting_on_further_information],
)
create_list(
:application_form,
3,
:submitted,
:assessment_stage,
statuses: [:received_further_information],
)
end

it do
expect(subject).to eq(
[
OpenStruct.new(
id: "waiting_on_further_information",
label: "Waiting on further information (2)",
),
OpenStruct.new(
id: "received_further_information",
label: "Received further information (3)",
),
],
)
end
end
end
end
Loading