Browse Source

Add logging

pull/1801/head
Kat 3 years ago
parent
commit
8b3b19ccba
  1. 7
      app/services/merge/merge_organisations_service.rb
  2. 12
      spec/services/merge/merge_organisations_service_spec.rb

7
app/services/merge/merge_organisations_service.rb

@ -6,6 +6,8 @@ class Merge::MergeOrganisationsService
def call def call
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
@users_success_message = "Absorbing organisation users: #{@absorbing_organisation.users.map { |user| "#{user.name} (#{user.email})" }.join(', ')}\n"
@schemes_success_message = ""
merge_organisation_details merge_organisation_details
@merging_organisations.each do |merging_organisation| @merging_organisations.each do |merging_organisation|
merge_rent_periods(merging_organisation) merge_rent_periods(merging_organisation)
@ -17,6 +19,8 @@ class Merge::MergeOrganisationsService
end end
@absorbing_organisation.save! @absorbing_organisation.save!
mark_organisations_as_merged mark_organisations_as_merged
Rails.logger.info(@users_success_message)
Rails.logger.info(@schemes_success_message)
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
Rails.logger.error("Organisation merge failed with: #{e.message}") Rails.logger.error("Organisation merge failed with: #{e.message}")
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
@ -53,10 +57,12 @@ private
end end
def merge_users(merging_organisation) def merge_users(merging_organisation)
@users_success_message += "Merged users from #{merging_organisation.name}: #{merging_organisation.users.map { |user| "#{user.name} (#{user.email})" }.join(', ')}\n"
merging_organisation.users.update_all(organisation_id: @absorbing_organisation.id) merging_organisation.users.update_all(organisation_id: @absorbing_organisation.id)
end end
def merge_schemes_and_locations(merging_organisation) def merge_schemes_and_locations(merging_organisation)
@schemes_success_message += "New schemes from #{merging_organisation.name}:\n"
merging_organisation.owned_schemes.each do |scheme| merging_organisation.owned_schemes.each do |scheme|
next if scheme.deactivated? next if scheme.deactivated?
@ -64,6 +70,7 @@ private
scheme.locations.each do |location| scheme.locations.each do |location|
new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id")) unless location.deactivated? new_scheme.locations << Location.new(location.attributes.except("id", "scheme_id")) unless location.deactivated?
end end
@schemes_success_message += "Scheme #{new_scheme.service_name} with locations: #{new_scheme.locations.map { |location| "#{location.name} (#{location.postcode})" }.join(', ')}\n"
SchemeDeactivationPeriod.create!(scheme:, deactivation_date: Time.zone.now) SchemeDeactivationPeriod.create!(scheme:, deactivation_date: Time.zone.now)
end end
end end

12
spec/services/merge/merge_organisations_service_spec.rb

@ -8,12 +8,14 @@ RSpec.describe Merge::MergeOrganisationsService do
describe "#call" do describe "#call" do
context "when merging a single organisation into an existing organisation" do context "when merging a single organisation into an existing organisation" do
let(:merging_organisation) { create(:organisation, holds_own_stock: true) } let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org") }
let(:merging_organisation_ids) { [merging_organisation.id] } let(:merging_organisation_ids) { [merging_organisation.id] }
let!(:merging_organisation_user) { create(:user, organisation: merging_organisation) } let!(:merging_organisation_user) { create(:user, organisation: merging_organisation, name: "fake name", email: "fake@email.com") }
it "moves the users from merging organisation to absorbing organisation" do it "moves the users from merging organisation to absorbing organisation" do
expect(Rails.logger).to receive(:info).with("Absorbing organisation users: Danny Rojas (#{absorbing_organisation.data_protection_officers.first.email})\nMerged users from fake org: Danny Rojas (#{merging_organisation.data_protection_officers.first.email}), fake name (fake@email.com)\n")
expect(Rails.logger).to receive(:info).with("New schemes from fake org:\n")
merge_organisations_service.call merge_organisations_service.call
merging_organisation_user.reload merging_organisation_user.reload
@ -125,7 +127,7 @@ RSpec.describe Merge::MergeOrganisationsService do
let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) } let!(:owned_lettings_log_no_location) { create(:lettings_log, :sh, scheme:, startdate: Time.zone.tomorrow, owning_organisation: merging_organisation) }
before do before do
create(:location, scheme:) create(:location, scheme:, name: "fake location", postcode: "A1 1AA")
create(:location, scheme: deactivated_scheme) create(:location, scheme: deactivated_scheme)
create(:scheme_deactivation_period, scheme: deactivated_scheme, deactivation_date: Time.zone.today - 1.month) 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) create(:location_deactivation_period, location: deactivated_location, deactivation_date: Time.zone.today - 1.month)
@ -133,7 +135,9 @@ RSpec.describe Merge::MergeOrganisationsService do
create(:lettings_log, startdate: Time.zone.tomorrow, managing_organisation: merging_organisation) create(:lettings_log, startdate: Time.zone.tomorrow, managing_organisation: merging_organisation)
end end
it "combines organisation relationships" do it "combines organisation schemes and locations" do
expect(Rails.logger).to receive(:info).with("Absorbing organisation users: Danny Rojas (#{absorbing_organisation.data_protection_officers.first.email})\nMerged users from fake org: Danny Rojas (#{merging_organisation.data_protection_officers.first.email}), fake name (fake@email.com)\n")
expect(Rails.logger).to receive(:info).with("New schemes from fake org:\nScheme #{scheme.service_name} with locations: #{location.name} (#{location.postcode}), fake location (A1 1AA)\n")
merge_organisations_service.call merge_organisations_service.call
absorbing_organisation.reload absorbing_organisation.reload

Loading…
Cancel
Save