Browse Source

CLDC-2996 Add correct availability details to merged schemes and locations (#2081)

* feat: use latest merge date/start date where available for availability text for schemes and locations

* feat: add tests for availability text

* feat: use startdate instead to track merges on schemes and locations

* feat: test startdates are set correctly on merged schemes and locations

* feat: add migration file

* feat: add scheme activating soon tests

* feat: add activating soon to scheme filtering

* feat: update scheme active_status

* feat: update tests

* refactor: lint

* feat: move merge orgs validation after orgs validation

* feat: update schema
pull/2080/head
natdeanlewissoftwire 1 year ago committed by GitHub
parent
commit
7a3c79d41d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/helpers/filters_helper.rb
  2. 8
      app/models/location.rb
  3. 10
      app/models/scheme.rb
  4. 20
      app/models/validations/setup_validations.rb
  5. 4
      app/services/merge/merge_organisations_service.rb
  6. 5
      db/migrate/20231204101105_add_startdate_to_schemes.rb
  7. 3
      db/schema.rb
  8. 9
      spec/helpers/locations_helper_spec.rb
  9. 11
      spec/helpers/schemes_helper_spec.rb
  10. 18
      spec/models/scheme_spec.rb
  11. 34
      spec/models/validations/setup_validations_spec.rb
  12. 14
      spec/services/merge/merge_organisations_service_spec.rb

1
app/helpers/filters_helper.rb

@ -52,6 +52,7 @@ module FiltersHelper
"incomplete" => "Incomplete", "incomplete" => "Incomplete",
"active" => "Active", "active" => "Active",
"deactivating_soon" => "Deactivating soon", "deactivating_soon" => "Deactivating soon",
"activating_soon" => "Activating soon",
"reactivating_soon" => "Reactivating soon", "reactivating_soon" => "Reactivating soon",
"deactivated" => "Deactivated", "deactivated" => "Deactivated",
}.freeze }.freeze

8
app/models/location.rb

@ -21,9 +21,9 @@ class Location < ApplicationRecord
scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") }
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) }
scope :started, -> { where("startdate <= ?", Time.zone.today).or(where(startdate: nil)) } scope :started, -> { where("locations.startdate <= ?", Time.zone.today).or(where(startdate: nil)) }
scope :active, -> { where(confirmed: true).and(started) } scope :active, -> { where(confirmed: true).and(started) }
scope :started_in_2_weeks, -> { where("startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) } scope :started_in_2_weeks, -> { where("locations.startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) }
scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) } scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) }
scope :confirmed, -> { where(confirmed: true) } scope :confirmed, -> { where(confirmed: true) }
scope :unconfirmed, -> { where.not(confirmed: true) } scope :unconfirmed, -> { where.not(confirmed: true) }
@ -122,9 +122,7 @@ class Location < ApplicationRecord
end end
def available_from def available_from
return startdate if startdate.present? startdate || FormHandler.instance.earliest_open_collection_start_date(now: created_at)
FormHandler.instance.earliest_open_collection_start_date(now: created_at)
end end
def open_deactivation def open_deactivation

10
app/models/scheme.rb

@ -65,11 +65,16 @@ class Scheme < ApplicationRecord
.where("scheme_deactivation_periods.reactivation_date > ?", Time.zone.now) .where("scheme_deactivation_periods.reactivation_date > ?", Time.zone.now)
} }
scope :activating_soon, lambda {
where("startdate > ?", Time.zone.now)
}
scope :active_status, lambda { scope :active_status, lambda {
where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id)) where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id)) .where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id))
.where.not(id: incomplete.pluck(:id)) .where.not(id: incomplete.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id)) .where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id))
.where.not(id: activating_soon.pluck(:id))
} }
validate :validate_confirmed validate :validate_confirmed
@ -243,7 +248,7 @@ class Scheme < ApplicationRecord
end end
def validate_confirmed def validate_confirmed
required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type] required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type startdate]
if confirmed == true if confirmed == true
required_attributes.any? do |attribute| required_attributes.any? do |attribute|
@ -262,7 +267,7 @@ class Scheme < ApplicationRecord
end end
def available_from def available_from
FormHandler.instance.earliest_open_collection_start_date(now: created_at) startdate || FormHandler.instance.earliest_open_collection_start_date(now: created_at)
end end
def open_deactivation def open_deactivation
@ -282,6 +287,7 @@ class Scheme < ApplicationRecord
return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date
return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date
return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date return :reactivating_soon if last_deactivation_before(date)&.reactivation_date.present? && date < last_deactivation_before(date).reactivation_date
return :activating_soon if startdate.present? && date < startdate
:active :active
end end

