From 44797637fbebe93532cde710956818dab45adf0f Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:23:12 +0100 Subject: [PATCH] CLDC-2769 Reimport unassigned logs (#1919) * Reimport logs with created by missing * Reimport unassigned sales logs --- .../lettings_logs_field_import_service.rb | 19 +++++ .../sales_logs_field_import_service.rb | 19 +++++ lib/tasks/data_import_field.rake | 4 +- spec/lib/tasks/data_import_field_spec.rb | 33 +++++++++ ...lettings_logs_field_import_service_spec.rb | 72 ++++++++++++++++++ .../sales_logs_field_import_service_spec.rb | 74 +++++++++++++++++++ 6 files changed, 219 insertions(+), 2 deletions(-) diff --git a/app/services/imports/lettings_logs_field_import_service.rb b/app/services/imports/lettings_logs_field_import_service.rb index 60246bf20..f69fe5792 100644 --- a/app/services/imports/lettings_logs_field_import_service.rb +++ b/app/services/imports/lettings_logs_field_import_service.rb @@ -18,6 +18,8 @@ module Imports import_from(folder, :update_reason) when "homeless" import_from(folder, :update_homelessness) + when "created_by" + import_from(folder, :update_created_by) else raise "Updating #{field} is not supported by the field import service" end @@ -230,5 +232,22 @@ module Imports @logger.warn("Could not find record matching legacy ID #{old_id}") end end + + # deletes and recreates the entire record if created_by is missing + def update_created_by(xml_doc) + return if meta_field_value(xml_doc, "form-name").include?("Sales") + + old_id = meta_field_value(xml_doc, "document-id") + record = LettingsLog.find_by(old_id:) + + return @logger.warn("lettings log with old id #{old_id} not found") unless record + return @logger.info("lettings log #{record.id} has created_by value, skipping update") if record.created_by.present? + + record.destroy! + @logger.info("lettings log #{record.id} has been deleted") + log_import_service = Imports::LettingsLogsImportService.new(nil, @logger) + log_import_service.send(:create_log, xml_doc) + @logger.info("lettings log \"#{record.old_id}\" has been reimported with id #{record.id}") + end end end diff --git a/app/services/imports/sales_logs_field_import_service.rb b/app/services/imports/sales_logs_field_import_service.rb index 4cda93d9d..cf5412729 100644 --- a/app/services/imports/sales_logs_field_import_service.rb +++ b/app/services/imports/sales_logs_field_import_service.rb @@ -8,6 +8,8 @@ module Imports import_from(folder, :update_owning_organisation_id) when "old_form_id" import_from(folder, :update_old_form_id) + when "created_by" + import_from(folder, :update_created_by) else raise "Updating #{field} is not supported by the field import service" end @@ -72,5 +74,22 @@ module Imports @logger.warn("sales log with old id #{old_id} not found") end end + + # deletes and recreates the entire record if created_by is missing + def update_created_by(xml_doc) + return unless meta_field_value(xml_doc, "form-name").include?("Sales") + + old_id = meta_field_value(xml_doc, "document-id") + record = SalesLog.find_by(old_id:) + + return @logger.warn("sales log with old id #{old_id} not found") unless record + return @logger.info("sales log #{record.id} has created_by value, skipping update") if record.created_by.present? + + record.destroy! + @logger.info("sales log #{record.id} has been deleted") + log_import_service = Imports::SalesLogsImportService.new(nil, @logger) + log_import_service.send(:create_log, xml_doc) + @logger.info("sales log \"#{record.old_id}\" has been reimported with id #{record.id}") + end end end diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index fe9b1b115..478c1f59b 100644 --- a/lib/tasks/data_import_field.rake +++ b/lib/tasks/data_import_field.rake @@ -7,7 +7,7 @@ namespace :core do # We only allow a reduced list of known fields to be updatable case field - when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless" + when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless", "created_by" s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) archive_io = s3_service.get_file_io(path) archive_service = Storage::ArchiveService.new(archive_io) @@ -29,7 +29,7 @@ namespace :core do # We only allow a reduced list of known fields to be updatable case field - when "owning_organisation_id", "old_form_id" + when "owning_organisation_id", "old_form_id", "created_by" s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) archive_io = s3_service.get_file_io(path) archive_service = Storage::ArchiveService.new(archive_io) diff --git a/spec/lib/tasks/data_import_field_spec.rb b/spec/lib/tasks/data_import_field_spec.rb index 40afd2ee6..c8d1dd056 100644 --- a/spec/lib/tasks/data_import_field_spec.rb +++ b/spec/lib/tasks/data_import_field_spec.rb @@ -128,6 +128,18 @@ describe "data_import_field imports" do end end + context "and we update the created_by field" do + let(:field) { "created_by" } + + it "updates the 2023 logs from the given XML file" do + expect(Storage::S3Service).to receive(:new).with(paas_config_service, instance_name) + expect(storage_service).to receive(:get_file_io).with("spec/fixtures/imports/logs") + expect(Imports::LettingsLogsFieldImportService).to receive(:new).with(archive_service) + expect(import_service).to receive(:update_field).with(field, "logs") + task.invoke(field, fixture_path) + end + end + it "raises an exception if no parameters are provided" do expect { task.invoke }.to raise_error(/Usage/) end @@ -217,6 +229,27 @@ describe "data_import_field imports" do end end + context "and we update the created_by field" do + let(:field) { "created_by" } + + it "updates the logs from the given XML file when the VCAP_SERVICES environment variable exists" do + expect(Storage::S3Service).to receive(:new).with(paas_config_service, instance_name) + expect(storage_service).to receive(:get_file_io).with("spec/fixtures/imports/sales_logs") + expect(Imports::SalesLogsFieldImportService).to receive(:new).with(archive_service) + expect(import_service).to receive(:update_field).with(field, "logs") + task.invoke(field, fixture_path) + end + + it "updates the logs from the given XML file when the VCAP_SERVICES environment variable does not exist" do + allow(ENV).to receive(:[]).with("VCAP_SERVICES") + expect(Storage::S3Service).to receive(:new).with(env_config_service, instance_name) + expect(storage_service).to receive(:get_file_io).with("spec/fixtures/imports/sales_logs") + expect(Imports::SalesLogsFieldImportService).to receive(:new).with(archive_service) + expect(import_service).to receive(:update_field).with(field, "logs") + task.invoke(field, fixture_path) + end + end + it "raises an exception if no parameters are provided" do expect { task.invoke }.to raise_error(/Usage/) end diff --git a/spec/services/imports/lettings_logs_field_import_service_spec.rb b/spec/services/imports/lettings_logs_field_import_service_spec.rb index 8e999107b..669482b8a 100644 --- a/spec/services/imports/lettings_logs_field_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_field_import_service_spec.rb @@ -707,4 +707,76 @@ RSpec.describe Imports::LettingsLogsFieldImportService do end end end + + context "when updating created_by" do + let(:field) { "created_by" } + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + let(:old_log_id) { lettings_log.id } + + before do + Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory) + old_log_id + lettings_log_file.rewind + lettings_log.update!(values_updated_at: nil) + end + + context "when the lettings log has created_by value" do + it "skips the update" do + expect(logger).to receive(:info).with(/lettings log \d+ has created_by value, skipping update/) + import_service.send(:update_created_by, lettings_log_xml) + + old_lettings_log = LettingsLog.find(old_log_id) + expect(old_lettings_log).not_to be_nil + + new_lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + expect(new_lettings_log).to eq(old_lettings_log) + expect(new_lettings_log.values_updated_at).to be_nil + end + end + + context "when the lettings log has no created_by value" do + before do + lettings_log.update!(created_by: nil) + end + + it "deletes the existing lettings log and creates a new log with correct created_by" do + expect(logger).to receive(:info).with(/lettings log \d+ has been deleted/) + expect(logger).to receive(:info).with(/lettings log "0ead17cb-1668-442d-898c-0d52879ff592" has been reimported with id \d+/) + import_service.send(:update_created_by, lettings_log_xml) + + old_lettings_log = LettingsLog.find_by(id: old_log_id) + expect(old_lettings_log).to be_nil + + new_lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + expect(new_lettings_log).not_to eq(old_lettings_log) + expect(new_lettings_log.values_updated_at).not_to be_nil + end + + it "deletes the existing lettings log and creates a new log with correct unassigned created_by" do + lettings_log_xml.at_xpath("//meta:owner-user-id").content = "fake_id" + + expect(logger).to receive(:info).with(/lettings log \d+ has been deleted/) + expect(logger).to receive(:info).with(/lettings log "0ead17cb-1668-442d-898c-0d52879ff592" has been reimported with id \d+/) + expect(logger).to receive(:error).with(/Lettings log '0ead17cb-1668-442d-898c-0d52879ff592' belongs to legacy user with owner-user-id: 'fake_id' which cannot be found. Assigning log to 'Unassigned' user./) + + import_service.send(:update_created_by, lettings_log_xml) + + old_lettings_log = LettingsLog.find_by(id: old_log_id) + expect(old_lettings_log).to be_nil + + new_lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + expect(new_lettings_log).not_to eq(old_lettings_log) + expect(new_lettings_log.created_by.name).to eq("Unassigned") + expect(new_lettings_log.values_updated_at).not_to be_nil + end + end + + context "and the log was not previously imported" do + it "logs a warning that the log has not been found in the db" do + lettings_log.destroy! + expect(logger).to receive(:warn).with("lettings log with old id #{lettings_log_id} not found") + expect { import_service.send(:update_created_by, lettings_log_xml) }.not_to change(LettingsLog, :count) + end + end + end end diff --git a/spec/services/imports/sales_logs_field_import_service_spec.rb b/spec/services/imports/sales_logs_field_import_service_spec.rb index 6b39b3acb..9bd7ce335 100644 --- a/spec/services/imports/sales_logs_field_import_service_spec.rb +++ b/spec/services/imports/sales_logs_field_import_service_spec.rb @@ -9,6 +9,7 @@ RSpec.describe Imports::SalesLogsFieldImportService do let(:fixture_directory) { "spec/fixtures/imports/sales_logs" } let(:sales_log_filename) { "shared_ownership_sales_log" } let(:sales_log_file) { File.open("#{fixture_directory}/#{sales_log_filename}.xml") } + let(:sales_log_xml) { Nokogiri::XML(sales_log_file) } let(:organisation) { create(:organisation, old_visible_id: "1", old_org_id: "7c5bd5fb549c09a2c55d7cb90d7ba84927e64618") } let(:old_user_id) { "c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa" } @@ -147,4 +148,77 @@ RSpec.describe Imports::SalesLogsFieldImportService do end end end + + context "when updating created_by" do + let(:field) { "created_by" } + let(:sales_log_filename) { "shared_ownership_sales_log" } + let(:sales_log) { SalesLog.find_by(old_id: sales_log_filename) } + let(:old_log_id) { sales_log.id } + + before do + Imports::SalesLogsImportService.new(storage_service, logger).create_logs(fixture_directory) + old_log_id + sales_log_file.rewind + sales_log.update!(values_updated_at: nil) + end + + context "when the sales log has created_by value" do + it "skips the update" do + expect(logger).to receive(:info).with(/sales log \d+ has created_by value, skipping update/) + import_service.send(:update_created_by, sales_log_xml) + + old_sales_log = SalesLog.find(old_log_id) + expect(old_sales_log).not_to be_nil + + new_sales_log = SalesLog.find_by(old_id: sales_log_filename) + expect(new_sales_log).to eq(old_sales_log) + expect(new_sales_log.values_updated_at).to be_nil + end + end + + context "when the sales log has no created_by value" do + before do + sales_log.update!(created_by: nil) + end + + it "deletes the existing sales log and creates a new log with correct created_by" do + expect(logger).to receive(:info).with(/sales log \d+ has been deleted/) + expect(logger).to receive(:info).with(/sales log "shared_ownership_sales_log" has been reimported with id \d+/) + import_service.send(:update_created_by, sales_log_xml) + + old_sales_log = SalesLog.find_by(id: old_log_id) + expect(old_sales_log).to be_nil + + new_sales_log = SalesLog.find_by(old_id: sales_log_filename) + expect(new_sales_log).not_to eq(old_sales_log) + expect(new_sales_log.values_updated_at).not_to be_nil + end + + it "deletes the existing sales log and creates a new log with correct unassigned created_by" do + sales_log_xml.at_xpath("//meta:owner-user-id").content = "fake_id" + + expect(logger).to receive(:info).with(/sales log \d+ has been deleted/) + expect(logger).to receive(:info).with(/sales log "shared_ownership_sales_log" has been reimported with id \d+/) + expect(logger).to receive(:error).with(/Sales log 'shared_ownership_sales_log' belongs to legacy user with owner-user-id: 'fake_id' which cannot be found. Assigning log to 'Unassigned' user./) + + import_service.send(:update_created_by, sales_log_xml) + + old_sales_log = SalesLog.find_by(id: old_log_id) + expect(old_sales_log).to be_nil + + new_sales_log = SalesLog.find_by(old_id: sales_log_filename) + expect(new_sales_log).not_to eq(old_sales_log) + expect(new_sales_log.created_by.name).to eq("Unassigned") + expect(new_sales_log.values_updated_at).not_to be_nil + end + end + + context "and the log was not previously imported" do + it "logs a warning that the log has not been found in the db" do + sales_log.destroy! + expect(logger).to receive(:warn).with("sales log with old id #{sales_log_filename} not found") + expect { import_service.send(:update_created_by, sales_log_xml) }.not_to change(SalesLog, :count) + end + end + end end