Browse Source

CLCD-2836 Use available from date for absorbing organisations validations (#2000)

* Use available from date for absorbing organisations validations

* Update sales validations

* typo

* Display correct date in the error message
pull/2003/head
kosiakkatrina 1 year ago committed by GitHub
parent
commit
4de6003f5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/models/validations/sales/setup_validations.rb
  2. 20
      app/models/validations/setup_validations.rb
  3. 5
      db/migrate/20231023142854_add_available_from_to_org.rb
  4. 3
      db/schema.rb
  5. 28
      spec/models/validations/sales/setup_validations_spec.rb
  6. 100
      spec/models/validations/setup_validations_spec.rb

8
app/models/validations/sales/setup_validations.rb

@ -37,7 +37,7 @@ module Validations::Sales::SetupValidations
if absorbing_owning_organisation_inactive?(record) if absorbing_owning_organisation_inactive?(record)
record.errors.add :saledate, I18n.t("validations.setup.saledate.invalid_absorbing_organisations_saledate", record.errors.add :saledate, I18n.t("validations.setup.saledate.invalid_absorbing_organisations_saledate",
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
end end
end end
@ -50,10 +50,10 @@ module Validations::Sales::SetupValidations
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date), owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date),
owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name) owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name)
elsif record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at.to_date > record.saledate.to_date elsif record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.available_from.present? && record.owning_organisation.available_from.to_date > record.saledate.to_date
record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation_sales", record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation_sales",
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
end end
end end
end end
@ -104,6 +104,6 @@ private
end end
def absorbing_owning_organisation_inactive?(record) def absorbing_owning_organisation_inactive?(record)
record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at.to_date > record.saledate.to_date record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.available_from.present? && record.owning_organisation.available_from.to_date > record.saledate.to_date
end end
end end

20
app/models/validations/setup_validations.rb

@ -61,10 +61,10 @@ module Validations::SetupValidations
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date), owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date),
owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name) owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name)
elsif owning_organisation&.absorbed_organisations.present? && owning_organisation.created_at.to_date > record.startdate.to_date elsif owning_organisation&.absorbed_organisations.present? && owning_organisation.available_from.present? && owning_organisation.available_from.to_date > record.startdate.to_date
record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation", record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation",
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
end end
end end
@ -74,10 +74,10 @@ module Validations::SetupValidations
managing_organisation: record.managing_organisation.name, managing_organisation: record.managing_organisation.name,
managing_organisation_merge_date: record.managing_organisation.merge_date.to_formatted_s(:govuk_date), managing_organisation_merge_date: record.managing_organisation.merge_date.to_formatted_s(:govuk_date),
managing_absorbing_organisation: record.managing_organisation.absorbing_organisation.name) managing_absorbing_organisation: record.managing_organisation.absorbing_organisation.name)
elsif managing_organisation&.absorbed_organisations.present? && managing_organisation.created_at.to_date > record.startdate.to_date elsif managing_organisation&.absorbed_organisations.present? && managing_organisation.available_from.present? && managing_organisation.available_from.to_date > record.startdate.to_date
record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.inactive_absorbing_organisation", record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.inactive_absorbing_organisation",
managing_organisation: record.managing_organisation.name, managing_organisation: record.managing_organisation.name,
managing_organisation_available_from: record.managing_organisation.created_at.to_formatted_s(:govuk_date)) managing_organisation_available_from: record.managing_organisation.available_from.to_formatted_s(:govuk_date))
end end
end end
end end
@ -194,20 +194,20 @@ private
if absorbing_owning_organisation_inactive?(record) && absorbing_managing_organisation_inactive?(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", record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.different_organisations",
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_active_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date), owning_organisation_active_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date),
managing_organisation: record.managing_organisation.name, managing_organisation: record.managing_organisation.name,
managing_organisation_active_from: record.managing_organisation.created_at.to_formatted_s(:govuk_date)) managing_organisation_active_from: record.managing_organisation.available_from.to_formatted_s(:govuk_date))
else else
if absorbing_owning_organisation_inactive?(record) if absorbing_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.owning_organisation", record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.owning_organisation",
owning_organisation: record.owning_organisation.name, owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date)) owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
end end
if absorbing_managing_organisation_inactive?(record) if absorbing_managing_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.managing_organisation", record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_absorbing_organisations_start_date.managing_organisation",
managing_organisation: record.managing_organisation.name, managing_organisation: record.managing_organisation.name,
managing_organisation_available_from: record.managing_organisation.created_at.to_formatted_s(:govuk_date)) managing_organisation_available_from: record.managing_organisation.available_from.to_formatted_s(:govuk_date))
end end
end end
end end
@ -221,11 +221,11 @@ private
end end
def absorbing_owning_organisation_inactive?(record) def absorbing_owning_organisation_inactive?(record)
record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at.to_date > record.startdate.to_date record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.available_from.present? && record.owning_organisation.available_from.to_date > record.startdate.to_date
end end
def absorbing_managing_organisation_inactive?(record) def absorbing_managing_organisation_inactive?(record)
record.managing_organisation&.absorbed_organisations.present? && record.managing_organisation.created_at.to_date > record.startdate.to_date record.managing_organisation&.absorbed_organisations.present? && record.managing_organisation.available_from.present? && record.managing_organisation.available_from.to_date > record.startdate.to_date
end end
def organisations_belong_to_same_merge?(organisation_a, organisation_b) def organisations_belong_to_same_merge?(organisation_a, organisation_b)

