diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index daea6f060..fa161ce80 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -221,4 +221,14 @@ class SalesLog < Log def shared_owhership_scheme? ownershipsch == 1 end + + def buyers_age_for_old_persons_shared_ownership_invalid? + return unless old_persons_shared_ownership? + + (joint_purchase? && ages_unknown_or_under_64?([1, 2])) || (not_joint_purchase? && ages_unknown_or_under_64?([1])) + end + + def ages_unknown_or_under_64?(person_indexes) + person_indexes.all? { |person_num| self["age#{person_num}"].present? && self["age#{person_num}"] < 64 || self["age#{person_num}_known"] == 1 } + end end diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 8c03d9db9..4f2290268 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -18,20 +18,6 @@ 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 - def validate_previous_postcode(record) return unless record.postcode_full && record.ppostcode_full && record.discounted_ownership_sale? @@ -116,8 +102,4 @@ 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/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 5b227c36d..26d369a35 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -164,121 +164,6 @@ 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 describe "previous postcode validations" do diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 93ed01a20..9ca9436fb 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -237,4 +237,78 @@ RSpec.describe Validations::SoftValidations do end end end + + describe "old persons shared ownership soft validations" do + 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 "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + 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 "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + 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 "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + 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 "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + 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 "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + 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 "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + 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 "returns false" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be false + 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 "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + 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 "returns true" do + expect(record.buyers_age_for_old_persons_shared_ownership_invalid?).to be true + end + end + end end