diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index d93a173f9..e6df9e54a 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -54,11 +54,17 @@ class LettingsLog < Log 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| - visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) - .where.not(id: log.id) - .where.not("startdate IS NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR tcharge IS NULL OR needstype IS NULL") - .where("location_id = ? OR needstype = 1", log.location_id) - .where("postcode_full = ? OR needstype = 2", log.postcode_full) + visible + .where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) + .where.not(id: log.id) + .where.not(startdate: nil) + .where.not(sex1: nil) + .where.not(ecstat1: nil) + .where.not(tcharge: nil) + .where.not(needstype: nil) + .where("age1 IS NOT NULL OR age1_known = 1") + .where("location_id = ? OR needstype = 1", log.location_id) + .where("postcode_full = ? OR needstype = 2", log.postcode_full) } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze @@ -68,7 +74,7 @@ class LettingsLog < Log NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 1 => 52, 10 => 53 }.freeze SUFFIX_FROM_PERIOD = { 2 => "every 2 weeks", 3 => "every 4 weeks", 4 => "every month" }.freeze RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze - DUPLICATE_LOG_ATTRIBUTES = %w[tenancycode startdate age1 sex1 ecstat1 tcharge].freeze + DUPLICATE_LOG_ATTRIBUTES = %w[tenancycode startdate age1_known age1 sex1 ecstat1 tcharge].freeze def form FormHandler.instance.get_form(form_name) || FormHandler.instance.current_lettings_form diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 311ecc565..ae76e32c3 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -44,12 +44,16 @@ class SalesLog < Log scope :duplicate_logs, lambda { |log| visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where.not(id: log.id) - .where.not("saledate is NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR postcode_full IS NULL") + .where.not(saledate: nil) + .where.not(sex1: nil) + .where.not(ecstat1: nil) + .where.not(postcode_full: nil) + .where("age1 IS NOT NULL OR age1_known = 1 OR age1_known = 2") } OPTIONAL_FIELDS = %w[purchid othtype].freeze RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze - DUPLICATE_LOG_ATTRIBUTES = %w[purchid saledate age1 sex1 ecstat1 postcode_full].freeze + DUPLICATE_LOG_ATTRIBUTES = %w[purchid saledate age1_known age1 sex1 ecstat1 postcode_full].freeze def lettings? false diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 4912ebbe6..edc50bb10 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2896,6 +2896,15 @@ RSpec.describe LettingsLog do end end + context "when there is a log with age1 not known" do + let!(:age1_not_known) { create(:lettings_log, :duplicate, age1_known: 1, age1: nil) } + + it "returns the log as a duplicate if age1 is not known" do + log.update!(age1_known: 1, age1: nil) + expect(described_class.duplicate_logs(log)).to include(age1_not_known) + end + end + context "when there is a duplicate supported housing log" do let(:scheme) { create(:scheme) } let(:location) { create(:location, scheme:) } diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index c382c364b..d032293c8 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -249,6 +249,33 @@ RSpec.describe SalesLog, type: :model do expect(described_class.duplicate_logs(log)).to include(purchid_not_given) end end + + context "when there is a log age not known" do + let!(:age1_not_known) { create(:sales_log, :duplicate, age1_known: 1, age1: nil) } + + it "returns the log as a duplicate if age is not known" do + log.update!(age1_known: 1, age1: nil) + expect(described_class.duplicate_logs(log)).to include(age1_not_known) + end + end + + context "when there is a log age pefers not to say" do + let!(:age1_prefers_not_to_say) { create(:sales_log, :duplicate, age1_known: 2, age1: nil) } + + it "returns the log as a duplicate if age is prefers not to say" do + log.update!(age1_known: 2, age1: nil) + expect(described_class.duplicate_logs(log)).to include(age1_prefers_not_to_say) + end + end + + context "when there is a log age pefers not to say and not known" do + let!(:age1_prefers_not_to_say) { create(:sales_log, :duplicate, age1_known: 2, age1: nil) } + + it "does not return the log as a duplicate if age is prefers not to say" do + log.update!(age1_known: 1, age1: nil) + expect(described_class.duplicate_logs(log)).not_to include(age1_prefers_not_to_say) + end + end end describe "derived variables" do