diff --git a/app/services/imports/lettings_logs_field_import_service.rb b/app/services/imports/lettings_logs_field_import_service.rb index f69fe5792..4530b5c38 100644 --- a/app/services/imports/lettings_logs_field_import_service.rb +++ b/app/services/imports/lettings_logs_field_import_service.rb @@ -20,6 +20,8 @@ module Imports import_from(folder, :update_homelessness) when "created_by" import_from(folder, :update_created_by) + when "sex_and_relat" + import_from(folder, :update_sex_and_relat) else raise "Updating #{field} is not supported by the field import service" end @@ -249,5 +251,35 @@ module Imports log_import_service.send(:create_log, xml_doc) @logger.info("lettings log \"#{record.old_id}\" has been reimported with id #{record.id}") end + + def update_sex_and_relat(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 no hhmemb value, skipping update") if record.hhmemb.blank? + + (2..record.hhmemb).each do |i| + next @logger.info("lettings log #{record.id} has values for sex#{i} and relat#{i}, skipping update") if record["sex#{i}"] && record["relat#{i}"] + next @logger.info("lettings log #{record.id} has value 'no' for details_known_#{i}, skipping update") if record["details_known_#{i}"] == 1 + + if record["sex#{i}"].blank? + record["sex#{i}"] = sex(xml_doc, i) + @logger.info("lettings log #{record.id}'s sex#{i} value has been set to #{record["sex#{i}"]}") + end + + if record["relat#{i}"].blank? + record["relat#{i}"] = relat(xml_doc, i) + @logger.info("lettings log #{record.id}'s relat#{i} value has been set to #{record["relat#{i}"]}") + end + end + + if record.changed? + record.values_updated_at = Time.zone.now + record.save! + end + end end end diff --git a/lib/tasks/data_import_field.rake b/lib/tasks/data_import_field.rake index 478c1f59b..db80d9af4 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" + when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless", "created_by", "sex_and_relat" 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 c8d1dd056..de527062b 100644 --- a/spec/lib/tasks/data_import_field_spec.rb +++ b/spec/lib/tasks/data_import_field_spec.rb @@ -140,6 +140,18 @@ describe "data_import_field imports" do end end + context "and we update the sex_and_relat fields" do + let(:field) { "sex_and_relat" } + + 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 669482b8a..782ce86c8 100644 --- a/spec/services/imports/lettings_logs_field_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_field_import_service_spec.rb @@ -779,4 +779,162 @@ RSpec.describe Imports::LettingsLogsFieldImportService do end end end + + context "when updating sex_and_relat" do + let(:field) { "sex_and_relat" } + 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:P2Sex").content = "Person prefers not to say" + lettings_log_xml.at_xpath("//xmlns:P2Rel").content = "Person prefers not to say" + end + + context "when the lettings log has no sex or relat value" do + before do + lettings_log.update!(sex2: nil, relat2: nil, details_known_2: 0, values_updated_at: nil) + end + + it "updates the lettings_log sex and relat value if details for person are known" do + expect(logger).to receive(:info).with(/lettings log \d+'s sex2 value has been set to R/) + expect(logger).to receive(:info).with(/lettings log \d+'s relat2 value has been set to R/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + lettings_log.reload + expect(lettings_log.sex2).to eq("R") + expect(lettings_log.relat2).to eq("R") + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "does not update the lettings_log sex and relat value if details for person are not known" do + lettings_log.update!(details_known_2: 1) + + expect(logger).to receive(:info).with(/lettings log \d+ has value 'no' for details_known_2, skipping update/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + lettings_log.reload + expect(lettings_log.sex2).to eq(nil) + expect(lettings_log.relat2).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "when the lettings log has sex and relat value" do + before do + lettings_log.update!(sex2: "F", relat2: "R", details_known_2: 0, values_updated_at: nil) + end + + it "does not update the lettings_log sex and relat values" do + expect(logger).to receive(:info).with(/lettings log \d+ has values for sex2 and relat2, skipping update/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + lettings_log.reload + expect(lettings_log.sex2).to eq("F") + expect(lettings_log.relat2).to eq("R") + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "when the lettings log has sex value" do + before do + lettings_log.update!(sex2: "F", relat2: nil, details_known_2: 0, values_updated_at: nil) + end + + it "only updates relat value" do + expect(logger).to receive(:info).with(/lettings log \d+'s relat2 value has been set to R/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + lettings_log.reload + expect(lettings_log.sex2).to eq("F") + expect(lettings_log.relat2).to eq("R") + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "when the lettings log has relat value" do + before do + lettings_log.update!(sex2: nil, relat2: "X", details_known_2: 0, values_updated_at: nil) + end + + it "only updates sex value" do + expect(logger).to receive(:info).with(/lettings log \d+'s sex2 value has been set to R/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + lettings_log.reload + expect(lettings_log.sex2).to eq("R") + expect(lettings_log.relat2).to eq("X") + expect(lettings_log.values_updated_at).not_to be_nil + end + end + + context "when the lettings log has no hhmemb value" do + before do + lettings_log.update!(sex2: nil, relat2: nil, hhmemb: nil, values_updated_at: nil) + end + + it "does not update the lettings_log sex and relat values" do + expect(logger).to receive(:info).with(/lettings log \d+ has no hhmemb value, skipping update/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + lettings_log.reload + expect(lettings_log.sex2).to eq(nil) + expect(lettings_log.relat2).to eq(nil) + expect(lettings_log.values_updated_at).to be_nil + end + end + + context "when there are multiple people details given" do + before do + (2..8).each do |i| + lettings_log_xml.at_xpath("//xmlns:P#{i}Sex").content = "Person prefers not to say" + lettings_log_xml.at_xpath("//xmlns:P#{i}Rel").content = "Person prefers not to say" + end + end + + it "correctly skips and sets sex and relat values" do + lettings_log.update!(sex2: "F", relat2: "X", details_known_2: 0, + sex3: nil, relat3: nil, details_known_3: 0, + sex4: "F", relat4: nil, details_known_4: 0, + sex5: nil, relat5: "X", details_known_5: 0, + sex6: nil, relat6: nil, details_known_6: 1, + hhmemb: 6, values_updated_at: nil) + + expect(logger).to receive(:info).with(/lettings log \d+ has values for sex2 and relat2, skipping update/) + expect(logger).to receive(:info).with(/lettings log \d+'s sex3 value has been set to R/) + expect(logger).to receive(:info).with(/lettings log \d+'s relat3 value has been set to R/) + expect(logger).to receive(:info).with(/lettings log \d+'s relat4 value has been set to R/) + expect(logger).to receive(:info).with(/lettings log \d+'s sex5 value has been set to R/) + expect(logger).to receive(:info).with(/lettings log \d+ has value 'no' for details_known_6, skipping update/) + import_service.send(:update_sex_and_relat, lettings_log_xml) + + lettings_log.reload + expect(lettings_log.sex2).to eq("F") + expect(lettings_log.relat2).to eq("X") + expect(lettings_log.sex3).to eq("R") + expect(lettings_log.relat3).to eq("R") + expect(lettings_log.sex4).to eq("F") + expect(lettings_log.relat4).to eq("R") + expect(lettings_log.sex5).to eq("R") + expect(lettings_log.relat5).to eq("X") + expect(lettings_log.sex6).to eq(nil) + expect(lettings_log.relat6).to eq(nil) + expect(lettings_log.sex7).to eq(nil) + expect(lettings_log.relat7).to eq(nil) + expect(lettings_log.sex8).to eq(nil) + expect(lettings_log.relat8).to eq(nil) + expect(lettings_log.values_updated_at).not_to be_nil + end + + it "correctly skips the entire update if none of the fields need to be updated" do + lettings_log.update!(sex2: "F", relat2: "X", details_known_2: 0, + sex3: nil, relat3: nil, details_known_3: 1, + sex4: "F", relat4: "X", details_known_4: 0, + sex5: nil, relat5: nil, details_known_5: 1, + hhmemb: 5, values_updated_at: nil) + + expect(logger).to receive(:info).with(/lettings log \d+ has values for sex2 and relat2, skipping update/) + expect(logger).to receive(:info).with(/lettings log \d+ has value 'no' for details_known_3, skipping update/) + expect(logger).to receive(:info).with(/lettings log \d+ has values for sex4 and relat4, skipping update/) + expect(logger).to receive(:info).with(/lettings log \d+ has value 'no' for details_known_5, skipping update/) + + import_service.send(:update_sex_and_relat, lettings_log_xml) + expect(lettings_log.values_updated_at).to be_nil + end + end + end end