diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index 4518ea082..cfdb2b435 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -48,13 +48,23 @@ module Validations::PropertyValidations 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") + return unless record.form.start_year_2025_or_later? + + if record.is_general_needs? + return unless record.la + 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") + elsif record.is_supported_housing? + return unless record.location + return if record.location.location_code.in?(LocalAuthority.england.pluck(:code)) + + record.errors.add :location_id, I18n.t("validations.lettings.property.location_id.not_in_england") + record.errors.add :scheme_id, I18n.t("validations.lettings.property.scheme_id.not_in_england") + end end end diff --git a/config/locales/validations/lettings/property_information.en.yml b/config/locales/validations/lettings/property_information.en.yml index 068b8f15c..3a6e7fe5c 100644 --- a/config/locales/validations/lettings/property_information.en.yml +++ b/config/locales/validations/lettings/property_information.en.yml @@ -26,3 +26,7 @@ en: 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" + scheme_id: + not_in_england: "It looks like you have selected a location with a local authority outside of England - only submit Lettings forms for Lettings that occur in England" + location_id: + not_in_england: "It looks like you have selected a location with a local authority outside of England - only submit Lettings forms for Lettings that occur in England" diff --git a/spec/models/validations/property_validations_spec.rb b/spec/models/validations/property_validations_spec.rb index ac22423b4..247bc481a 100644 --- a/spec/models/validations/property_validations_spec.rb +++ b/spec/models/validations/property_validations_spec.rb @@ -211,8 +211,8 @@ RSpec.describe Validations::PropertyValidations 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") } + context "and the local authority is not in England for general needs log" do + let(:log) { build(:lettings_log, la: "S12000019", needstype: 1) } it "adds an error" do property_validator.validate_la_in_england(log) @@ -221,6 +221,24 @@ RSpec.describe Validations::PropertyValidations do 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")) + expect(log.errors["scheme_id"]).to be_empty + expect(log.errors["location_id"]).to be_empty + end + end + + context "and the local authority is not in England for supported housing log" do + let(:location) { create(:location, location_code: "S12000019") } + let(:log) { build(:lettings_log, la: "S12000019", needstype: 2, location:) } + + it "adds an error" do + property_validator.validate_la_in_england(log) + expect(log.errors["scheme_id"]).to include(I18n.t("validations.lettings.property.scheme_id.not_in_england")) + expect(log.errors["location_id"]).to include(I18n.t("validations.lettings.property.location_id.not_in_england")) + 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 diff --git a/spec/models/validations/sales/property_validations_spec.rb b/spec/models/validations/sales/property_validations_spec.rb index 68b23f593..83ef7c2c9 100644 --- a/spec/models/validations/sales/property_validations_spec.rb +++ b/spec/models/validations/sales/property_validations_spec.rb @@ -150,7 +150,7 @@ RSpec.describe Validations::Sales::PropertyValidations do end context "and the local authority is not in England" do - let(:log) { build(:lettings_log, la: "S12000019") } + let(:log) { build(:sales_log, la: "S12000019") } it "adds an error" do property_validator.validate_la_in_england(log) @@ -163,7 +163,7 @@ RSpec.describe Validations::Sales::PropertyValidations do end context "and the local authority is in England" do - let(:log) { build(:lettings_log, la: "E06000002") } + let(:log) { build(:sales_log, la: "E06000002") } it "does not add an error" do property_validator.validate_la_in_england(log) @@ -182,7 +182,7 @@ RSpec.describe Validations::Sales::PropertyValidations do end context "and the local authority is not in England" do - let(:log) { build(:lettings_log, la: "S12000019") } + let(:log) { build(:sales_log, la: "S12000019") } it "does not add an error" do property_validator.validate_la_in_england(log)