From 3672122cde6c010eaf2b39d68e0f1f54034d5c9e Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 19 Jan 2024 11:17:07 +0000 Subject: [PATCH] Filter logs list by existing forms only --- app/controllers/lettings_logs_controller.rb | 2 +- app/controllers/organisations_controller.rb | 4 +-- app/controllers/sales_logs_controller.rb | 2 +- app/models/form_handler.rb | 16 ++++++++++++ app/models/lettings_log.rb | 7 ++++++ app/models/sales_log.rb | 7 ++++++ spec/models/lettings_log_spec.rb | 28 +++++++++++++++++++++ spec/models/sales_log_spec.rb | 27 ++++++++++++++++++++ 8 files changed, 89 insertions(+), 4 deletions(-) diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index dffdec69f..a546488b0 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -13,7 +13,7 @@ class LettingsLogsController < LogsController before_action :redirect_if_bulk_upload_resolved, only: [:index] def index - all_logs = current_user.lettings_logs.visible + all_logs = current_user.lettings_logs.visible.filter_by_years_or_nil(FormHandler.instance.years_of_available_lettings_forms) unpaginated_filtered_logs = filter_manager.filtered_logs(all_logs, search_term, session_filters) @delete_logs_path = delete_logs_lettings_logs_path(search: search_term) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 3bf4cf5a9..0952d12ac 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -106,7 +106,7 @@ class OrganisationsController < ApplicationController end def lettings_logs - organisation_logs = LettingsLog.visible.filter_by_organisation(@organisation) + organisation_logs = LettingsLog.visible.filter_by_organisation(@organisation).filter_by_years_or_nil(FormHandler.instance.years_of_available_lettings_forms) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters) @search_term = search_term @@ -134,7 +134,7 @@ class OrganisationsController < ApplicationController end def sales_logs - organisation_logs = SalesLog.visible.filter_by_organisation(@organisation) + organisation_logs = SalesLog.visible.filter_by_organisation(@organisation).filter_by_years_or_nil(FormHandler.instance.years_of_available_sales_forms) unpaginated_filtered_logs = filter_manager.filtered_logs(organisation_logs, search_term, session_filters) respond_to do |format| diff --git a/app/controllers/sales_logs_controller.rb b/app/controllers/sales_logs_controller.rb index d97ed8b2a..25cf405a7 100644 --- a/app/controllers/sales_logs_controller.rb +++ b/app/controllers/sales_logs_controller.rb @@ -15,7 +15,7 @@ class SalesLogsController < LogsController end def index - all_logs = current_user.sales_logs.visible + all_logs = current_user.sales_logs.visible.filter_by_years_or_nil(FormHandler.instance.years_of_available_sales_forms) unpaginated_filtered_logs = filter_manager.filtered_logs(all_logs, search_term, session_filters) @delete_logs_path = delete_logs_sales_logs_path(search: search_term) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index c79d17a6b..383ef0cc1 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -207,6 +207,22 @@ class FormHandler end end + def years_of_available_lettings_forms + years = [] + lettings_forms.each_value do |form| + years << form.start_date.year + end + years + end + + def years_of_available_sales_forms + years = [] + sales_forms.each_value do |form| + years << form.start_date.year + end + years + end + private def get_all_forms diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 8ebf8092f..9b128dd25 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -40,6 +40,13 @@ class LettingsLog < Log belongs_to :managing_organisation, class_name: "Organisation", optional: true scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } + scope :filter_by_years_or_nil, lambda { |years, _user = nil| + first_year = years.shift + query = filter_by_year(first_year) + years.each { |year| query = query.or(filter_by_year(year)) } + query = query.or(where(startdate: nil)) + query.all + } scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") } scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index ed5b2424c..f6086a918 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -36,6 +36,13 @@ class SalesLog < Log belongs_to :managing_organisation, class_name: "Organisation", optional: true scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } + scope :filter_by_years_or_nil, lambda { |years, _user = nil| + first_year = years.shift + query = filter_by_year(first_year) + years.each { |year| query = query.or(filter_by_year(year)) } + query = query.or(where(saledate: nil)) + query.all + } scope :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") } scope :search_by, lambda { |param| filter_by_purchaser_code(param) diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 1b641f452..f2bbbe4be 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2849,6 +2849,34 @@ RSpec.describe LettingsLog do end end + context "when filtering by year or nil" do + before do + Timecop.freeze(Time.utc(2021, 5, 3)) + end + + after do + Timecop.unfreeze + end + + it "allows filtering on a single year or nil" do + lettings_log_1.startdate = nil + lettings_log_1.save!(validate: false) + expect(described_class.filter_by_years_or_nil(%w[2021]).count).to eq(2) + end + + it "allows filtering by multiple years or nil using OR" do + lettings_log_1.startdate = nil + lettings_log_1.save!(validate: false) + expect(described_class.filter_by_years_or_nil(%w[2021 2022]).count).to eq(3) + end + + it "can filter by year(s) AND status" do + lettings_log_2.startdate = nil + lettings_log_2.save!(validate: false) + expect(described_class.filter_by_years_or_nil(%w[2021 2022]).filter_by_status("in_progress").count).to eq(3) + end + end + context "when filtering by organisation" do let(:organisation_1) { create(:organisation) } let(:organisation_2) { create(:organisation) } diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 15f4e6518..edaf4b60a 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -160,6 +160,33 @@ RSpec.describe SalesLog, type: :model do end end + context "when filtering by year or nil" do + before do + Timecop.freeze(Time.utc(2021, 5, 3)) + create(:sales_log, :in_progress, saledate: nil) + create(:sales_log, :in_progress, saledate: Time.zone.local(2021, 4, 1)) + sales_log_3 = create(:sales_log, :in_progress) + sales_log_3.saledate = Time.zone.local(2022, 5, 1) + sales_log_3.save!(validate: false) + end + + after do + Timecop.unfreeze + end + + it "allows filtering on a single year or nil" do + expect(described_class.filter_by_years_or_nil(%w[2021]).count).to eq(2) + end + + it "allows filtering by multiple years or nil using OR" do + expect(described_class.filter_by_years_or_nil(%w[2021 2022]).count).to eq(3) + end + + it "can filter by year(s) AND status" do + expect(described_class.filter_by_years_or_nil(%w[2021 2022]).filter_by_status("in_progress").count).to eq(3) + end + end + context "when filtering duplicate logs" do let(:organisation) { create(:organisation) } let(:log) { create(:sales_log, :duplicate, owning_organisation: organisation) }