Browse Source

hard validation on value of cash discount (#1221)

pull/1213/head
Arthur Campbell 2 years ago committed by GitHub
parent
commit
1cced31e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/form/sales/questions/deposit_discount.rb
  2. 8
      app/models/validations/sales/financial_validations.rb
  3. 1
      config/locales/en.yml
  4. 22
      spec/models/validations/sales/financial_validations_spec.rb

1
app/models/form/sales/questions/deposit_discount.rb

@ -6,6 +6,7 @@ class Form::Sales::Questions::DepositDiscount < ::Form::Question
@header = "How much cash discount was given through Social HomeBuy?" @header = "How much cash discount was given through Social HomeBuy?"
@type = "numeric" @type = "numeric"
@min = 0 @min = 0
@max = 999_999
@width = 5 @width = 5
@prefix = "£" @prefix = "£"
@hint_text = "Enter the total cash discount given on the property being purchased through the Social HomeBuy scheme" @hint_text = "Enter the total cash discount given on the property being purchased through the Social HomeBuy scheme"

8
app/models/validations/sales/financial_validations.rb

@ -11,4 +11,12 @@ module Validations::Sales::FinancialValidations
end end
end end
end end
def validate_cash_discount(record)
return unless record.cashdis
unless record.cashdis.between?(0, 999_999)
record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid")
end
end
end end

1
config/locales/en.yml

@ -274,6 +274,7 @@ en:
carehome: carehome:
out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}" out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}"
not_provided: "Enter how much rent and other charges the household pays %{period}" not_provided: "Enter how much rent and other charges the household pays %{period}"
cash_discount_invalid: "Cash discount must be £0 - £999,999"
household: household:
reasonpref: reasonpref:
not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting" not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting"

22
spec/models/validations/sales/financial_validations_spec.rb

@ -53,4 +53,26 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
end end
end end
describe "#validate_cash_discount" do
let(:record) { FactoryBot.create(:sales_log) }
it "adds an error if the cash discount is below zero" do
record.cashdis = -1
financial_validator.validate_cash_discount(record)
expect(record.errors["cashdis"]).to include(match I18n.t("validations.financial.cash_discount_invalid"))
end
it "adds an error if the cash discount is one million or more" do
record.cashdis = 1_000_000
financial_validator.validate_cash_discount(record)
expect(record.errors["cashdis"]).to include(match I18n.t("validations.financial.cash_discount_invalid"))
end
it "does not add an error if the cash discount is in the expected range" do
record.cashdis = 10_000
financial_validator.validate_cash_discount(record)
expect(record.errors["cashdis"]).to be_empty
end
end
end end

Loading…
Cancel
Save