From d2afa469465321daac6c9d2b5b5511111ea2a53a Mon Sep 17 00:00:00 2001 From: Arthur Campbell <51094020+arfacamble@users.noreply.github.com> Date: Mon, 23 Jan 2023 14:21:46 +0000 Subject: [PATCH] CLDC-1461 add validation ensure postcodes match if discounted ownership purchase (#1200) * new property validation file for sales logs with postcode validation regarding discounted ownership, tests also * linting problem --- Gemfile.lock | 1 + app/models/sales_log.rb | 6 +++ .../validations/sales/property_validations.rb | 10 ++++ config/locales/en.yml | 2 + .../sales/property_validations_spec.rb | 51 +++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 app/models/validations/sales/property_validations.rb create mode 100644 spec/models/validations/sales/property_validations_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 8ba27ec01..cb46ee64a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -426,6 +426,7 @@ GEM PLATFORMS arm64-darwin-21 + arm64-darwin-22 x86_64-darwin-19 x86_64-darwin-20 x86_64-darwin-21 diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 10999cbe4..cdf2964f1 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -1,5 +1,7 @@ class SalesLogValidator < ActiveModel::Validator include Validations::Sales::HouseholdValidations + include Validations::Sales::PropertyValidations + include Validations::SharedValidations include Validations::Sales::FinancialValidations include Validations::Sales::SaleInformationValidations include Validations::SharedValidations @@ -167,6 +169,10 @@ class SalesLog < Log ownershipsch == 3 end + def discounted_ownership_sale? + ownershipsch == 2 + end + def mortgage_not_used? mortgageused == 2 end diff --git a/app/models/validations/sales/property_validations.rb b/app/models/validations/sales/property_validations.rb new file mode 100644 index 000000000..879e37ff1 --- /dev/null +++ b/app/models/validations/sales/property_validations.rb @@ -0,0 +1,10 @@ +module Validations::Sales::PropertyValidations + def validate_postcodes_match_if_discounted_ownership(record) + return unless record.ppostcode_full.present? && record.postcode_full.present? + + if record.discounted_ownership_sale? && record.ppostcode_full != record.postcode_full + record.errors.add :postcode_full, I18n.t("validations.property.postcode.must_match_previous") + record.errors.add :ppostcode_full, I18n.t("validations.property.postcode.must_match_previous") + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 446e1343e..fbfd0c8e0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -195,6 +195,8 @@ en: beds: non_positive: "Number of bedrooms has to be greater than 0" over_max: "Number of bedrooms cannot be more than 12" + postcode: + must_match_previous: "Buyer's last accommodation and discounted ownership postcodes must match" financial: tshortfall: diff --git a/spec/models/validations/sales/property_validations_spec.rb b/spec/models/validations/sales/property_validations_spec.rb new file mode 100644 index 000000000..fc3fdd1a8 --- /dev/null +++ b/spec/models/validations/sales/property_validations_spec.rb @@ -0,0 +1,51 @@ +require "rails_helper" + +RSpec.describe Validations::Sales::PropertyValidations do + subject(:property_validator) { property_validator_class.new } + + let(:property_validator_class) { Class.new { include Validations::Sales::PropertyValidations } } + + describe "#validate_postcodes_match_if_discounted_ownership" do + context "when ownership scheme is not discounted ownership" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 1) } + + it "when postcodes match no error is added" do + record.postcode_full = "SW1A 1AA" + record.ppostcode_full = "SW1A 1AA" + + property_validator.validate_postcodes_match_if_discounted_ownership(record) + expect(record.errors["postcode_full"]).to be_empty + end + end + + context "when ownership scheme is discounted ownership" do + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 2) } + + it "when ppostcode_full is not present no error is added" do + record.postcode_full = "SW1A 1AA" + property_validator.validate_postcodes_match_if_discounted_ownership(record) + expect(record.errors["postcode_full"]).to be_empty + end + + it "when postcode_full is not present no error is added" do + record.ppostcode_full = "SW1A 1AA" + property_validator.validate_postcodes_match_if_discounted_ownership(record) + expect(record.errors["postcode_full"]).to be_empty + end + + it "when postcodes match no error is added" do + record.postcode_full = "SW1A 1AA" + record.ppostcode_full = "SW1A 1AA" + property_validator.validate_postcodes_match_if_discounted_ownership(record) + expect(record.errors["postcode_full"]).to be_empty + end + + it "when postcodes do not match an error is added" do + record.postcode_full = "SW1A 1AA" + record.ppostcode_full = "SW1A 0AA" + property_validator.validate_postcodes_match_if_discounted_ownership(record) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.property.postcode.must_match_previous")) + end + end + end +end