diff --git a/app/services/imports/lettings_logs_field_import_service.rb b/app/services/imports/lettings_logs_field_import_service.rb index 7e7932771..d63cc3354 100644 --- a/app/services/imports/lettings_logs_field_import_service.rb +++ b/app/services/imports/lettings_logs_field_import_service.rb @@ -22,6 +22,8 @@ module Imports import_from(folder, :update_created_by) when "sex_and_relat" import_from(folder, :update_sex_and_relat) + when "general_needs_referral" + import_from(folder, :update_general_needs_referral) else raise "Updating #{field} is not supported by the field import service" end @@ -281,5 +283,21 @@ module Imports record.save! end end + + def update_general_needs_referral(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 a value for referral, skipping update") if record.referral.present? + return @logger.info("lettings log #{record.id} is a supported housing log, skipping update") if record.needstype == 2 + return @logger.info("lettings log #{record.id}'s owning organisation's provider type is LA, skipping update") if record.owning_organisation.provider_type == "LA" + return @logger.info("lettings log #{record.id} reimport referral value is not 4, skipping update") if unsafe_string_as_integer(xml_doc, "Q16") != 4 + + record.update!(referral: 4, referral_value_check: 0, values_updated_at: Time.zone.now) + @logger.info("lettings log #{record.id}'s referral value has been set to 4") + end end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 1d6626e9d..6c529f68a 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -7136,6 +7136,9 @@ "3": { "value": "Nominated by a local housing authority" }, + "4": { + "value": "Referred by local authority housing department" + }, "8": { "value": "Re-located through official housing mobility scheme" }, diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index db80d9af4..f9879b4c6 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", "created_by", "sex_and_relat" + when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless", "created_by", "sex_and_relat", "general_needs_referral" 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 de527062b..6973e0762 100644 --- a/spec/lib/tasks/data_import_field_spec.rb +++ b/spec/lib/tasks/data_import_field_spec.rb @@ -152,6 +152,18 @@ describe "data_import_field imports" do end end + context "and we update the referral (for general needs) field" do + let(:field) { "general_needs_referral" } + + 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 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 31cbcb269..9cc3ddfe5 100644 --- a/spec/services/imports/lettings_logs_field_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_field_import_service_spec.rb @@ -937,4 +937,88 @@ RSpec.describe Imports::LettingsLogsFieldImportService do end end end + + context "when updating referral" do + let(:field) { "referral" } + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory) + lettings_log_file.rewind + + lettings_log_xml.at_xpath("//xmlns:Q16").content = "4" + lettings_log.owning_organisation.update!(provider_type: "PRP") + end + + context "when the lettings log has no referral value" do + before do + lettings_log.update!(renewal: 0, referral: nil, values_updated_at: nil) + end + + it "updates the lettings_log referral value" do + expect(logger).to receive(:info).with(/lettings log \d+'s referral value has been set to 4/) + expect { import_service.send(:update_general_needs_referral, lettings_log_xml) } + .to(change { lettings_log.reload.referral }.from(nil).to(4)) + expect(lettings_log.referral_value_check).to eq(0) + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "when the lettings log has a referral value" do + before do + lettings_log.update!(referral: 2, values_updated_at: nil) + end + + it "does not update the lettings_log referral value" do + expect(logger).to receive(:info).with(/lettings log \d+ has a value for referral, skipping update/) + import_service.send(:update_general_needs_referral, lettings_log_xml) + lettings_log.reload + expect(lettings_log.referral).to eq(2) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "when the lettings log is supported housing" do + before do + lettings_log.update!(referral: nil, needstype: 2, values_updated_at: nil) + end + + it "does not update the lettings_log referral value" do + expect(logger).to receive(:info).with(/lettings log \d+ is a supported housing log, skipping update/) + expect { import_service.send(:update_general_needs_referral, lettings_log_xml) } + .not_to(change { lettings_log.reload.referral }) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "when the owning organisation is LA" do + before do + lettings_log.update!(referral: nil, values_updated_at: nil) + lettings_log.owning_organisation.update!(provider_type: "LA") + end + + it "does not update the lettings_log referral value" do + expect(logger).to receive(:info).with(/lettings log \d+'s owning organisation's provider type is LA, skipping update/) + expect { import_service.send(:update_general_needs_referral, lettings_log_xml) } + .not_to(change { lettings_log.reload.referral }) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "and the new value does not set referral to 4" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(referral: nil, values_updated_at: nil) + lettings_log_xml.at_xpath("//xmlns:Q16").content = "1" + end + + it "skips update" do + expect(logger).to receive(:info).with(/lettings log \d+ reimport referral value is not 4, skipping update/) + expect { import_service.send(:update_general_needs_referral, lettings_log_xml) } + .not_to(change { lettings_log.reload.referral }) + expect(lettings_log.values_updated_at).to be_nil + end + end + end end