Browse Source

CLDC-864 Add exdate validations

pull/1231/head
Jack S 4 years ago
parent
commit
a1a97c730d
  1. 10
      app/models/validations/sales/sale_information_validations.rb
  2. 5
      config/locales/en.yml
  3. 76
      spec/models/validations/sales/sale_information_validations_spec.rb

10
app/models/validations/sales/sale_information_validations.rb

@ -15,6 +15,16 @@ module Validations::Sales::SaleInformationValidations
end
end
def validate_exchange_date(record)
return unless record.exdate && record.saledate
record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_before_saledate")) if record.exdate > record.saledate
return if (record.saledate.to_date - record.exdate.to_date).to_i / 365 < 1
record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")) if record.exdate < record.saledate
end
def validate_previous_property_unit_type(record)
return unless record.fromprop && record.frombeds

5
config/locales/en.yml

@ -413,6 +413,11 @@ en:
deactivation:
during_deactivated_period: "The location is already deactivated during this date, please enter a different date"
sale_information:
exdate:
must_be_before_saledate:
Contract exchange date must be less than 1 year before completion date
must_be_less_than_1_year_from_saledate:
Contract exchange date must be less than 1 year before completion date
previous_property_beds:
property_type_bedsit: "Bedsit bedroom maximum 1"
previous_property_type:

76
spec/models/validations/sales/sale_information_validations_spec.rb

@ -109,6 +109,82 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end
end
describe "#validate_exchange_date" do
context "when exdate blank" do
let(:record) { build(:sales_log, exdate: nil) }
it "does not add an error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).not_to be_present
end
end
context "when saledate blank" do
let(:record) { build(:sales_log, saledate: nil) }
it "does not add an error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).not_to be_present
end
end
context "when saledate and exdate blank" do
let(:record) { build(:sales_log, exdate: nil, saledate: nil) }
it "does not add an error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).not_to be_present
end
end
context "when exdate before saledate" do
let(:record) { build(:sales_log, exdate: 2.months.ago, saledate: 1.month.ago) }
it "does not add the error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).not_to be_present
end
end
context "when exdate more than 1 year before saledate" do
let(:record) { build(:sales_log, exdate: 2.years.ago, saledate: 1.month.ago) }
it "does not add the error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).to eq(
["Contract exchange date must be less than 1 year before completion date"],
)
end
end
context "when exdate after saledate" do
let(:record) { build(:sales_log, exdate: 1.month.ago, saledate: 2.months.ago) }
it "adds error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).to eq(
["Contract exchange date must be less than 1 year before completion date"],
)
end
end
context "when exdate == saledate" do
let(:record) { build(:sales_log, exdate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) }
it "does not add an error" do
sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).not_to be_present
end
end
end
describe "#validate_previous_property_unit_type" do
context "when number of bedrooms is <= 1" do
let(:record) { FactoryBot.build(:sales_log, frombeds: 1, fromprop: 2) }

Loading…
Cancel
Save