Browse Source

Add duplicate_logs scope to sales

pull/1762/head
Kat 3 years ago
parent
commit
cd03e70477
  1. 2
      app/models/lettings_log.rb
  2. 8
      app/models/sales_log.rb
  3. 12
      spec/factories/sales_log.rb
  4. 91
      spec/models/sales_log_spec.rb

2
app/models/lettings_log.rb

@ -58,7 +58,7 @@ class LettingsLog < Log
where(log.slice(*attrs_to_check)) where(log.slice(*attrs_to_check))
.where.not(id: log.id) .where.not(id: log.id)
.where.not(status: "deleted") .where.not(status: "deleted")
.where.not("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.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) .where("location_id = ? OR needstype = 1", log.location_id)
} }
scope :duplicate_logs_for_organisation, ->(org, log) { filter_by_organisation(org).duplicate_logs(log) } scope :duplicate_logs_for_organisation, ->(org, log) { filter_by_organisation(org).duplicate_logs(log) }

8
app/models/sales_log.rb

@ -41,6 +41,14 @@ class SalesLog < Log
.or(filter_by_id(param)) .or(filter_by_id(param))
} }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) }
scope :duplicate_logs, lambda { |log|
attrs_to_check = %w[purchid saledate age1 sex1 ecstat1 postcode_full]
where(log.slice(*attrs_to_check))
.where.not(id: log.id)
.where.not(status: "deleted")
.where.not("saledate is NULL OR age1 IS NULL OR sex1 IS NULL OR ecstat1 IS NULL OR postcode_full IS NULL")
}
scope :duplicate_logs_for_organisation, ->(org, log) { filter_by_organisation(org).duplicate_logs(log) }
OPTIONAL_FIELDS = %w[purchid othtype].freeze OPTIONAL_FIELDS = %w[purchid othtype].freeze
RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze RETIREMENT_AGES = { "M" => 65, "F" => 60, "X" => 65 }.freeze

12
spec/factories/sales_log.rb

@ -35,6 +35,18 @@ FactoryBot.define do
buylivein { 1 } buylivein { 1 }
jointpur { 2 } jointpur { 2 }
end end
trait :duplicate do
purchid { "PC123" }
ownershipsch { 2 }
type { 8 }
jointpur { 2 }
saledate { Time.zone.today }
age1 { 20 }
sex1 { "F" }
ecstat1 { 1 }
postcode_full { "A1 1AA" }
privacynotice { 1 }
end
trait :completed do trait :completed do
purchid { rand(999_999_999).to_s } purchid { rand(999_999_999).to_s }
ownershipsch { 2 } ownershipsch { 2 }

91
spec/models/sales_log_spec.rb

@ -160,6 +160,97 @@ RSpec.describe SalesLog, type: :model do
end end
end end
context "when filtering duplicate logs" do
let(:log) { create(:sales_log, :duplicate) }
let!(:duplicate_log) { create(:sales_log, :duplicate) }
it "returns all duplicate logs for given log" do
expect(described_class.duplicate_logs(log).count).to eq(1)
end
it "returns duplicate log" do
expect(described_class.duplicate_logs(log)).to include(duplicate_log)
end
it "does not return the given log" do
expect(described_class.duplicate_logs(log)).not_to include(log)
end
context "when there is a deleted duplicate log" do
let!(:deleted_duplicate_log) { create(:sales_log, :duplicate, discarded_at: Time.zone.now) }
it "does not return the deleted log as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(deleted_duplicate_log)
end
end
context "when there is a log with a different sale date" do
let!(:different_sale_date_log) { create(:sales_log, :duplicate, saledate: Time.zone.tomorrow) }
it "does not return a log with a different sale date as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(different_sale_date_log)
end
end
context "when there is a log with a different age1" do
let!(:different_age1) { create(:sales_log, :duplicate, age1: 50) }
it "does not return a log with a different age1 as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(different_age1)
end
end
context "when there is a log with a different sex1" do
let!(:different_sex1) { create(:sales_log, :duplicate, sex1: "M") }
it "does not return a log with a different sex1 as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(different_sex1)
end
end
context "when there is a log with a different ecstat1" do
let!(:different_ecstat1) { create(:sales_log, :duplicate, ecstat1: 0) }
it "does not return a log with a different ecstat1 as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(different_ecstat1)
end
end
context "when there is a log with a different purchid" do
let!(:different_purchid) { create(:sales_log, :duplicate, purchid: "different") }
it "does not return a log with a different purchid as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(different_purchid)
end
end
context "when there is a log with a different postcode_full" do
let!(:different_postcode_full) { create(:sales_log, :duplicate, postcode_full: "B1 1AA") }
it "does not return a log with a different postcode_full as a duplicate" do
expect(described_class.duplicate_logs(log)).not_to include(different_postcode_full)
end
end
context "when there is a log with nil values for duplicate check fields" do
let!(:duplicate_check_fields_not_given) { create(:sales_log, :duplicate, age1: nil, sex1: nil, ecstat1: nil, pcodenk: 1, postcode_full: nil) }
it "does not return a log with nil values as a duplicate" do
log.update!(age1: nil, sex1: nil, ecstat1: nil, pcodenk: 1, postcode_full: nil)
expect(described_class.duplicate_logs(log)).not_to include(duplicate_check_fields_not_given)
end
end
context "when there is a log with nil values for purchid" do
let!(:purchid_not_given) { create(:sales_log, :duplicate, purchid: nil) }
it "returns the log as a duplicate if purchid is nil" do
log.update!(purchid: nil)
expect(described_class.duplicate_logs(log)).to include(purchid_not_given)
end
end
end
describe "derived variables" do describe "derived variables" do
let(:sales_log) { create(:sales_log, :completed) } let(:sales_log) { create(:sales_log, :completed) }

Loading…
Cancel
Save