From 5f2ab7e65c3ba8284de2990c6f1c7cd23dbf4df8 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 27 Nov 2023 14:46:36 +0000 Subject: [PATCH] Deactivate locations on merged orgs --- app/services/merge/merge_organisations_service.rb | 5 ++++- .../merge/merge_organisations_service_spec.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/services/merge/merge_organisations_service.rb b/app/services/merge/merge_organisations_service.rb index 46426a119..c45ade893 100644 --- a/app/services/merge/merge_organisations_service.rb +++ b/app/services/merge/merge_organisations_service.rb @@ -70,7 +70,10 @@ private new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id", "old_id", "old_visible_id").merge(owning_organisation: @absorbing_organisation)) scheme.locations.each do |location| - new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id")) unless location.deactivated? + next if location.deactivated? + + new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id", "old_id", "old_visible_id")) + LocationDeactivationPeriod.create!(location:, deactivation_date: @merge_date) end @merged_schemes[merging_organisation.name] << { name: new_scheme.service_name, code: new_scheme.id } SchemeDeactivationPeriod.create!(scheme:, deactivation_date: @merge_date) diff --git a/spec/services/merge/merge_organisations_service_spec.rb b/spec/services/merge/merge_organisations_service_spec.rb index ad15975ad..31230fea4 100644 --- a/spec/services/merge/merge_organisations_service_spec.rb +++ b/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 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 @@ -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 @@ -333,6 +336,8 @@ RSpec.describe Merge::MergeOrganisationsService do expect(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 @@ -359,6 +364,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 @@ -676,6 +682,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 @@ -702,6 +710,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 @@ -842,6 +851,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 @@ -868,6 +879,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