From 4ef43c87382f627bfa147bdb5937922050ab6895 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 21 Apr 2022 13:35:08 +0100 Subject: [PATCH] Add validation --- .../local_authority_validations.rb | 32 ++++++++++------- config/locales/en.yml | 1 + .../local_authority_validations_spec.rb | 35 +++++++++++++++++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index 72846f4e8..94f02a745 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -9,6 +9,26 @@ module Validations::LocalAuthorityValidations end end + def validate_la(record) + if record.la.present? && !LONDON_BOROUGHS.include?(record.la) && record.is_london_rent? + record.errors.add :la, I18n.t("validations.property.la.london_rent") + if record.postcode_known? && record.postcode_full.present? + record.errors.add :postcode_full, I18n.t("validations.property.la.london_rent_postcode") + end + end + + if record.la_known? && record.la.blank? + record.errors.add :la, I18n.t("validations.property.la.la_known") + end + + if record.owning_organisation && record.owning_organisation.local_authorities.present? && + record.la && !record.owning_organisation.local_authorities.include?(record.la) + la_name = record.form.get_question("la", record).label_from_value(record.la) + org_name = record.owning_organisation.name + record.errors.add :la, I18n.t("validations.property.la.la_invalid_for_org", org_name:, la_name:) + end + end + LONDON_BOROUGHS = %w[E09000001 E09000002 E09000003 @@ -42,16 +62,4 @@ module Validations::LocalAuthorityValidations E09000031 E09000032 E09000033].freeze - def validate_la(record) - if record.la.present? && !LONDON_BOROUGHS.include?(record.la) && record.is_london_rent? - record.errors.add :la, I18n.t("validations.property.la.london_rent") - if record.postcode_known? && record.postcode_full.present? - record.errors.add :postcode_full, I18n.t("validations.property.la.london_rent_postcode") - end - end - - if record.la_known? && record.la.blank? - record.errors.add :la, I18n.t("validations.property.la.la_known") - end - end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 076f547bf..2d66ac916 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -70,6 +70,7 @@ en: london_rent: "Local authority must be in London" london_rent_postcode: "The postcode must be a London postcode because you told us the rent type is London Affordable Rent or London Living Rent" la_known: "Enter name of local authority" + la_invalid_for_org: "%{org_name} does not operate in %{la_name}" rsnvac: first_let_not_social: "Reason for vacancy cannot be 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" diff --git a/spec/models/validations/local_authority_validations_spec.rb b/spec/models/validations/local_authority_validations_spec.rb index a2394f49d..ddd3afe8f 100644 --- a/spec/models/validations/local_authority_validations_spec.rb +++ b/spec/models/validations/local_authority_validations_spec.rb @@ -77,5 +77,40 @@ RSpec.describe Validations::LocalAuthorityValidations do .to include(match I18n.t("validations.property.la.la_known")) end end + + context "when the organisation only operates in specific local authorities" do + let(:organisation) { FactoryBot.create(:organisation) } + let(:record) { FactoryBot.create(:case_log, owning_organisation: organisation) } + + before do + FactoryBot.create(:organisation_la, organisation:, ons_code: "E07000178") + FactoryBot.create(:organisation_la, organisation:, ons_code: "E09000033") + end + + it "validates that the local authority is one the owning organisation operates in" do + record.la = "E06000014" + local_auth_validator.validate_la(record) + expect(record.errors["la"]) + .to include(match I18n.t( + "validations.property.la.la_invalid_for_org", + org_name: organisation.name, + la_name: "York", + )) + end + + it "expects that the local authority can be one that the owning organisation operates in" do + record.la = "E07000178" + local_auth_validator.validate_la(record) + expect(record.errors["la"]).to be_empty + end + end + + context "when the organisation has not listed specific local authorities it operates in" do + it "does not validate the local authority for the organisation" do + record.la = "E06000014" + local_auth_validator.validate_la(record) + expect(record.errors["la"]).to be_empty + end + end end end