5
db/migrate/20231023142854_add_available_from_to_org.rb

@ -0,0 +1,5 @@
class AddAvailableFromToOrg < ActiveRecord::Migration[7.0]
def change
add_column :organisations, :available_from, :datetime
end
end

3
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_09_13_093443) do ActiveRecord::Schema[7.0].define(version: 2023_10_23_142854) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -440,6 +440,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_13_093443) do
t.string "old_visible_id" t.string "old_visible_id"
t.datetime "merge_date" t.datetime "merge_date"
t.bigint "absorbing_organisation_id" t.bigint "absorbing_organisation_id"
t.datetime "available_from"
t.index ["absorbing_organisation_id"], name: "index_organisations_on_absorbing_organisation_id" t.index ["absorbing_organisation_id"], name: "index_organisations_on_absorbing_organisation_id"
t.index ["old_visible_id"], name: "index_organisations_on_old_visible_id", unique: true t.index ["old_visible_id"], name: "index_organisations_on_old_visible_id", unique: true
end end

28
spec/models/validations/sales/setup_validations_spec.rb

@ -188,7 +188,7 @@ RSpec.describe Validations::Sales::SetupValidations do
describe "#validate_merged_organisations_saledate" do describe "#validate_merged_organisations_saledate" do
let(:record) { build(:sales_log) } let(:record) { build(:sales_log) }
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example| around do |example|
@ -218,19 +218,27 @@ RSpec.describe Validations::Sales::SetupValidations do
end end
context "and owning organisation is not yet active during the saledate" do context "and owning organisation is not yet active during the saledate" do
it "does not allow saledate before absorbing organisation has been created" do it "does not allow saledate before absorbing organisation has become available" do
record.saledate = Time.zone.local(2023, 1, 1) record.saledate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_merged_organisations_saledate(record) setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to include(match "Enter a date when the owning organisation was active. Absorbing org became active on 1 February 2023.") expect(record.errors["saledate"]).to include(match "Enter a date when the owning organisation was active. Absorbing org became active on 1 February 2023.")
end end
it "allows saledate after absorbing organisation has been created" do it "allows saledate after absorbing organisation has become available" do
record.saledate = Time.zone.local(2023, 2, 2) record.saledate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_merged_organisations_saledate(record) setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to be_empty expect(record.errors["saledate"]).to be_empty
end end
it "allows saledate if available from is not given" do
record.saledate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to be_empty
end
end end
end end
@ -238,7 +246,7 @@ RSpec.describe Validations::Sales::SetupValidations do
let(:record) { build(:sales_log) } let(:record) { build(:sales_log) }
context "when organisations are merged" do context "when organisations are merged" do
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example| around do |example|
@ -265,19 +273,27 @@ RSpec.describe Validations::Sales::SetupValidations do
end end
context "and owning organisation is not yet active during the saledate" do context "and owning organisation is not yet active during the saledate" do
it "does not allow absorbing organisation before it had been created" do it "does not allow absorbing organisation before it has become available" do
record.saledate = Time.zone.local(2023, 1, 1) record.saledate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record) setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the sale completion date. Absorbing org became active on 1 February 2023.") expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the sale completion date. Absorbing org became active on 1 February 2023.")
end end
it "allows absorbing organisation after it has been created" do it "allows absorbing organisation after it has become available" do
record.saledate = Time.zone.local(2023, 2, 2) record.saledate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record) setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to be_empty expect(record.errors["owning_organisation_id"]).to be_empty
end end
it "allows absorbing organisation if available from is not given" do
record.saledate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to be_empty
end
end end
end end
end end

