Browse Source

Add owning organisation filter

pull/1772/head
Kat 3 years ago
parent
commit
ec0742add7
  1. 7
      app/helpers/filters_helper.rb
  2. 1
      app/models/lettings_log.rb
  3. 4
      app/models/organisation.rb
  4. 1
      app/models/sales_log.rb
  5. 4
      app/models/user.rb
  6. 7
      app/services/filter_manager.rb
  7. 20
      app/views/logs/_log_filters.html.erb
  8. 19
      spec/models/lettings_log_spec.rb
  9. 12
      spec/requests/lettings_logs_controller_spec.rb

7
app/helpers/filters_helper.rb

@ -5,7 +5,9 @@ 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?("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["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? return false if selected_filters[filter].blank?
selected_filters[filter].include?(value.to_s) 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) } [OpenStruct.new(id: "", name: "Select an option")] + organisation_options.map { |org| OpenStruct.new(id: org.id, name: org.name) }
end 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 def collection_year_options
{ "2023": "2023/24", "2022": "2022/23", "2021": "2021/22" } { "2023": "2023/24", "2022": "2022/23", "2021": "2021/22" }
end end

1
app/models/lettings_log.rb

@ -53,6 +53,7 @@ class LettingsLog < Log
scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :filter_by_before_startdate, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :unresolved, -> { where(unresolved: true) } scope :unresolved, -> { where(unresolved: true) }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } 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| scope :duplicate_logs, lambda { |log|
visible visible
.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))

4
app/models/organisation.rb

@ -118,4 +118,8 @@ class Organisation < ApplicationRecord
def has_managing_agents? def has_managing_agents?
managing_agents.count.positive? managing_agents.count.positive?
end end
def has_stock_owners?
stock_owners.count.positive?
end
end end

1
app/models/sales_log.rb

@ -41,6 +41,7 @@ class SalesLog < Log
.or(filter_by_id(param)) .or(filter_by_id(param))
} }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } 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| scope :duplicate_logs, lambda { |log|
visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))
.where.not(id: log.id) .where.not(id: log.id)

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? if (support? && !specific_org) || organisation.has_managing_agents? || organisation.has_stock_owners?
%w[status years user organisation bulk_upload_id] %w[status years user organisation owning_organisation bulk_upload_id]
else else
%w[status years user bulk_upload_id] %w[status years user bulk_upload_id]
end end

7
app/services/filter_manager.rb

@ -22,6 +22,7 @@ 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 == "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)
end end
@ -53,11 +54,13 @@ class FilterManager
new_filters[filter] = params[filter] if params[filter].present? new_filters[filter] = params[filter] if params[filter].present?
end end
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 end
def filtered_logs(logs, search_term, filters) 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) FilterManager.filter_logs(logs, search_term, filters, all_orgs, current_user)
end end

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

@ -44,6 +44,26 @@
category: "user", 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" %> <% if (@current_user.support? || @current_user.organisation.has_managing_agents?) && request.path == "/lettings-logs" %>
<%= render partial: "filters/radio_filter", locals: { <%= render partial: "filters/radio_filter", locals: {
f:, f:,

19
spec/models/lettings_log_spec.rb

@ -2769,6 +2769,25 @@ RSpec.describe LettingsLog 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(: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 context "when filtering on status" do
it "allows filtering on a single status" do it "allows filtering on a single status" do
expect(described_class.filter_by_status(%w[in_progress]).count).to eq(2) expect(described_class.filter_by_status(%w[in_progress]).count).to eq(2)

12
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) expect(page).not_to have_link(in_progress_lettings_log.id.to_s)
end 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 it "does not reset the filters" do
get "/lettings-logs?status[]=in_progress", headers:, params: {} get "/lettings-logs?status[]=in_progress", headers:, params: {}
expect(page).to have_link(in_progress_lettings_log.id.to_s) expect(page).to have_link(in_progress_lettings_log.id.to_s)

Loading…
Cancel
Save