From a85e447816858835c807a23953a7f259e09cbcb1 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 12 Dec 2022 15:38:03 +0000 Subject: [PATCH] Add london income validation --- app/models/sales_log.rb | 40 +++++++++++ .../sales/financial_validations.rb | 2 + .../sales/financial_validations_spec.rb | 68 ++++++++++++++----- 3 files changed, 93 insertions(+), 17 deletions(-) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 77b83e446..651a34343 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -66,4 +66,44 @@ class SalesLog < Log def buyer_1_child? ecstat1 == 9 end + + LONDON_BOROUGHS = %w[ + E09000001 + E09000033 + E09000020 + E09000013 + E09000032 + E09000022 + E09000028 + E09000030 + E09000012 + E09000019 + E09000007 + E09000005 + E09000009 + E09000018 + E09000027 + E09000021 + E09000024 + E09000029 + E09000008 + E09000006 + E09000023 + E09000011 + E09000004 + E09000016 + E09000002 + E09000026 + E09000025 + E09000031 + E09000014 + E09000010 + E09000003 + E09000015 + E09000017 + ].freeze + + def london_property? + la && LONDON_BOROUGHS.include?(la) + end end diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index a85b274b0..2bfa37c05 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -6,6 +6,8 @@ module Validations::Sales::FinancialValidations if record.ecstat1 && record.income1 && record.ownershipsch == 1 if record.buyer_1_child? && record.income1.positive? record.errors.add :income1, I18n.t("validations.financial.income1.child_income") + elsif record.london_property? + record.errors.add :income1, I18n.t("validations.financial.income1.over_hard_max", hard_max: 90_000) if record.income1 > 90_000 elsif record.income1 > 80_000 record.errors.add :income1, I18n.t("validations.financial.income1.over_hard_max", hard_max: 80_000) end diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 2e9c14bd6..22533f331 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -9,29 +9,63 @@ RSpec.describe Validations::Sales::FinancialValidations do let(:record) { FactoryBot.create(:sales_log, ownershipsch: 1) } context "with shared ownership" do - (0..8).each do |ecstat| - it "adds an error when buyer 1 income is over hard max for ecstat #{ecstat}" do - record.income1 = 85_000 - record.ecstat1 = ecstat + context "and non london borough" do + (0..8).each do |ecstat| + it "adds an error when buyer 1 income is over hard max for ecstat #{ecstat}" do + record.income1 = 85_000 + record.ecstat1 = ecstat + financial_validator.validate_income1(record) + expect(record.errors["income1"]) + .to include(match I18n.t("validations.financial.income1.over_hard_max", hard_max: 80_000)) + end + end + + it "validates that the income is within the expected range for the tenant’s employment status" do + record.income1 = 75_000 + record.ecstat1 = 1 + financial_validator.validate_income1(record) + expect(record.errors["income1"]).to be_empty + end + + it "validates income correctly if the ecstat is child" do + record.income1 = 1 + record.ecstat1 = 9 financial_validator.validate_income1(record) expect(record.errors["income1"]) - .to include(match I18n.t("validations.financial.income1.over_hard_max", hard_max: 80_000)) + .to include(match I18n.t("validations.financial.income1.child_income")) end end - it "validates that the income is within the expected range for the tenant’s employment status" do - record.income1 = 75_000 - record.ecstat1 = 1 - financial_validator.validate_income1(record) - expect(record.errors["income1"]).to be_empty - end + context "and a london borough" do + before do + record.update!(la: "E09000030") + record.reload + end - it "validates income correctly if the ecstat is child" do - record.income1 = 1 - record.ecstat1 = 9 - financial_validator.validate_income1(record) - expect(record.errors["income1"]) - .to include(match I18n.t("validations.financial.income1.child_income")) + (0..8).each do |ecstat| + it "adds an error when buyer 1 income is over hard max for ecstat #{ecstat}" do + record.income1 = 95_000 + record.ecstat1 = ecstat + financial_validator.validate_income1(record) + expect(record.errors["income1"]) + .to include(match I18n.t("validations.financial.income1.over_hard_max", hard_max: 90_000)) + end + end + + it "validates that the income is within the expected range for the tenant’s employment status" do + record.income1 = 85_000 + record.ecstat1 = 1 + financial_validator.validate_income1(record) + expect(record.errors["income1"]).to be_empty + end + + it "validates income correctly if the ecstat is child" do + record.income1 = 1 + record.ecstat1 = 9 + financial_validator.validate_income1(record) + expect(record.errors["income1"]) + .to include(match I18n.t("validations.financial.income1.child_income")) + end end end end