Browse Source

Deactivate locations on merged orgs

pull/2069/head
Kat 3 years ago
parent
commit
45c57dfa3f
  1. 5
      app/services/merge/merge_organisations_service.rb
  2. 12
      spec/services/merge/merge_organisations_service_spec.rb

5
app/services/merge/merge_organisations_service.rb

@ -71,7 +71,10 @@ private
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|
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?
next if 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))
LocationDeactivationPeriod.create!(location:, deactivation_date: @merge_date) unless (location.startdate.present? && location.startdate >= @merge_date) || (scheme.startdate.present? && scheme.startdate >= @merge_date)
end
@merged_schemes[merging_organisation.name] << { name: new_scheme.service_name, code: new_scheme.id }
SchemeDeactivationPeriod.create!(scheme:, deactivation_date: @merge_date)

12
spec/services/merge/merge_organisations_service_spec.rb

@ -167,6 +167,8 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(absorbing_organisation.owned_schemes.first.locations.first.old_visible_id).to eq(nil)
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.today)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(1)
expect(scheme.locations.first.location_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.today)
end
it "moves relevant logs and assigns the new scheme" do
@ -193,6 +195,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merging_organisation.reload
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(scheme.scheme_deactivation_periods.count).to eq(0)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(0)
expect(owned_lettings_log.owning_organisation).to eq(merging_organisation)
expect(owned_lettings_log_no_location.owning_organisation).to eq(merging_organisation)
end
@ -382,6 +385,8 @@ RSpec.describe Merge::MergeOrganisationsService do
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.first.deactivation_date.to_date).to eq(Time.zone.yesterday)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(1)
expect(scheme.locations.first.location_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday)
end
end
@ -409,6 +414,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merging_organisation.reload
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(scheme.scheme_deactivation_periods.count).to eq(0)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(0)
expect(owned_lettings_log.owning_organisation).to eq(merging_organisation)
expect(owned_lettings_log_no_location.owning_organisation).to eq(merging_organisation)
end
@ -766,6 +772,8 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(new_absorbing_organisation.owned_schemes.first.locations.first.old_visible_id).to be_nil
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.today)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(1)
expect(scheme.locations.first.location_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.today)
end
it "moves relevant logs and assigns the new scheme" do
@ -792,6 +800,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merging_organisation.reload
expect(new_absorbing_organisation.owned_schemes.count).to eq(0)
expect(scheme.scheme_deactivation_periods.count).to eq(0)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(0)
expect(owned_lettings_log.owning_organisation).to eq(merging_organisation)
expect(owned_lettings_log_no_location.owning_organisation).to eq(merging_organisation)
end
@ -932,6 +941,8 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(new_absorbing_organisation.owned_schemes.first.locations.first.postcode).to eq(location.postcode)
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.locations.first.location_deactivation_periods.count).to eq(1)
expect(scheme.locations.first.location_deactivation_periods.first.deactivation_date.to_date).to eq(Time.zone.yesterday)
end
it "moves relevant logs and assigns the new scheme" do
@ -959,6 +970,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merging_organisation.reload
expect(new_absorbing_organisation.owned_schemes.count).to eq(0)
expect(scheme.scheme_deactivation_periods.count).to eq(0)
expect(scheme.locations.first.location_deactivation_periods.count).to eq(0)
expect(owned_lettings_log.owning_organisation).to eq(merging_organisation)
expect(owned_lettings_log_no_location.owning_organisation).to eq(merging_organisation)
end

Loading…
Cancel
Save