20
app/models/validations/setup_validations.rb

@ -14,8 +14,6 @@ module Validations::SetupValidations
unless record.startdate.between?(first_collection_start_date, current_collection_end_date) unless record.startdate.between?(first_collection_start_date, current_collection_end_date)
record.errors.add :startdate, startdate_validation_error_message record.errors.add :startdate, startdate_validation_error_message
end end
validate_merged_organisations_start_date(record)
end end
def validate_organisation(record) def validate_organisation(record)
@ -54,6 +52,16 @@ module Validations::SetupValidations
end end
end end
def validate_merged_organisations_start_date(record)
return unless record.startdate && date_valid?("startdate", 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 validate_irproduct_other(record) def validate_irproduct_other(record)
if intermediate_product_rent_type?(record) && record.irproduct_other.blank? if intermediate_product_rent_type?(record) && record.irproduct_other.blank?
record.errors.add :irproduct_other, I18n.t("validations.setup.intermediate_rent_product_name.blank") record.errors.add :irproduct_other, I18n.t("validations.setup.intermediate_rent_product_name.blank")
@ -134,14 +142,6 @@ private
record.rent_type == 5 record.rent_type == 5
end 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) def add_same_merge_organisation_error(record)
if merged_owning_organisation_inactive?(record) if merged_owning_organisation_inactive?(record)
record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.same_organisation", record.errors.add :startdate, I18n.t("validations.setup.startdate.invalid_merged_organisations_start_date.same_organisation",

4
app/services/merge/merge_organisations_service.rb

@ -69,9 +69,9 @@ private
merging_organisation.owned_schemes.each do |scheme| merging_organisation.owned_schemes.each do |scheme|
next if scheme.deactivated? next if scheme.deactivated?
new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation)) new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation, startdate: @merge_date))
scheme.locations.each do |location| scheme.locations.each do |location|
new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id")) unless location.deactivated? new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id").merge(startdate: [location&.startdate, @merge_date].compact.max)) unless location.deactivated?
end end
@merged_schemes[merging_organisation.name] << { name: new_scheme.service_name, code: new_scheme.id } @merged_schemes[merging_organisation.name] << { name: new_scheme.service_name, code: new_scheme.id }
SchemeDeactivationPeriod.create!(scheme:, deactivation_date: @merge_date) SchemeDeactivationPeriod.create!(scheme:, deactivation_date: @merge_date)

5
db/migrate/20231204101105_add_startdate_to_schemes.rb

@ -0,0 +1,5 @@
class AddStartdateToSchemes < ActiveRecord::Migration[7.0]
def change
add_column :schemes, :startdate, :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_11_21_131725) do ActiveRecord::Schema[7.0].define(version: 2023_12_04_101105) 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"
@ -661,6 +661,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_11_21_131725) do
t.string "old_visible_id" t.string "old_visible_id"
t.integer "total_units" t.integer "total_units"
t.boolean "confirmed" t.boolean "confirmed"
t.datetime "startdate"
t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id" t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id"
end end

9
spec/helpers/locations_helper_spec.rb

@ -214,6 +214,15 @@ RSpec.describe LocationsHelper do
expect(availability_attribute).to eq("Active from 1 April 2022") expect(availability_attribute).to eq("Active from 1 April 2022")
end end
context "when location was merged" do
it "displays merge date as availability date" do
location.update!(startdate: Time.zone.local(2022, 4, 16))
availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 16 April 2022")
end
end
end end
context "with previous deactivations" do context "with previous deactivations" do

11
spec/helpers/schemes_helper_spec.rb

@ -303,6 +303,17 @@ RSpec.describe SchemesHelper do
expect(display_scheme_attributes(scheme)).to eq(attributes) expect(display_scheme_attributes(scheme)).to eq(attributes)
end end
end end
context "when scheme was merged from another organisation" do
before do
FactoryBot.create(:location, scheme:)
scheme.startdate = Time.zone.local(2023, 1, 5)
end
it "returns correct availability" do
expect(display_scheme_attributes(scheme)).to include({ name: "Availability", value: "Active from 5 January 2023" })
end
end
end end
describe "edit_scheme_text" do describe "edit_scheme_text" do

