Browse Source

Add care home charges validation (#682)

pull/687/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
bf8587e9fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/models/case_log.rb
  2. 14
      app/models/validations/financial_validations.rb
  3. 4
      config/locales/en.yml
  4. 2
      spec/fixtures/complete_case_log.json
  5. 4
      spec/fixtures/forms/2021_2022.json
  6. 2
      spec/models/rent_period_spec.rb
  7. 94
      spec/models/validations/financial_validations_spec.rb

6
app/models/case_log.rb

@ -131,6 +131,12 @@ class CaseLog < ApplicationRecord
(field_value / 52 * num_of_weeks).round(2) (field_value / 52 * num_of_weeks).round(2)
end end
def weekly_to_value_per_period(field_value)
num_of_weeks = NUM_OF_WEEKS_FROM_PERIOD[period]
((field_value * 52) / num_of_weeks).round(2)
end
def applicable_income_range def applicable_income_range
return unless ecstat1 return unless ecstat1

14
app/models/validations/financial_validations.rb

@ -102,6 +102,20 @@ module Validations::FinancialValidations
end end
end end
def validate_care_home_charges(record)
if record.is_carehome?
period = record.form.get_question("period", record).label_from_value(record.period).downcase
if record.chcharge.blank?
record.errors.add :chcharge, I18n.t("validations.financial.carehome.not_provided", period:)
elsif !weekly_value_in_range(record, "chcharge", 0, 1000)
max_chcharge = record.weekly_to_value_per_period(1000)
max_chcharge = [record.form.get_question("chcharge", record).prefix, max_chcharge].join("") if record.form.get_question("chcharge", record).present?
record.errors.add :period, I18n.t("validations.financial.carehome.over_1000", period:, min_chcharge: "£0", max_chcharge:)
record.errors.add :chcharge, I18n.t("validations.financial.carehome.over_1000", period:, min_chcharge: "£0", max_chcharge:)
end
end
end
private private
CHARGE_MAXIMUMS = { CHARGE_MAXIMUMS = {

4
config/locales/en.yml

@ -145,7 +145,9 @@ en:
under_10: "Total charge must be at least £10 per week" under_10: "Total charge must be at least £10 per week"
rent_period: rent_period:
invalid_for_org: "%{org_name} does not charge rent %{rent_period}" invalid_for_org: "%{org_name} does not charge rent %{rent_period}"
carehome:
over_1000: "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}"
household: household:
reasonpref: reasonpref:
not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as you already told us the tenant was not homeless immediately prior to this letting" not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as you already told us the tenant was not homeless immediately prior to this letting"

2
spec/fixtures/complete_case_log.json vendored

@ -130,7 +130,7 @@
"new_build_handover_date": "01/01/2019", "new_build_handover_date": "01/01/2019",
"has_benefits": 1, "has_benefits": 1,
"household_charge": 0, "household_charge": 0,
"is_carehome": 1, "is_carehome": 0,
"sheltered": 0, "sheltered": 0,
"declaration": 1, "declaration": 1,
"referral": 1 "referral": 1

4
spec/fixtures/forms/2021_2022.json vendored

@ -703,11 +703,11 @@
"header": "Which period are rent and other charges due?", "header": "Which period are rent and other charges due?",
"type": "radio", "type": "radio",
"answer_options": { "answer_options": {
"2": { "1": {
"value": "Weekly for 52 weeks" "value": "Weekly for 52 weeks"
}, },
"3": { "3": {
"value": "Every 2 weeks" "value": "Every 4 weeks"
} }
} }
}, },

2
spec/models/rent_period_spec.rb

@ -10,7 +10,7 @@ RSpec.describe RentPeriod, type: :model do
it "maps rent period id to display names" do it "maps rent period id to display names" do
expect(described_class.rent_period_mappings).to be_a(Hash) expect(described_class.rent_period_mappings).to be_a(Hash)
expect(described_class.rent_period_mappings["2"]).to eq({ "value" => "Weekly for 52 weeks" }) expect(described_class.rent_period_mappings["1"]).to eq({ "value" => "Weekly for 52 weeks" })
end end
end end
end end

94
spec/models/validations/financial_validations_spec.rb

@ -102,7 +102,7 @@ RSpec.describe Validations::FinancialValidations do
.to include(match I18n.t( .to include(match I18n.t(
"validations.financial.rent_period.invalid_for_org", "validations.financial.rent_period.invalid_for_org",
org_name: organisation.name, org_name: organisation.name,
rent_period: "every 2 weeks", rent_period: "every 4 weeks",
)) ))
end end
end end
@ -835,5 +835,97 @@ RSpec.describe Validations::FinancialValidations do
end end
end end
end end
context "when the accommodation is care home" do
before do
record.is_carehome = 1
end
context "and charges are over the valid limit (£1000 per week)" do
it "validates charge when period is weekly for 52 weeks" do
record.period = 1
record.chcharge = 1001
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "weekly for 52 weeks", max_chcharge: 1000))
expect(record.errors["period"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "weekly for 52 weeks", max_chcharge: 1000))
end
it "validates charge when period is monthly" do
record.period = 4
record.chcharge = 4334
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "4", max_chcharge: 4333))
expect(record.errors["period"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "4", max_chcharge: 4333))
end
it "validates charge when period is every 2 weeks" do
record.period = 2
record.chcharge = 2001
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "2", max_chcharge: 2000))
expect(record.errors["period"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "2", max_chcharge: 2000))
end
it "validates charge when period is every 4 weeks" do
record.period = 3
record.chcharge = 4001
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "every 4 weeks", max_chcharge: 4000))
expect(record.errors["period"])
.to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "every 4 weeks", max_chcharge: 4000))
end
end
context "and charges are within the valid limit (£1000 per week)" do
it "does not throw error when period is weekly for 52 weeks" do
record.period = 1
record.chcharge = 999
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"]).to be_empty
expect(record.errors["period"]).to be_empty
end
it "does not throw error when period is monthly" do
record.period = 4
record.chcharge = 4333
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"]).to be_empty
expect(record.errors["period"]).to be_empty
end
it "does not throw error when period is every 2 weeks" do
record.period = 2
record.chcharge = 1999.99
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"]).to be_empty
expect(record.errors["period"]).to be_empty
end
it "does not throw error when period is every 4 weeks" do
record.period = 3
record.chcharge = 3999
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"]).to be_empty
expect(record.errors["period"]).to be_empty
end
end
context "and charges are not provided" do
it "throws and error" do
record.period = 3
record.chcharge = nil
financial_validator.validate_care_home_charges(record)
expect(record.errors["chcharge"])
.to include(match I18n.t("validations.financial.carehome.not_provided", period: "every 4 weeks"))
end
end
end
end end
end end

Loading…
Cancel
Save