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) } [OpenStruct.new(id: "", name: "Select an option")] + organisation_options.map { |org| OpenStruct.new(id: org.id, name: org.name) }
end 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 private
def applied_filters_count(filter_type) 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_completion, -> { order("schemes.confirmed ASC NULLS FIRST") }
scope :order_by_service_name, -> { order(service_name: :asc) } 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| scope :filter_by_status, lambda { |statuses, _user = nil|
filtered_records = all filtered_records = all
scopes = [] scopes = []

20
app/services/filter_manager.rb

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

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

@ -55,7 +55,7 @@
type: "select", type: "select",
label: "User", label: "User",
category: "user", category: "user",
options: assigned_to_filter_options(@current_user), options: assigned_to_filter_options(current_user),
}, },
}, },
}, },
@ -63,7 +63,7 @@
category: "assigned_to", 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: { <%= render partial: "filters/radio_filter", locals: {
f:, f:,
options: { options: {
@ -74,7 +74,7 @@
type: "select", type: "select",
label: "Owning Organisation", label: "Owning Organisation",
category: "owning_organisation", category: "owning_organisation",
options: owning_organisation_filter_options(@current_user), options: owning_organisation_filter_options(current_user),
}, },
}, },
}, },
@ -83,7 +83,7 @@
} %> } %>
<% end %> <% 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: { <%= render partial: "filters/radio_filter", locals: {
f:, f:,
options: { options: {
@ -94,7 +94,7 @@
type: "select", type: "select",
label: "Managed by", label: "Managed by",
category: "managing_organisation", 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", label: "Status",
category: "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" %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %>
<% end %> <% end %>
</div> </div>

70
spec/helpers/filters_helper_spec.rb

@ -290,4 +290,74 @@ RSpec.describe FiltersHelper do
end end
end 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 end

19
spec/models/scheme_spec.rb

@ -90,6 +90,25 @@ RSpec.describe Scheme, type: :model do
end end
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 context "when filtering by status" do
let!(:incomplete_scheme) { FactoryBot.create(:scheme, :incomplete) } let!(:incomplete_scheme) { FactoryBot.create(:scheme, :incomplete) }
let(:active_scheme) { FactoryBot.create(:scheme) } let(:active_scheme) { FactoryBot.create(:scheme) }

88
spec/requests/schemes_controller_spec.rb

@ -57,13 +57,13 @@ RSpec.describe SchemesController, type: :request do
end end
context "when parent organisation has schemes" do context "when parent organisation has schemes" do
let(:parent_organisation) { FactoryBot.create(:organisation) } let(:parent_organisation) { create(:organisation) }
let!(:parent_schemes) { FactoryBot.create_list(:scheme, 5, owning_organisation: parent_organisation) } let!(:parent_schemes) { create_list(:scheme, 5, owning_organisation: parent_organisation) }
before do before do
create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation) create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation)
parent_schemes.each do |scheme| parent_schemes.each do |scheme|
FactoryBot.create(:location, scheme:) create(:location, scheme:)
end end
get "/schemes" get "/schemes"
end end
@ -77,6 +77,48 @@ RSpec.describe SchemesController, type: :request do
end end
context "when filtering" do 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 context "with status filter" do
let!(:incomplete_scheme) { create(:scheme, :incomplete, owning_organisation: user.organisation) } let!(:incomplete_scheme) { create(:scheme, :incomplete, owning_organisation: user.organisation) }
let(:active_scheme) { create(:scheme, owning_organisation: user.organisation) } let(:active_scheme) { create(:scheme, owning_organisation: user.organisation) }
@ -282,6 +324,40 @@ RSpec.describe SchemesController, type: :request do
end end
context "when filtering" do 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 context "with status filter" do
let!(:incomplete_scheme) { create(:scheme, :incomplete) } let!(:incomplete_scheme) { create(:scheme, :incomplete) }
let(:active_scheme) { create(:scheme) } let(:active_scheme) { create(:scheme) }
@ -457,11 +533,11 @@ RSpec.describe SchemesController, type: :request do
end end
context "when coordinator attempts to see scheme belonging to a parent organisation" do context "when coordinator attempts to see scheme belonging to a parent organisation" do
let(:parent_organisation) { FactoryBot.create(:organisation) } let(:parent_organisation) { create(:organisation) }
let!(:specific_scheme) { FactoryBot.create(:scheme, owning_organisation: parent_organisation) } let!(:specific_scheme) { create(:scheme, owning_organisation: parent_organisation) }
before do before do
FactoryBot.create(:location, scheme: specific_scheme) create(:location, scheme: specific_scheme)
create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation) create(:organisation_relationship, parent_organisation:, child_organisation: user.organisation)
get "/schemes/#{specific_scheme.id}" get "/schemes/#{specific_scheme.id}"
end end

Loading…
Cancel
Save