diff --git a/app/services/imports/lettings_logs_field_import_service.rb b/app/services/imports/lettings_logs_field_import_service.rb index 75acef8fd..cc53991a4 100644 --- a/app/services/imports/lettings_logs_field_import_service.rb +++ b/app/services/imports/lettings_logs_field_import_service.rb @@ -26,6 +26,8 @@ module Imports import_from(folder, :update_general_needs_referral) when "person_details" import_from(folder, :update_person_details) + when "childrens_care_referral" + import_from(folder, :update_childrens_care_referral) else raise "Updating #{field} is not supported by the field import service" end @@ -409,5 +411,19 @@ module Imports def person_exists_on_the_log?(record, person_index) person_index <= record.hhmemb end + + def update_childrens_care_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} reimport referral value is not 17, skipping update") if unsafe_string_as_integer(xml_doc, "Q16") != 17 + + record.update!(referral: 17, values_updated_at: Time.zone.now) + @logger.info("lettings log #{record.id}'s referral value has been set to 17") + end end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 6c529f68a..9751bf569 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -7103,6 +7103,9 @@ "13": { "value": "Youth offending team" }, + "17": { + "value": "Children’s Social Care" + }, "16": { "value": "Other" } @@ -7163,6 +7166,9 @@ "13": { "value": "Youth offending team" }, + "17": { + "value": "Children’s Social Care" + }, "16": { "value": "Other" } @@ -7220,6 +7226,9 @@ "13": { "value": "Youth offending team" }, + "17": { + "value": "Children’s Social Care" + }, "16": { "value": "Other" } @@ -7280,6 +7289,9 @@ "13": { "value": "Youth offending team" }, + "17": { + "value": "Children’s Social Care" + }, "16": { "value": "Other" } diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index 3c3be4d2f..e2f709929 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", "general_needs_referral", "person_details" + when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless", "created_by", "sex_and_relat", "general_needs_referral", "person_details", "childrens_care_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 b84cb9d23..aaed8d990 100644 --- a/spec/lib/tasks/data_import_field_spec.rb +++ b/spec/lib/tasks/data_import_field_spec.rb @@ -176,6 +176,18 @@ describe "data_import_field imports" do end end + context "and we update the childrens_care_referral field" do + let(:field) { "childrens_care_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 7f7c8bf3c..733d73b8a 100644 --- a/spec/services/imports/lettings_logs_field_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_field_import_service_spec.rb @@ -1016,7 +1016,7 @@ RSpec.describe Imports::LettingsLogsFieldImportService do 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 }) + .not_to(change { lettings_log.reload.referral }) expect(lettings_log.values_updated_at).to be_nil end end @@ -1353,4 +1353,60 @@ RSpec.describe Imports::LettingsLogsFieldImportService do end end end + + context "when updating referral with children's care" do + 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 = "17" + 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 17/) + expect { import_service.send(:update_childrens_care_referral, lettings_log_xml) } + .to(change { lettings_log.reload.referral }.from(nil).to(17)) + 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_childrens_care_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 "and the new value does not set referral to 17" 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 17, skipping update/) + expect { import_service.send(:update_childrens_care_referral, lettings_log_xml) } + .not_to(change { lettings_log.reload.referral }) + expect(lettings_log.values_updated_at).to be_nil + end + end + end end