Browse Source

Extract lettings property validations copy

pull/2742/head
Kat 1 year ago
parent
commit
659d647c7a
  1. 30
      app/models/validations/property_validations.rb
  2. 14
      config/locales/en.yml
  3. 23
      config/locales/validations/lettings/property_information.en.yml
  4. 42
      spec/models/validations/property_validations_spec.rb

30
app/models/validations/property_validations.rb

@ -5,30 +5,30 @@ module Validations::PropertyValidations
REFERRAL_INVALID_TMP = [8, 10, 12, 13, 14, 15].freeze REFERRAL_INVALID_TMP = [8, 10, 12, 13, 14, 15].freeze
def validate_rsnvac(record) def validate_rsnvac(record)
if !record.first_time_property_let_as_social_housing? && record.has_first_let_vacancy_reason? 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") record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.first_let_not_social")
end end
if record.first_time_property_let_as_social_housing? && record.rsnvac.present? && !record.has_first_let_vacancy_reason? 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") record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.first_let_social")
end end
if record.is_relet_to_temp_tenant? && !record.previous_tenancy_was_temporary? 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 end
if record.is_relet_to_temp_tenant? && REFERRAL_INVALID_TMP.include?(record.referral) 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 :rsnvac, I18n.t("validations.lettings.property.rsnvac.referral_invalid")
record.errors.add :referral, :referral_invalid, message: I18n.t("validations.household.referral.rsnvac_non_temp") record.errors.add :referral, :referral_invalid, message: I18n.t("validations.lettings.property.referral.rsnvac_non_temp")
end end
if record.renewal.present? && record.renewal.zero? && record.rsnvac == 14 if record.renewal.present? && record.renewal.zero? && record.rsnvac == 14
record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.not_a_renewal") record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.not_a_renewal")
end end
end end
def validate_unitletas(record) def validate_unitletas(record)
if record.first_time_property_let_as_social_housing? && record.unitletas.present? 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 :unitletas, I18n.t("validations.lettings.property.unitletas.previous_let_social")
end end
end end
@ -36,17 +36,17 @@ module Validations::PropertyValidations
return unless record.unittype_gn return unless record.unittype_gn
if record.is_bedsit? && record.beds != 1 && record.beds.present? && !record.form.start_year_2024_or_later? 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 :unittype_gn, I18n.t("validations.lettings.property.unittype_gn.one_bedroom_bedsit")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit") record.errors.add :beds, I18n.t("validations.lettings.property.beds.one_bedroom_bedsit")
end end
if record.hhmemb == 1 && record.is_shared_housing? && if record.hhmemb == 1 && record.is_shared_housing? &&
!record.beds.to_i.between?(1, 3) && record.beds.present? !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 :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.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) 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 :unittype_gn, I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") record.errors.add :beds, I18n.t("validations.lettings.property.beds.one_seven_bedroom_shared")
end end
end end
@ -55,13 +55,13 @@ module Validations::PropertyValidations
return if record.uprn.match?(/^[0-9]{1,12}$/) 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 end
def validate_property_postcode(record) def validate_property_postcode(record)
postcode = record.postcode_full postcode = record.postcode_full
if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) 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 record.errors.add :postcode_full, :wrong_format, message: error_message
end end
end end

14
config/locales/en.yml

@ -265,24 +265,11 @@ en:
owning_organisation: owning_organisation:
data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the 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: property:
uprn:
invalid: "UPRN must be 12 digits or less."
uprn_known: uprn_known:
invalid: "You must answer UPRN known?" invalid: "You must answer UPRN known?"
la: la:
la_invalid_for_org: "%{org_name} does not operate in %{la_name}." 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}." 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: financial:
income: income:
@ -370,7 +357,6 @@ en:
internal_transfer: "Answer cannot be a fixed-term or lifetime local authority general needs tenancy as it’s an internal transfer and a private registered provider is on the tenancy agreement." internal_transfer: "Answer cannot be a fixed-term or lifetime local authority general needs tenancy as it’s an internal transfer and a private registered provider is on the tenancy agreement."
referral: referral:
secure_tenancy: "Answer must be internal transfer as this is a secure tenancy." 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." 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." 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." other_homeless: "Answer cannot be internal transfer as the tenant was considered homeless by their landlord."

23
config/locales/validations/lettings/property_information.en.yml

@ -1,6 +1,27 @@
en: en:
validations: validations:
lettings: lettings:
property_information: property:
postcode_full: postcode_full:
invalid: "Enter a postcode in the correct format, for example AA1 1AA." invalid: "Enter a postcode in the correct format, for example AA1 1AA."
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."
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."
unitletas:
previous_let_social: "Property cannot have a previous let type if being let as social housing for the first time."
unittype_gn:
one_bedroom_bedsit: "A bedsit can only have one bedroom."
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_bedroom_bedsit: "A bedsit can only have one bedroom."
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."

