From 3308d86a106b2bde9fe1dae41af687b98c46526d Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 24 Jan 2023 15:21:09 +0000 Subject: [PATCH] Last settled accommodation and discounted ownership property postcodes must match (#1213) --- .../sales/household_validations.rb | 9 +++ config/locales/en.yml | 4 +- .../sales/household_validations_spec.rb | 58 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 8ee00fdfd..42846273c 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -32,6 +32,15 @@ module Validations::Sales::HouseholdValidations end end + def validate_previous_postcode(record) + return unless record.postcode_full && record.ppostcode_full && record.discounted_ownership_sale? + + unless record.postcode_full == record.ppostcode_full + record.errors.add :postcode_full, I18n.t("validations.household.postcode.discounted_ownership") + record.errors.add :ppostcode_full, I18n.t("validations.household.postcode.discounted_ownership") + end + end + private def validate_person_age_matches_relationship(record, person_num) diff --git a/config/locales/en.yml b/config/locales/en.yml index d84429637..68c7199de 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -369,7 +369,9 @@ en: condition_effects: no_choices: "You cannot answer this question as you told us nobody in the household has a physical or mental health condition (or other illness) expected to last 12 months or more" old_persons_shared_ownership: "Are you sure? At least one buyer should be aged over 64 for Older persons‘ shared ownership scheme" - + postcode: + discounted_ownership: "Last settled accommodation and discounted ownership property postcodes must match" + tenancy: length: fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term" diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 61a204878..5b227c36d 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -280,4 +280,62 @@ RSpec.describe Validations::Sales::HouseholdValidations do end end end + + describe "previous postcode validations" do + let(:record) { build(:sales_log) } + + context "with a discounted sale" do + before do + record.ownershipsch = 2 + end + + it "adds an error when previous and current postcodes are not the same" do + record.postcode_full = "SO32 3PT" + record.ppostcode_full = "DN6 7FB" + household_validator.validate_previous_postcode(record) + expect(record.errors["postcode_full"]) + .to include(match I18n.t("validations.household.postcode.discounted_ownership")) + expect(record.errors["ppostcode_full"]) + .to include(match I18n.t("validations.household.postcode.discounted_ownership")) + end + + it "allows same postcodes" do + record.postcode_full = "SO32 3PT" + record.ppostcode_full = "SO32 3PT" + household_validator.validate_previous_postcode(record) + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["ppostcode_full"]).to be_empty + end + + it "does not add an error when postcode is missing" do + record.postcode_full = nil + record.ppostcode_full = "SO32 3PT" + household_validator.validate_previous_postcode(record) + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["ppostcode_full"]).to be_empty + end + + it "does not add an error when previous postcode is missing" do + record.postcode_full = "SO32 3PT" + record.ppostcode_full = nil + household_validator.validate_previous_postcode(record) + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["ppostcode_full"]).to be_empty + end + end + + context "without a discounted sale" do + before do + record.ownershipsch = 1 + end + + it "allows different postcodes" do + record.postcode_full = "SO32 3PT" + record.ppostcode_full = "DN6 7FB" + household_validator.validate_previous_postcode(record) + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["ppostcode_full"]).to be_empty + end + end + end end