diff --git a/app/services/imports/case_logs_field_import_service.rb b/app/services/imports/case_logs_field_import_service.rb index d7be932bd..0e14ebda4 100644 --- a/app/services/imports/case_logs_field_import_service.rb +++ b/app/services/imports/case_logs_field_import_service.rb @@ -4,6 +4,8 @@ module Imports case field when "tenant_code" import_from(folder, :update_tenant_code) + when "major_repairs" + import_from(folder, :update_major_repairs) else raise "Updating #{field} is not supported by the field import service" end @@ -11,9 +13,30 @@ module Imports private + def update_major_repairs(xml_doc) + record = log(xml_doc) + + if record.present? + previous_status = field_value(xml_doc, "meta", "status") + major_repairs_date = compose_date(xml_doc, "MRCDAY", "MRCMONTH", "MRCYEAR") + major_repairs = if major_repairs_date.present? && previous_status.include?("submitted") + 1 + elsif previous_status.include?("submitted") + 0 + end + if major_repairs.present? && record.majorrepairs.nil? && record.status != "completed" + record.update!(mrcdate: major_repairs_date, majorrepairs: major_repairs) + @logger.info("Case Log #{record.id}'s major repair value has been updated'") + elsif record.majorrepairs.present? + @logger.info("Case Log #{record.id} has a value for major repairs, skipping update") + end + else + @logger.warn("Could not find record matching legacy ID #{old_id}") + end + end + def update_tenant_code(xml_doc) - old_id = field_value(xml_doc, "meta", "document-id") - record = CaseLog.find_by(old_id:) + record = log(xml_doc) if record.present? tenant_code = string_or_nil(xml_doc, "_2bTenCode") @@ -27,6 +50,22 @@ module Imports end end + def log(xml_doc) + old_id = field_value(xml_doc, "meta", "document-id") + CaseLog.find_by(old_id:) + end + + def compose_date(xml_doc, day_str, month_str, year_str) + day = Integer(field_value(xml_doc, "xmlns", day_str), exception: false) + month = Integer(field_value(xml_doc, "xmlns", month_str), exception: false) + year = Integer(field_value(xml_doc, "xmlns", year_str), exception: false) + if day.nil? || month.nil? || year.nil? + nil + else + Time.zone.local(year, month, day) + end + end + def string_or_nil(xml_doc, attribute) str = field_value(xml_doc, "xmlns", attribute) str.presence diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 5cdf80c80..5041fedd1 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -918,6 +918,38 @@ } }, "depends_on": [ + { + "renewal": 0, + "rsnvac": 5 + }, + { + "renewal": 0, + "rsnvac": 6 + }, + { + "renewal": 0, + "rsnvac": 8 + }, + { + "renewal": 0, + "rsnvac": 9 + }, + { + "renewal": 0, + "rsnvac": 10 + }, + { + "renewal": 0, + "rsnvac": 11 + }, + { + "renewal": 0, + "rsnvac": 12 + }, + { + "renewal": 0, + "rsnvac": 13 + }, { "renewal": 0, "rsnvac": 16 @@ -925,6 +957,14 @@ { "renewal": 0, "rsnvac": 17 + }, + { + "renewal": 0, + "rsnvac": 18 + }, + { + "renewal": 0, + "rsnvac": 19 } ] } diff --git a/config/forms/2022_2023.json b/config/forms/2022_2023.json index bd18826c8..20ae77cc4 100644 --- a/config/forms/2022_2023.json +++ b/config/forms/2022_2023.json @@ -918,6 +918,38 @@ } }, "depends_on": [ + { + "renewal": 0, + "rsnvac": 5 + }, + { + "renewal": 0, + "rsnvac": 6 + }, + { + "renewal": 0, + "rsnvac": 8 + }, + { + "renewal": 0, + "rsnvac": 9 + }, + { + "renewal": 0, + "rsnvac": 10 + }, + { + "renewal": 0, + "rsnvac": 11 + }, + { + "renewal": 0, + "rsnvac": 12 + }, + { + "renewal": 0, + "rsnvac": 13 + }, { "renewal": 0, "rsnvac": 16 @@ -925,6 +957,14 @@ { "renewal": 0, "rsnvac": 17 + }, + { + "renewal": 0, + "rsnvac": 18 + }, + { + "renewal": 0, + "rsnvac": 19 } ] } diff --git a/config/locales/en.yml b/config/locales/en.yml index 32d82808a..80382be01 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -66,7 +66,7 @@ en: void_date: ten_years_before_tenancy_start: "The void date must be no more than 10 years before the tenancy start date" before_tenancy_start: "Void date must be before the tenancy start date" - after_mrcdate: "Void date must be after the major repairs date if provided" + after_mrcdate: "Void date must be before the major repairs date if provided" offered: relet_number: "Number of times the property has been re-let must be between 0 and 20" la: diff --git a/spec/fixtures/softwire_imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml b/spec/fixtures/softwire_imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml index 8413abb7e..c43bcf0bf 100644 --- a/spec/fixtures/softwire_imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml +++ b/spec/fixtures/softwire_imports/case_logs/0ead17cb-1668-442d-898c-0d52879ff592.xml @@ -177,8 +177,8 @@ 1 Flat / maisonette 1 Purpose built 2 No - - 15 First let of newbuild property + 4 An Intermediate Rent basis + 10 Relet - tenant evicted due to arrears Leeds @@ -516,14 +516,14 @@ 0 0 30 - 9 + 11 2021 30 9 2021 - - - + 16 + 10 + 2021 SR7 0A5 TS27 diff --git a/spec/services/imports/case_logs_field_import_service_spec.rb b/spec/services/imports/case_logs_field_import_service_spec.rb index c071b1ea7..3233d6706 100644 --- a/spec/services/imports/case_logs_field_import_service_spec.rb +++ b/spec/services/imports/case_logs_field_import_service_spec.rb @@ -9,6 +9,12 @@ RSpec.describe Imports::CaseLogsFieldImportService do let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:fixture_directory) { "spec/fixtures/softwire_imports/case_logs" } + let(:case_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } + let(:case_log_file) { open_file(fixture_directory, case_log_id) } + let(:case_log_xml) { Nokogiri::XML(case_log_file) } + let(:remote_folder) { "case_logs" } + let(:old_user_id) { "c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa" } + def open_file(directory, filename) File.open("#{directory}/#{filename}.xml") end @@ -18,31 +24,25 @@ RSpec.describe Imports::CaseLogsFieldImportService do FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") # Created by users - FactoryBot.create(:user, old_user_id: "c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa") + FactoryBot.create(:user, old_user_id:) # Stub the form handler to use the real form allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/) .to_return(status: 200, body: '{"status":200,"result":{"codes":{"admin_district":"E08000035"}}}', headers: {}) + + # Stub the S3 file listing and download + allow(storage_service).to receive(:list_files) + .and_return(["#{remote_folder}/#{case_log_id}.xml"]) + allow(storage_service).to receive(:get_file_io) + .with("#{remote_folder}/#{case_log_id}.xml") + .and_return(case_log_file) end - context "when updating a specific log value" do - let(:case_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } - let(:case_log_file) { open_file(fixture_directory, case_log_id) } - let(:case_log_xml) { Nokogiri::XML(case_log_file) } - let(:remote_folder) { "case_logs" } + context "when updating tenant code" do let(:field) { "tenant_code" } - before do - # Stub the S3 file listing and download - allow(storage_service).to receive(:list_files) - .and_return(["#{remote_folder}/#{case_log_id}.xml"]) - allow(storage_service).to receive(:get_file_io) - .with("#{remote_folder}/#{case_log_id}.xml") - .and_return(case_log_file) - end - context "and the case log was previously imported" do let(:case_log) { CaseLog.find_by(old_id: case_log_id) } @@ -73,4 +73,52 @@ RSpec.describe Imports::CaseLogsFieldImportService do end end end + + context "when updating major repairs" do + let(:field) { "major_repairs" } + + context "and the case log already has a value" do + let(:case_log) { CaseLog.find_by(old_id: case_log_id) } + + before do + Imports::CaseLogsImportService.new(storage_service, logger).create_logs(fixture_directory) + case_log_file.rewind + case_log.update!(majorrepairs: 0, mrcdate: Time.zone.local(2021, 10, 30, 10, 10, 10)) + end + + it "logs that major repairs already has a value and does not update major repairs" do + expect(logger).to receive(:info).with(/Case Log \d+ has a value for major repairs, skipping update/) + expect { import_service.send(:update_field, field, remote_folder) } + .not_to(change { case_log.reload.majorrepairs }) + end + + it "logs that major repairs already has a value and does not update the major repairs date" do + expect(logger).to receive(:info).with(/Case Log \d+ has a value for major repairs, skipping update/) + expect { import_service.send(:update_field, field, remote_folder) } + .not_to(change { case_log.reload.mrcdate }) + end + end + + context "and the case log was previously imported with empty fields" do + let(:case_log) { CaseLog.find_by(old_id: case_log_id) } + + before do + Imports::CaseLogsImportService.new(storage_service, logger).create_logs(fixture_directory) + case_log_file.rewind + case_log.update!(mrcdate: nil, majorrepairs: nil) + end + + it "updates the case_log major repairs date" do + expect(logger).to receive(:info).with(/Case Log \d+'s major repair value has been updated/) + expect { import_service.send(:update_field, field, remote_folder) } + .to(change { case_log.reload.mrcdate }) + end + + it "updates the case_log major repairs" do + expect(logger).to receive(:info).with(/Case Log \d+'s major repair value has been updated/) + expect { import_service.send(:update_field, field, remote_folder) } + .to(change { case_log.reload.majorrepairs }) + end + end + end end