From cbcd6355c72f933414f10a06c83c995db9d20c3a Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:09:50 +0000 Subject: [PATCH] Don't validate locations/schemes and deactivations (#2087) --- .../merge/merge_organisations_service.rb | 28 +++++++++---- .../merge/merge_organisations_service_spec.rb | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/app/services/merge/merge_organisations_service.rb b/app/services/merge/merge_organisations_service.rb index 9f0759d33..0662853fb 100644 --- a/app/services/merge/merge_organisations_service.rb +++ b/app/services/merge/merge_organisations_service.rb @@ -67,19 +67,27 @@ private def merge_schemes_and_locations(merging_organisation) @merged_schemes[merging_organisation.name] = [] merging_organisation.owned_schemes.each do |scheme| - new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation, startdate: [scheme&.startdate, @merge_date].compact.max)) + new_scheme = Scheme.new(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation, startdate: [scheme&.startdate, @merge_date].compact.max)) + new_scheme.save!(validate: false) scheme.scheme_deactivation_periods.each do |deactivation_period| split_scheme_deactivation_period_between_organisations(deactivation_period, new_scheme) end scheme.locations.each do |location| - new_location = Location.create!(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id").merge(scheme: new_scheme, startdate: [location&.startdate, @merge_date].compact.max)) + new_location = Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id").merge(scheme: new_scheme, startdate: [location&.startdate, @merge_date].compact.max)) + new_location.save!(validate: false) location.location_deactivation_periods.each do |deactivation_period| split_location_deactivation_period_between_organisations(deactivation_period, new_location) end - LocationDeactivationPeriod.create!(location:, deactivation_date: [location&.startdate, scheme&.startdate, @merge_date].compact.max) unless location.status_at(@merge_date) == :deactivated + unless location.status_at(@merge_date) == :deactivated + deactivation_period = LocationDeactivationPeriod.new(location:, deactivation_date: [location&.startdate, scheme&.startdate, @merge_date].compact.max) + deactivation_period.save!(validate: false) + end end @merged_schemes[merging_organisation.name] << { name: new_scheme.service_name, code: new_scheme.id } - SchemeDeactivationPeriod.create!(scheme:, deactivation_date: [scheme&.startdate, @merge_date].compact.max) unless scheme.status_at(@merge_date) == :deactivated + unless scheme.status_at(@merge_date) == :deactivated + deactivation_period = SchemeDeactivationPeriod.new(scheme:, deactivation_date: [scheme&.startdate, @merge_date].compact.max) + deactivation_period.save!(validate: false) + end end end @@ -188,13 +196,15 @@ private return if deactivation_happenned_before_merge?(deactivation_period) if deactivation_happenned_during_merge?(deactivation_period) - SchemeDeactivationPeriod.create!(deactivation_period.attributes.except("id", "scheme_id", "deactivation_date").merge(scheme: new_scheme, deactivation_date: @merge_date)) + new_deactivation_period = SchemeDeactivationPeriod.new(deactivation_period.attributes.except("id", "scheme_id", "deactivation_date").merge(scheme: new_scheme, deactivation_date: @merge_date)) + new_deactivation_period.save!(validate: false) if deactivation_period.reactivation_date.present? deactivation_period.reactivation_date = nil deactivation_period.save!(validate: false) end else - SchemeDeactivationPeriod.create!(deactivation_period.attributes.except("id", "scheme_id").merge(scheme: new_scheme)) + new_deactivation_period = SchemeDeactivationPeriod.new(deactivation_period.attributes.except("id", "scheme_id").merge(scheme: new_scheme)) + new_deactivation_period.save!(validate: false) deactivation_period.destroy! end end @@ -203,13 +213,15 @@ private return if deactivation_happenned_before_merge?(deactivation_period) if deactivation_happenned_during_merge?(deactivation_period) - LocationDeactivationPeriod.create!(deactivation_period.attributes.except("id", "location_id", "deactivation_date").merge(location: new_location, deactivation_date: @merge_date)) + new_deactivation_period = LocationDeactivationPeriod.new(deactivation_period.attributes.except("id", "location_id", "deactivation_date").merge(location: new_location, deactivation_date: @merge_date)) + new_deactivation_period.save!(validate: false) if deactivation_period.reactivation_date.present? deactivation_period.reactivation_date = nil deactivation_period.save!(validate: false) end else - LocationDeactivationPeriod.create!(deactivation_period.attributes.except("id", "location_id").merge(location: new_location)) + new_deactivation_period = LocationDeactivationPeriod.new(deactivation_period.attributes.except("id", "location_id").merge(location: new_location)) + new_deactivation_period.save!(validate: false) deactivation_period.destroy! end end diff --git a/spec/services/merge/merge_organisations_service_spec.rb b/spec/services/merge/merge_organisations_service_spec.rb index d01b0525a..4c4b8a1d4 100644 --- a/spec/services/merge/merge_organisations_service_spec.rb +++ b/spec/services/merge/merge_organisations_service_spec.rb @@ -308,6 +308,47 @@ RSpec.describe Merge::MergeOrganisationsService do end end + context "and deactivation is after the merge date and before an open collection window" do + subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids], merge_date: Time.zone.today - 6.years) } + + let!(:scheme) { create(:scheme, owning_organisation: merging_organisation, old_id: "scheme_old_id", old_visible_id: "scheme_old_visible_id", startdate: nil) } + let!(:location) { create(:location, scheme:, old_id: "location_old_id", old_visible_id: "location_old_visible_id", startdate: nil) } + + before do + scheme_deactivation_period = build(:scheme_deactivation_period, scheme:, deactivation_date: Time.zone.today - 3.years, reactivation_date: Time.zone.today - 3.months) + scheme_deactivation_period.save!(validate: false) + location_deactivation_period = build(:location_deactivation_period, location:, deactivation_date: Time.zone.today - 4.years) + location_deactivation_period.save!(validate: false) + merge_organisations_service.call + absorbing_organisation.reload + scheme.scheme_deactivation_periods.reload + location.location_deactivation_periods.reload + end + + it "moves the deactivations to absorbing organisation and removes them from merging organisations" do + expect(absorbing_organisation.owned_schemes.count).to eq(1) + + absorbed_scheme = absorbing_organisation.owned_schemes.first + expect(absorbed_scheme.locations.count).to eq(1) + absorbed_location = absorbed_scheme.locations.first + + expect(absorbed_scheme.startdate).to eq(Time.zone.today - 6.years) + expect(absorbed_scheme.scheme_deactivation_periods.count).to eq(1) + + expect(absorbed_location.startdate).to eq(Time.zone.today - 6.years) + expect(absorbed_location.location_deactivation_periods.count).to eq(1) + end + + it "deactivates schemes and locations on the merged organisation" do + expect(scheme.owning_organisation).to eq(merging_organisation) + expect(location.scheme).to eq(scheme) + expect(scheme.scheme_deactivation_periods.count).to eq(1) + expect(scheme.scheme_deactivation_periods.last.deactivation_date).to eq(Time.zone.today - 6.years) + expect(location.location_deactivation_periods.count).to eq(1) + expect(location.location_deactivation_periods.last.deactivation_date).to eq(Time.zone.today - 6.years) + end + end + context "and deactivation is during the merge date and it has a reactivation date" do let!(:scheme) { create(:scheme, owning_organisation: merging_organisation, old_id: "scheme_old_id", old_visible_id: "scheme_old_visible_id", startdate: nil) } let!(:location) { create(:location, scheme:, old_id: "location_old_id", old_visible_id: "location_old_visible_id", startdate: nil) }