Skip to content

Commit

Permalink
Merge pull request #2078 from nickdip/all-applicants-filter
Browse files Browse the repository at this point in the history
All applicants filter
  • Loading branch information
thomasleese authored Mar 21, 2024
2 parents 6732136 + 9bb9512 commit 6498fa4
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 70 deletions.
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 @@ -12,5 +12,6 @@ class AssessorInterface::FilterForm
:reference,
:stage,
:submitted_at_after,
:submitted_at_before
:submitted_at_before,
:show_all
end
25 changes: 25 additions & 0 deletions app/lib/filters/show_all_applications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Filters
class ShowAllApplications < Base
def apply
unless show_all?
ninety_days_ago = 90.days.ago
return(
scope.where(
"(awarded_at >= :ninety_days OR awarded_at IS NULL)
AND (declined_at >= :ninety_days OR declined_at IS NULL)
AND (withdrawn_at >= :ninety_days OR withdrawn_at IS NULL)",
ninety_days: ninety_days_ago,
)
)
end

scope
end

private

def show_all?
ActiveModel::Type::Boolean.new.cast(params[:show_all])
end
end
end
8 changes: 1 addition & 7 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,7 @@ class ApplicationForm < ApplicationRecord

scope :assessable, -> { where.not(stage: %i[draft completed]) }

scope :active,
-> do
assessable
.or(where("awarded_at >= ?", 90.days.ago))
.or(where("declined_at >= ?", 90.days.ago))
.or(where("withdrawn_at >= ?", 90.days.ago))
end
scope :submitted, -> { where.not(stage: "draft") }

scope :destroyable,
-> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def filter_form

def assessor_filter_options
ApplicationForm
.active
.joins(:assessor)
.pluck(Arel.sql("DISTINCT ON(assessor_id) assessor_id"), "staff.name")
.sort_by { |_id, name| name }
Expand Down Expand Up @@ -75,9 +74,10 @@ def application_forms_without_counted_filters
::Filters::Email,
::Filters::Reference,
::Filters::SubmittedAt,
::Filters::ShowAllApplications,
]
filters.reduce(
ApplicationForm.includes(region: :country).active,
ApplicationForm.includes(region: :country).submitted,
) { |scope, filter| filter.apply(scope:, params: filter_params) }
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/views/assessor_interface/application_forms/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
<% end %>
<% end %>
</section>

<% if FeatureFlags::FeatureFlag.active?(:show_all_applicants_filter) %>
<section id="app-applications-show-all-applicants">
<%= f.govuk_check_boxes_fieldset :show_all, legend: nil, multiple: false, small: true do %>
<%= f.govuk_check_box :show_all, :true, multiple: false, label: { text: "Show applications completed older than 90 days ago" }, checked: @view_object.filter_form.show_all == "true" %>
<% end %>
</section>
<% end %>
<% end %>
</div>

Expand Down
5 changes: 5 additions & 0 deletions config/feature_flags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ feature_flags:
Allow starting an application on this service directly after completing
an eligibility check.
show_all_applicants_filter:
author: Nick Diplos
description: >
Adds a filter that allows assessors to view applications that are older than 90 days old.
parent_controller: SupportInterface::FeatureFlagsController
70 changes: 70 additions & 0 deletions spec/lib/filters/show_all_applications_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require "rails_helper"

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

let!(:application_awarded_recently) do
create(:application_form, awarded_at: 2.days.ago)
end
let!(:application_declined_recently) do
create(:application_form, declined_at: 30.days.ago)
end
let!(:application_withdrawn_recently) do
create(:application_form, withdrawn_at: 89.days.ago)
end

let!(:application_awarded_old) do
create(:application_form, awarded_at: 100.days.ago)
end
let!(:application_declined_old) do
create(:application_form, declined_at: 100.days.ago)
end
let!(:application_withdrawn_old) do
create(:application_form, withdrawn_at: 100.days.ago)
end

context "the params does not include display" do
let(:params) { {} }
let(:scope) { ApplicationForm.all }

it "includes recent applications and excludes old ones" do
expect(filtered_scope).to include(
application_awarded_recently,
application_declined_recently,
application_withdrawn_recently,
)

expect(filtered_scope).not_to include(
application_awarded_old,
application_declined_old,
application_withdrawn_old,
)
end
end

context "the params does not include 'show_all' under display" do
let(:params) { { show_all: nil } }
let(:scope) { ApplicationForm.all }

it "includes recent applications and excludes old ones" do
expect(filtered_scope).to include(
application_awarded_recently,
application_declined_recently,
application_withdrawn_recently,
)

expect(filtered_scope).not_to include(
application_awarded_old,
application_declined_old,
application_withdrawn_old,
)
end
end

context "the params does include 'show_all' but not true under display" do
let(:params) { { show_all: "true" } }
let(:scope) { double }

it { is_expected.to eq(scope) }
end
end
52 changes: 0 additions & 52 deletions spec/models/application_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,58 +276,6 @@
end

describe "scopes" do
describe "#active" do
subject { described_class.active }

context "draft" do
let!(:application_form) { create(:application_form, :draft) }

it { is_expected.to be_empty }
end

context "submitted" do
let!(:application_form) { create(:application_form, :submitted) }

it { is_expected.to eq([application_form]) }
end

context "awarded" do
let!(:application_form) { create(:application_form, :awarded) }

it { is_expected.to eq([application_form]) }

context "older than 90 days" do
let!(:application_form) do
create(:application_form, :awarded, awarded_at: 90.days.ago)
end

it { is_expected.to be_empty }
end
end

context "declined" do
let!(:application_form) { create(:application_form, :declined) }

