Browse Source

Remove organisation filter

pull/1772/head
Kat 3 years ago
parent
commit
d251f66820
  1. 7
      app/helpers/filters_helper.rb
  2. 4
      app/models/user.rb
  3. 2
      app/services/filter_manager.rb
  4. 20
      app/views/logs/_log_filters.html.erb
  5. 18
      spec/features/user_spec.rb
  6. 36
      spec/helpers/filters_helper_spec.rb
  7. 10
      spec/models/user_spec.rb
  8. 19
      spec/requests/lettings_logs_controller_spec.rb
  9. 6
      spec/requests/sales_logs_controller_spec.rb

7
app/helpers/filters_helper.rb

@ -4,9 +4,7 @@ module FiltersHelper
selected_filters = JSON.parse(session[session_name_for(filter_type)]) selected_filters = JSON.parse(session[session_name_for(filter_type)])
return true if selected_filters.blank? && filter == "user" && value == :all return true if selected_filters.blank? && filter == "user" && value == :all
return true if !selected_filters.key?("organisation") && filter == "organisation_select" && value == :all
return true if !selected_filters.key?("owning_organisation") && filter == "owning_organisation_select" && value == :all return true if !selected_filters.key?("owning_organisation") && filter == "owning_organisation_select" && value == :all
return true if selected_filters["organisation"].present? && filter == "organisation_select" && value == :specific_org
return true if selected_filters["owning_organisation"].present? && filter == "owning_organisation_select" && value == :specific_org return true if selected_filters["owning_organisation"].present? && filter == "owning_organisation_select" && value == :specific_org
return false if selected_filters[filter].blank? return false if selected_filters[filter].blank?
@ -39,11 +37,6 @@ module FiltersHelper
JSON.parse(session[session_name_for(filter_type)])[filter] || "" JSON.parse(session[session_name_for(filter_type)])[filter] || ""
end end
def organisations_filter_options(user)
organisation_options = user.support? ? Organisation.all : [user.organisation] + user.organisation.managing_agents
[OpenStruct.new(id: "", name: "Select an option")] + organisation_options.map { |org| OpenStruct.new(id: org.id, name: org.name) }
end
def owning_organisations_filter_options(user) def owning_organisations_filter_options(user)
organisation_options = user.support? ? Organisation.all : [user.organisation] + user.organisation.stock_owners organisation_options = user.support? ? Organisation.all : [user.organisation] + user.organisation.stock_owners
[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) }

4
app/models/user.rb

@ -143,8 +143,8 @@ class User < ApplicationRecord
end end
def logs_filters(specific_org: false) def logs_filters(specific_org: false)
if (support? && !specific_org) || organisation.has_managing_agents? || organisation.has_stock_owners? if (support? && !specific_org) || organisation.has_stock_owners?
%w[status years user organisation owning_organisation bulk_upload_id] %w[status years user owning_organisation bulk_upload_id]
else else
%w[status years user bulk_upload_id] %w[status years user bulk_upload_id]
end end

2
app/services/filter_manager.rb

@ -21,7 +21,6 @@ class FilterManager
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 == "organisation" && all_orgs
next if category == "owning_organisation" && all_orgs next if category == "owning_organisation" && all_orgs
logs = logs.public_send("filter_by_#{category}", values, user) logs = logs.public_send("filter_by_#{category}", values, user)
@ -54,7 +53,6 @@ class FilterManager
new_filters[filter] = params[filter] if params[filter].present? new_filters[filter] = params[filter] if params[filter].present?
end end
end end
new_filters = new_filters.except("organisation") if params["organisation_select"] == "all"
new_filters = new_filters.except("owning_organisation") if params["owning_organisation_select"] == "all" new_filters = new_filters.except("owning_organisation") if params["owning_organisation_select"] == "all"
new_filters new_filters
end end

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

