diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index 09e6d1436..4518ea082 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -46,4 +46,15 @@ module Validations::PropertyValidations record.errors.add :postcode_full, :wrong_format, message: error_message end end + + def validate_la_in_england(record) + return unless record.form.start_year_2025_or_later? && record.la.present? + return if record.la.in?(LocalAuthority.england.pluck(:code)) + + record.errors.add :la, I18n.t("validations.lettings.property.la.not_in_england") + record.errors.add :postcode_full, I18n.t("validations.lettings.property.postcode_full.not_in_england") + record.errors.add :uprn, I18n.t("validations.lettings.property.uprn.not_in_england") + record.errors.add :uprn_confirmation, I18n.t("validations.lettings.property.uprn_confirmation.not_in_england") + record.errors.add :uprn_selection, I18n.t("validations.lettings.property.uprn_selection.not_in_england") + end end diff --git a/app/models/validations/sales/property_validations.rb b/app/models/validations/sales/property_validations.rb index 5cf70ed8d..0c6ea1b67 100644 --- a/app/models/validations/sales/property_validations.rb +++ b/app/models/validations/sales/property_validations.rb @@ -36,4 +36,15 @@ module Validations::Sales::PropertyValidations record.errors.add :postcode_full, :wrong_format, message: error_message end end + + def validate_la_in_england(record) + return unless record.form.start_year_2025_or_later? && record.la.present? + return if record.la.in?(LocalAuthority.england.pluck(:code)) + + record.errors.add :la, I18n.t("validations.sales.property_information.la.not_in_england") + record.errors.add :postcode_full, I18n.t("validations.sales.property_information.postcode_full.not_in_england") + record.errors.add :uprn, I18n.t("validations.sales.property_information.uprn.not_in_england") + record.errors.add :uprn_confirmation, I18n.t("validations.sales.property_information.uprn_confirmation.not_in_england") + record.errors.add :uprn_selection, I18n.t("validations.sales.property_information.uprn_selection.not_in_england") + end end diff --git a/config/locales/validations/lettings/property_information.en.yml b/config/locales/validations/lettings/property_information.en.yml index 6530c9488..068b8f15c 100644 --- a/config/locales/validations/lettings/property_information.en.yml +++ b/config/locales/validations/lettings/property_information.en.yml @@ -4,6 +4,7 @@ en: property: postcode_full: invalid: "Enter a postcode in the correct format, for example AA1 1AA." + not_in_england: "It looks like you have entered a postcode for a local authority outside of England - only submit Lettings forms for Lettings that occur in England" 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." @@ -18,4 +19,10 @@ en: one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms." uprn: invalid: "UPRN must be 12 digits or less." - + not_in_england: "It looks like you have entered a UPRN for a local authority outside of England - only submit Lettings forms for Lettings that occur in England" + uprn_confirmation: + not_in_england: "It looks like you have entered a UPRN for a local authority outside of England - only submit Lettings forms for Lettings that occur in England" + uprn_selection: + not_in_england: "It looks like you have selected an address for a local authority outside of England - only submit Lettings forms for Lettings that occur in England" + la: + not_in_england: "It looks like you have entered a local authority outside of England - only submit Lettings forms for Lettings that occur in England" diff --git a/config/locales/validations/sales/property_information.en.yml b/config/locales/validations/sales/property_information.en.yml index a91e47849..8bfefbd6d 100644 --- a/config/locales/validations/sales/property_information.en.yml +++ b/config/locales/validations/sales/property_information.en.yml @@ -7,6 +7,7 @@ en: joint_purchase: "Buyers’ last accommodation and discounted ownership postcodes must match." not_joint_purchase: "Buyer’s last accommodation and discounted ownership postcodes must match." invalid: "Enter a postcode in the correct format, for example AA1 1AA." + not_in_england: "It looks like you have entered a postcode for a local authority outside of England - only submit Sales forms for Sales that occur in England" ppostcode_full: postcode_must_match_previous: joint_purchase: "Buyers’ last accommodation and discounted ownership postcodes must match." @@ -20,9 +21,16 @@ en: joint_purchase: "Buyers’ last accommodation and discounted ownership postcodes must match." not_joint_purchase: "Buyer’s last accommodation and discounted ownership postcodes must match." invalid: "UPRN must be 12 digits or less." + not_in_england: "It looks like you have entered a UPRN for a local authority outside of England - only submit Sales forms for Sales that occur in England" beds: bedsits_have_max_one_bedroom: "Number of bedrooms must be 1 if the property is a bedsit." proptype: bedsits_have_max_one_bedroom: "Answer cannot be 'Bedsit' if the property has 2 or more bedrooms." uprn_known: invalid: "You must answer UPRN known?" + la: + not_in_england: "It looks like you have entered a local authority outside of England - only submit Sales forms for Sales that occur in England" + uprn_confirmation: + not_in_england: "It looks like you have entered a UPRN for a local authority outside of England - only submit Sales forms for Sales that occur in England" + uprn_selection: + not_in_england: "It looks like you have selected an address for a local authority outside of England - only submit Sales forms for Sales that occur in England" diff --git a/spec/models/validations/property_validations_spec.rb b/spec/models/validations/property_validations_spec.rb index f478ae466..ac22423b4 100644 --- a/spec/models/validations/property_validations_spec.rb +++ b/spec/models/validations/property_validations_spec.rb @@ -204,4 +204,57 @@ RSpec.describe Validations::PropertyValidations do end end end + + describe "#validate_la_in_england" do + context "with a log on or after 2025" do + before do + allow(log.form).to receive(:start_year_2025_or_later?).and_return true + end + + context "and the local authority is not in England" do + let(:log) { build(:lettings_log, la: "S12000019") } + + it "adds an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["la"]).to include(I18n.t("validations.lettings.property.la.not_in_england")) + expect(log.errors["postcode_full"]).to include(I18n.t("validations.lettings.property.postcode_full.not_in_england")) + expect(log.errors["uprn"]).to include(I18n.t("validations.lettings.property.uprn.not_in_england")) + expect(log.errors["uprn_confirmation"]).to include(I18n.t("validations.lettings.property.uprn_confirmation.not_in_england")) + expect(log.errors["uprn_selection"]).to include(I18n.t("validations.lettings.property.uprn_selection.not_in_england")) + end + end + + context "and the local authority is in England" do + let(:log) { build(:lettings_log, la: "E06000002") } + + it "does not add an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["la"]).to be_empty + expect(log.errors["postcode_full"]).to be_empty + expect(log.errors["uprn"]).to be_empty + expect(log.errors["uprn_confirmation"]).to be_empty + expect(log.errors["uprn_selection"]).to be_empty + end + end + end + + context "with a log before 2025" do + before do + allow(log.form).to receive(:start_year_2025_or_later?).and_return false + end + + context "and the local authority is not in England" do + let(:log) { build(:lettings_log, la: "S12000019") } + + it "does not add an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["la"]).to be_empty + expect(log.errors["postcode_full"]).to be_empty + expect(log.errors["uprn"]).to be_empty + expect(log.errors["uprn_confirmation"]).to be_empty + expect(log.errors["uprn_selection"]).to be_empty + end + end + end + end end diff --git a/spec/models/validations/sales/property_validations_spec.rb b/spec/models/validations/sales/property_validations_spec.rb index df0396845..68b23f593 100644 --- a/spec/models/validations/sales/property_validations_spec.rb +++ b/spec/models/validations/sales/property_validations_spec.rb @@ -142,4 +142,57 @@ RSpec.describe Validations::Sales::PropertyValidations do end end end + + describe "#validate_la_in_england" do + context "with a log on or after 2025" do + before do + allow(log.form).to receive(:start_year_2025_or_later?).and_return true + end + + context "and the local authority is not in England" do + let(:log) { build(:lettings_log, la: "S12000019") } + + it "adds an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["la"]).to include(I18n.t("validations.sales.property_information.la.not_in_england")) + expect(log.errors["postcode_full"]).to include(I18n.t("validations.sales.property_information.postcode_full.not_in_england")) + expect(log.errors["uprn"]).to include(I18n.t("validations.sales.property_information.uprn.not_in_england")) + expect(log.errors["uprn_confirmation"]).to include(I18n.t("validations.sales.property_information.uprn_confirmation.not_in_england")) + expect(log.errors["uprn_selection"]).to include(I18n.t("validations.sales.property_information.uprn_selection.not_in_england")) + end + end + + context "and the local authority is in England" do + let(:log) { build(:lettings_log, la: "E06000002") } + + it "does not add an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["la"]).to be_empty + expect(log.errors["postcode_full"]).to be_empty + expect(log.errors["uprn"]).to be_empty + expect(log.errors["uprn_confirmation"]).to be_empty + expect(log.errors["uprn_selection"]).to be_empty + end + end + end + + context "with a log before 2025" do + before do + allow(log.form).to receive(:start_year_2025_or_later?).and_return false + end + + context "and the local authority is not in England" do + let(:log) { build(:lettings_log, la: "S12000019") } + + it "does not add an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["la"]).to be_empty + expect(log.errors["postcode_full"]).to be_empty + expect(log.errors["uprn"]).to be_empty + expect(log.errors["uprn_confirmation"]).to be_empty + expect(log.errors["uprn_selection"]).to be_empty + end + end + end + end end