From d67e4710c8ff85b3f52c398062ff5573f6d6581e Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Thu, 26 Jan 2023 12:21:12 +0000 Subject: [PATCH] CLDC-861 Add length of stay validations (#1218) * faet: add validation and tests * refactor: linting * feat: respond to PR comments --- .../sales/sale_information_validations.rb | 15 +++- config/locales/en.yml | 3 + .../sale_information_validations_spec.rb | 68 +++++++++++++++++-- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index c70a8d57d..be0295f2c 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -1,5 +1,5 @@ module Validations::Sales::SaleInformationValidations - def validate_pratical_completion_date_before_saledate(record) + def validate_practical_completion_date_before_saledate(record) return if record.saledate.blank? || record.hodate.blank? unless record.saledate > record.hodate @@ -7,6 +7,19 @@ module Validations::Sales::SaleInformationValidations end end + def validate_years_living_in_property_before_purchase(record) + return unless record.proplen && record.proplen.nonzero? + + case record.type + when 18 + record.errors.add :type, I18n.t("validations.sale_information.proplen.social_homebuy") + record.errors.add :proplen, I18n.t("validations.sale_information.proplen.social_homebuy") + when 28, 29 + record.errors.add :type, I18n.t("validations.sale_information.proplen.rent_to_buy") + record.errors.add :proplen, I18n.t("validations.sale_information.proplen.rent_to_buy") + end + end + def validate_exchange_date(record) return unless record.exdate && record.saledate diff --git a/config/locales/en.yml b/config/locales/en.yml index cdad12a52..c988d2dd9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -413,6 +413,9 @@ en: deactivation: during_deactivated_period: "The location is already deactivated during this date, please enter a different date" sale_information: + proplen: + social_homebuy: "Social HomeBuy buyers should not have lived here before" + rent_to_buy: "Rent to Buy buyers should not have lived here before" exdate: must_be_before_saledate: Contract exchange date must be less than 1 year before completion date diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 409972040..fb3aea06c 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -5,12 +5,12 @@ RSpec.describe Validations::Sales::SaleInformationValidations do let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } } - describe "#validate_pratical_completion_date_before_saledate" do + describe "#validate_practical_completion_date_before_saledate" do context "when hodate blank" do let(:record) { build(:sales_log, hodate: nil) } it "does not add an error" do - sale_information_validator.validate_pratical_completion_date_before_saledate(record) + sale_information_validator.validate_practical_completion_date_before_saledate(record) expect(record.errors).not_to be_present end @@ -20,7 +20,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do let(:record) { build(:sales_log, saledate: nil) } it "does not add an error" do - sale_information_validator.validate_pratical_completion_date_before_saledate(record) + sale_information_validator.validate_practical_completion_date_before_saledate(record) expect(record.errors).not_to be_present end @@ -30,7 +30,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do let(:record) { build(:sales_log, hodate: nil, saledate: nil) } it "does not add an error" do - sale_information_validator.validate_pratical_completion_date_before_saledate(record) + sale_information_validator.validate_practical_completion_date_before_saledate(record) expect(record.errors).not_to be_present end @@ -40,7 +40,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do let(:record) { build(:sales_log, hodate: 2.months.ago, saledate: 1.month.ago) } it "does not add the error" do - sale_information_validator.validate_pratical_completion_date_before_saledate(record) + sale_information_validator.validate_practical_completion_date_before_saledate(record) expect(record.errors).not_to be_present end @@ -50,7 +50,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) } it "adds error" do - sale_information_validator.validate_pratical_completion_date_before_saledate(record) + sale_information_validator.validate_practical_completion_date_before_saledate(record) expect(record.errors[:hodate]).to be_present end @@ -60,7 +60,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do let(:record) { build(:sales_log, hodate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } it "does not add an error" do - sale_information_validator.validate_pratical_completion_date_before_saledate(record) + sale_information_validator.validate_practical_completion_date_before_saledate(record) expect(record.errors[:hodate]).to be_present end @@ -164,4 +164,58 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_years_living_in_property_before_purchase" do + context "when proplen blank" do + let(:record) { build(:sales_log, proplen: nil) } + + it "does not add an error" do + sale_information_validator.validate_years_living_in_property_before_purchase(record) + + expect(record.errors).not_to be_present + end + end + + context "when type blank" do + let(:record) { build(:sales_log, type: nil) } + + it "does not add an error" do + sale_information_validator.validate_years_living_in_property_before_purchase(record) + + expect(record.errors).not_to be_present + end + end + + context "when proplen 0" do + let(:record) { build(:sales_log, proplen: 0) } + + it "does not add an error" do + sale_information_validator.validate_years_living_in_property_before_purchase(record) + + expect(record.errors).not_to be_present + end + end + + context "when type Rent to Buy and proplen > 0" do + let(:record) { build(:sales_log, proplen: 1, type: 28) } + + it "adds an error" do + sale_information_validator.validate_years_living_in_property_before_purchase(record) + + expect(record.errors[:type]).to include(I18n.t("validations.sale_information.proplen.rent_to_buy")) + expect(record.errors[:proplen]).to include(I18n.t("validations.sale_information.proplen.rent_to_buy")) + end + end + + context "when type Social HomeBuy and proplen > 0" do + let(:record) { build(:sales_log, proplen: 1, type: 18) } + + it "adds an error" do + sale_information_validator.validate_years_living_in_property_before_purchase(record) + + expect(record.errors[:type]).to include(I18n.t("validations.sale_information.proplen.social_homebuy")) + expect(record.errors[:proplen]).to include(I18n.t("validations.sale_information.proplen.social_homebuy")) + end + end + end end