Browse Source

Add start date validations for merging orgs

pull/1824/head
Kat 3 years ago
parent
commit
a606687523
  1. 101
      app/models/validations/setup_validations.rb
  2. 11
      config/locales/en.yml
  3. 180
      spec/models/validations/setup_validations_spec.rb

101
app/models/validations/setup_validations.rb

@ -14,6 +14,8 @@ module Validations::SetupValidations
unless record.startdate.between?(first_collection_start_date, current_collection_end_date)
record.errors.add :startdate, startdate_validation_error_message
end
validate_merged_organisations_start_date(record)
end
def validate_irproduct_other(record)
@ -98,4 +100,103 @@ private
def intermediate_product_rent_type?(record)
record.rent_type == 5
end
def validate_merged_organisations_start_date(record)
return add_same_merge_organisation_error(record) if record.owning_organisation == record.managing_organisation
return add_same_merge_error(record) if organisations_belong_to_same_merge?(record.owning_organisation, record.managing_organisation)
add_merged_organisations_errors(record)
add_absorbing_organisations_errors(record)
end
def add_same_merge_organisation_error(record)
if merged_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.same_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 absorbing_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.same_organisation",
owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date))
end
end
def add_same_merge_error(record)
if merged_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.same_merge",
owning_organisation: record.owning_organisation.name,
managing_organisation: record.managing_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)
end
end
def add_merged_organisations_errors(record)
if merged_owning_organisation_inactive?(record) && merged_managing_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.different_merge",
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,
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)
else
if merged_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.owning_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)
end
if merged_managing_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.managing_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)
end
end
end
def add_absorbing_organisations_errors(record)
if absorbing_owning_organisation_inactive?(record) && absorbing_managing_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.different_organisations",
owning_organisation: record.owning_organisation.name,
owning_organisation_active_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date),
managing_organisation: record.managing_organisation.name,
managing_organisation_active_from: record.managing_organisation.created_at.to_formatted_s(:govuk_date))
else
if absorbing_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.owning_organisation",
owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date))
end
if absorbing_managing_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.managing_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 merged_owning_organisation_inactive?(record)
record.owning_organisation&.merge_date.present? && record.owning_organisation.merge_date < record.startdate
end
def merged_managing_organisation_inactive?(record)
record.managing_organisation&.merge_date.present? && record.managing_organisation.merge_date < record.startdate
end
def absorbing_owning_organisation_inactive?(record)
record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at > record.startdate
end
def absorbing_managing_organisation_inactive?(record)
record.managing_organisation&.absorbed_organisations.present? && record.managing_organisation.created_at > record.startdate
end
def organisations_belong_to_same_merge?(organisation_a, organisation_b)
organisation_a.merge_date.present? && organisation_a.merge_date == organisation_b.merge_date && organisation_a.absorbing_organisation == organisation_b.absorbing_organisation
end
end

11
config/locales/en.yml

@ -252,6 +252,17 @@ en:
year_not_two_digits: Tenancy start year must be 2 digits
ten_years_after_void_date: "Enter a tenancy start date that is no more than 10 years after the void date"
ten_years_after_mrc_date: "Enter a tenancy start date that is no more than 10 years after the major repairs completion date"
invalid_merged_organisations_start_date:
same_organisation: "Enter a date when the owning and managing organisation was active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}."
same_merge: "Enter a date when the owning and managing organisations were active. %{owning_organisation} and %{managing_organisation} became inactive on %{owning_organisation_merge_date} and were replaced by %{owning_absorbing_organisation}."
owning_organisation: "Enter a date when the owning organisation was active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}."
managing_organisation: "Enter a date when the managing organisation was active. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}."
different_merge: "Enter a date when the owning and managing organisations were active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}."
invalid_absorbing_organisations_start_date:
same_organisation: "Enter a date when the owning and managing organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}."
owning_organisation: "Enter a date when the owning organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}."
managing_organisation: "Enter a date when the managing organisation was active. %{managing_organisation} became active on %{managing_organisation_available_from}."
different_organisations: "Enter a date when the owning and managing organisations were active. %{owning_organisation} became active on %{owning_organisation_active_from}, and %{managing_organisation} became active on %{managing_organisation_active_from}."
location:
deactivated:

180
spec/models/validations/setup_validations_spec.rb

@ -128,6 +128,186 @@ RSpec.describe Validations::SetupValidations do
end
end
end
context "when organisations were merged" do
around do |example|
Timecop.freeze(Time.zone.local(2023, 5, 1))
example.run
Timecop.return
end
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:absorbing_organisation_2) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org 2") }
let(:merged_organisation) { create(:organisation, name: "Merged org") }
let(:merged_organisation_2) { create(:organisation, name: "Merged org 2") }
before do
merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2))
merged_organisation_2.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2))
end
context "and owning organisation is no longer active" do
it "does not allow startate after organisation has been merged" do
record.startdate = Time.zone.local(2023, 3, 1)
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning organisation was active. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.")
end
it "allows startate before organisation has been merged" do
record.startdate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and owning organisation is not yet active during the startdate" do
it "does not allow startate before absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning organisation was active. Absorbing org became active on 1 February 2023.")
end
it "allows startate after absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and managing organisation is no longer active during the startdate" do
it "does not allow startate after organisation has been merged" do
record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the managing organisation was active. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.")
end
it "allows startate before organisation has been merged" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and managing organisation is not yet active during the startdate" do
it "does not allow startate before absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the managing organisation was active. Absorbing org became active on 1 February 2023.")
end
it "allows startate after absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and owning and managing organisation is no longer active during the startdate" do
it "does not allow startate after organisation has been merged" do
record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning and managing organisation was active. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.")
end
it "allows startate before organisation has been merged" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and owning and managing organisation is not yet active during the startdate" do
it "does not allow startate before absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning and managing organisation was active. Absorbing org became active on 1 February 2023.")
end
it "allows startate after absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and owning and managing organisations are no longer active during the startdate" do
it "does not allow startate after organisation have been merged" do
record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning and managing organisations were active. Merged org 2 and Merged org became inactive on 2 February 2023 and were replaced by Absorbing org.")
end
it "allows startate before organisations have been merged" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and owning and managing organisations are from different merges and no longer active during the startdate" do
before do
merged_organisation_2.update!(absorbing_organisation: absorbing_organisation_2, merge_date: Time.zone.local(2023, 2, 2))
end
it "does not allow startate after organisations have been merged" do
record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning and managing organisations were active. Merged org 2 became inactive on 2 February 2023 and was replaced by Absorbing org 2. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.")
end
it "allows startate before organisations have been merged" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
context "and owning and managing organisation have different merges and are not yet active during the startdate" do
before do
merged_organisation_2.update!(absorbing_organisation: absorbing_organisation_2, merge_date: Time.zone.local(2023, 2, 2))
end
it "does not allow startate before absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation_2.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to include(match "Enter a date when the owning and managing organisations were active. Absorbing org 2 became active on 1 February 2023, and Absorbing org became active on 1 February 2023.")
end
it "allows startate after absorbing organisation has been created" do
record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end
end
end
describe "#validate_irproduct" do

Loading…
Cancel
Save