diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 124140b06..590498c3a 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -44,23 +44,23 @@ class LettingsLog < Log 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(' ')}%") } scope :search_by, lambda { |param| - filter_by_location_postcode(param) - .or(filter_by_tenant_code(param)) - .or(filter_by_propcode(param)) - .or(filter_by_postcode(param)) - .or(filter_by_id(param)) - } + filter_by_location_postcode(param) + .or(filter_by_tenant_code(param)) + .or(filter_by_propcode(param)) + .or(filter_by_postcode(param)) + .or(filter_by_id(param)) + } 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 :duplicate_logs, lambda { |log| - attrs_to_check = %w[tenancycode propcode startdate age1 sex1 ecstat1 tcharge postcode_full] - where(log.slice(*attrs_to_check)) - .where.not(id: log.id) - .where.not(status: "deleted") - .where.not("startdate IS NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR tcharge IS NULL OR postcode_full IS NULL OR propcode IS NULL OR needstype IS NULL") - .where("location_id = ? OR needstype = 1", log.location_id) - } + attrs_to_check = %w[tenancycode propcode startdate age1 sex1 ecstat1 tcharge postcode_full] + where(log.slice(*attrs_to_check)) + .where.not(id: log.id) + .where.not(status: "deleted") + .where.not("startdate IS NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR tcharge IS NULL OR postcode_full IS NULL OR propcode IS NULL OR needstype IS NULL") + .where("location_id = ? OR needstype = 1", log.location_id) + } scope :duplicate_logs_for_organisation, ->(org, log) { filter_by_organisation(org).duplicate_logs(log) } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index b5b5e99cb..3299bce59 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2921,6 +2921,24 @@ RSpec.describe LettingsLog do end end end + + context "when filtering duplicate logs for organisation" do + let(:organisation) { create(:organisation) } + let(:log) { create(:lettings_log, :duplicate, owning_organisation: organisation) } + let!(:duplicate_log_same_owning_org) { create(:lettings_log, :duplicate, owning_organisation: organisation) } + let!(:duplicate_log_same_managing_org) { create(:lettings_log, :duplicate, managing_organisation: organisation) } + let!(:duplicate_log_different_org) { create(:lettings_log, :duplicate) } + + it "returns all duplicate logs for given log" do + expect(described_class.duplicate_logs_for_organisation(organisation, log).count).to eq(2) + end + + it "returns duplicate log" do + expect(described_class.duplicate_logs_for_organisation(organisation, log)).to include(duplicate_log_same_owning_org) + expect(described_class.duplicate_logs_for_organisation(organisation, log)).to include(duplicate_log_same_managing_org) + expect(described_class.duplicate_logs_for_organisation(organisation, log)).not_to include(duplicate_log_different_org) + end + end end describe "#retirement_age_for_person" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index c382c364b..39b77ed03 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -251,6 +251,22 @@ RSpec.describe SalesLog, type: :model do end end + context "when filtering duplicate logs for organisation" do + let(:organisation) { create(:organisation) } + let(:log) { create(:sales_log, :duplicate, owning_organisation: organisation) } + let!(:duplicate_log_same_owning_org) { create(:sales_log, :duplicate, owning_organisation: organisation) } + let!(:duplicate_log_different_org) { create(:sales_log, :duplicate) } + + it "returns all duplicate logs for given log" do + expect(described_class.duplicate_logs_for_organisation(organisation, log).count).to eq(1) + end + + it "returns duplicate log" do + expect(described_class.duplicate_logs_for_organisation(organisation, log)).to include(duplicate_log_same_owning_org) + expect(described_class.duplicate_logs_for_organisation(organisation, log)).not_to include(duplicate_log_different_org) + end + end + describe "derived variables" do let(:sales_log) { create(:sales_log, :completed) }