From 9d6d904bba9d959b5c10e1c66685373e53e5c28a Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 26 Jan 2023 11:01:28 +0000 Subject: [PATCH] feat: merge with main, improve date behaviour --- .../sales/sale_information_validations.rb | 12 ++++++++---- config/locales/en.yml | 14 +++++--------- .../sales/sale_information_validations_spec.rb | 8 +++++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index c70a8d57d..77523d555 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -10,11 +10,15 @@ module Validations::Sales::SaleInformationValidations 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 + if record.exdate > record.saledate + record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_before_saledate")) + record.errors.add(:saledate, I18n.t("validations.sale_information.saledate.must_be_after_exdate")) + end - record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")) + if record.saledate.to_date - record.exdate.to_date > 1.year.in_days + record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")) + record.errors.add(:saledate, I18n.t("validations.sale_information.saledate.must_be_less_than_1_year_from_exdate")) + end end def validate_previous_property_unit_type(record) diff --git a/config/locales/en.yml b/config/locales/en.yml index fc398232f..c90ad0ce8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -416,19 +416,15 @@ en: 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 + 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" + saledate: + must_be_after_exdate: "Completion date must be less than 1 year after contract exchange date" + must_be_less_than_1_year_from_exdate: "Completion date must be less than 1 year after contract exchange date" previous_property_beds: property_type_bedsit: "Bedsit bedroom maximum 1" previous_property_type: property_type_bedsit: "A bedsit can not have more than 1 bedroom" - completion_exchange: - exchange_before_completion: "Exchange date must be before completion date" - completion_after_exchange: "Completion date must be after exchange date" - exchange_after_one_year_before_completion: "Exchange date must be less than 1 year before completion date" - completion_before_one_year_after_exchange: "Completion date must be less than 1 year after exchange date" soft_validations: net_income: title_text: "Net income is outside the expected range based on the lead tenant’s working situation" diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 409972040..c40a631a9 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -111,12 +111,15 @@ RSpec.describe Validations::Sales::SaleInformationValidations do 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 + 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"], ) + expect(record.errors[:saledate]).to eq( + ["Completion date must be less than 1 year after contract exchange date"], + ) end end @@ -129,6 +132,9 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors[:exdate]).to eq( ["Contract exchange date must be less than 1 year before completion date"], ) + expect(record.errors[:saledate]).to eq( + ["Completion date must be less than 1 year after contract exchange date"], + ) end end