From 44abe22ea912000beec6662f30af30e2ef226880 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 5 Feb 2024 14:47:26 +0000 Subject: [PATCH] CLDC-3183 Validate previous tenure for discounted sales (#2206) * Validate previous tenure for discounted sales * typo --- .../sales/household_validations.rb | 10 +++ config/locales/en.yml | 1 + .../sales/household_validations_spec.rb | 75 +++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index c597cb225..6e10b89b5 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -30,6 +30,16 @@ module Validations::Sales::HouseholdValidations end end + def validate_buyer1_previous_tenure(record) + return unless record.saledate && record.form.start_year_after_2024? + return unless record.discounted_ownership_sale? && record.prevten + + if [3, 4, 5, 6, 7, 9, 0].include?(record.prevten) + record.errors.add :prevten, I18n.t("validations.household.prevten.invalid_for_discounted_sale") + record.errors.add :ownershipsch, I18n.t("validations.household.prevten.invalid_for_discounted_sale") + 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 e04d9465b..8d23821a4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -503,6 +503,7 @@ en: internal_transfer: "Answer cannot be %{prevten} as this tenancy is an internal transfer" la_general_needs: internal_transfer: "Answer cannot be a fixed-term or lifetime local authority general needs tenancy as it’s an internal transfer and a private registered provider is on the tenancy agreement" + invalid_for_discounted_sale: "Buyer 1’s previous tenure should be “local authority tenant” or “private registered provider or housing association tenant” for discounted sales" referral: secure_tenancy: "Answer must be internal transfer as this is a secure tenancy" rsnvac_non_temp: "Answer cannot be this source of referral as this is a re-let to tenant who occupied the same property as temporary accommodation" diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 21d205e03..898a47fcc 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -268,4 +268,79 @@ RSpec.describe Validations::Sales::HouseholdValidations do end end end + + describe "#validate_buyer1_previous_tenure" do + let(:record) { build(:sales_log) } + + let(:now) { Time.zone.local(2024, 4, 4) } + + before do + Timecop.freeze(now) + Singleton.__init__(FormHandler) + record.ownershipsch = 2 + record.saledate = now + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + it "adds an error when previous tenure is not valid" do + [3, 4, 5, 6, 7, 9, 0].each do |prevten| + record.prevten = prevten + household_validator.validate_buyer1_previous_tenure(record) + expect(record.errors["prevten"]).to include("Buyer 1’s previous tenure should be “local authority tenant” or “private registered provider or housing association tenant” for discounted sales") + expect(record.errors["ownershipsch"]).to include("Buyer 1’s previous tenure should be “local authority tenant” or “private registered provider or housing association tenant” for discounted sales") + end + end + + it "does not add an error when previous tenure is allowed" do + [1, 2].each do |prevten| + record.prevten = prevten + household_validator.validate_buyer1_previous_tenure(record) + expect(record.errors).to be_empty + end + end + + it "does not add an error if previous tenure is not given" do + record.prevten = nil + household_validator.validate_buyer1_previous_tenure(record) + expect(record.errors).to be_empty + end + + it "does not add an error for shared ownership sale" do + record.ownershipsch = 1 + + [1, 2, 3, 4, 5, 6, 7, 9, 0].each do |prevten| + record.prevten = prevten + household_validator.validate_buyer1_previous_tenure(record) + expect(record.errors).to be_empty + end + end + + it "does not add an error for outright sale" do + record.ownershipsch = 3 + + [1, 2, 3, 4, 5, 6, 7, 9, 0].each do |prevten| + record.prevten = prevten + household_validator.validate_buyer1_previous_tenure(record) + expect(record.errors).to be_empty + end + end + + context "with 23/24 logs" do + let(:now) { Time.zone.local(2023, 4, 4) } + + it "does not add an error for outright sale" do + record.ownershipsch = 2 + + [1, 2, 3, 4, 5, 6, 7, 9, 0].each do |prevten| + record.prevten = prevten + household_validator.validate_buyer1_previous_tenure(record) + expect(record.errors).to be_empty + end + end + end + end end