diff --git a/app/services/imports/lettings_logs_field_import_service.rb b/app/services/imports/lettings_logs_field_import_service.rb index 8b4124796..60246bf20 100644 --- a/app/services/imports/lettings_logs_field_import_service.rb +++ b/app/services/imports/lettings_logs_field_import_service.rb @@ -16,6 +16,8 @@ module Imports import_from(folder, :update_address) when "reason" import_from(folder, :update_reason) + when "homeless" + import_from(folder, :update_homelessness) else raise "Updating #{field} is not supported by the field import service" end @@ -203,5 +205,30 @@ module Imports @logger.warn("lettings log with old id #{old_id} not found") end end + + def update_homelessness(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:) + if record.present? + return @logger.info("lettings log #{record.id} has a value for homeless and rp_homeless, skipping update") if record.rp_homeless == 1 && record.homeless.present? + return @logger.info("lettings log #{record.id} has a value for homeless and reasonpref is not yes, skipping update") if record.reasonpref != 1 && record.homeless.present? + return @logger.info("lettings log #{record.id} reimport values are not homeless - 11 and rp_homeless - yes, skipping update") if unsafe_string_as_integer(xml_doc, "Q14b1").blank? || unsafe_string_as_integer(xml_doc, "Q13") != 11 + + if record.rp_homeless != 1 && record.reasonpref == 1 + record.rp_homeless = 1 + @logger.info("updating lettings log #{record.id}'s rp_homeless value to 1") + end + if record.homeless.blank? + record.homeless = 11 + @logger.info("updating lettings log #{record.id}'s homeless value to 11") + end + record.values_updated_at = Time.zone.now + record.save! + else + @logger.warn("Could not find record matching legacy ID #{old_id}") + end + end end end diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index c52d69d99..fe9b1b115 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" + when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless" 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 18eb656bb..40afd2ee6 100644 --- a/spec/lib/tasks/data_import_field_spec.rb +++ b/spec/lib/tasks/data_import_field_spec.rb @@ -116,6 +116,18 @@ describe "data_import_field imports" do end end + context "and we update homeless fields" do + let(:field) { "homeless" } + + it "updates the 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 ac35169a5..8e999107b 100644 --- a/spec/services/imports/lettings_logs_field_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_field_import_service_spec.rb @@ -545,4 +545,166 @@ RSpec.describe Imports::LettingsLogsFieldImportService do end end end + + context "when updating homeless fields" do + let(:field) { "homeless" } + + before do + Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory) + lettings_log_file.rewind + lettings_log_xml.at_xpath("//xmlns:Q14b1").content = "1" + lettings_log_xml.at_xpath("//xmlns:Q13").content = "11" + lettings_log.update!(values_updated_at: nil) + end + + context "and the lettings log already has a value for homeless (yes) and rp_homeless" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: 11, reasonpref: 1, rp_homeless: 1) + end + + it "logs that homeless and rp_homeless already has a value and does not update homeless fields" do + expect(logger).to receive(:info).with(/lettings log \d+ has a value for homeless and rp_homeless, skipping update/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(11) + expect(lettings_log.reasonpref).to eq(1) + expect(lettings_log.rp_homeless).to eq(1) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "and the lettings log already has a value for homeless (no) and rp_homeless" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: 1, reasonpref: 1, rp_homeless: 1) + end + + it "logs that homeless and rp_homeless already has a value and does not update homeless fields" do + expect(logger).to receive(:info).with(/lettings log \d+ has a value for homeless and rp_homeless, skipping update/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(1) + expect(lettings_log.reasonpref).to eq(1) + expect(lettings_log.rp_homeless).to eq(1) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "and the lettings log already has a value for homeless and reasonpref(no or don't know)" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: 11, reasonpref: 2, rp_homeless: nil) + end + + it "logs that homeless and reasonpref already has a value and does not update homeless" do + expect(logger).to receive(:info).with(/lettings log \d+ has a value for homeless and reasonpref is not yes, skipping update/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(11) + expect(lettings_log.reasonpref).to eq(2) + expect(lettings_log.rp_homeless).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "and the lettings log has a value for homeless and reasonpref(yes)" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: 11, reasonpref: 1, rp_insan_unsat: nil) + end + + it "only updates rp_homeless" do + expect(logger).to receive(:info).with(/updating lettings log \d+'s rp_homeless value to 1/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(11) + expect(lettings_log.reasonpref).to eq(1) + expect(lettings_log.rp_homeless).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "and the lettings log has no values for homeless and rp_homeless and reasonpref is Yes" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: nil, reasonpref: 1, rp_homeless: 0) + end + + it "updates homeless and rp_homeless" do + expect(logger).to receive(:info).with(/updating lettings log \d+'s homeless value to 11/) + expect(logger).to receive(:info).with(/updating lettings log \d+'s rp_homeless value to 1/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(11) + expect(lettings_log.reasonpref).to eq(1) + expect(lettings_log.rp_homeless).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "and the lettings log has no values for homeless and reasonpref is Yes" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: nil, reasonpref: 1, rp_homeless: 1) + end + + it "updates homeless" do + expect(logger).to receive(:info).with(/updating lettings log \d+'s homeless value to 11/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(11) + expect(lettings_log.reasonpref).to eq(1) + expect(lettings_log.rp_homeless).to eq(1) + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "and the lettings log has no values for homeless and reasonpref is No" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log.update!(homeless: nil, reasonpref: 2, rp_homeless: nil) + end + + it "updates homeless" do + expect(logger).to receive(:info).with(/updating lettings log \d+'s homeless value to 11/) + import_service.send(:update_homelessness, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.homeless).to eq(11) + expect(lettings_log.reasonpref).to eq(2) + expect(lettings_log.rp_homeless).to eq(nil) + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "and the new values do not set rp_homeless" do + let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) } + + before do + lettings_log_xml.at_xpath("//xmlns:Q14b1").content = "" + lettings_log_xml.at_xpath("//xmlns:Q13").content = "1" + end + + it "skips update for any fields" do + expect(logger).to receive(:info).with(/lettings log \d+ reimport values are not homeless - 11 and rp_homeless - yes, skipping update/) + expect { import_service.send(:update_homelessness, lettings_log_xml) } + .not_to(change { lettings_log.reload.homeless }) + expect(lettings_log.values_updated_at).to be_nil + end + end + end end