18
spec/models/scheme_spec.rb

@ -123,6 +123,7 @@ RSpec.describe Scheme, type: :model do
let(:deactivated_scheme_2) { FactoryBot.create(:scheme) } let(:deactivated_scheme_2) { FactoryBot.create(:scheme) }
let(:reactivating_soon_scheme) { FactoryBot.create(:scheme) } let(:reactivating_soon_scheme) { FactoryBot.create(:scheme) }
let(:reactivating_soon_scheme_2) { FactoryBot.create(:scheme) } let(:reactivating_soon_scheme_2) { FactoryBot.create(:scheme) }
let(:activating_soon_scheme) { FactoryBot.create(:scheme, startdate: Time.zone.today + 1.week) }
before do before do
scheme.destroy! scheme.destroy!
@ -242,11 +243,18 @@ RSpec.describe Scheme, type: :model do
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns reactivating soon if the location has a future reactivation date" do it "returns reactivating soon if the scheme has a future reactivation date" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the scheme has a future startdate" do
Timecop.freeze(2022, 6, 4)
scheme.startdate = Time.zone.local(2022, 7, 7)
scheme.save!
expect(scheme.status).to eq(:activating_soon)
end
end end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
@ -283,12 +291,18 @@ RSpec.describe Scheme, type: :model do
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns if the scheme had a deactivation during another deactivation" do it "returns reactivating soon if the scheme had a deactivation during another deactivation" do
Timecop.freeze(2022, 6, 4) Timecop.freeze(2022, 6, 4)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the scheme has a future startdate" do
scheme.startdate = Time.zone.local(2022, 7, 7)
scheme.save!
expect(scheme.status).to eq(:activating_soon)
end
end end
end end

34
spec/models/validations/setup_validations_spec.rb

