Browse Source

set absorbing organisation relationship

pull/1801/head
Kat 3 years ago
parent
commit
a94db0c460
  1. 4
      app/services/merge/merge_organisations_service.rb
  2. 2
      db/schema.rb
  3. 19
      spec/services/merge/merge_organisations_service_spec.rb

4
app/services/merge/merge_organisations_service.rb

@ -93,13 +93,13 @@ private
end
def merge_sales_logs(merging_organisation)
merging_organisation.owned_sales_logs.after_date(Time.zone.today).each do |sales_log|
merging_organisation.sales_logs.after_date(Time.zone.today).each do |sales_log|
sales_log.update(owning_organisation: @absorbing_organisation)
end
end
def mark_organisation_as_merged(merging_organisation)
merging_organisation.update(merge_date: Time.zone.today)
merging_organisation.update(merge_date: Time.zone.today, absorbing_organisation: @absorbing_organisation)
end
def log_success_message

2
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_07_25_081029) do
ActiveRecord::Schema[7.0].define(version: 2023_07_19_150610) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

19
spec/services/merge/merge_organisations_service_spec.rb

@ -29,6 +29,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merging_organisation.reload
expect(merging_organisation.merge_date.to_date).to eq(Time.zone.today)
expect(merging_organisation.absorbing_organisation_id).to eq(absorbing_organisation.id)
end
it "combines organisation data" do
@ -46,8 +47,10 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
merging_organisation.reload
expect(absorbing_organisation.holds_own_stock).to eq(false)
expect(merging_organisation.merge_date).to eq(nil)
expect(merging_organisation.absorbing_organisation_id).to eq(nil)
expect(merging_organisation_user.organisation).to eq(merging_organisation)
end
@ -78,6 +81,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
merging_organisation.reload
expect(absorbing_organisation.rent_periods.count).to eq(2)
expect(merging_organisation.rent_periods.count).to eq(2)
end
@ -159,6 +163,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
merging_organisation.reload
expect(absorbing_organisation.owned_lettings_logs.count).to eq(2)
expect(absorbing_organisation.managed_lettings_logs.count).to eq(1)
expect(absorbing_organisation.owned_lettings_logs.find(owned_lettings_log.id).scheme).to eq(absorbing_organisation.owned_schemes.first)
@ -175,6 +180,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
merging_organisation.reload
expect(absorbing_organisation.owned_schemes.count).to eq(0)
expect(scheme.scheme_deactivation_periods.count).to eq(0)
expect(owned_lettings_log.owning_organisation).to eq(merging_organisation)
@ -193,8 +199,8 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
expect(absorbing_organisation.owned_sales_logs.count).to eq(1)
expect(absorbing_organisation.owned_sales_logs.first).to eq(sales_log)
expect(SalesLog.filter_by_owning_organisation(absorbing_organisation).count).to eq(1)
expect(SalesLog.filter_by_owning_organisation(absorbing_organisation).first).to eq(sales_log)
end
it "rolls back if there's an error" do
@ -205,7 +211,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
expect(absorbing_organisation.owned_sales_logs.count).to eq(0)
expect(absorbing_organisation.sales_logs.count).to eq(0)
expect(sales_log.owning_organisation).to eq(merging_organisation)
end
end
@ -236,13 +242,15 @@ RSpec.describe Merge::MergeOrganisationsService do
expect(merging_organisation_user.organisation).to eq(absorbing_organisation)
end
it "sets merge date on merged organisations" do
it "sets merge date and absorbing organisation on merged organisations" do
merge_organisations_service.call
merging_organisation.reload
merging_organisation_too.reload
expect(merging_organisation.merge_date.to_date).to eq(Time.zone.today)
expect(merging_organisation.absorbing_organisation_id).to eq(absorbing_organisation.id)
expect(merging_organisation_too.merge_date.to_date).to eq(Time.zone.today)
expect(merging_organisation_too.absorbing_organisation_id).to eq(absorbing_organisation.id)
end
it "combines organisation data" do
@ -260,8 +268,10 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
merging_organisation.reload
expect(absorbing_organisation.holds_own_stock).to eq(false)
expect(merging_organisation.merge_date).to eq(nil)
expect(merging_organisation.absorbing_organisation_id).to eq(nil)
expect(merging_organisation_user.organisation).to eq(merging_organisation)
end
@ -300,6 +310,7 @@ RSpec.describe Merge::MergeOrganisationsService do
merge_organisations_service.call
absorbing_organisation.reload
merging_organisation.reload
expect(absorbing_organisation.child_organisations.count).to eq(2)
expect(absorbing_organisation.parent_organisations.count).to eq(1)
expect(absorbing_organisation.child_organisations).to include(other_organisation)

Loading…
Cancel
Save