diff --git a/app/controllers/case_logs_controller.rb b/app/controllers/case_logs_controller.rb index ca9804056..b4b4f4851 100644 --- a/app/controllers/case_logs_controller.rb +++ b/app/controllers/case_logs_controller.rb @@ -7,7 +7,7 @@ class CaseLogsController < ApplicationController before_action :find_resource, except: %i[create index edit] def index - set_session_filters if params[:status].present? + set_session_filters @pagy, @case_logs = pagy(filtered_case_logs) @@ -121,14 +121,21 @@ private end def filtered_case_logs - user_case_logs = current_user.case_logs - status_filter = JSON.parse(session[:case_logs_filters])["status"] if session[:case_logs_filters].present? - return user_case_logs unless status_filter + query = CaseLog.for_organisation(current_user.organisation) + if session[:case_logs_filters].present? + filters = JSON.parse(session[:case_logs_filters]) + filters.each do |category, values| + next if values.reject(&:empty?).blank? - user_case_logs.filter_by_status(status_filter) + query = query.public_send("filter_by_#{category}", values) + end + end + query.all.includes(:owning_organisation, :managing_organisation) end def set_session_filters - session[:case_logs_filters] = { status: params[:status] }.to_json + 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? } + session[:case_logs_filters] = new_filters.to_json end end diff --git a/app/helpers/filters_helper.rb b/app/helpers/filters_helper.rb index aaee74fb6..f448bbeb5 100644 --- a/app/helpers/filters_helper.rb +++ b/app/helpers/filters_helper.rb @@ -1,9 +1,11 @@ module FiltersHelper - def filter_selected?(filter) - return true unless session[:case_logs_filters] + def filter_selected?(filter, value) + return false unless session[:case_logs_filters] selected_filters = JSON.parse(session[:case_logs_filters]) - selected_filters["status"].present? && selected_filters["status"].include?(filter.to_s) + return false if selected_filters[filter].blank? + + selected_filters[filter].include?(value.to_s) end def status_filters diff --git a/app/models/case_log.rb b/app/models/case_log.rb index aa926d5b4..99ea8fdb5 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -35,6 +35,13 @@ class CaseLog < ApplicationRecord scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_status, ->(status) { where status: status } + scope :filter_by_years, lambda { |years| + first_year = years.shift + query = filter_by_year(first_year) + years.each { |year| query = query.or(filter_by_year(year)) } + query.all + } + scope :filter_by_year, ->(year) { where(startdate: Time.utc(year.to_i, 4, 1)...Time.utc(year.to_i + 1, 4, 1)) } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze OPTIONAL_FIELDS = %w[postcode_known la_known first_time_property_let_as_social_housing tenant_code propcode].freeze @@ -51,6 +58,8 @@ class CaseLog < ApplicationRecord end def collection_start_year + return unless startdate + window_end_date = Time.zone.local(startdate.year, 4, 1) startdate < window_end_date ? startdate.year - 1 : startdate.year end diff --git a/app/views/case_logs/_log_filters.erb b/app/views/case_logs/_log_filters.erb index 4e5593a62..09432faa7 100644 --- a/app/views/case_logs/_log_filters.erb +++ b/app/views/case_logs/_log_filters.erb @@ -2,9 +2,9 @@

Filters

-