Browse Source

CLDC-2244 Add managed by filter (#1778)

pull/1782/head
Jack 1 year ago committed by GitHub
parent
commit
e1c6a4ec04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/helpers/filters_helper.rb
  2. 3
      app/models/lettings_log.rb
  3. 4
      app/models/user.rb
  4. 9
      app/services/filter_manager.rb
  5. 20
      app/views/logs/_log_filters.html.erb
  6. 4
      spec/features/user_spec.rb
  7. 19
      spec/models/lettings_log_spec.rb
  8. 8
      spec/models/user_spec.rb

11
app/helpers/filters_helper.rb

@ -4,8 +4,13 @@ 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?("owning_organisation") && filter == "owning_organisation_select" && value == :all
return true if !selected_filters.key?("managing_organisation") && filter == "managing_organisation_select" && value == :all
return true if selected_filters["owning_organisation"].present? && filter == "owning_organisation_select" && value == :specific_org
return true if selected_filters["managing_organisation"].present? && filter == "managing_organisation_select" && value == :specific_org
return false if selected_filters[filter].blank?
selected_filters[filter].include?(value.to_s)
@ -18,6 +23,7 @@ module FiltersHelper
filters = JSON.parse(filters_json)
filters["user"] == "yours" ||
filters["organisation"].present? ||
filters["managing_organisation"].present? ||
filters["status"]&.compact_blank&.any? ||
filters["years"]&.compact_blank&.any? ||
filters["bulk_upload_id"].present?
@ -56,6 +62,11 @@ module FiltersHelper
end
end
def managing_organisation_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
private
def applied_filters_count(filter_type)

3
app/models/lettings_log.rb

@ -52,8 +52,11 @@ 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 :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) }
scope :duplicate_logs, lambda { |log|
visible
.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))

4
app/models/user.rb

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

9
app/services/filter_manager.rb

@ -21,7 +21,9 @@ class FilterManager
filters.each do |category, values|
next if Array(values).reject(&:empty?).blank?
next if category == "owning_organisation" && all_orgs
next if category == "managing_organisation" && all_orgs
logs = logs.public_send("filter_by_#{category}", values, user)
end
@ -53,12 +55,17 @@ class FilterManager
new_filters[filter] = params[filter] if params[filter].present?
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("managing_organisation") if params["managing_organisation_select"] == "all"
new_filters
end
def filtered_logs(logs, search_term, filters)
all_orgs = params["organisation_select"] == "all" && params["owning_organisation_select"] == "all"
all_orgs = params["managing_organisation_select"] == "all" && params["owning_organisation_select"] == "all"
FilterManager.filter_logs(logs, search_term, filters, all_orgs, current_user)
end

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

@ -72,6 +72,26 @@
} %>
<% end %>
<% if (@current_user.support? || @current_user.organisation.managing_agents.count > 1) && request.path == "/lettings-logs" %>
<%= render partial: "filters/radio_filter", locals: {
f:,
options: {
"all": { label: "Any managing organisation" },
"specific_org": {
label: "Specific managing organisation",
conditional_filter: {
type: "select",
label: "Managed by",
category: "managing_organisation",
options: managing_organisation_filter_options(@current_user),
},
},
},
label: "Managed by",
category: "managing_organisation_select",
} %>
<% end %>
<%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %>
<% end %>
</div>

4
spec/features/user_spec.rb

@ -726,10 +726,10 @@ RSpec.describe "User Features" do
expect(page).to have_field("owning-organisation-field", with: "")
find("#owning-organisation-field").click.native.send_keys("F", "i", "l", "t", :down, :enter)
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=specific_org&owning_organisation=#{parent_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}&managing_organisation_select=all")
choose("owning-organisation-select-all-field", allow_label_click: true)
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")
expect(page).to have_current_path("/lettings-logs?%5Byears%5D%5B%5D=&%5Bstatus%5D%5B%5D=&user=all&owning_organisation_select=all&managing_organisation_select=all")
end
end
end

19
spec/models/lettings_log_spec.rb

@ -2788,6 +2788,25 @@ RSpec.describe LettingsLog do
end
end
context "when filtering by managing organisation" do
let(:organisation_1) { create(:organisation) }
let(:organisation_2) { create(:organisation) }
let(:organisation_3) { create(:organisation) }
before do
create(:lettings_log, :in_progress, managing_organisation: organisation_1)
create(:lettings_log, :completed, managing_organisation: organisation_1)
create(:lettings_log, :completed, managing_organisation: organisation_2)
create(:lettings_log, :completed, managing_organisation: organisation_2)
end
it "filters by given managing organisation" do
expect(described_class.filter_by_managing_organisation([organisation_1]).count).to eq(2)
expect(described_class.filter_by_managing_organisation([organisation_1, organisation_2]).count).to eq(4)
expect(described_class.filter_by_managing_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)

8
spec/models/user_spec.rb

@ -151,8 +151,8 @@ RSpec.describe User, type: :model do
create(:organisation_relationship, child_organisation: user.organisation)
end
it "can filter lettings logs by user, owning_organisation, year and status" do
expect(user.logs_filters).to eq(%w[status years user owning_organisation bulk_upload_id])
it "can filter lettings logs by user, year, status, managing_organisation and owning_organisation" do
expect(user.logs_filters).to match_array(%w[status years user managing_organisation owning_organisation bulk_upload_id])
end
end
end
@ -192,8 +192,8 @@ RSpec.describe User, type: :model do
})
end
it "can filter lettings logs by user, owning_organisation, year and status" do
expect(user.logs_filters).to eq(%w[status years user owning_organisation bulk_upload_id])
it "can filter lettings logs by user, year, status, managing_organisation and owning_organisation" do
expect(user.logs_filters).to match_array(%w[status years user owning_organisation managing_organisation bulk_upload_id])
end
end

Loading…
Cancel
Save