Browse Source

CLDC-1844 Allow filtering by organisation if organisation has managing agents (#1182)

* Allow filtering by organisation if organisation has managing agents

* update test name

* Displayed owned/managed by in the logs list if the org has managing agents
pull/1175/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
15e1f73cf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/components/log_summary_component.html.erb
  2. 4
      app/controllers/organisations_controller.rb
  3. 5
      app/helpers/filters_helper.rb
  4. 4
      app/models/organisation.rb
  5. 2
      app/models/user.rb
  6. 4
      app/views/logs/_log_filters.erb
  7. 35
      spec/helpers/filters_helper_spec.rb
  8. 16
      spec/models/user_spec.rb

2
app/components/log_summary_component.html.erb

@ -36,7 +36,7 @@
</p> </p>
<% end %> <% end %>
<% if current_user.support? %> <% if current_user.support? || current_user.organisation.has_managing_agents? %>
<% if log.owning_organisation or log.managing_organisation %> <% if log.owning_organisation or log.managing_organisation %>
<dl class="app-metadata"> <dl class="app-metadata">
<div class="app-metadata__item"> <div class="app-metadata__item">

4
app/controllers/organisations_controller.rb

@ -6,8 +6,8 @@ class OrganisationsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
before_action :find_resource, except: %i[index new create] before_action :find_resource, except: %i[index new create]
before_action :authenticate_scope!, except: [:index] before_action :authenticate_scope!, except: [:index]
before_action -> { session_filters(specific_org: true) }, if: -> { current_user.support? } before_action -> { session_filters(specific_org: true) }, if: -> { current_user.support? || current_user.organisation.has_managing_agents? }
before_action :set_session_filters, if: -> { current_user.support? } before_action :set_session_filters, if: -> { current_user.support? || current_user.organisation.has_managing_agents? }
def index def index
redirect_to organisation_path(current_user.organisation) unless current_user.support? redirect_to organisation_path(current_user.organisation) unless current_user.support?

5
app/helpers/filters_helper.rb

@ -22,4 +22,9 @@ module FiltersHelper
JSON.parse(session[:logs_filters])[filter] || "" JSON.parse(session[:logs_filters])[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
end end

4
app/models/organisation.rb

@ -92,4 +92,8 @@ class Organisation < ApplicationRecord
{ name: "Data protection agreement", value: data_protection_agreement_string, editable: false }, { name: "Data protection agreement", value: data_protection_agreement_string, editable: false },
].compact ].compact
end end
def has_managing_agents?
managing_agents.count.positive?
end
end end

2
app/models/user.rb

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

4
app/views/logs/_log_filters.erb

@ -10,7 +10,7 @@
<%= render partial: "filters/checkbox_filter", locals: { f: f, options: years, label: "Collection year", category: "years" } %> <%= render partial: "filters/checkbox_filter", locals: { f: f, options: years, label: "Collection year", category: "years" } %>
<%= render partial: "filters/checkbox_filter", locals: { f: f, options: status_filters, label: "Status", category: "status" } %> <%= render partial: "filters/checkbox_filter", locals: { f: f, options: status_filters, label: "Status", category: "status" } %>
<%= render partial: "filters/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %> <%= render partial: "filters/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %>
<% if @current_user.support? && 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, f: f,
options: { options: {
@ -21,7 +21,7 @@
type: "select", type: "select",
label: "Organisation", label: "Organisation",
category: "organisation", category: "organisation",
options: [OpenStruct.new(id: "", name: "Select an option")] + Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } options: organisations_filter_options(@current_user)
} }
} }
}, },

35
spec/helpers/filters_helper_spec.rb

@ -81,4 +81,39 @@ RSpec.describe FiltersHelper do
end end
end end
end end
describe "#organisations_filter_options" do
let(:parent_organisation) { FactoryBot.create(:organisation, name: "Parent organisation") }
let(:child_organisation) { FactoryBot.create(:organisation, name: "Child organisation") }
before do
FactoryBot.create(:organisation_relationship, parent_organisation:, child_organisation:)
FactoryBot.create(:organisation, name: "Other organisation", id: 99)
end
context "with a support user" do
let(:user) { FactoryBot.create(:user, :support, organisation: parent_organisation) }
it "returns a list of all organisations" do
expect(organisations_filter_options(user)).to eq([
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: 99, name: "Other organisation"),
])
end
end
context "with a data coordinator user" do
let(:user) { FactoryBot.create(:user, :data_coordinator, organisation: parent_organisation) }
it "returns a list of managing agents and your own organisation" do
expect(organisations_filter_options(user)).to eq([
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"),
])
end
end
end
end end

16
spec/models/user_spec.rb

@ -117,11 +117,27 @@ RSpec.describe User, type: :model do
}) })
end end
context "and their organisation does not have managing agents" do
before do
user.organisation.update(holds_own_stock: false)
end
it "can filter lettings logs by user, year and status" do it "can filter lettings logs by user, year and status" do
expect(user.logs_filters).to eq(%w[status years user]) expect(user.logs_filters).to eq(%w[status years user])
end end
end end
context "and their organisation has managing agents" do
before do
FactoryBot.create(:organisation_relationship, parent_organisation: user.organisation)
end
it "can filter lettings logs by user, year, status and organisation" do
expect(user.logs_filters).to eq(%w[status years user organisation])
end
end
end
context "when the user is a Customer Support person" do context "when the user is a Customer Support person" do
let(:user) { FactoryBot.create(:user, :support) } let(:user) { FactoryBot.create(:user, :support) }
let!(:other_orgs_log) { FactoryBot.create(:lettings_log) } let!(:other_orgs_log) { FactoryBot.create(:lettings_log) }

Loading…
Cancel
Save