it { is_expected.to eq([application_form]) }

context "older than 90 days" do
let!(:application_form) do
create(:application_form, :declined, declined_at: 90.days.ago)
end

it { is_expected.to be_empty }
end
end

context "preliminary_check" do
let!(:application_form) do
create(:application_form, :preliminary_check)
end

it { is_expected.to eq([application_form]) }
end
end

describe "#destroyable" do
subject(:destroyable) { described_class.destroyable }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Applications < SitePrism::Page
sections :items, GovukCheckboxItem, ".govuk-checkboxes__item"
end

section :show_all_filter, "#app-applications-show-all-applicants" do
sections :items, GovukCheckboxItem, ".govuk-checkboxes__item"
end

sections :search_results, ".app-search-results__item" do
element :name, "h2"
end
Expand Down
60 changes: 53 additions & 7 deletions spec/system/assessor_interface/filtering_application_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

RSpec.describe "Assessor filtering application forms", type: :system do
before do
FeatureFlags::FeatureFlag.activate(:show_all_applicants_filter)
given_the_service_is_open
given_there_are_application_forms
given_i_am_authorized_as_an_assessor_user
Expand All @@ -12,6 +13,9 @@
it "applies the filters" do
when_i_visit_the(:assessor_applications_page)

when_i_clear_the_filters
then_i_see_applications_in_the_last_90_days

when_i_clear_the_filters
and_i_apply_the_assessor_filter
then_i_see_a_list_of_applications_filtered_by_assessor
Expand Down Expand Up @@ -43,6 +47,10 @@
when_i_clear_the_filters
and_i_apply_the_stage_filter
then_i_see_a_list_of_applications_filtered_by_stage

when_i_clear_the_filters
and_i_apply_the_show_all_filter
then_i_see_a_list_of_all_applications
end

private
Expand All @@ -59,6 +67,10 @@ def when_i_clear_the_filters
assessor_applications_page.clear_filters.click
end

def then_i_see_applications_in_the_last_90_days
expect(assessor_applications_page.search_results.count).to eq(4)
end

def and_i_apply_the_assessor_filter
expect(assessor_applications_page.assessor_filter.assessors.count).to eq(3)
assessor_applications_page.assessor_filter.assessors.first.checkbox.click
Expand Down Expand Up @@ -121,9 +133,16 @@ def then_i_see_a_list_of_applications_filtered_by_email
end

def and_i_apply_the_submitted_at_filter
assessor_applications_page.submitted_at_filter.start_day.set(1)
assessor_applications_page.submitted_at_filter.start_month.set(1)
assessor_applications_page.submitted_at_filter.start_year.set(2020)
ten_days_ago = 10.days.ago
assessor_applications_page.submitted_at_filter.start_day.set(
ten_days_ago.day,
)
assessor_applications_page.submitted_at_filter.start_month.set(
ten_days_ago.month,
)
assessor_applications_page.submitted_at_filter.start_year.set(
ten_days_ago.year,
)
assessor_applications_page.apply_filters.click
end

Expand Down Expand Up @@ -170,6 +189,21 @@ def then_i_see_a_list_of_applications_filtered_by_stage
)
end

def and_i_apply_the_show_all_filter
show_all =
assessor_applications_page.show_all_filter.items.find do |item|
item.label.text == "Show applications completed older than 90 days ago"
rescue Capybara::ElementNotFound
false
end
show_all.checkbox.click
assessor_applications_page.apply_filters.click
end

def then_i_see_a_list_of_all_applications
expect(assessor_applications_page.search_results.count).to eq(5)
end

def application_forms
@application_forms ||= [
create(
Expand All @@ -179,7 +213,7 @@ def application_forms
given_names: "Cher",
family_name: "Bert",
assessor: assessors.second,
submitted_at: Date.new(2019, 12, 1),
submitted_at: 2.months.ago,
reference: "CHERBERT",
),
create(
Expand All @@ -190,7 +224,7 @@ def application_forms
given_names: "Emma",
family_name: "Dubois",
assessor: assessors.second,
submitted_at: Date.new(2019, 12, 1),
submitted_at: 2.months.ago,
teacher: create(:teacher, email: "[email protected]"),
),
create(
Expand All @@ -201,18 +235,30 @@ def application_forms
given_names: "Arnold",
family_name: "Drummond",
assessor: assessors.first,
submitted_at: Date.new(2019, 12, 1),
submitted_at: 2.months.ago,
),
create(
:application_form,
:awarded,
awarded_at: 2.days.ago,
region: create(:region, country: create(:country, code: "PT")),
given_names: "John",
family_name: "Smith",
assessor: assessors.second,
submitted_at: Date.new(2020, 1, 1),
submitted_at: 10.days.ago,
),
create(
:application_form,
:awarded,
awarded_at: 6.months.ago,
region: create(:region, country: create(:country, code: "DE")),
given_names: "Nick",
family_name: "Johnson",
assessor: assessors.second,
submitted_at: 7.months.ago,
),
]
@application_forms
end

def assessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@
end

context "with a stage filter" do
before do
expect_any_instance_of(Filters::ShowAllApplications).to receive(
:apply,
).and_return(ApplicationForm.none)
end

it { is_expected.to be_empty }
end

context "with show all filter" do
before do
expect_any_instance_of(Filters::Stage).to receive(:apply).and_return(
ApplicationForm.none,
Expand Down Expand Up @@ -150,7 +160,6 @@

describe "#country_filter_options" do
subject(:country_filter_options) { view_object.country_filter_options }

it do
is_expected.to include(
'<option value="country:US">United States</option>',
Expand Down

0 comments on commit 6498fa4

Please sign in to comment.