From 46a2783a4aeca3edec53fadd0aa087b528fb882a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Wed, 25 May 2022 09:47:05 +0100 Subject: [PATCH] CLDC-1225: At import updates relationship to child when a person is under 16 (#609) --- .../imports/case_logs_import_service.rb | 15 ++++++- .../imports/case_logs_import_service_spec.rb | 44 ++++++++++++++----- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index e2297e760..02cb2ca35 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -138,8 +138,6 @@ module Imports 0 end - apply_date_consistency!(attributes) - attributes["offered"] = safe_string_as_integer(xml_doc, "Q20") attributes["propcode"] = string_or_nil(xml_doc, "Q21a") attributes["beds"] = safe_string_as_integer(xml_doc, "Q22") @@ -188,6 +186,9 @@ module Imports attributes["created_by"] = User.find_by(old_user_id: owner_id) end + apply_date_consistency!(attributes) + apply_household_consistency!(attributes) + case_log = CaseLog.new(attributes) save_case_log(case_log, attributes) compute_differences(case_log, attributes) @@ -524,5 +525,15 @@ module Imports attributes.delete("voiddate") end end + + def apply_household_consistency!(attributes) + (2..8).each do |index| + next if attributes["age#{index}"].nil? + + if attributes["age#{index}"] < 16 && attributes["relat#{index}"] == "R" + attributes["relat#{index}"] = "C" + end + end + end end end diff --git a/spec/services/imports/case_logs_import_service_spec.rb b/spec/services/imports/case_logs_import_service_spec.rb index 92da82907..0f1ee40b6 100644 --- a/spec/services/imports/case_logs_import_service_spec.rb +++ b/spec/services/imports/case_logs_import_service_spec.rb @@ -71,35 +71,55 @@ RSpec.describe Imports::CaseLogsImportService do context "when importing a specific log" 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) } context "and the void date is after the start date" do - let(:case_log_xml) do - xml_doc = Nokogiri::XML(case_log_file) - xml_doc.at_xpath("//xmlns:VYEAR").content = 2023 - xml_doc - end + before { case_log_xml.at_xpath("//xmlns:VYEAR").content = 2023 } it "does not import the voiddate" do allow(logger).to receive(:warn).with(/is not completed/) case_log_service.send(:create_log, case_log_xml) case_log = CaseLog.where(old_id: case_log_id).first - expect(case_log).not_to be_nil - expect(case_log.voiddate).to be_nil + expect(case_log&.voiddate).to be_nil end end context "and the organisation legacy ID does not exist" do - let(:case_log_xml) do - xml_doc = Nokogiri::XML(case_log_file) - xml_doc.at_xpath("//xmlns:OWNINGORGID").content = 99_999 - xml_doc - end + before { case_log_xml.at_xpath("//xmlns:OWNINGORGID").content = 99_999 } it "raises an exception" do expect { case_log_service.send(:create_log, case_log_xml) } .to raise_error(RuntimeError, "Organisation not found with legacy ID 99999") end end + + context "and a person is under 16" do + before { case_log_xml.at_xpath("//xmlns:P2Age").content = 14 } + + context "when the economic status is set to refuse" do + before { case_log_xml.at_xpath("//xmlns:P2Eco").content = "10) Refused" } + + it "sets the economic status to child under 16" do + # The update is done when calculating derived variables + allow(logger).to receive(:warn).with(/Differences found when saving log/) + case_log_service.send(:create_log, case_log_xml) + + case_log = CaseLog.where(old_id: case_log_id).first + expect(case_log&.ecstat2).to be(9) + end + end + + context "when the relationship to lead tenant is set to refuse" do + before { case_log_xml.at_xpath("//xmlns:P2Rel").content = "Refused" } + + it "sets the relationship to lead tenant to child" do + case_log_service.send(:create_log, case_log_xml) + + case_log = CaseLog.where(old_id: case_log_id).first + expect(case_log&.relat2).to eq("C") + end + end + end end end