From 4923b08bdd9be9d10ecdcdd749ab9b7d1147bd0c Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 29 Nov 2023 14:28:43 +0000 Subject: [PATCH] CLDC-2997 Validate organisation before schemes (#2057) * Validate organisation before schemes * Remove redundant validation --- app/models/validations/date_validations.rb | 3 - app/models/validations/setup_validations.rb | 55 +++-- .../validations/date_validations_spec.rb | 207 ------------------ .../validations/setup_validations_spec.rb | 115 ---------- 4 files changed, 27 insertions(+), 353 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 3757f1cfb..47c5d9638 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -53,9 +53,6 @@ module Validations::DateValidations if record["mrcdate"].present? && record["startdate"].to_date - record["mrcdate"].to_date > 3650 record.errors.add :startdate, I18n.t("validations.setup.startdate.ten_years_after_mrc_date") end - - location_during_startdate_validation(record) - scheme_during_startdate_validation(record) end private diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 5ea921e4f..63b15c39f 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -18,34 +18,6 @@ module Validations::SetupValidations validate_merged_organisations_start_date(record) end - def validate_irproduct_other(record) - if intermediate_product_rent_type?(record) && record.irproduct_other.blank? - record.errors.add :irproduct_other, I18n.t("validations.setup.intermediate_rent_product_name.blank") - end - end - - def validate_location(record) - location_during_startdate_validation(record) - - if record.location&.status == :incomplete - record.errors.add :location_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") - record.errors.add :scheme_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") - end - end - - def validate_scheme_has_confirmed_locations_validation(record) - return unless record.scheme - - unless record.scheme.locations.confirmed.any? - record.errors.add :scheme_id, :no_completed_locations, message: I18n.t("validations.scheme.no_completed_locations") - end - end - - def validate_scheme(record) - location_during_startdate_validation(record) - scheme_during_startdate_validation(record) - end - def validate_organisation(record) created_by, managing_organisation, owning_organisation = record.values_at("created_by", "managing_organisation", "owning_organisation") unless [created_by, managing_organisation, owning_organisation].any?(&:blank?) || ((created_by.organisation.absorbed_organisations + [created_by.organisation]) & [managing_organisation, owning_organisation]).present? @@ -82,6 +54,33 @@ module Validations::SetupValidations end end + def validate_irproduct_other(record) + if intermediate_product_rent_type?(record) && record.irproduct_other.blank? + record.errors.add :irproduct_other, I18n.t("validations.setup.intermediate_rent_product_name.blank") + end + end + + def validate_location(record) + location_during_startdate_validation(record) + + if record.location&.status == :incomplete + record.errors.add :location_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") + record.errors.add :scheme_id, :incomplete, message: I18n.t("validations.setup.location.incomplete") + end + end + + def validate_scheme_has_confirmed_locations_validation(record) + return unless record.scheme + + unless record.scheme.locations.confirmed.any? + record.errors.add :scheme_id, :no_completed_locations, message: I18n.t("validations.scheme.no_completed_locations") + end + end + + def validate_scheme(record) + scheme_during_startdate_validation(record) + end + def validate_managing_organisation_data_sharing_agremeent_signed(record) if record.managing_organisation_id_changed? && record.managing_organisation.present? && !record.managing_organisation.data_protection_confirmed? record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.data_sharing_agreement_not_signed") diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 977d47e4c..0fc706707 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -72,213 +72,6 @@ RSpec.describe Validations::DateValidations do date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty end - - context "with a deactivated location" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } - - before do - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) - location.reload - end - - it "produces error when tenancy start date is during deactivated location period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) - end - - it "produces no error when tenancy start date is during an active location period" do - record.startdate = Time.zone.local(2022, 6, 1) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - end - - context "with a location that is reactivating soon" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } - - before do - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - location.reload - end - - it "produces error when tenancy start date is during deactivated location period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) - end - - it "produces no error when tenancy start date is during an active location period" do - record.startdate = Time.zone.local(2022, 9, 1) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - end - - context "with a location that has many reactivations soon" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } - - before do - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) - location.reload - end - - it "produces error when tenancy start date is during deactivated location period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) - end - - it "produces no error when tenancy start date is during an active location period" do - record.startdate = Time.zone.local(2022, 10, 1) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - end - - context "with a location that is activating soon (has no deactivation periods)" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) } - - it "produces no error" do - record.startdate = Time.zone.local(2022, 10, 15) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - - it "produces an error when the date is before available_from date" do - record.startdate = Time.zone.local(2022, 8, 15) - record.location = location - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) - end - end - - context "with a deactivated scheme" do - let(:scheme) { create(:scheme) } - - before do - create(:location, scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) - scheme.reload - end - - it "produces error when tenancy start date is during deactivated scheme period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.scheme = scheme - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.scheme.deactivated.startdate", name: scheme.service_name, date: "4 June 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.scheme.deactivated.scheme_id", name: scheme.service_name, date: "4 June 2022")) - end - - it "produces no error when tenancy start date is during an active scheme period" do - record.startdate = Time.zone.local(2022, 6, 1) - record.scheme = scheme - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - end - - context "with a scheme that is reactivating soon" do - let(:scheme) { create(:scheme) } - - before do - create(:location, scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - scheme.reload - end - - it "produces error when tenancy start date is during deactivated scheme period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.scheme = scheme - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022")) - end - - it "produces no error when tenancy start date is during an active scheme period" do - record.startdate = Time.zone.local(2022, 9, 1) - record.scheme = scheme - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - end - - context "with a scheme that has many reactivations soon" do - let(:scheme) { create(:scheme) } - - before do - create(:location, scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) - scheme.reload - end - - it "produces error when tenancy start date is during deactivated scheme period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.scheme = scheme - date_validator.validate_startdate(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 September 2022")) - expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 September 2022")) - end - - it "produces no error when tenancy start date is during an active scheme period" do - record.startdate = Time.zone.local(2022, 10, 1) - record.scheme = scheme - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["scheme_id"]).to be_empty - end - end end describe "major repairs date" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index a7a038cc4..2a05ecb77 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -461,121 +461,6 @@ RSpec.describe Validations::SetupValidations do expect(record.errors["scheme_id"]).to be_empty end end - - context "with a deactivated location" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } - - before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) - Timecop.return - location.reload - end - - it "produces error when tenancy start date is during deactivated location period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: location.postcode, date: "4 June 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated.location_id", postcode: location.postcode, date: "4 June 2022")) - end - - it "produces no error when tenancy start date is during an active location period" do - record.startdate = Time.zone.local(2022, 6, 1) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - end - end - - context "with a location that is reactivating soon" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } - - before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - Timecop.return - location.reload - end - - it "produces error when tenancy start date is during deactivated location period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 August 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 August 2022")) - end - - it "produces no error when tenancy start date is during an active location period" do - record.startdate = Time.zone.local(2022, 9, 1) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - end - end - - context "with a location that has many reactivations soon" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:) } - - before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) - Timecop.return - location.reload - end - - it "produces error when tenancy start date is during deactivated location period" do - record.startdate = Time.zone.local(2022, 7, 5) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.startdate", postcode: location.postcode, date: "4 September 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon.location_id", postcode: location.postcode, date: "4 September 2022")) - end - - it "produces no error when tenancy start date is during an active location period" do - record.startdate = Time.zone.local(2022, 10, 1) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - end - end - - context "with a location that is activating soon (has no deactivation periods)" do - let(:scheme) { create(:scheme) } - let(:location) { create(:location, scheme:, startdate: Time.zone.local(2022, 9, 15)) } - - it "produces no error" do - record.startdate = Time.zone.local(2022, 10, 15) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]).to be_empty - expect(record.errors["location_id"]).to be_empty - end - - it "produces an error when the date is before available_from date" do - record.startdate = Time.zone.local(2022, 8, 15) - record.location = location - setup_validator.validate_scheme(record) - expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon.startdate", postcode: location.postcode, date: "15 September 2022")) - expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon.location_id", postcode: location.postcode, date: "15 September 2022")) - end - end end describe "#validate_location" do