From 5bcd6779611d9e778ee632987571b750b1546fd5 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 7 Feb 2024 10:04:41 +0000 Subject: [PATCH] CLDC-3178 Update grant validation type (#2209) * Remove soft grant validation for 2024 onwards * Validate grant amount for 24/25 onwards * Only run the hard validation for Social HomeBuy and RTA * Fix soft validation --- .../sales/sale_information_validations.rb | 9 +++ .../validations/sales/soft_validations.rb | 3 +- config/locales/en.yml | 2 + .../sale_information_validations_spec.rb | 72 +++++++++++++++++++ .../sales/soft_validations_spec.rb | 30 ++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 5b036580d..54d49b7e0 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -55,4 +55,13 @@ module Validations::Sales::SaleInformationValidations record.errors.add :type, I18n.t("validations.sale_information.monthly_rent.higher_than_expected") end end + + def validate_grant_amount(record) + return unless record.saledate && record.form.start_year_after_2024? + return unless record.grant && (record.type == 8 || record.type == 21) + + unless record.grant.between?(9_000, 16_000) + record.errors.add :grant, I18n.t("validations.sale_information.grant.out_of_range") + end + end end diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index fce544754..ae9ee66cc 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -116,7 +116,8 @@ module Validations::Sales::SoftValidations end def grant_outside_common_range? - return unless grant + return unless grant && type && saledate + return if form.start_year_after_2024? && (type == 21 || type == 8) !grant.between?(9_000, 16_000) end diff --git a/config/locales/en.yml b/config/locales/en.yml index b9638ba94..7fc5f9d57 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -615,6 +615,8 @@ en: discounted_ownership_value: "The mortgage, deposit, and grant when added together is %{mortgage_deposit_and_grant_total}, and the purchase purchase price times by the discount is %{value_with_discount}. These figures should be the same" monthly_rent: higher_than_expected: "Basic monthly rent must be between £0.00 and £9,999.00" + grant: + out_of_range: "Loan, grants or subsidies must be between £9,000 and £16,000" merge_request: organisation_part_of_another_merge: "This organisation is part of another merge - select a different one" organisation_not_selected: "Select an organisation from the search list" diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 392686ac7..c890afed1 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -467,4 +467,76 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_grant_amount" do + context "when within permitted bounds" do + let(:record) { build(:sales_log, grant: 10_000, saledate: Time.zone.local(2024, 4, 5)) } + + it "does not add an error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors).not_to be_present + end + end + + context "when over the max" do + let(:record) { build(:sales_log, type: 8, grant: 17_000, saledate: Time.zone.local(2024, 4, 5)) } + + it "adds an error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors[:grant]).to include("Loan, grants or subsidies must be between £9,000 and £16,000") + end + end + + context "when under the min" do + let(:record) { build(:sales_log, type: 21, grant: 3, saledate: Time.zone.local(2024, 4, 5)) } + + it "adds an error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors[:grant]).to include("Loan, grants or subsidies must be between £9,000 and £16,000") + end + end + + context "when grant is blank" do + let(:record) { build(:sales_log, type: 21, grant: nil, saledate: Time.zone.local(2024, 4, 5)) } + + it "does not add an error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors).not_to be_present + end + end + + context "when over the max and type is not RTA of social homebuy" do + let(:record) { build(:sales_log, type: 9, grant: 17_000, saledate: Time.zone.local(2024, 4, 5)) } + + it "does not add an error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors).not_to be_present + end + end + + context "when under the min and type is not RTA of social homebuy" do + let(:record) { build(:sales_log, type: 9, grant: 17_000, saledate: Time.zone.local(2024, 4, 5)) } + + it "does not add error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors).not_to be_present + end + end + + context "with log before 2024/25 collection" do + let(:record) { build(:sales_log, type: 8, grant: 3, saledate: Time.zone.local(2023, 4, 5)) } + + it "does not add an error" do + sale_information_validator.validate_grant_amount(record) + + expect(record.errors).not_to be_present + end + end + end end diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 8718eee86..e417fbd8e 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -660,21 +660,51 @@ RSpec.describe Validations::Sales::SoftValidations do describe "#grant_outside_common_range?" do it "returns true if grant is below 9000" do record.grant = 1_000 + record.type = 9 + record.saledate = Time.zone.local(2024, 1, 1) expect(record).to be_grant_outside_common_range end it "returns true if grant is above 16000" do record.grant = 100_000 + record.type = 9 + record.saledate = Time.zone.local(2024, 1, 1) expect(record).to be_grant_outside_common_range end it "returns false if grant is within expected range" do record.grant = 10_000 + record.type = 9 + record.saledate = Time.zone.local(2024, 1, 1) expect(record).not_to be_grant_outside_common_range end + + it "returns false for logs after 2024 with RTA" do + record.grant = 100_000 + record.type = 8 + record.saledate = Time.zone.local(2025, 1, 1) + + expect(record).not_to be_grant_outside_common_range + end + + it "returns false for logs after 2024 with socialBuy" do + record.grant = 100_000 + record.type = 21 + record.saledate = Time.zone.local(2025, 1, 1) + + expect(record).not_to be_grant_outside_common_range + end + + it "returns true for logs after 2024 with other type" do + record.grant = 100_000 + record.type = 9 + record.saledate = Time.zone.local(2025, 1, 1) + + expect(record).to be_grant_outside_common_range + end end describe "#staircase_bought_above_fifty" do