@ -64,26 +64,6 @@
} %> } %>
<% end %> <% end %>
<% if (@current_user.support? || @current_user.organisation.has_managing_agents?) && request.path == "/lettings-logs" %>
<%= render partial: "filters/radio_filter", locals: {
f:,
options: {
"all": { label: "All" },
"specific_org": {
label: "Specific organisation",
conditional_filter: {
type: "select",
label: "Organisation",
category: "organisation",
options: organisations_filter_options(@current_user),
},
},
},
label: "Organisation",
category: "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>

18
spec/features/user_spec.rb

@ -708,10 +708,12 @@ RSpec.describe "User Features" do
end end
context "when viewing logs" do context "when viewing logs" do
context "when filtering by organisation and then switching back to all organisations", js: true do context "when filtering by owning organisation and then switching back to all organisations", js: true do
let!(:organisation) { FactoryBot.create(:organisation, name: "Filtered Org") } let!(:organisation) { FactoryBot.create(:organisation) }
let(:parent_organisation) { FactoryBot.create(:organisation, name: "Filtered Org") }
before do before do
create(:organisation_relationship, child_organisation: organisation, parent_organisation:)
allow(SecureRandom).to receive(:random_number).and_return(otp) allow(SecureRandom).to receive(:random_number).and_return(otp)
click_button("Sign in") click_button("Sign in")
fill_in("code", with: otp) fill_in("code", with: otp)
@ -720,14 +722,14 @@ RSpec.describe "User Features" do
it "clears the previously selected organisation value" do it "clears the previously selected organisation value" do
visit("/lettings-logs") visit("/lettings-logs")
choose("organisation-select-specific-org-field", allow_label_click: true) choose("owning-organisation-select-specific-org-field", allow_label_click: true)
expect(page).to have_field("organisation-field", with: "") expect(page).to have_field("owning-organisation-field", with: "")
find("#organisation-field").click.native.send_keys("F", "i", "l", "t", :down, :enter) find("#owning-organisation-field").click.native.send_keys("F", "i", "l", "t", :down, :enter)
click_button("Apply filters") click_button("Apply filters")
expect(page).to have_current_path("/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&owning_organisation_select=all&organisation_select=specific_org&organisation=#{organisation.id}") expect(page).to have_current_path("/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&owning_organisation_select=specific_org&owning_organisation=#{parent_organisation.id}")
choose("organisation-select-all-field", allow_label_click: true) choose("owning-organisation-select-all-field", allow_label_click: true)
click_button("Apply filters") click_button("Apply filters")
expect(page).to have_current_path("/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&owning_organisation_select=all&organisation_select=all") expect(page).to have_current_path("/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&owning_organisation_select=all")
end end
end end
end end

36
spec/helpers/filters_helper_spec.rb

@ -38,34 +38,34 @@ RSpec.describe FiltersHelper do
context "when support user is using the organisation filter" do context "when support user is using the organisation filter" do
before do before do
session[:lettings_logs_filters] = { "organisation": "1" }.to_json session[:lettings_logs_filters] = { "owning_organisation": "1" }.to_json
end end
it "returns true for the parent organisation_select filter" do it "returns true for the parent owning_organisation_select filter" do
expect(filter_selected?("organisation_select", :specific_org, "lettings_logs")).to be true expect(filter_selected?("owning_organisation_select", :specific_org, "lettings_logs")).to be true
expect(filter_selected?("organisation_select", :all, "lettings_logs")).to be false expect(filter_selected?("owning_organisation_select", :all, "lettings_logs")).to be false
end end
end end
context "when support user has not set the organisation_select filter" do context "when support user has not set the owning_organisation_select filter" do
before do before do
session[:lettings_logs_filters] = {}.to_json session[:lettings_logs_filters] = {}.to_json
end end
it "defaults to all organisations" do it "defaults to all organisations" do
expect(filter_selected?("organisation_select", :all, "lettings_logs")).to be true expect(filter_selected?("owning_organisation_select", :all, "lettings_logs")).to be true
expect(filter_selected?("organisation_select", :specific_org, "lettings_logs")).to be false expect(filter_selected?("owning_organisation_select", :specific_org, "lettings_logs")).to be false
end end
end end
context "when the specific organisation filter is not set" do context "when the specific owning organisation filter is not set" do
before do before do
session[:lettings_logs_filters] = { "status" => [""], "years" => [""], "user" => "all" }.to_json session[:lettings_logs_filters] = { "status" => [""], "years" => [""], "user" => "all" }.to_json
end end
it "marks the all options as checked" do it "marks the all options as checked" do
expect(filter_selected?("organisation_select", :all, "lettings_logs")).to be true expect(filter_selected?("owning_organisation_select", :all, "lettings_logs")).to be true
expect(filter_selected?("organisation_select", :specific_org, "lettings_logs")).to be false expect(filter_selected?("owning_organisation_select", :specific_org, "lettings_logs")).to be false
end end
end end
end end
@ -85,7 +85,7 @@ RSpec.describe FiltersHelper do
end end
context "when organisation and user are set to all" do context "when organisation and user are set to all" do
let(:filters) { { "organisation_select" => "all", "user" => "all" } } let(:filters) { { "owning_organisation_select" => "all", "user" => "all" } }
it "returns false" do it "returns false" do
expect(result).to be_falsey expect(result).to be_falsey
@ -160,7 +160,7 @@ RSpec.describe FiltersHelper do
end end
end end
describe "#organisations_filter_options" do describe "#owning_organisations_filter_options" do
let(:parent_organisation) { FactoryBot.create(:organisation, name: "Parent organisation") } let(:parent_organisation) { FactoryBot.create(:organisation, name: "Parent organisation") }
let(:child_organisation) { FactoryBot.create(:organisation, name: "Child organisation") } let(:child_organisation) { FactoryBot.create(:organisation, name: "Child organisation") }
@ -170,10 +170,10 @@ RSpec.describe FiltersHelper do
end end
context "with a support user" do context "with a support user" do
let(:user) { FactoryBot.create(:user, :support, organisation: parent_organisation) } let(:user) { FactoryBot.create(:user, :support, organisation: child_organisation) }
it "returns a list of all organisations" do it "returns a list of all organisations" do
expect(organisations_filter_options(user)).to eq([ expect(owning_organisations_filter_options(user)).to eq([
OpenStruct.new(id: "", name: "Select an option"), OpenStruct.new(id: "", name: "Select an option"),
OpenStruct.new(id: parent_organisation.id, name: "Parent organisation"), OpenStruct.new(id: parent_organisation.id, name: "Parent organisation"),
OpenStruct.new(id: child_organisation.id, name: "Child organisation"), OpenStruct.new(id: child_organisation.id, name: "Child organisation"),
@ -183,13 +183,13 @@ RSpec.describe FiltersHelper do
end end
context "with a data coordinator user" do context "with a data coordinator user" do
let(:user) { FactoryBot.create(:user, :data_coordinator, organisation: parent_organisation) } let(:user) { FactoryBot.create(:user, :data_coordinator, organisation: child_organisation) }
it "returns a list of managing agents and your own organisation" do it "returns a list of paret orgs and your own organisation" do
expect(organisations_filter_options(user)).to eq([ expect(owning_organisations_filter_options(user)).to eq([
OpenStruct.new(id: "", name: "Select an option"), OpenStruct.new(id: "", name: "Select an option"),
OpenStruct.new(id: parent_organisation.id, name: "Parent organisation"),
OpenStruct.new(id: child_organisation.id, name: "Child organisation"), OpenStruct.new(id: child_organisation.id, name: "Child organisation"),
OpenStruct.new(id: parent_organisation.id, name: "Parent organisation"),
]) ])
end end
end end

10
spec/models/user_spec.rb

@ -148,11 +148,11 @@ RSpec.describe User, type: :model do
context "and their organisation has managing agents" do context "and their organisation has managing agents" do
before do before do
create(:organisation_relationship, parent_organisation: user.organisation) create(:organisation_relationship, child_organisation: user.organisation)
end end
it "can filter lettings logs by user, owning_organisation, year, status and organisation" do it "can filter lettings logs by user, owning_organisation, year and status" do
expect(user.logs_filters).to eq(%w[status years user organisation owning_organisation bulk_upload_id]) expect(user.logs_filters).to eq(%w[status years user owning_organisation bulk_upload_id])
end end
end end
end end
@ -192,8 +192,8 @@ RSpec.describe User, type: :model do
}) })
end end
it "can filter lettings logs by user, owning_organisation, year, status and organisation" do it "can filter lettings logs by user, owning_organisation, year and status" do
expect(user.logs_filters).to eq(%w[status years user organisation owning_organisation bulk_upload_id]) expect(user.logs_filters).to eq(%w[status years user owning_organisation bulk_upload_id])
end end
end end

19
spec/requests/lettings_logs_controller_spec.rb

@ -354,12 +354,6 @@ RSpec.describe LettingsLogsController, type: :request do
expect(page).not_to have_link(completed_lettings_log.id.to_s) expect(page).not_to have_link(completed_lettings_log.id.to_s)
end end
it "filters on organisation" do
get "/lettings-logs?organisation[]=#{organisation_2.id}", headers:, params: {}
expect(page).to have_link(completed_lettings_log.id.to_s)
expect(page).not_to have_link(in_progress_lettings_log.id.to_s)
end
it "filters on owning organisation" do it "filters on owning organisation" do
get "/lettings-logs?owning_organisation[]=#{organisation_2.id}", headers:, params: {} get "/lettings-logs?owning_organisation[]=#{organisation_2.id}", headers:, params: {}
expect(page).to have_link(completed_lettings_log.id.to_s) expect(page).to have_link(completed_lettings_log.id.to_s)
@ -784,25 +778,12 @@ RSpec.describe LettingsLogsController, type: :request do
sign_in user sign_in user
end end
it "does show the organisation filter" do
get "/lettings-logs", headers:, params: {}
expect(page).to have_field("organisation-field")
end
it "shows all logs by default" do it "shows all logs by default" do
get "/lettings-logs", headers:, params: {} get "/lettings-logs", headers:, params: {}
expect(page).to have_content(tenant_code_1) expect(page).to have_content(tenant_code_1)
expect(page).to have_content(tenant_code_2) expect(page).to have_content(tenant_code_2)
end end
context "when filtering by organisation" do
it "only show the selected organisations logs" do
get "/lettings-logs?organisation_select=specific_org&organisation=#{org_1.id}", headers:, params: {}
expect(page).to have_content(tenant_code_1)
expect(page).not_to have_content(tenant_code_2)
end
end
context "when the support user has filtered by organisation, then switches back to all organisations" do context "when the support user has filtered by organisation, then switches back to all organisations" do
it "shows all organisations" do it "shows all organisations" do
get "http://localhost:3000/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&organisation_select=all&organisation=#{org_1.id}", headers:, params: {} get "http://localhost:3000/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&organisation_select=all&organisation=#{org_1.id}", headers:, params: {}

6
spec/requests/sales_logs_controller_spec.rb

@ -250,12 +250,6 @@ RSpec.describe SalesLogsController, type: :request do
expect(page).not_to have_link(completed_sales_log.id.to_s) expect(page).not_to have_link(completed_sales_log.id.to_s)
end end
it "filters on organisation" do
get "/sales-logs?organisation[]=#{organisation_2.id}", headers: headers, params: {}
expect(page).to have_link(completed_sales_log.id.to_s)
expect(page).not_to have_link(not_started_sales_log.id.to_s)
end
it "does not reset the filters" do it "does not reset the filters" do
get "/sales-logs?status[]=not_started", headers: headers, params: {} get "/sales-logs?status[]=not_started", headers: headers, params: {}
expect(page).to have_link(not_started_sales_log.id.to_s) expect(page).to have_link(not_started_sales_log.id.to_s)

Loading…
Cancel
Save