Browse Source

Add validation

pull/487/head
baarkerlounger 3 years ago
parent
commit
e6e2c00abd
  1. 4
      app/models/organisation.rb
  2. 11
      app/models/validations/financial_validations.rb
  3. 2
      config/locales/en.yml
  4. 2
      spec/factories/organisation.rb
  5. 4
      spec/models/case_log_spec.rb
  6. 24
      spec/models/validations/financial_validations_spec.rb

4
app/models/organisation.rb

@ -43,7 +43,7 @@ class Organisation < ApplicationRecord
def local_authority_names def local_authority_names
names = local_authorities.map { |ons_code| LocalAuthority.ons_code_mappings[ons_code] } names = local_authorities.map { |ons_code| LocalAuthority.ons_code_mappings[ons_code] }
names.present? ? names : ["All"] names.presence || %w[All]
end end
def rent_periods def rent_periods
@ -52,7 +52,7 @@ class Organisation < ApplicationRecord
def rent_period_labels def rent_period_labels
labels = rent_periods.map { |period| RentPeriod.rent_period_mappings[period.to_s]["value"] } labels = rent_periods.map { |period| RentPeriod.rent_period_mappings[period.to_s]["value"] }
labels.present? ? labels : ["All"] labels.presence || %w[All]
end end
def display_attributes def display_attributes

11
app/models/validations/financial_validations.rb

@ -80,6 +80,17 @@ module Validations::FinancialValidations
validate_rent_range(record) validate_rent_range(record)
end end
def validate_rent_period(record)
if record.owning_organisation.present? && record.owning_organisation.rent_periods.present? &&
record.period && !record.owning_organisation.rent_periods.include?(record.period)
record.errors.add :period, I18n.t(
"validations.financial.rent_period.invalid_for_org",
org_name: record.owning_organisation.name,
rent_period: record.form.get_question("period", record).label_from_value(record.period).downcase,
)
end
end
private private
CHARGE_MAXIMUMS = { CHARGE_MAXIMUMS = {

2
config/locales/en.yml

@ -138,6 +138,8 @@ en:
complete_1_of_3: "Answer either the ‘household rent and charges’ question or 'is this accommodation a care home', or select ‘no’ for ‘does the household pay rent or charges for the accommodation?’" complete_1_of_3: "Answer either the ‘household rent and charges’ question or 'is this accommodation a care home', or select ‘no’ for ‘does the household pay rent or charges for the accommodation?’"
tcharge: tcharge:
under_10: "Total charge must be at least £10 per week" under_10: "Total charge must be at least £10 per week"
rent_period:
invalid_for_org: "%{org_name} does not charge rent %{rent_period}"
household: household:
reasonpref: reasonpref:

2
spec/factories/organisation.rb

@ -18,7 +18,7 @@ FactoryBot.define do
factory :organisation_rent_period do factory :organisation_rent_period do
organisation organisation
rent_period { 1 } rent_period { 2 }
created_at { Time.zone.now } created_at { Time.zone.now }
updated_at { Time.zone.now } updated_at { Time.zone.now }
end end

4
spec/models/case_log_spec.rb

@ -125,6 +125,10 @@ RSpec.describe CaseLog do
expect(validator).to receive(:validate_outstanding_rent_amount) expect(validator).to receive(:validate_outstanding_rent_amount)
end end
it "validates the rent period" do
expect(validator).to receive(:validate_rent_period)
end
it "validates housing benefit rent shortfall" do it "validates housing benefit rent shortfall" do
expect(validator).to receive(:validate_tshortfall) expect(validator).to receive(:validate_tshortfall)
end end

24
spec/models/validations/financial_validations_spec.rb

@ -86,6 +86,28 @@ RSpec.describe Validations::FinancialValidations do
end end
end end
describe "rent period validations" do
let(:organisation) { FactoryBot.create(:organisation) }
let(:record) { FactoryBot.create(:case_log, owning_organisation: organisation) }
before do
FactoryBot.create(:organisation_rent_period, organisation:, rent_period: 2)
end
context "when the organisation only uses specific rent periods" do
it "validates that the selected rent period is used by the organisation" do
record.period = 3
financial_validator.validate_rent_period(record)
expect(record.errors["period"])
.to include(match I18n.t(
"validations.financial.rent_period.invalid_for_org",
org_name: organisation.name,
rent_period: "every 2 weeks",
))
end
end
end
describe "housing benefit rent shortfall validations" do describe "housing benefit rent shortfall validations" do
context "when shortfall is yes" do context "when shortfall is yes" do
it "validates that housing benefit is not none" do it "validates that housing benefit is not none" do
@ -121,7 +143,7 @@ RSpec.describe Validations::FinancialValidations do
end end
end end
describe "Net income validations" do describe "net income validations" do
it "validates that the net income is within the expected range for the tenant's employment status" do it "validates that the net income is within the expected range for the tenant's employment status" do
record.earnings = 200 record.earnings = 200
record.incfreq = 1 record.incfreq = 1

Loading…
Cancel
Save