42
spec/models/validations/property_validations_spec.rb

@ -54,8 +54,8 @@ RSpec.describe Validations::PropertyValidations do
property_validator.validate_shared_housing_rooms(log) 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["unittype_gn"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_bedroom_bedsit"))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_bedroom_bedsit"))
end end
end end
end end
@ -80,14 +80,14 @@ RSpec.describe Validations::PropertyValidations do
property_validator.validate_shared_housing_rooms(log) 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["unittype_gn"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_bedroom_bedsit"))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")) expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_bedroom_bedsit"))
end end
end end
end end
context "when shared housing has more than 7 bedrooms" do 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 it "adds an error if the number of bedrooms is not between 1 and 7" do
log.beds = 8 log.beds = 8
@ -95,12 +95,12 @@ RSpec.describe Validations::PropertyValidations do
log.hhmemb = 3 log.hhmemb = 3
property_validator.validate_shared_housing_rooms(log) property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(match(expected_error)) 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
end end
context "when shared housing has less than 1 bedrooms" do 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 it "adds an error if the number of bedrooms is not between 1 and 7" do
log.beds = 0 log.beds = 0
@ -108,12 +108,12 @@ RSpec.describe Validations::PropertyValidations do
log.hhmemb = 3 log.hhmemb = 3
property_validator.validate_shared_housing_rooms(log) property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(match(expected_error)) 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
end end
context "when there are too many bedrooms for the number of household members and unit type" do 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 it "adds an error" do
log.beds = 4 log.beds = 4
@ -121,7 +121,7 @@ RSpec.describe Validations::PropertyValidations do
log.hhmemb = 1 log.hhmemb = 1
property_validator.validate_shared_housing_rooms(log) property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(match(expected_error)) 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")) expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared"))
end end
end end
end end
@ -133,19 +133,19 @@ RSpec.describe Validations::PropertyValidations do
log.unitletas = 0 log.unitletas = 0
property_validator.validate_unitletas(log) property_validator.validate_unitletas(log)
expect(log.errors["unitletas"]) expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social")) .to include(match I18n.t("validations.lettings.property.unitletas.previous_let_social"))
log.unitletas = 1 log.unitletas = 1
property_validator.validate_unitletas(log) property_validator.validate_unitletas(log)
expect(log.errors["unitletas"]) expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social")) .to include(match I18n.t("validations.lettings.property.unitletas.previous_let_social"))
log.unitletas = 2 log.unitletas = 2
property_validator.validate_unitletas(log) property_validator.validate_unitletas(log)
expect(log.errors["unitletas"]) expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social")) .to include(match I18n.t("validations.lettings.property.unitletas.previous_let_social"))
log.unitletas = 3 log.unitletas = 3
property_validator.validate_unitletas(log) property_validator.validate_unitletas(log)
expect(log.errors["unitletas"]) expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social")) .to include(match I18n.t("validations.lettings.property.unitletas.previous_let_social"))
end end
end end
@ -166,7 +166,7 @@ RSpec.describe Validations::PropertyValidations do
log.rsnvac = 6 log.rsnvac = 6
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_social")) .to include(match I18n.t("validations.lettings.property.rsnvac.first_let_social"))
end end
it "expects to have a first let reason for vacancy" do it "expects to have a first let reason for vacancy" do
@ -189,15 +189,15 @@ RSpec.describe Validations::PropertyValidations do
log.rsnvac = 15 log.rsnvac = 15
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social")) .to include(match I18n.t("validations.lettings.property.rsnvac.first_let_not_social"))
log.rsnvac = 16 log.rsnvac = 16
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social")) .to include(match I18n.t("validations.lettings.property.rsnvac.first_let_not_social"))
log.rsnvac = 17 log.rsnvac = 17
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social")) .to include(match I18n.t("validations.lettings.property.rsnvac.first_let_not_social"))
end end
it "expects the reason for vacancy to be a first let as social housing reason" do it "expects the reason for vacancy to be a first let as social housing reason" do
@ -220,7 +220,7 @@ RSpec.describe Validations::PropertyValidations do
log.rsnvac = 14 log.rsnvac = 14
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) 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 end
end end
@ -237,7 +237,7 @@ RSpec.describe Validations::PropertyValidations do
log.prevten = prevten log.prevten = prevten
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) 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
end end
@ -247,7 +247,7 @@ RSpec.describe Validations::PropertyValidations do
log.referral = src log.referral = src
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) 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 end
end end

Loading…
Cancel
Save