From f3c4628502be7aed3fc329d1a43b51739e759134 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 14 Mar 2022 14:55:42 +0000 Subject: [PATCH] Add brent/tshortfall validation --- app/models/validations/financial_validations.rb | 7 +++++++ config/locales/en.yml | 5 ++++- .../validations/financial_validations_spec.rb | 15 +++++++++++++++ .../models/validations/shared_validations_spec.rb | 4 ++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 59a38e3a7..5d3d13bc1 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -58,4 +58,11 @@ module Validations::FinancialValidations record.errors.add :tshortfall, I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits") end end + + def validate_rent_amount(record) + if record.brent.present? && record.tshortfall.present? && record.brent < record.tshortfall * 2 + record.errors.add :brent, I18n.t("validations.financial.rent.less_than_double_shortfall", tshortfall: record.tshortfall * 2) + record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 7eb9a57e4..5cb80710f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -78,7 +78,8 @@ en: financial: tshortfall: - outstanding_amount_not_required: "You must not answer the outstanding amount question if you don’t have outstanding rent or charges." + outstanding_amount_not_required: "You must not answer the outstanding amount question if you don’t have outstanding rent or charges" + more_than_rent: "Answer cannot be more than half of the basic rent amount" hbrentshortfall: outstanding_no_benefits: "Outstanding amount for basic rent and/or benefit eligible charges cannot be 'Yes' if tenant is not in receipt of housing benefit or universal benefit or if benefit is unknown" benefits: @@ -89,6 +90,8 @@ en: freq_missing: "Select how often the household receives income" earnings_missing: "Enter how much income the household has in total" negative_currency: "Enter an amount above 0" + rent: + less_than_double_shortfall: "Answer cannot be less than double shortfall in basic rent" household: reasonpref: diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index d75df4f91..63d107275 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -186,4 +186,19 @@ RSpec.describe Validations::FinancialValidations do end end end + + describe "rent and charges validations" do + context "when shortfall amount is provided" do + it "validates that basic rent is no less than double the shortfall" do + record.hbrentshortfall = 1 + record.tshortfall = 99.50 + record.brent = 198 + financial_validator.validate_rent_amount(record) + expect(record.errors["brent"]) + .to include(match I18n.t("validations.financial.rent.less_than_double_shortfall", shortfall: 198)) + expect(record.errors["tshortfall"]) + .to include(match I18n.t("validations.financial.tshortfall.more_than_rent")) + end + end + end end diff --git a/spec/models/validations/shared_validations_spec.rb b/spec/models/validations/shared_validations_spec.rb index 1938e531b..043f3d057 100644 --- a/spec/models/validations/shared_validations_spec.rb +++ b/spec/models/validations/shared_validations_spec.rb @@ -1,9 +1,9 @@ require "rails_helper" -RSpec.describe Validations::HouseholdValidations do +RSpec.describe Validations::SharedValidations do subject(:household_validator) { validator_class.new } - let(:validator_class) { Class.new { include Validations::HouseholdValidations } } + let(:validator_class) { Class.new { include Validations::SharedValidations } } let(:record) { FactoryBot.create(:case_log) } describe "numeric min max validations" do