Browse Source

Add owning org filter to schemes (#1809)

* Add owning org filter to schemes

* Use current_user helper method

* Add filtering specs

* Check if org holds own stock
pull/1817/head
Jack 1 year ago committed by GitHub
parent
commit
4bc1625247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/helpers/filters_helper.rb
  2. 1
      app/models/scheme.rb
  3. 20
      app/services/filter_manager.rb
  4. 10
      app/views/logs/_log_filters.html.erb
  5. 21
      app/views/schemes/_scheme_filters.html.erb
  6. 70
      spec/helpers/filters_helper_spec.rb
  7. 19
      spec/models/scheme_spec.rb
  8. 88
      spec/requests/schemes_controller_spec.rb

6
app/helpers/filters_helper.rb

@ -91,6 +91,12 @@ module FiltersHelper
[OpenStruct.new(id: "", name: "Select an option")] + organisation_options.map { |org| OpenStruct.new(id: org.id, name: org.name) }
end
def show_scheme_managing_org_filter?(user)
org = user.organisation
user.support? || org.stock_owners.count > 1 || (org.holds_own_stock? && org.stock_owners.count.positive?)
end
private
def applied_filters_count(filter_type)

1
app/models/scheme.rb

@ -19,6 +19,7 @@ class Scheme < ApplicationRecord
scope :order_by_completion, -> { order("schemes.confirmed ASC NULLS FIRST") }
scope :order_by_service_name, -> { order(service_name: :asc) }
scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) }
scope :filter_by_status, lambda { |statuses, _user = nil|
filtered_records = all
scopes = []

20
app/services/filter_manager.rb

@ -50,11 +50,12 @@ class FilterManager
users
end
def self.filter_schemes(schemes, search_term, filters, user)
def self.filter_schemes(schemes, search_term, filters, user, all_orgs)
schemes = filter_by_search(schemes, search_term)
filters.each do |category, values|
next if Array(values).reject(&:empty?).blank?
next if category == "owning_organisation" && all_orgs
schemes = schemes.public_send("filter_by_#{category}", values, user)
end
@ -72,7 +73,8 @@ class FilterManager
def deserialize_filters_from_session(specific_org)
current_filters = session[session_name_for(filter_type)]
new_filters = current_filters.present? ? JSON.parse(current_filters) : {}
if @filter_type.include?("logs")
if filter_type.include?("logs")
current_user.logs_filters(specific_org:).each do |filter|
new_filters[filter] = params[filter] if params[filter].present?
end
@ -84,10 +86,18 @@ class FilterManager
new_filters["user"] = current_user.id.to_s if params["assigned_to"] == "you"
end
if (@filter_type.include?("schemes") || @filter_type.include?("users")) && params["status"].present?
if (filter_type.include?("schemes") || filter_type.include?("users")) && params["status"].present?
new_filters["status"] = params["status"]
end
if filter_type.include?("schemes")
current_user.logs_filters(specific_org:).each do |filter|
new_filters[filter] = params[filter] if params[filter].present?
end
new_filters = new_filters.except("owning_organisation") if params["owning_organisation_select"] == "all"
end
new_filters
end
@ -102,7 +112,9 @@ class FilterManager
end
def filtered_schemes(schemes, search_term, filters)
FilterManager.filter_schemes(schemes, search_term, filters, current_user)
all_orgs = params["owning_organisation_select"] == "all"
FilterManager.filter_schemes(schemes, search_term, filters, current_user, all_orgs)
end
def bulk_upload

10
app/views/logs/_log_filters.html.erb

@ -55,7 +55,7 @@
type: "select",
label: "User",
category: "user",
options: assigned_to_filter_options(@current_user),
options: assigned_to_filter_options(current_user),
},
},
},
@ -63,7 +63,7 @@
category: "assigned_to",
} %>
<% if @current_user.support? || @current_user.organisation.stock_owners.count > 1 && request.path == "/lettings-logs" %>
<% if current_user.support? || current_user.organisation.stock_owners.count > 1 && request.path == "/lettings-logs" %>
<%= render partial: "filters/radio_filter", locals: {
f:,
options: {
@ -74,7 +74,7 @@
type: "select",
label: "Owning Organisation",
category: "owning_organisation",
options: owning_organisation_filter_options(@current_user),
options: owning_organisation_filter_options(current_user),
},
},
},
@ -83,7 +83,7 @@
} %>
<% end %>
<% if (@current_user.support? || @current_user.organisation.managing_agents.count > 1) && request.path == "/lettings-logs" %>
<% if (current_user.support? || current_user.organisation.managing_agents.count > 1) && request.path == "/lettings-logs" %>
<%= render partial: "filters/radio_filter", locals: {
f:,
options: {
@ -94,7 +94,7 @@
type: "select",
label: "Managed by",
category: "managing_organisation",
options: managing_organisation_filter_options(@current_user),
options: managing_organisation_filter_options(current_user),
},
},
},

