From 7acbfd1a818556315ad9f320e16c469ea15fcbc1 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 23 Oct 2023 16:05:49 +0100 Subject: [PATCH] Use available from date for absorbing organisations validations --- app/models/validations/setup_validations.rb | 12 +-- ...0231023142854_add_available_from_to_org.rb | 5 ++ db/schema.rb | 3 +- .../validations/setup_validations_spec.rb | 80 +++++++++++++++---- 4 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20231023142854_add_available_from_to_org.rb diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index e826c6302..95881eeb0 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -61,10 +61,10 @@ module Validations::SetupValidations 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.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", 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 @@ -74,10 +74,10 @@ module Validations::SetupValidations 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.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", 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 @@ -221,11 +221,11 @@ private end 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 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 def organisations_belong_to_same_merge?(organisation_a, organisation_b) diff --git a/db/migrate/20231023142854_add_available_from_to_org.rb b/db/migrate/20231023142854_add_available_from_to_org.rb new file mode 100644 index 000000000..7cfb4b1aa --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index e353ff9c7..7eda570d1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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 enable_extension "plpgsql" @@ -440,6 +440,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_09_13_093443) do t.string "old_visible_id" t.datetime "merge_date" t.bigint "absorbing_organisation_id" + t.datetime "available_from" 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 end diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 75b890b24..989756484 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -136,8 +136,8 @@ RSpec.describe Validations::SetupValidations do Timecop.return end - let(:absorbing_organisation) { create(:organisation, created_at: 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) { 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), available_from: 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") } @@ -163,19 +163,27 @@ RSpec.describe Validations::SetupValidations do 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 + it "does not allow startate before absorbing organisation has become available" 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 + it "allows startate after absorbing organisation has become available" 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 + + it "allows startate 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 context "and managing organisation is no longer active during the startdate" do @@ -195,19 +203,27 @@ RSpec.describe Validations::SetupValidations do 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 + it "does not allow startate before absorbing organisation has become available'" 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 + it "allows startate after absorbing organisation has become available" 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 + + it "allows startate 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 context "and owning and managing organisation is no longer active during the startdate" do @@ -229,7 +245,7 @@ RSpec.describe Validations::SetupValidations do 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 + it "does not allow startate before absorbing organisation has become available" do record.startdate = Time.zone.local(2023, 1, 1) record.managing_organisation_id = absorbing_organisation.id record.owning_organisation_id = absorbing_organisation.id @@ -237,13 +253,22 @@ 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.") end - it "allows startate after absorbing organisation has been created" do + it "allows startate after absorbing organisation has become available" do record.startdate = Time.zone.local(2023, 2, 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 + + it "allows startate 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 context "and owning and managing organisations are no longer active during the startdate" do @@ -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)) end - it "does not allow startate before absorbing organisation has been created" do + it "does not allow startate before absorbing organisation has become available" do record.startdate = Time.zone.local(2023, 1, 1) record.managing_organisation_id = absorbing_organisation.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.") end - it "allows startate after absorbing organisation has been created" do + it "allows startate after absorbing organisation has become available" 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 + + it "allows startate 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 @@ -723,7 +757,7 @@ RSpec.describe Validations::SetupValidations do end 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") } around do |example| @@ -750,19 +784,27 @@ RSpec.describe Validations::SetupValidations do 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 + it "does not allow absorbing organisation before it has become available'" 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 + it "allows absorbing organisation after it has become available" 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 + + it "allows startate 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 context "when managing organisation is no longer active" do @@ -782,19 +824,27 @@ RSpec.describe Validations::SetupValidations do 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 + it "does not allow absorbing organisation before it has become available" 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 + it "allows absorbing organisation after it has become available'" 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 + + it "allows startate 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