diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index ca3cde434..d40df0fc1 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -76,14 +76,19 @@ module Imports attributes["irproduct_other"] = string_or_nil(xml_doc, "IRProductOther") attributes["rent_type"] = rent_type(xml_doc, attributes["lar"], attributes["irproduct"]) attributes["hhmemb"] = household_members(xml_doc, previous_status) + + people_indexes = people_with_details_ids(xml_doc) + available_people_indexes = people_indexes + (people_indexes.max + 1..8).to_a (1..8).each do |index| - attributes["age#{index}"] = safe_string_as_integer(xml_doc, "P#{index}Age") - attributes["age#{index}_known"] = age_known(xml_doc, index, attributes["hhmemb"]) - attributes["sex#{index}"] = sex(xml_doc, index) - attributes["ecstat#{index}"] = unsafe_string_as_integer(xml_doc, "P#{index}Eco") + person_index = available_people_indexes[index - 1] + attributes["age#{index}"] = safe_string_as_integer(xml_doc, "P#{person_index}Age") + attributes["age#{index}_known"] = age_known(xml_doc, index, person_index, attributes["hhmemb"]) + attributes["sex#{index}"] = sex(xml_doc, person_index) + attributes["ecstat#{index}"] = unsafe_string_as_integer(xml_doc, "P#{person_index}Eco") end (2..8).each do |index| - attributes["relat#{index}"] = relat(xml_doc, index) + person_index = available_people_indexes[index - 1] + attributes["relat#{index}"] = relat(xml_doc, person_index) attributes["details_known_#{index}"] = details_known(index, attributes) # Trips validation @@ -449,10 +454,10 @@ module Imports end end - def age_known(xml_doc, index, hhmemb) + def age_known(xml_doc, index, person_index, hhmemb) return nil if hhmemb.present? && index > hhmemb - age_refused = string_or_nil(xml_doc, "P#{index}AR") + age_refused = string_or_nil(xml_doc, "P#{person_index}AR") if age_refused.present? if age_refused.casecmp?("AGE_REFUSED") || age_refused.casecmp?("No") return 1 # No @@ -577,6 +582,15 @@ module Imports ((2..8).map { |x| string_or_nil(xml_doc, "P#{x}Rel") } + [string_or_nil(xml_doc, "P1Sex")]).compact end + def people_with_details_ids(xml_doc) + [1] + (2..8).select do |x| + string_or_nil(xml_doc, "P#{x}Rel").present? || + string_or_nil(xml_doc, "P#{x}Sex").present? || + string_or_nil(xml_doc, "P#{x}Age").present? || + string_or_nil(xml_doc, "P#{x}Eco").present? + end + end + def tshortfall_known?(xml_doc, attributes) if attributes["tshortfall"].blank? && attributes["hbrentshortfall"] == 1 && overridden?(xml_doc, "xmlns", "Q18dyes") 1 diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index dad2e68ed..efef0997a 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -1303,6 +1303,226 @@ RSpec.describe Imports::LettingsLogsImportService do expect(lettings_log.status).to eq("completed") end end + + context "and there are several household members" do + context "and one person details are skipped" do + before do + lettings_log_xml.at_xpath("//xmlns:HHMEMB").content = 3 + lettings_log_xml.at_xpath("//xmlns:P2AR").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Eco").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Age").content = 7 + lettings_log_xml.at_xpath("//xmlns:P3Sex").content = "Male" + lettings_log_xml.at_xpath("//xmlns:P3Rel").content = "Child" + lettings_log_xml.at_xpath("//xmlns:P3Eco").content = "9) Child under 16" + end + + it "correctly moves person details" do + lettings_log_service.send(:create_log, lettings_log_xml) + + lettings_log = LettingsLog.where(old_id: lettings_log_id).first + expect(lettings_log&.hhmemb).to eq(3) + expect(lettings_log&.details_known_2).to eq(0) + expect(lettings_log&.age2_known).to eq(0) + expect(lettings_log&.age2).to eq(7) + expect(lettings_log&.sex2).to eq("M") + expect(lettings_log&.relat2).to eq("C") + + expect(lettings_log&.details_known_3).to eq(0) + expect(lettings_log&.age3_known).to eq(0) + expect(lettings_log&.age3).to eq(nil) + expect(lettings_log&.sex3).to eq(nil) + expect(lettings_log&.relat3).to eq(nil) + + [4, 5].each do |i| + expect(lettings_log&.send("details_known_#{i}")).to eq(nil) + expect(lettings_log&.send("age#{i}_known")).to eq(nil) + expect(lettings_log&.send("age#{i}")).to eq(nil) + expect(lettings_log&.send("sex#{i}")).to eq(nil) + expect(lettings_log&.send("relat#{i}")).to eq(nil) + expect(lettings_log&.send("ecstat#{i}")).to eq(nil) + end + end + end + + context "and several consecutive person details are skipped" do + before do + lettings_log_xml.at_xpath("//xmlns:HHMEMB").content = 4 + + lettings_log_xml.at_xpath("//xmlns:P2AR").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P3AR").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P4Age").content = 7 + lettings_log_xml.at_xpath("//xmlns:P4Sex").content = "Male" + lettings_log_xml.at_xpath("//xmlns:P4Rel").content = "Child" + lettings_log_xml.at_xpath("//xmlns:P4Eco").content = "9) Child under 16" + + lettings_log_xml.at_xpath("//xmlns:P5Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P5Sex").content = "Male" + lettings_log_xml.at_xpath("//xmlns:P5Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P5Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P6Age").content = 12 + lettings_log_xml.at_xpath("//xmlns:P6Sex").content = "Female" + lettings_log_xml.at_xpath("//xmlns:P6Rel").content = "Child" + lettings_log_xml.at_xpath("//xmlns:P6Eco").content = "9) Child under 16" + end + + it "correctly moves person details" do + lettings_log_service.send(:create_log, lettings_log_xml) + + lettings_log = LettingsLog.where(old_id: lettings_log_id).first + expect(lettings_log&.hhmemb).to eq(4) + expect(lettings_log&.details_known_2).to eq(0) + expect(lettings_log&.age2_known).to eq(0) + expect(lettings_log&.age2).to eq(7) + expect(lettings_log&.sex2).to eq("M") + expect(lettings_log&.relat2).to eq("C") + + expect(lettings_log&.details_known_3).to eq(0) + expect(lettings_log&.age3_known).to eq(0) + expect(lettings_log&.age3).to eq(nil) + expect(lettings_log&.sex3).to eq("M") + expect(lettings_log&.relat3).to eq(nil) + + expect(lettings_log&.details_known_4).to eq(0) + expect(lettings_log&.age4_known).to eq(0) + expect(lettings_log&.age4).to eq(12) + expect(lettings_log&.sex4).to eq("F") + expect(lettings_log&.relat4).to eq("C") + + [5, 6, 7, 8].each do |i| + expect(lettings_log&.send("details_known_#{i}")).to eq(nil) + expect(lettings_log&.send("age#{i}_known")).to eq(nil) + expect(lettings_log&.send("age#{i}")).to eq(nil) + expect(lettings_log&.send("sex#{i}")).to eq(nil) + expect(lettings_log&.send("relat#{i}")).to eq(nil) + expect(lettings_log&.send("ecstat#{i}")).to eq(nil) + end + end + end + + context "and several non consecutive person details are skipped" do + before do + lettings_log_xml.at_xpath("//xmlns:HHMEMB").content = 4 + lettings_log_xml.at_xpath("//xmlns:P2AR").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P3Age").content = 7 + lettings_log_xml.at_xpath("//xmlns:P3Sex").content = "Male" + lettings_log_xml.at_xpath("//xmlns:P3Rel").content = "Child" + lettings_log_xml.at_xpath("//xmlns:P3Eco").content = "9) Child under 16" + + lettings_log_xml.at_xpath("//xmlns:P4AR").content = nil + lettings_log_xml.at_xpath("//xmlns:P4Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P4Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P4Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P4Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P5Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P5Sex").content = "Male" + lettings_log_xml.at_xpath("//xmlns:P5Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P5Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P6Age").content = 12 + lettings_log_xml.at_xpath("//xmlns:P6Sex").content = "Female" + lettings_log_xml.at_xpath("//xmlns:P6Rel").content = "Child" + lettings_log_xml.at_xpath("//xmlns:P6Eco").content = "9) Child under 16" + end + + it "correctly moves person details" do + lettings_log_service.send(:create_log, lettings_log_xml) + + lettings_log = LettingsLog.where(old_id: lettings_log_id).first + expect(lettings_log&.hhmemb).to eq(4) + expect(lettings_log&.details_known_2).to eq(0) + expect(lettings_log&.age2_known).to eq(0) + expect(lettings_log&.age2).to eq(7) + expect(lettings_log&.sex2).to eq("M") + expect(lettings_log&.relat2).to eq("C") + + expect(lettings_log&.details_known_3).to eq(0) + expect(lettings_log&.age3_known).to eq(0) + expect(lettings_log&.age3).to eq(nil) + expect(lettings_log&.sex3).to eq("M") + expect(lettings_log&.relat3).to eq(nil) + + expect(lettings_log&.details_known_4).to eq(0) + expect(lettings_log&.age4_known).to eq(0) + expect(lettings_log&.age4).to eq(12) + expect(lettings_log&.sex4).to eq("F") + expect(lettings_log&.relat4).to eq("C") + + [5, 6, 7, 8].each do |i| + expect(lettings_log&.send("details_known_#{i}")).to eq(nil) + expect(lettings_log&.send("age#{i}_known")).to eq(nil) + expect(lettings_log&.send("age#{i}")).to eq(nil) + expect(lettings_log&.send("sex#{i}")).to eq(nil) + expect(lettings_log&.send("relat#{i}")).to eq(nil) + expect(lettings_log&.send("ecstat#{i}")).to eq(nil) + end + end + end + + context "with 3 houusehold members without any person data" do + before do + lettings_log_xml.at_xpath("//xmlns:HHMEMB").content = 3 + lettings_log_xml.at_xpath("//xmlns:P2AR").content = "No" + lettings_log_xml.at_xpath("//xmlns:P2Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P2Eco").content = nil + + lettings_log_xml.at_xpath("//xmlns:P3AR").content = "No" + lettings_log_xml.at_xpath("//xmlns:P3Age").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Sex").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Rel").content = nil + lettings_log_xml.at_xpath("//xmlns:P3Eco").content = nil + end + + it "correctly sets person details" do + lettings_log_service.send(:create_log, lettings_log_xml) + + lettings_log = LettingsLog.where(old_id: lettings_log_id).first + expect(lettings_log&.hhmemb).to eq(3) + + expect(lettings_log&.details_known_2).to eq(0) + expect(lettings_log&.age2_known).to eq(1) + expect(lettings_log&.age2).to eq(nil) + expect(lettings_log&.sex2).to eq(nil) + expect(lettings_log&.relat2).to eq(nil) + + expect(lettings_log&.details_known_3).to eq(0) + expect(lettings_log&.age3_known).to eq(1) + expect(lettings_log&.age3).to eq(nil) + expect(lettings_log&.sex3).to eq(nil) + expect(lettings_log&.relat3).to eq(nil) + + [4, 5, 6, 7, 8].each do |i| + expect(lettings_log&.send("details_known_#{i}")).to eq(nil) + expect(lettings_log&.send("age#{i}_known")).to eq(nil) + expect(lettings_log&.send("age#{i}")).to eq(nil) + expect(lettings_log&.send("sex#{i}")).to eq(nil) + expect(lettings_log&.send("relat#{i}")).to eq(nil) + expect(lettings_log&.send("ecstat#{i}")).to eq(nil) + end + end + end + end end end