From 0083485b4278dea30e66a052a9f92685ed945f74 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 13 Nov 2024 17:10:27 +0000 Subject: [PATCH] CLDC-3690 Extract lettings property validations copy (#2742) * Extract lettings property validations copy * Update uprn question * Remove redundant validations (can't trigger them because on inferrance) --- app/models/form/lettings/questions/uprn.rb | 2 +- .../validations/property_validations.rb | 39 ++---- config/locales/en.yml | 14 --- .../lettings/property_information.en.yml | 17 ++- .../form/lettings/questions/uprn_spec.rb | 2 +- spec/models/lettings_log_spec.rb | 12 +- .../validations/property_validations_spec.rb | 112 ++---------------- 7 files changed, 41 insertions(+), 157 deletions(-) diff --git a/app/models/form/lettings/questions/uprn.rb b/app/models/form/lettings/questions/uprn.rb index 53df6bcf2..e6741d25b 100644 --- a/app/models/form/lettings/questions/uprn.rb +++ b/app/models/form/lettings/questions/uprn.rb @@ -15,7 +15,7 @@ class Form::Lettings::Questions::Uprn < ::Form::Question end def unanswered_error_message - I18n.t("validations.property.uprn.invalid") + I18n.t("validations.lettings.property.uprn.invalid") end def get_extra_check_answer_value(log) diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index 1864adc5c..09e6d1436 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -4,49 +4,30 @@ module Validations::PropertyValidations REFERRAL_INVALID_TMP = [8, 10, 12, 13, 14, 15].freeze def validate_rsnvac(record) - if !record.first_time_property_let_as_social_housing? && record.has_first_let_vacancy_reason? - record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.first_let_not_social") - end - - if record.first_time_property_let_as_social_housing? && record.rsnvac.present? && !record.has_first_let_vacancy_reason? - record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.first_let_social") - end - if record.is_relet_to_temp_tenant? && !record.previous_tenancy_was_temporary? - record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.non_temp_accommodation") + record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.non_temp_accommodation") end if record.is_relet_to_temp_tenant? && REFERRAL_INVALID_TMP.include?(record.referral) - record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.referral_invalid") - record.errors.add :referral, :referral_invalid, message: I18n.t("validations.household.referral.rsnvac_non_temp") + record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.referral_invalid") + record.errors.add :referral, :referral_invalid, message: I18n.t("validations.lettings.property.referral.rsnvac_non_temp") end if record.renewal.present? && record.renewal.zero? && record.rsnvac == 14 - record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.not_a_renewal") - end - end - - def validate_unitletas(record) - if record.first_time_property_let_as_social_housing? && record.unitletas.present? - record.errors.add :unitletas, I18n.t("validations.property.rsnvac.previous_let_social") + record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.not_a_renewal") end end def validate_shared_housing_rooms(record) return unless record.unittype_gn - if record.is_bedsit? && record.beds != 1 && record.beds.present? && !record.form.start_year_2024_or_later? - record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit") - record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit") - end - if record.hhmemb == 1 && record.is_shared_housing? && !record.beds.to_i.between?(1, 3) && record.beds.present? - record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") - record.errors.add :beds, :one_three_bedroom_single_tenant_shared, message: I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") + record.errors.add :unittype_gn, I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared") + record.errors.add :beds, :one_three_bedroom_single_tenant_shared, message: I18n.t("validations.lettings.property.beds.one_three_bedroom_single_tenant_shared") elsif record.is_shared_housing? && record.beds.present? && !record.beds.to_i.between?(1, 7) - record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") - record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") + record.errors.add :unittype_gn, I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared") + record.errors.add :beds, I18n.t("validations.lettings.property.beds.one_seven_bedroom_shared") end end @@ -55,13 +36,13 @@ module Validations::PropertyValidations return if record.uprn.match?(/^[0-9]{1,12}$/) - record.errors.add :uprn, I18n.t("validations.property.uprn.invalid") + record.errors.add :uprn, I18n.t("validations.lettings.property.uprn.invalid") end def validate_property_postcode(record) postcode = record.postcode_full if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) - error_message = I18n.t("validations.lettings.property_information.postcode_full.invalid") + error_message = I18n.t("validations.lettings.property.postcode_full.invalid") record.errors.add :postcode_full, :wrong_format, message: error_message end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 45f0d2317..f8bb8255b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -263,24 +263,11 @@ en: owning_organisation: data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation." property: - uprn: - invalid: "UPRN must be 12 digits or less." uprn_known: invalid: "You must answer UPRN known?" la: la_invalid_for_org: "%{org_name} does not operate in %{la_name}." postcode_invalid_for_org: "Enter a postcode in an area covered by %{org_name}." - rsnvac: - first_let_not_social: "Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing." - first_let_social: "Reason for vacancy must be first let if unit has been previously let as social housing." - previous_let_social: "Property cannot have a previous let type if being let as social housing for the first time." - non_temp_accommodation: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as this accommodation is not temporary." - referral_invalid: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as a different source of referral for this letting." - not_a_renewal: "Reason for vacancy cannot be 'Renewal of fixed-term tenancy' if letting is not a renewal." - unittype_gn: - one_bedroom_bedsit: "A bedsit can only have one bedroom." - one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms." - one_three_bedroom_single_tenant_shared: "A shared house with fewer than two tenants must have 1 to 3 bedrooms." financial: charges: @@ -319,7 +306,6 @@ en: one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’." referral: secure_tenancy: "Answer must be internal transfer as this is a secure tenancy." - rsnvac_non_temp: "Answer cannot be this source of referral as this is a re-let to tenant who occupied the same property as temporary accommodation." cannot_be_secure_tenancy: "Answer cannot be secure tenancy as this is not an internal transfer." assessed_homeless: "Answer cannot be internal transfer as the tenant was assessed as homeless." other_homeless: "Answer cannot be internal transfer as the tenant was considered homeless by their landlord." diff --git a/config/locales/validations/lettings/property_information.en.yml b/config/locales/validations/lettings/property_information.en.yml index ed42acb4c..6530c9488 100644 --- a/config/locales/validations/lettings/property_information.en.yml +++ b/config/locales/validations/lettings/property_information.en.yml @@ -1,6 +1,21 @@ en: validations: lettings: - property_information: + property: postcode_full: invalid: "Enter a postcode in the correct format, for example AA1 1AA." + rsnvac: + non_temp_accommodation: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as this accommodation is not temporary." + referral_invalid: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as a different source of referral for this letting." + not_a_renewal: "Reason for vacancy cannot be 'Renewal of fixed-term tenancy' if letting is not a renewal." + referral: + rsnvac_non_temp: "Answer cannot be this source of referral as this is a re-let to tenant who occupied the same property as temporary accommodation." + unittype_gn: + one_three_bedroom_single_tenant_shared: "A shared house with fewer than two tenants must have 1 to 3 bedrooms." + one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms." + beds: + one_three_bedroom_single_tenant_shared: "A shared house with fewer than two tenants must have 1 to 3 bedrooms." + one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms." + uprn: + invalid: "UPRN must be 12 digits or less." + diff --git a/spec/models/form/lettings/questions/uprn_spec.rb b/spec/models/form/lettings/questions/uprn_spec.rb index 21f3e078b..8bb932b35 100644 --- a/spec/models/form/lettings/questions/uprn_spec.rb +++ b/spec/models/form/lettings/questions/uprn_spec.rb @@ -28,7 +28,7 @@ RSpec.describe Form::Lettings::Questions::Uprn, type: :model do end it "has the correct unanswered_error_message" do - expect(question.unanswered_error_message).to eq("UPRN must be 12 digits or less.") + expect(question.unanswered_error_message).to eq(I18n.t("validations.lettings.property.uprn.invalid")) end describe "get_extra_check_answer_value" do diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index e28f0f2c5..c47de8cf6 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -194,10 +194,6 @@ RSpec.describe LettingsLog do expect(validator).to receive(:validate_tshortfall) end - it "validates let type" do - expect(validator).to receive(:validate_unitletas) - end - it "validates reason for vacancy" do expect(validator).to receive(:validate_rsnvac) end @@ -1010,8 +1006,8 @@ RSpec.describe LettingsLog do end it "does not impact other validations" do - expect { lettings_log.update!(startdate: Time.zone.yesterday, first_time_property_let_as_social_housing: 0, rsnvac: 16) } - .to raise_error(ActiveRecord::RecordInvalid, /Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing/) + expect { lettings_log.update!(startdate: Time.zone.yesterday, referral: 8, rsnvac: 9) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.lettings.property.rsnvac.referral_invalid")}/) end end @@ -1046,8 +1042,8 @@ RSpec.describe LettingsLog do end it "does not impact other validations" do - expect { lettings_log.update!(location:, scheme:, first_time_property_let_as_social_housing: 0, rsnvac: 16) } - .to raise_error(ActiveRecord::RecordInvalid, /Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing/) + expect { lettings_log.update!(startdate: Time.zone.yesterday, referral: 8, rsnvac: 9) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.lettings.property.rsnvac.referral_invalid")}/) end end end diff --git a/spec/models/validations/property_validations_spec.rb b/spec/models/validations/property_validations_spec.rb index 8cf484f91..f478ae466 100644 --- a/spec/models/validations/property_validations_spec.rb +++ b/spec/models/validations/property_validations_spec.rb @@ -47,47 +47,10 @@ RSpec.describe Validations::PropertyValidations do expect(log.errors).to be_empty end end - - context "and the log is from before 24/25" do - it "adds an error" do - allow(log.form).to receive(:start_year_2024_or_later?).and_return false - - property_validator.validate_shared_housing_rooms(log) - - expect(log.errors["unittype_gn"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) - expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) - end - end - end - - context "when a bedsit has less than 1 bedroom" do - before do - log.beds = 0 - log.unittype_gn = 2 - end - - context "and the log is for 24/25 or later" do - it "does not add an error" do - property_validator.validate_shared_housing_rooms(log) - - expect(log.errors).to be_empty - end - end - - context "and the log is from before 24/25" do - it "adds an error" do - allow(log.form).to receive(:start_year_2024_or_later?).and_return false - - property_validator.validate_shared_housing_rooms(log) - - expect(log.errors["unittype_gn"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) - expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) - end - end end context "when shared housing has more than 7 bedrooms" do - let(:expected_error) { I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") } + let(:expected_error) { I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared") } it "adds an error if the number of bedrooms is not between 1 and 7" do log.beds = 8 @@ -95,12 +58,12 @@ RSpec.describe Validations::PropertyValidations do log.hhmemb = 3 property_validator.validate_shared_housing_rooms(log) expect(log.errors["unittype_gn"]).to include(match(expected_error)) - expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")) + expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared")) end end context "when shared housing has less than 1 bedrooms" do - let(:expected_error) { I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") } + let(:expected_error) { I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared") } it "adds an error if the number of bedrooms is not between 1 and 7" do log.beds = 0 @@ -108,12 +71,12 @@ RSpec.describe Validations::PropertyValidations do log.hhmemb = 3 property_validator.validate_shared_housing_rooms(log) expect(log.errors["unittype_gn"]).to include(match(expected_error)) - expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")) + expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared")) end end context "when there are too many bedrooms for the number of household members and unit type" do - let(:expected_error) { I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") } + let(:expected_error) { I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared") } it "adds an error" do log.beds = 4 @@ -121,54 +84,13 @@ RSpec.describe Validations::PropertyValidations do log.hhmemb = 1 property_validator.validate_shared_housing_rooms(log) expect(log.errors["unittype_gn"]).to include(match(expected_error)) - expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared")) - end - end - end - - describe "#validate_unitletas" do - context "when the property has not been let before" do - it "validates that no previous let type is provided" do - log.first_time_property_let_as_social_housing = 1 - log.unitletas = 0 - property_validator.validate_unitletas(log) - expect(log.errors["unitletas"]) - .to include(match I18n.t("validations.property.rsnvac.previous_let_social")) - log.unitletas = 1 - property_validator.validate_unitletas(log) - expect(log.errors["unitletas"]) - .to include(match I18n.t("validations.property.rsnvac.previous_let_social")) - log.unitletas = 2 - property_validator.validate_unitletas(log) - expect(log.errors["unitletas"]) - .to include(match I18n.t("validations.property.rsnvac.previous_let_social")) - log.unitletas = 3 - property_validator.validate_unitletas(log) - expect(log.errors["unitletas"]) - .to include(match I18n.t("validations.property.rsnvac.previous_let_social")) - end - end - - context "when the property has been let previously" do - it "expects to have a previous let type" do - log.first_time_property_let_as_social_housing = 0 - log.unitletas = 0 - property_validator.validate_unitletas(log) - expect(log.errors["unitletas"]).to be_empty + expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared")) end end end describe "validate_rsnvac" do context "when the property has not been let before" do - it "validates that it has a first let reason for vacancy" do - log.first_time_property_let_as_social_housing = 1 - log.rsnvac = 6 - property_validator.validate_rsnvac(log) - expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.first_let_social")) - end - it "expects to have a first let reason for vacancy" do log.first_time_property_let_as_social_housing = 1 log.rsnvac = 15 @@ -184,22 +106,6 @@ RSpec.describe Validations::PropertyValidations do end context "when the property has been let as social housing before" do - it "validates that the reason for vacancy is not a first let as social housing reason" do - log.first_time_property_let_as_social_housing = 0 - log.rsnvac = 15 - property_validator.validate_rsnvac(log) - expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.first_let_not_social")) - log.rsnvac = 16 - property_validator.validate_rsnvac(log) - expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.first_let_not_social")) - log.rsnvac = 17 - property_validator.validate_rsnvac(log) - expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.first_let_not_social")) - end - it "expects the reason for vacancy to be a first let as social housing reason" do log.first_time_property_let_as_social_housing = 1 log.rsnvac = 15 @@ -220,7 +126,7 @@ RSpec.describe Validations::PropertyValidations do log.rsnvac = 14 property_validator.validate_rsnvac(log) expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.not_a_renewal")) + .to include(match I18n.t("validations.lettings.property.rsnvac.not_a_renewal")) end end end @@ -237,7 +143,7 @@ RSpec.describe Validations::PropertyValidations do log.prevten = prevten property_validator.validate_rsnvac(log) expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.non_temp_accommodation")) + .to include(match I18n.t("validations.lettings.property.rsnvac.non_temp_accommodation")) end end @@ -247,7 +153,7 @@ RSpec.describe Validations::PropertyValidations do log.referral = src property_validator.validate_rsnvac(log) expect(log.errors["rsnvac"]) - .to include(match I18n.t("validations.property.rsnvac.referral_invalid")) + .to include(match I18n.t("validations.lettings.property.rsnvac.referral_invalid")) end end end