Browse Source

Don't validate locations/schemes and deactivations (#2087)

pull/2088/head
kosiakkatrina 1 year ago committed by GitHub
parent
commit
cbcd6355c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app/services/merge/merge_organisations_service.rb
  2. 41
      spec/services/merge/merge_organisations_service_spec.rb

28
app/services/merge/merge_organisations_service.rb

@ -67,19 +67,27 @@ private
def merge_schemes_and_locations(merging_organisation) def merge_schemes_and_locations(merging_organisation)
@merged_schemes[merging_organisation.name] = [] @merged_schemes[merging_organisation.name] = []
merging_organisation.owned_schemes.each do |scheme| 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| scheme.scheme_deactivation_periods.each do |deactivation_period|
split_scheme_deactivation_period_between_organisations(deactivation_period, new_scheme) split_scheme_deactivation_period_between_organisations(deactivation_period, new_scheme)
end end
scheme.locations.each do |location| 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| location.location_deactivation_periods.each do |deactivation_period|
split_location_deactivation_period_between_organisations(deactivation_period, new_location) split_location_deactivation_period_between_organisations(deactivation_period, new_location)
end 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 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: [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
end end
@ -188,13 +196,15 @@ private
return if deactivation_happenned_before_merge?(deactivation_period) return if deactivation_happenned_before_merge?(deactivation_period)
if deactivation_happenned_during_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? if deactivation_period.reactivation_date.present?
deactivation_period.reactivation_date = nil deactivation_period.reactivation_date = nil
deactivation_period.save!(validate: false) deactivation_period.save!(validate: false)
end end
else 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! deactivation_period.destroy!
end end
end end
@ -203,13 +213,15 @@ private
return if deactivation_happenned_before_merge?(deactivation_period) return if deactivation_happenned_before_merge?(deactivation_period)
if deactivation_happenned_during_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? if deactivation_period.reactivation_date.present?
deactivation_period.reactivation_date = nil deactivation_period.reactivation_date = nil
deactivation_period.save!(validate: false) deactivation_period.save!(validate: false)
end end
else 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! deactivation_period.destroy!
end end
end end

41
spec/services/merge/merge_organisations_service_spec.rb

@ -308,6 +308,47 @@ RSpec.describe Merge::MergeOrganisationsService do
end end
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 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!(: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) } let!(:location) { create(:location, scheme:, old_id: "location_old_id", old_visible_id: "location_old_visible_id", startdate: nil) }

Loading…
Cancel
Save