From c8859dc8f73e01b1c5ae443fd65da3d210c696d5 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 13 Mar 2024 09:13:38 +0000 Subject: [PATCH] Add ecstat buyer validation --- .../sales/household_validations.rb | 7 +++ config/locales/en.yml | 1 + .../sales/household_validations_spec.rb | 51 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 8c4673c07..2d4c1f615 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -107,6 +107,13 @@ module Validations::Sales::HouseholdValidations end end + def validate_buyer_not_child(record) + return unless record.saledate && record.form.start_year_after_2024? + + record.errors.add "ecstat1", I18n.t("validations.household.ecstat.buyer_cannot_be_child", buyer_index: "1") if person_is_economic_child?(record.ecstat1) + record.errors.add "ecstat2", I18n.t("validations.household.ecstat.buyer_cannot_be_child", buyer_index: "2") if person_is_economic_child?(record.ecstat2) && record.joint_purchase? + end + private def person_is_fulltime_student?(economic_status) diff --git a/config/locales/en.yml b/config/locales/en.yml index 60d95345d..8d9502b23 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -485,6 +485,7 @@ en: retired_female: "Answer cannot be ‘retired’ as the female tenant is under 60" not_child_16_19: cannot_be_student: "Person cannot be a student if they are aged 16-19 but are not a child" + buyer_cannot_be_child: "Buyer %{buyer_index} cannot have a working situation of child under 16" relat: child_under_16_sales: "Answer cannot be ‘partner’ as you told us person %{person_num}'s age is under 16" child_under_16_lettings: "Answer cannot be ‘partner’ as you told us person %{person_num}'s age is under 16" diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 8cefccdb1..86ce01fb0 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -446,4 +446,55 @@ RSpec.describe Validations::Sales::HouseholdValidations do end end end + + describe "#validate_buyer_not_child" do + before do + Timecop.freeze(log_date) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + context "with 2023 logs" do + let(:log_date) { Time.zone.local(2023, 4, 1) } + + it "does not add an error if either buyer is a child" do + record.jointpur = 1 + record.ecstat1 = 9 + record.ecstat2 = 9 + household_validator.validate_buyer_not_child(record) + expect(record.errors["ecstat1"]).to be_empty + expect(record.errors["ecstat2"]).to be_empty + end + end + + context "with 2024 logs" do + let(:log_date) { Time.zone.local(2024, 4, 1) } + + it "validates buyer 1 isn't a child" do + record.ecstat1 = 9 + household_validator.validate_buyer_not_child(record) + expect(record.errors["ecstat1"]) + .to include("Buyer 1 cannot have a working situation of child under 16") + end + + it "validates buyer 2 isn't a child" do + record.jointpur = 1 + record.ecstat2 = 9 + household_validator.validate_buyer_not_child(record) + expect(record.errors["ecstat2"]) + .to include("Buyer 2 cannot have a working situation of child under 16") + end + + it "allows person 2 to be a child" do + record.jointpur = 2 + record.ecstat2 = 9 + household_validator.validate_buyer_not_child(record) + expect(record.errors["ecstat2"]).to be_empty + end + end + end end