From 9c42d43d73968ccf20383788e893a692482059d8 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 24 Jul 2023 16:54:25 +0100 Subject: [PATCH] update schemes merge --- .../merge/merge_organisations_service.rb | 18 +++++-- .../merge/merge_organisations_service_spec.rb | 50 ++++++++++--------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/app/services/merge/merge_organisations_service.rb b/app/services/merge/merge_organisations_service.rb index 77c81ec8a..eb66bb770 100644 --- a/app/services/merge/merge_organisations_service.rb +++ b/app/services/merge/merge_organisations_service.rb @@ -9,6 +9,7 @@ class Merge::MergeOrganisationsService merge_rent_periods merge_organisation_relationships merge_users + merge_schemes_and_locations mark_organisations_as_merged @absorbing_organisation.save! end @@ -17,9 +18,6 @@ private def merge_organisation_details @absorbing_organisation.holds_own_stock = merge_boolean_organisation_attribute("holds_own_stock") - @absorbing_organisation.choice_based_lettings = merge_boolean_organisation_attribute("choice_based_lettings") - @absorbing_organisation.common_housing_register = merge_boolean_organisation_attribute("common_housing_register") - @absorbing_organisation.choice_allocation_policy = merge_boolean_organisation_attribute("choice_allocation_policy") end def merge_rent_periods @@ -55,6 +53,20 @@ private end end + def merge_schemes_and_locations + @merging_organisations.each do |merging_organisation| + merging_organisation.owned_schemes.each do |scheme| + next if scheme.deactivated? + + new_scheme = Scheme.create!(scheme.attributes.except("id", "owning_organisation_id").merge(owning_organisation: @absorbing_organisation)) + scheme.locations.each do |location| + new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id")) unless location.deactivated? + end + SchemeDeactivationPeriod.create!(scheme:, deactivation_date: Time.zone.now) + end + end + end + def mark_organisations_as_merged # @merging_organisations.update_all(merge_date: Time.zone.today) end diff --git a/spec/services/merge/merge_organisations_service_spec.rb b/spec/services/merge/merge_organisations_service_spec.rb index bf323c33e..51692c96c 100644 --- a/spec/services/merge/merge_organisations_service_spec.rb +++ b/spec/services/merge/merge_organisations_service_spec.rb @@ -3,24 +3,12 @@ require "rails_helper" RSpec.describe Merge::MergeOrganisationsService do subject(:merge_organisations_service) { described_class.new(absorbing_organisation_id: absorbing_organisation.id, merging_organisation_ids: [merging_organisation_ids]) } - let(:absorbing_organisation) do - create(:organisation, - holds_own_stock: false, - choice_based_lettings: false, - common_housing_register: true, - choice_allocation_policy: true) - end + let(:absorbing_organisation) { create(:organisation, holds_own_stock: false) } let(:absorbing_organisation_user) { create(:user, organisation: absorbing_organisation) } describe "#call" do context "when merging a single organisation into an existing organisation" do - let(:merging_organisation) do - create(:organisation, - holds_own_stock: true, - choice_based_lettings: false, - common_housing_register: false, - choice_allocation_policy: true) - end + let(:merging_organisation) { create(:organisation, holds_own_stock: true) } let(:merging_organisation_ids) { [merging_organisation.id] } let!(:merging_organisation_user) { create(:user, organisation: merging_organisation) } @@ -43,15 +31,6 @@ RSpec.describe Merge::MergeOrganisationsService do absorbing_organisation.reload expect(absorbing_organisation.holds_own_stock).to eq(true) - expect(absorbing_organisation.choice_based_lettings).to eq(false) - expect(absorbing_organisation.common_housing_register).to eq(true) - expect(absorbing_organisation.choice_allocation_policy).to eq(true) - # expect(absorbing_organisation.cbl_proportion_percentage).to eq(0) - # expect(absorbing_organisation.enter_affordable_logs).to eq(true) - # expect(absorbing_organisation.owns_affordable_logs).to eq(true) - # expect(absorbing_organisation.general_needs_units).to eq(2) - # expect(absorbing_organisation.supported_housing_units).to eq(2) - # expect(absorbing_organisation.unspecified_units).to eq(2) end context "and merging organisation rent periods" do @@ -95,6 +74,31 @@ RSpec.describe Merge::MergeOrganisationsService do expect(absorbing_organisation.child_organisations.count).to eq(3) end end + + context "and merging organisation schemes and locations" do + let!(:scheme) { create(:scheme, owning_organisation: merging_organisation) } + let!(:location) { create(:location, scheme:) } + let!(:deactivated_location) { create(:location, scheme:) } + let!(:deactivated_scheme) { create(:scheme, owning_organisation: merging_organisation) } + let!(:deactivated_scheme_location) { create(:location, scheme: deactivated_scheme) } + + before do + create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month) + create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month) + end + + it "combines organisation relationships" do + merge_organisations_service.call + + absorbing_organisation.reload + 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.locations.count).to eq(1) + 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.today) + end + end end end end