diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index fa33ad5df..ef53784a1 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -137,8 +137,7 @@ private def set_session_filters new_filters = session[:case_logs_filters].present? ? JSON.parse(session[:case_logs_filters]) : {} - %i[status years].each { |filter| new_filters[filter] = params[filter] if params[filter].present? } - new_filters[:user] = [params[:user]] if params[:user].present? + current_user.case_logs_filters.each { |filter| new_filters[filter] = params[filter] if params[filter].present? } session[:case_logs_filters] = new_filters.to_json end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index ff2a962c4..df91376cf 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -34,7 +34,7 @@ class CaseLog < ApplicationRecord belongs_to :managing_organisation, class_name: "Organisation" belongs_to :created_by, class_name: "User" - scope :for_organisation, ->(org) { 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_status, ->(status, _user = nil) { where status: } scope :filter_by_years, lambda { |years, _user = nil| first_year = years.shift diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 8096c6be1..c52c3d922 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -18,7 +18,7 @@ class Organisation < ApplicationRecord validates :provider_type, presence: true def case_logs - CaseLog.for_organisation(self) + CaseLog.filter_by_organisation(self) end def completed_case_logs diff --git a/app/models/user.rb b/app/models/user.rb index 27a884fa4..09fc93c3c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -36,7 +36,7 @@ class User < ApplicationRecord if support? CaseLog.all else - CaseLog.for_organisation(organisation) + CaseLog.filter_by_organisation(organisation) end end @@ -88,4 +88,12 @@ class User < ApplicationRecord ROLES.except(:support) end + + def case_logs_filters + if support? + %i[status years user organisation] + else + %i[status years user] + end + end end diff --git a/app/views/case_logs/_log_filters.erb b/app/views/case_logs/_log_filters.erb index 4e294c81e..5f69e97a7 100644 --- a/app/views/case_logs/_log_filters.erb +++ b/app/views/case_logs/_log_filters.erb @@ -10,8 +10,11 @@ <%= 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/radio_filter", locals: { f: f, options: all_or_yours, label: "Logs", category: "user", } %> + <% if @current_user.support? %> + <%= render partial: "filters/checkbox_filter", locals: { f: f, options: {"all": "All", "yours": "Yours"}, label: "Organisation", category: "organisation" } %> + <% end %> <%= f.govuk_submit "Apply filters", class: "govuk-!-margin-bottom-0" %> <% end %> - \ No newline at end of file + diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 4a8bc8677..e24222d3e 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1893,6 +1893,22 @@ RSpec.describe CaseLog do end end + context "when filtering by organisation" do + let(:organisation_1) { FactoryBot.create(:organisation) } + let(:organisation_2) { FactoryBot.create(:organisation) } + let(:organisation_3) { FactoryBot.create(:organisation) } + let!(:case_log_1) { FactoryBot.create(:case_log, :in_progress, owning_organisation: organisation_1, managing_organisation: organisation_1) } + let!(:case_log_2) { FactoryBot.create(:case_log, :completed, owning_organisation: organisation_1, managing_organisation: organisation_2) } + let!(:case_log_3) { FactoryBot.create(:case_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_1) } + let!(:case_log_4) { FactoryBot.create(:case_log, :completed, owning_organisation: organisation_2, managing_organisation: organisation_2) } + + it "filters by given organisation" do + expect(described_class.filter_by_organisation([organisation_1]).count).to eq(3) + expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(4) + expect(described_class.filter_by_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/models/user_spec.rb b/spec/models/user_spec.rb index 97b1fdaf1..4cbebcdae 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -97,6 +97,10 @@ RSpec.describe User, type: :model do data_coordinator: 2, }) end + + it "can filter case logs by user, year and status" do + expect(user.case_logs_filters).to eq(%i[status years user]) + end end context "when the user is a Customer Support person" do @@ -119,6 +123,10 @@ RSpec.describe User, type: :model do support: 99, }) end + + it "can filter case logs by user, year, status and organisation" do + expect(user.case_logs_filters).to eq(%i[status years user organisation]) + end end end