From ec0742add721318cfbe9cb065fffbaa8b71ff0ef Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 13 Jul 2023 09:51:09 +0100 Subject: [PATCH] Add owning organisation filter --- app/helpers/filters_helper.rb | 7 +++++++ app/models/lettings_log.rb | 1 + app/models/organisation.rb | 4 ++++ app/models/sales_log.rb | 1 + app/models/user.rb | 4 ++-- app/services/filter_manager.rb | 7 +++++-- app/views/logs/_log_filters.html.erb | 20 +++++++++++++++++++ spec/models/lettings_log_spec.rb | 19 ++++++++++++++++++ .../requests/lettings_logs_controller_spec.rb | 12 +++++++++++ 9 files changed, 71 insertions(+), 4 deletions(-) diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index 34e768603..f63b80b2e 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -5,7 +5,9 @@ module FiltersHelper 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.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["organisation"].present? && filter == "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? selected_filters[filter].include?(value.to_s) @@ -42,6 +44,11 @@ module FiltersHelper [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) + 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) } + end + def collection_year_options { "2023": "2023/24", "2022": "2022/23", "2021": "2021/22" } end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index a1094ee37..d1525590d 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -53,6 +53,7 @@ class LettingsLog < Log scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :unresolved, -> { where(unresolved: true) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } + scope :filter_by_owning_organisation, ->(org, _user = nil) { where(owning_organisation: org) } scope :duplicate_logs, lambda { |log| visible .where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) diff --git a/app/models/organisation.rb b/app/models/organisation.rb index ad74ac3e6..91c091720 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -118,4 +118,8 @@ class Organisation < ApplicationRecord def has_managing_agents? managing_agents.count.positive? end + + def has_stock_owners? + stock_owners.count.positive? + end end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index ae76e32c3..47a3ef9f9 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -41,6 +41,7 @@ class SalesLog < Log .or(filter_by_id(param)) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } + scope :filter_by_owning_organisation, ->(org, _user = nil) { where(owning_organisation: org) } scope :duplicate_logs, lambda { |log| visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where.not(id: log.id) diff --git a/app/models/user.rb b/app/models/user.rb index 2cf45de45..c0305b52a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -143,8 +143,8 @@ class User < ApplicationRecord end def logs_filters(specific_org: false) - if (support? && !specific_org) || organisation.has_managing_agents? - %w[status years user organisation bulk_upload_id] + if (support? && !specific_org) || organisation.has_managing_agents? || organisation.has_stock_owners? + %w[status years user organisation owning_organisation bulk_upload_id] else %w[status years user bulk_upload_id] end diff --git a/app/services/filter_manager.rb b/app/services/filter_manager.rb index 39cd7cdd3..9a950ca93 100644 --- a/app/services/filter_manager.rb +++ b/app/services/filter_manager.rb @@ -22,6 +22,7 @@ class FilterManager filters.each do |category, values| next if Array(values).reject(&:empty?).blank? next if category == "organisation" && all_orgs + next if category == "owning_organisation" && all_orgs logs = logs.public_send("filter_by_#{category}", values, user) end @@ -53,11 +54,13 @@ class FilterManager new_filters[filter] = params[filter] if params[filter].present? end end - params["organisation_select"] == "all" ? new_filters.except("organisation") : new_filters + 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 end def filtered_logs(logs, search_term, filters) - all_orgs = params["organisation_select"] == "all" + all_orgs = params["organisation_select"] == "all" && params["owning_organisation_select"] == "all" FilterManager.filter_logs(logs, search_term, filters, all_orgs, current_user) end diff --git a/app/views/logs/_log_filters.html.erb b/app/views/logs/_log_filters.html.erb index d8235d350..b2a81a6a6 100644 --- a/app/views/logs/_log_filters.html.erb +++ b/app/views/logs/_log_filters.html.erb @@ -44,6 +44,26 @@ category: "user", } %> + <% if @current_user.support? || @current_user.organisation.has_stock_owners? && request.path == "/lettings-logs" %> + <%= render partial: "filters/radio_filter", locals: { + f:, + options: { + "all": { label: "All" }, + "specific_org": { + label: "Specific owning organisation", + conditional_filter: { + type: "select", + label: "Owning Organisation", + category: "owning_organisation", + options: owning_organisations_filter_options(@current_user), + }, + }, + }, + label: "Owning organisation", + category: "owning_organisation_select", + } %> + <% end %> + <% if (@current_user.support? || @current_user.organisation.has_managing_agents?) && request.path == "/lettings-logs" %> <%= render partial: "filters/radio_filter", locals: { f:, diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index e2a15874a..80ac0d510 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2769,6 +2769,25 @@ RSpec.describe LettingsLog 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(:lettings_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1, created_by: nil) + create(:lettings_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2, created_by: nil) + create(:lettings_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1, created_by: nil) + create(:lettings_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2, created_by: nil) + 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 on status" do it "allows filtering on a single status" do expect(described_class.filter_by_status(%w[in_progress]).count).to eq(2) diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 2fc1ef461..a6e05b94f 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -360,6 +360,18 @@ RSpec.describe LettingsLogsController, type: :request do expect(page).not_to have_link(in_progress_lettings_log.id.to_s) end + it "filters on owning organisation" do + get "/lettings-logs?owning_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 "filtering on owning organisation does not return managed orgs" do + get "/lettings-logs?owning_organisation[]=#{organisation.id}", headers:, params: {} + expect(page).not_to have_link(completed_lettings_log.id.to_s) + expect(page).to have_link(in_progress_lettings_log.id.to_s) + end + it "does not reset the filters" do get "/lettings-logs?status[]=in_progress", headers:, params: {} expect(page).to have_link(in_progress_lettings_log.id.to_s)