diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index c70da3ab4..1d5ddfca3 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -246,6 +246,7 @@ module Imports apply_date_consistency!(attributes) apply_household_consistency!(attributes) + create_organisation_relationship!(attributes) lettings_log = save_lettings_log(attributes, previous_status) compute_differences(lettings_log, attributes) @@ -681,5 +682,13 @@ module Imports 0 end end + + def create_organisation_relationship!(attributes) + parent_organisation_id = attributes["owning_organisation_id"] + child_organisation_id = attributes["managing_organisation_id"] + return if parent_organisation_id == child_organisation_id + + OrganisationRelationship.find_or_create_by!(parent_organisation_id:, child_organisation_id:) + end end end diff --git a/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml b/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml index e9835d1e1..8bc6a935c 100644 --- a/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml +++ b/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml @@ -413,7 +413,7 @@ 655 655 - 1 + 2 3 diff --git a/spec/fixtures/imports/logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml b/spec/fixtures/imports/logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml index 3faacf28b..38cef1339 100644 --- a/spec/fixtures/imports/logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml +++ b/spec/fixtures/imports/logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml @@ -413,7 +413,7 @@ N/A N/A - 1 + 2 false diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index acb9d9dd4..7850008a1 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -11,6 +11,7 @@ RSpec.describe Imports::LettingsLogsImportService do let(:fixture_directory) { "spec/fixtures/imports/logs" } let(:organisation) { FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") } + let(:managing_organisation) { FactoryBot.create(:organisation, old_visible_id: "2", provider_type: "PRP") } let(:scheme1) { FactoryBot.create(:scheme, old_visible_id: "0123", owning_organisation: organisation) } let(:scheme2) { FactoryBot.create(:scheme, old_visible_id: "456", owning_organisation: organisation) } @@ -24,6 +25,7 @@ RSpec.describe Imports::LettingsLogsImportService do allow(Organisation).to receive(:find_by).and_return(nil) allow(Organisation).to receive(:find_by).with(old_visible_id: organisation.old_visible_id).and_return(organisation) + allow(Organisation).to receive(:find_by).with(old_visible_id: managing_organisation.old_visible_id).and_return(managing_organisation) # Created by users FactoryBot.create(:user, old_user_id: "c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa", organisation:) @@ -80,6 +82,13 @@ RSpec.describe Imports::LettingsLogsImportService do .to change(LettingsLog, :count).by(4) end + it "creates organisation relationship once" do + expect(logger).not_to receive(:error) + expect(logger).not_to receive(:warn) + expect { lettings_log_service.create_logs(remote_folder) } + .to change(OrganisationRelationship, :count).by(1) + end + context "when there are status discrepancies" do let(:lettings_log_id5) { "893ufj2s-lq77-42m4-rty6-ej09gh585uy1" } let(:lettings_log_id6) { "5ybz29dj-l33t-k1l0-hj86-n4k4ma77xkcd" }