21
app/views/schemes/_scheme_filters.html.erb

@ -22,6 +22,27 @@
label: "Status",
category: "status",
} %>
<% if show_scheme_managing_org_filter?(current_user) %>
<%= render partial: "filters/radio_filter", locals: {
f:,
options: {
"all": { label: "Any owning organisation" },
"specific_org": {
label: "Specific owning organisation",
conditional_filter: {
type: "select",
label: "Owning Organisation",
category: "owning_organisation",
options: owning_organisation_filter_options(current_user),
},
},
},
label: "Owned by",
category: "owning_organisation_select",
} %>
<% end %>
<%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %>
<% end %>
</div>

70
spec/helpers/filters_helper_spec.rb

@ -290,4 +290,74 @@ RSpec.describe FiltersHelper do
end
end
end
describe "#show_scheme_managing_org_filter?" do
context "when support user" do
let(:user) { create(:user, :support, organisation: create(:organisation, stock_owners: [])) }
it "returns true" do
expect(show_scheme_managing_org_filter?(user)).to be true
end
end
context "when not support user" do
let(:stock_owner1) { create(:organisation) }
let(:stock_owner2) { create(:organisation) }
context "when org's stock_owners > 1" do
let(:user) { create(:user, organisation: create(:organisation, holds_own_stock: false)) }
before do
create(
:organisation_relationship,
child_organisation: user.organisation,
parent_organisation: stock_owner1,
)
create(
:organisation_relationship,
child_organisation: user.organisation,
parent_organisation: stock_owner2,
)
end
it "returns true" do
expect(show_scheme_managing_org_filter?(user)).to be true
end
end
context "when org's stock_owners == 1" do
before do
create(
:organisation_relationship,
child_organisation: user.organisation,
parent_organisation: stock_owner1,
)
end
context "when holds own stock" do
let(:user) { create(:user, organisation: create(:organisation, holds_own_stock: true)) }
it "returns true" do
expect(show_scheme_managing_org_filter?(user)).to be true
end
end
context "when does not hold own stock" do
let(:user) { create(:user, organisation: create(:organisation, holds_own_stock: false)) }
it "returns false" do
expect(show_scheme_managing_org_filter?(user)).to be false
end
end
end
context "when org's stock_owners == 0" do
let(:user) { create(:user) }
it "returns false" do
expect(show_scheme_managing_org_filter?(user)).to be false
end
end
end
end
end

19
spec/models/scheme_spec.rb

@ -90,6 +90,25 @@ RSpec.describe Scheme, type: :model do
end
end
context "when filtering by owning organisation" do
let(:organisation_1) { create(:organisation) }
let(:organisation_2) { create(:organisation) }
let(:organisation_3) { create(:organisation) }
before do
create(:scheme, owning_organisation: organisation_1)
create(:scheme, owning_organisation: organisation_1)
create(:scheme, owning_organisation: organisation_2)
create(:scheme, owning_organisation: organisation_2)
end
it "filters by given owning organisation" do
expect(described_class.filter_by_owning_organisation([organisation_1]).count).to eq(2)
expect(described_class.filter_by_owning_organisation([organisation_1, organisation_2]).count).to eq(4)
expect(described_class.filter_by_owning_organisation([organisation_3]).count).to eq(0)
end
end
context "when filtering by status" do
let!(:incomplete_scheme) { FactoryBot.create(:scheme, :incomplete) }
let(:active_scheme) { FactoryBot.create(:scheme) }

