diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index c9515b0fa..78ece1d82 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -48,6 +48,32 @@ module Validations::SetupValidations record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.invalid") record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.invalid") end + + if owning_organisation.present? + if owning_organisation&.merge_date.present? && owning_organisation.merge_date < record.startdate + record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_merged_organisation", + owning_organisation: record.owning_organisation.name, + owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date), + owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name) + elsif owning_organisation&.absorbed_organisations.present? && owning_organisation.created_at > record.startdate + record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation", + owning_organisation: record.owning_organisation.name, + owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) + end + end + + if managing_organisation.present? + if managing_organisation&.merge_date.present? && managing_organisation.merge_date < record.startdate + record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.inactive_merged_organisation", + managing_organisation: record.managing_organisation.name, + managing_organisation_merge_date: record.managing_organisation.merge_date.to_formatted_s(:govuk_date), + managing_absorbing_organisation: record.managing_organisation.absorbing_organisation.name) + elsif managing_organisation&.absorbed_organisations.present? && managing_organisation.created_at > record.startdate + record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.inactive_absorbing_organisation", + managing_organisation: record.managing_organisation.name, + managing_organisation_available_from: record.managing_organisation.created_at.to_formatted_s(:govuk_date)) + end + end end def validate_managing_organisation_data_sharing_agremeent_signed(record) diff --git a/config/locales/en.yml b/config/locales/en.yml index 113f56c41..56ed869c1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -284,9 +284,13 @@ en: owning_organisation: invalid: "Please select the owning organisation or managing organisation that you belong to" data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation." + inactive_merged_organisation: "The owning organisation must be active on the tenancy start date. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}." + inactive_absorbing_organisation: "The owning organisation must be active on the tenancy start date. %{owning_organisation} became active on %{owning_organisation_available_from}." managing_organisation: invalid: "Please select the owning organisation or managing organisation that you belong to" data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the managing organisation." + inactive_merged_organisation: "The managing organisation must be active on the tenancy start date. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}." + inactive_absorbing_organisation: "The managing organisation must be active on the tenancy start date. %{managing_organisation} became active on %{managing_organisation_available_from}." created_by: invalid: "Please select the owning organisation or managing organisation that you belong to" lettype: diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index b2ef0f809..8b29e72ff 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -701,6 +701,82 @@ RSpec.describe Validations::SetupValidations do expect(record.errors["owning_organisation_id"]).to be_empty expect(record.errors["managing_organisation_id"]).to be_empty end + + context "when organisations are merged" do + let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") } + let(:merged_organisation) { create(:organisation, name: "Merged org") } + + around do |example| + Timecop.freeze(Time.zone.local(2023, 5, 1)) + merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) + example.run + Timecop.return + end + + context "and owning organisation is no longer active" do + it "does not allow organisation that has been merged" do + record.startdate = Time.zone.local(2023, 3, 1) + record.owning_organisation_id = merged_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the tenancy start date. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.") + end + + it "allows organisation before it has been merged" do + record.startdate = Time.zone.local(2023, 1, 1) + record.owning_organisation_id = merged_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to be_empty + end + end + + context "and owning organisation is not yet active during the startdate" do + it "does not allow absorbing organisation before it had been created" do + record.startdate = Time.zone.local(2023, 1, 1) + record.owning_organisation_id = absorbing_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the tenancy start date. Absorbing org became active on 1 February 2023.") + end + + it "allows absorbing organisation after it has been created" do + record.startdate = Time.zone.local(2023, 2, 2) + record.owning_organisation_id = absorbing_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["owning_organisation_id"]).to be_empty + end + end + + context "when managing organisation is no longer active" do + it "does not allow organisation that has been merged" do + record.startdate = Time.zone.local(2023, 3, 1) + record.managing_organisation_id = merged_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["managing_organisation_id"]).to include(match "The managing organisation must be active on the tenancy start date. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.") + end + + it "allows organisation before it has been merged" do + record.startdate = Time.zone.local(2023, 1, 1) + record.managing_organisation_id = merged_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["managing_organisation_id"]).to be_empty + end + end + + context "when managing organisation is not yet active during the startdate" do + it "does not allow absorbing organisation before it had been created" do + record.startdate = Time.zone.local(2023, 1, 1) + record.managing_organisation_id = absorbing_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["managing_organisation_id"]).to include(match "The managing organisation must be active on the tenancy start date. Absorbing org became active on 1 February 2023.") + end + + it "allows absorbing organisation after it has been created" do + record.startdate = Time.zone.local(2023, 2, 2) + record.managing_organisation_id = absorbing_organisation.id + setup_validator.validate_organisation(record) + expect(record.errors["managing_organisation_id"]).to be_empty + end + end + end end describe "#validate_scheme_has_confirmed_locations_validation" do