100
spec/models/validations/setup_validations_spec.rb

@ -136,8 +136,8 @@ RSpec.describe Validations::SetupValidations do
Timecop.return Timecop.return
end end
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") }
let(:absorbing_organisation_2) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org 2") } let(:absorbing_organisation_2) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org 2") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
let(:merged_organisation_2) { create(:organisation, name: "Merged org 2") } let(:merged_organisation_2) { create(:organisation, name: "Merged org 2") }
@ -147,14 +147,14 @@ RSpec.describe Validations::SetupValidations do
end end
context "and owning organisation is no longer active" do context "and owning organisation is no longer active" do
it "does not allow startate after organisation has been merged" do it "does not allow startdate after organisation has been merged" do
record.startdate = Time.zone.local(2023, 3, 1) record.startdate = Time.zone.local(2023, 3, 1)
record.owning_organisation_id = merged_organisation.id record.owning_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record) 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.") 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 end
it "allows startate before organisation has been merged" do it "allows startdate before organisation has been merged" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = merged_organisation.id record.owning_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
@ -163,30 +163,38 @@ RSpec.describe Validations::SetupValidations do
end end
context "and owning organisation is not yet active during the startdate" do context "and owning organisation is not yet active during the startdate" do
it "does not allow startate before absorbing organisation has been created" do it "does not allow startdate before absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record) 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.") 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 end
it "allows startate after absorbing organisation has been created" do it "allows startdate after absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 2, 2) record.startdate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
it "allows startdate if organisation does not have available from date" do
record.startdate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end end
context "and managing organisation is no longer active during the startdate" do context "and managing organisation is no longer active during the startdate" do
it "does not allow startate after organisation has been merged" do it "does not allow startdate after organisation has been merged" do
record.startdate = Time.zone.local(2023, 3, 1) record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record) 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.") 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 end
it "allows startate before organisation has been merged" do it "allows startdate before organisation has been merged" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
@ -195,23 +203,31 @@ RSpec.describe Validations::SetupValidations do
end end
context "and managing organisation is not yet active during the startdate" do context "and managing organisation is not yet active during the startdate" do
it "does not allow startate before absorbing organisation has been created" do it "does not allow startdate before absorbing organisation has become available'" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record) 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.") 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 end
it "allows startate after absorbing organisation has been created" do it "allows startdate after absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 2, 2) record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
it "allows startdate if organisation does not have available from date" do
record.startdate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty
end
end end
context "and owning and managing organisation is no longer active during the startdate" do 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 it "does not allow startdate after organisation has been merged" do
record.startdate = Time.zone.local(2023, 3, 1) record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation.id record.owning_organisation_id = merged_organisation.id
@ -219,7 +235,7 @@ RSpec.describe Validations::SetupValidations do
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.") 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 end
it "allows startate before organisation has been merged" do it "allows startdate before organisation has been merged" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation.id record.owning_organisation_id = merged_organisation.id
@ -229,7 +245,7 @@ RSpec.describe Validations::SetupValidations do
end end
context "and owning and managing organisation is not yet active during the startdate" do 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 it "does not allow startdate before absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
@ -237,17 +253,26 @@ RSpec.describe Validations::SetupValidations do
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.") 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 end
it "allows startate after absorbing organisation has been created" do it "allows startdate after absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 2, 1) record.startdate = Time.zone.local(2023, 2, 1)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
it "allows startdate if organisation does not have available from date" do
record.startdate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
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
context "and owning and managing organisations are no longer active during the startdate" do 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 it "does not allow startdate after organisation have been merged" do
record.startdate = Time.zone.local(2023, 2, 2) record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id record.owning_organisation_id = merged_organisation_2.id
@ -255,7 +280,7 @@ RSpec.describe Validations::SetupValidations do
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.") 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 end
it "allows startate before organisations have been merged" do it "allows startdate before organisations have been merged" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id record.owning_organisation_id = merged_organisation_2.id
@ -269,7 +294,7 @@ RSpec.describe Validations::SetupValidations do
merged_organisation_2.update!(absorbing_organisation: absorbing_organisation_2, merge_date: Time.zone.local(2023, 2, 2)) merged_organisation_2.update!(absorbing_organisation: absorbing_organisation_2, merge_date: Time.zone.local(2023, 2, 2))
end end
it "does not allow startate after organisations have been merged" do it "does not allow startdate after organisations have been merged" do
record.startdate = Time.zone.local(2023, 3, 1) record.startdate = Time.zone.local(2023, 3, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id record.owning_organisation_id = merged_organisation_2.id
@ -277,7 +302,7 @@ RSpec.describe Validations::SetupValidations do
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.") 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 end
it "allows startate before organisations have been merged" do it "allows startdate before organisations have been merged" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = merged_organisation.id record.managing_organisation_id = merged_organisation.id
record.owning_organisation_id = merged_organisation_2.id record.owning_organisation_id = merged_organisation_2.id
@ -291,7 +316,7 @@ RSpec.describe Validations::SetupValidations do
merged_organisation_2.update!(absorbing_organisation: absorbing_organisation_2, merge_date: Time.zone.local(2023, 2, 2)) merged_organisation_2.update!(absorbing_organisation: absorbing_organisation_2, merge_date: Time.zone.local(2023, 2, 2))
end end
it "does not allow startate before absorbing organisation has been created" do it "does not allow startdate before absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation_2.id record.owning_organisation_id = absorbing_organisation_2.id
@ -299,13 +324,22 @@ RSpec.describe Validations::SetupValidations do
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.") 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 end
it "allows startate after absorbing organisation has been created" do it "allows startdate after absorbing organisation has become available" do
record.startdate = Time.zone.local(2023, 2, 2) record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
it "allows startdate if organisation does not have available from date" do
absorbing_organisation.update!(available_from: nil)
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 be_empty
end
end end
end end
end end
@ -723,7 +757,7 @@ RSpec.describe Validations::SetupValidations do
end end
context "when organisations are merged" do context "when organisations are merged" do
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example| around do |example|
@ -750,19 +784,27 @@ RSpec.describe Validations::SetupValidations do
end end
context "and owning organisation is not yet active during the startdate" do context "and owning organisation is not yet active during the startdate" do
it "does not allow absorbing organisation before it had been created" do it "does not allow absorbing organisation before it has become available'" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record) 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.") 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 end
it "allows absorbing organisation after it has been created" do it "allows absorbing organisation after it has become available" do
record.startdate = Time.zone.local(2023, 2, 2) record.startdate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record) setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to be_empty expect(record.errors["owning_organisation_id"]).to be_empty
end end
it "allows startdate if organisation does not have available from date" do
absorbing_organisation.update!(available_from: nil)
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 be_empty
end
end end
context "when managing organisation is no longer active" do context "when managing organisation is no longer active" do
@ -782,19 +824,27 @@ RSpec.describe Validations::SetupValidations do
end end
context "when managing organisation is not yet active during the startdate" do context "when managing organisation is not yet active during the startdate" do
it "does not allow absorbing organisation before it had been created" do it "does not allow absorbing organisation before it has become available" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record) 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.") 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 end
it "allows absorbing organisation after it has been created" do it "allows absorbing organisation after it has become available'" do
record.startdate = Time.zone.local(2023, 2, 2) record.startdate = Time.zone.local(2023, 2, 2)
record.managing_organisation_id = absorbing_organisation.id record.managing_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record) setup_validator.validate_organisation(record)
expect(record.errors["managing_organisation_id"]).to be_empty expect(record.errors["managing_organisation_id"]).to be_empty
end end
it "allows startdate if organisation does not have available from date" do
absorbing_organisation.update!(available_from: nil)
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 be_empty
end
end end
end end
end end

Loading…
Cancel
Save