88
spec/requests/schemes_controller_spec.rb

@ -57,13 +57,13 @@ RSpec.describe SchemesController, type: :request do
end
context "when parent organisation has schemes" do
let(:parent_organisation) { FactoryBot.create(:organisation) }
let!(:parent_schemes) { FactoryBot.create_list(:scheme, 5, owning_organisation: parent_organisation) }
let(:parent_organisation) { create(:organisation) }
let!(:parent_schemes) { create_list(:scheme, 5, owning_organisation: parent_organisation) }
before do
create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation)
parent_schemes.each do |scheme|
FactoryBot.create(:location, scheme:)
create(:location, scheme:)
end
get "/schemes"
end
@ -77,6 +77,48 @@ RSpec.describe SchemesController, type: :request do
end
context "when filtering" do
context "with owning organisation filter" do
context "when user org does not have owning orgs" do
it "does not show filter" do
expect(page).not_to have_content("Owned by")
end
end
context "when user org has owning orgs" do
let!(:organisation1) { create(:organisation) }
let!(:scheme1) { create(:scheme, owning_organisation: organisation1) }
let!(:scheme2) { create(:scheme, owning_organisation: user.organisation) }
before do
org = user.organisation
org.stock_owners = [organisation1, user.organisation]
org.save!
end
context "when filtering by all owning orgs" do
it "shows schemes for all owning orgs" do
get "/schemes?owning_organisation_select=all", headers:, params: {}
follow_redirect!
expect(page).to have_content("Owned by")
expect(page).to have_link(scheme1.service_name)
expect(page).to have_link(scheme2.service_name)
end
end
context "when filtering by an owning org" do
it "when filtering by an owning org" do
get "/schemes?owning_organisation=#{organisation1.id}", headers:, params: {}
follow_redirect!
expect(page).to have_content("Owned by")
expect(page).to have_link(scheme1.service_name)
expect(page).not_to have_link(scheme2.service_name)
end
end
end
end
context "with status filter" do
let!(:incomplete_scheme) { create(:scheme, :incomplete, owning_organisation: user.organisation) }
let(:active_scheme) { create(:scheme, owning_organisation: user.organisation) }
@ -282,6 +324,40 @@ RSpec.describe SchemesController, type: :request do
end
context "when filtering" do
context "with owning organisation filter" do
context "when user org does not have owning orgs" do
it "shows the filter" do
expect(page).to have_content("Owned by")
end
end
context "when user org has owning orgs" do
let!(:organisation1) { create(:organisation) }
let!(:scheme1) { create(:scheme, owning_organisation: organisation1) }
let!(:scheme2) { create(:scheme, owning_organisation: user.organisation) }
context "when filtering by all owning orgs" do
it "shows schemes for all owning orgs" do
get "/schemes?owning_organisation_select=all", headers:, params: {}
expect(page).to have_content("Owned by")
expect(page).to have_link(scheme1.service_name)
expect(page).to have_link(scheme2.service_name)
end
end
context "when filtering by an owning org" do
it "when filtering by an owning org" do
get "/schemes?owning_organisation=#{organisation1.id}", headers:, params: {}
expect(page).to have_content("Owned by")
expect(page).to have_link(scheme1.service_name)
expect(page).not_to have_link(scheme2.service_name)
end
end
end
end
context "with status filter" do
let!(:incomplete_scheme) { create(:scheme, :incomplete) }
let(:active_scheme) { create(:scheme) }
@ -457,11 +533,11 @@ RSpec.describe SchemesController, type: :request do
end
context "when coordinator attempts to see scheme belonging to a parent organisation" do
let(:parent_organisation) { FactoryBot.create(:organisation) }
let!(:specific_scheme) { FactoryBot.create(:scheme, owning_organisation: parent_organisation) }
let(:parent_organisation) { create(:organisation) }
let!(:specific_scheme) { create(:scheme, owning_organisation: parent_organisation) }
before do
FactoryBot.create(:location, scheme: specific_scheme)
create(:location, scheme: specific_scheme)
create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation)
get "/schemes/#{specific_scheme.id}"
end

Loading…
Cancel
Save