diff --git a/app/models/organisation.rb b/app/models/organisation.rb index e3e7f05c2..95f2b4a48 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -43,7 +43,7 @@ class Organisation < ApplicationRecord def local_authority_names names = local_authorities.map { |ons_code| LocalAuthority.ons_code_mappings[ons_code] } - names.present? ? names : ["All"] + names.presence || %w[All] end def rent_periods @@ -52,7 +52,7 @@ class Organisation < ApplicationRecord def rent_period_labels labels = rent_periods.map { |period| RentPeriod.rent_period_mappings[period.to_s]["value"] } - labels.present? ? labels : ["All"] + labels.presence || %w[All] end def display_attributes diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index a95cf25c1..857ceb45a 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -80,6 +80,17 @@ module Validations::FinancialValidations validate_rent_range(record) 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 CHARGE_MAXIMUMS = { diff --git a/config/locales/en.yml b/config/locales/en.yml index 2d66ac916..ab1c95918 100644 --- a/config/locales/en.yml +++ b/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?’" tcharge: 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: reasonpref: diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index 7da708cae..981217736 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -18,7 +18,7 @@ FactoryBot.define do factory :organisation_rent_period do organisation - rent_period { 1 } + rent_period { 2 } created_at { Time.zone.now } updated_at { Time.zone.now } end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 3633fba51..852b2eaec 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -125,6 +125,10 @@ RSpec.describe CaseLog do expect(validator).to receive(:validate_outstanding_rent_amount) end + it "validates the rent period" do + expect(validator).to receive(:validate_rent_period) + end + it "validates housing benefit rent shortfall" do expect(validator).to receive(:validate_tshortfall) end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 68b5f4629..7f4eb438c 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -86,6 +86,28 @@ RSpec.describe Validations::FinancialValidations do 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 context "when shortfall is yes" do it "validates that housing benefit is not none" do @@ -121,7 +143,7 @@ RSpec.describe Validations::FinancialValidations do 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 record.earnings = 200 record.incfreq = 1