From 4c58e69af2d5c6209dca8343822d0bbe73ad87ac Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 18 Jan 2023 10:13:05 +0000 Subject: [PATCH] Add old_persons_shared_ownership validation --- app/models/sales_log.rb | 14 ++- .../sales/household_validations.rb | 18 +++ config/locales/en.yml | 3 +- .../sales/household_validations_spec.rb | 115 ++++++++++++++++++ 4 files changed, 148 insertions(+), 2 deletions(-) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 613d79f37..45bd9f729 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -183,11 +183,23 @@ class SalesLog < Log self.postcode_full = upcase_and_remove_whitespace(postcode_full) process_postcode(postcode_full, "pcodenk", "is_la_inferred", "la") end - + def retirement_age_for_person(person_num) gender = public_send("sex#{person_num}".to_sym) return unless gender RETIREMENT_AGES[gender] end + + def joint_purchase? + jointpur == 1 + end + + def not_joint_purchase? + jointpur == 2 + end + + def old_persons_shared_ownership? + type == 24 + end end diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 6e16a0476..84a19849a 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -18,6 +18,20 @@ module Validations::Sales::HouseholdValidations shared_validate_partner_count(record, 6) end + def validate_buyers_age_for_old_persons_shared_ownership(record) + if record.old_persons_shared_ownership? + if record.joint_purchase? && ages_unknown_or_under_64?(record, [1, 2]) + record.errors.add :age1, I18n.t("validations.household.old_persons_shared_ownership") + record.errors.add :age2, I18n.t("validations.household.old_persons_shared_ownership") + record.errors.add :type, I18n.t("validations.household.old_persons_shared_ownership") + end + if record.not_joint_purchase? && ages_unknown_or_under_64?(record, [1]) + record.errors.add :age1, I18n.t("validations.household.old_persons_shared_ownership") + record.errors.add :type, I18n.t("validations.household.old_persons_shared_ownership") + end + end + end + private def validate_person_age_matches_relationship(record, person_num) @@ -97,4 +111,8 @@ private def tenant_is_economic_child?(economic_status) economic_status == 9 end + + def ages_unknown_or_under_64?(record, person_indexes) + person_indexes.all? { |person_num| record["age#{person_num}"].present? && record["age#{person_num}"] < 64 || record["age#{person_num}_known"] == 1 } + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 73bd8bdf7..ac6a2fa7f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -367,7 +367,8 @@ en: not_internal_transfer: "Answer cannot be ‘permanently decanted from another property owned by this landlord’ as you told us the source of referral for this tenancy was not an internal transfer" 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" + 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 bdfb01ea4..61a204878 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -164,5 +164,120 @@ RSpec.describe Validations::Sales::HouseholdValidations do expect(record.errors["ecstat2"]) .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.child_not_16_19")) end + + context "when it is a joint purchase and both buyers are over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 66, type: 24) } + + it "does not add an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors).not_to be_present + end + end + + context "when it is a joint purchase and first buyer is over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 65, age2: 40, type: 24) } + + it "does not add an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors).not_to be_present + end + end + + context "when it is a joint purchase and second buyer is over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 64, type: 24) } + + it "does not add an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors).not_to be_present + end + end + + context "when it is a joint purchase and neither of the buyers are over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2: 33, type: 24) } + + it "adds an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors["age1"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["age2"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["type"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + end + end + + context "when it is a joint purchase and first buyer is under 64 and the second buyers' age is unknown" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1: 43, age2_known: 1, type: 24) } + + it "adds an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors["age1"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["age2"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["type"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + end + end + + context "when it is a joint purchase and neither of the buyers ages are known" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 1, age1_known: 1, age2_known: 1, type: 24) } + + it "adds an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors["age1"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["age2"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["type"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + end + end + + context "when it is not a joint purchase and the buyer is over 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 70, type: 24) } + + it "does not add an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors).not_to be_present + end + end + + context "when it is not a joint purchase and the buyer is under 64" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1: 20, type: 24) } + + it "adds an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors["age1"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["age2"]) + .to be_empty + expect(record.errors["type"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + end + end + + context "when it is not a joint purchase and the buyers age is not known" do + let(:record) { FactoryBot.build(:sales_log, jointpur: 2, age1_known: 1, type: 24) } + + it "adds an error" do + household_validator.validate_buyers_age_for_old_persons_shared_ownership(record) + + expect(record.errors["age1"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + expect(record.errors["age2"]) + .to be_empty + expect(record.errors["type"]) + .to include(match I18n.t("validations.household.old_persons_shared_ownership")) + end + end end end