@ -17,12 +17,14 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2021, 1, 1) record.startdate = Time.zone.local(2021, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 collection years, which is between 1st April 2021 and 31st March 2023") expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 collection years, which is between 1st April 2021 and 31st March 2023")
end end
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1, 6) record.startdate = Time.zone.local(2023, 7, 1, 6)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 collection years, which is between 1st April 2021 and 31st March 2023") expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 collection years, which is between 1st April 2021 and 31st March 2023")
end end
end end
@ -36,12 +38,14 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2022, 1, 1) record.startdate = Time.zone.local(2022, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023") expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023")
end end
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1, 6) record.startdate = Time.zone.local(2023, 7, 1, 6)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023") expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023")
end end
end end
@ -57,12 +61,14 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2022, 1, 1) record.startdate = Time.zone.local(2022, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 collection years, which is between 1st April 2022 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 collection years, which is between 1st April 2022 and 31st March 2024")
end end
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2024, 7, 1, 6) record.startdate = Time.zone.local(2024, 7, 1, 6)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 collection years, which is between 1st April 2022 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 collection years, which is between 1st April 2022 and 31st March 2024")
end end
end end
@ -76,12 +82,14 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024")
end end
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2024, 7, 1, 6) record.startdate = Time.zone.local(2024, 7, 1, 6)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024")
end end
end end
@ -95,6 +103,7 @@ RSpec.describe Validations::SetupValidations do
record.update!(startdate: nil) record.update!(startdate: nil)
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024")
end end
@ -103,6 +112,7 @@ RSpec.describe Validations::SetupValidations do
record.save!(validate: false) record.save!(validate: false)
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).not_to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") expect(record.errors["startdate"]).not_to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024")
end end
end end
@ -116,6 +126,7 @@ RSpec.describe Validations::SetupValidations do
record.update!(startdate: nil) record.update!(startdate: nil)
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024")
end end
@ -124,6 +135,7 @@ RSpec.describe Validations::SetupValidations do
record.save!(validate: false) record.save!(validate: false)
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024") expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 collection year, which is between 1st April 2023 and 31st March 2024")
end end
end end
@ -151,6 +163,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(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
@ -158,6 +171,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -167,6 +181,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(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
@ -174,6 +189,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
@ -182,6 +198,7 @@ RSpec.describe Validations::SetupValidations do
absorbing_organisation.update!(available_from: nil) absorbing_organisation.update!(available_from: nil)
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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -191,6 +208,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(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
@ -198,6 +216,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -207,6 +226,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(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
@ -214,6 +234,7 @@ RSpec.describe Validations::SetupValidations 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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
@ -222,6 +243,7 @@ RSpec.describe Validations::SetupValidations do
absorbing_organisation.update!(available_from: nil) absorbing_organisation.update!(available_from: nil)
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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -232,6 +254,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(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.") 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
@ -240,6 +263,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -250,6 +274,7 @@ RSpec.describe Validations::SetupValidations do
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)
setup_validator.validate_merged_organisations_start_date(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.") 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
@ -258,6 +283,7 @@ RSpec.describe Validations::SetupValidations do
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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
@ -267,6 +293,7 @@ RSpec.describe Validations::SetupValidations do
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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -277,6 +304,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(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.") 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
@ -285,6 +313,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -299,6 +328,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(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.") 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
@ -307,6 +337,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end
@ -321,6 +352,7 @@ RSpec.describe Validations::SetupValidations do
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
setup_validator.validate_startdate_setup(record) setup_validator.validate_startdate_setup(record)
setup_validator.validate_merged_organisations_start_date(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.") 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
@ -329,6 +361,7 @@ RSpec.describe Validations::SetupValidations do
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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
@ -338,6 +371,7 @@ RSpec.describe Validations::SetupValidations do
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)
setup_validator.validate_merged_organisations_start_date(record)
expect(record.errors["startdate"]).to be_empty expect(record.errors["startdate"]).to be_empty
end end
end end

14
spec/services/merge/merge_organisations_service_spec.rb

@ -318,6 +318,11 @@ RSpec.describe Merge::MergeOrganisationsService do
create(:lettings_log, startdate: Time.zone.tomorrow, managing_organisation: merging_organisation) create(:lettings_log, startdate: Time.zone.tomorrow, managing_organisation: merging_organisation)
end end
context "with multiple locations" do
let!(:location_without_startdate) { create(:location, scheme:, startdate: nil) }
let!(:location_with_past_startdate) { create(:location, scheme:, startdate: Time.zone.today - 2.months) }
let!(:location_with_future_startdate) { create(:location, scheme:, startdate: Time.zone.today + 2.months) }
it "combines organisation schemes and locations" do it "combines organisation schemes and locations" do
expect(Rails.logger).to receive(:info).with("Merged users from fake org:") expect(Rails.logger).to receive(:info).with("Merged users from fake org:")
expect(Rails.logger).to receive(:info).with("\tDanny Rojas (#{merging_organisation.data_protection_officers.first.email})") expect(Rails.logger).to receive(:info).with("\tDanny Rojas (#{merging_organisation.data_protection_officers.first.email})")
@ -329,11 +334,16 @@ RSpec.describe Merge::MergeOrganisationsService do
absorbing_organisation.reload absorbing_organisation.reload
expect(absorbing_organisation.owned_schemes.count).to eq(1) expect(absorbing_organisation.owned_schemes.count).to eq(1)
expect(absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name) expect(absorbing_organisation.owned_schemes.first.service_name).to eq(scheme.service_name)
expect(absorbing_organisation.owned_schemes.first.locations.count).to eq(1) expect(absorbing_organisation.owned_schemes.first.startdate).to eq(Time.zone.yesterday)
expect(absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode) expect(absorbing_organisation.owned_schemes.first.locations.count).to eq(4)
expect(absorbing_organisation.owned_schemes.first.locations.map(&:postcode)).to match_array([location, location_without_startdate, location_with_past_startdate, location_with_future_startdate].map(&:postcode))
expect(absorbing_organisation.owned_schemes.first.locations.find_by(postcode: location_without_startdate.postcode).startdate).to eq(Time.zone.yesterday)
expect(absorbing_organisation.owned_schemes.first.locations.find_by(postcode: location_with_past_startdate.postcode).startdate).to eq(Time.zone.yesterday)
expect(absorbing_organisation.owned_schemes.first.locations.find_by(postcode: location_with_future_startdate.postcode).startdate).to eq(Time.zone.today + 2.months)
expect(scheme.scheme_deactivation_periods.count).to eq(1) expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday) expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday)
end end
end
it "moves relevant logs and assigns the new scheme" do it "moves relevant logs and assigns the new scheme" do
merge_organisations_service.call merge_organisations_service.call

Loading…
Cancel
Save