diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 95af610b6..76dfbc538 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -97,7 +97,7 @@ module Imports attributes["pregla"] = 1 if string_or_nil(xml_doc, "PREGLA") == "Yes" attributes["pregghb"] = 1 if string_or_nil(xml_doc, "PREGHBA") == "Yes" attributes["pregother"] = 1 if string_or_nil(xml_doc, "PREGOTHER") == "Yes" - attributes["ppostcode_full"] = compose_postcode(xml_doc, "PPOSTC1", "PPOSTC2") + attributes["ppostcode_full"] = parse_postcode(string_or_nil(xml_doc, "Q7Postcode")) attributes["prevloc"] = string_or_nil(xml_doc, "Q7ONSLACode") attributes["ppcodenk"] = previous_postcode_known(xml_doc, attributes["ppostcode_full"], attributes["prevloc"]) # Q7UNKNOWNPOSTCODE check mapping attributes["ppostc1"] = string_or_nil(xml_doc, "PPOSTC1") @@ -124,7 +124,7 @@ module Imports attributes["mortgagelenderother"] = mortgage_lender_other(xml_doc, attributes) attributes["pcode1"] = string_or_nil(xml_doc, "PCODE1") attributes["pcode2"] = string_or_nil(xml_doc, "PCODE2") - attributes["postcode_full"] = compose_postcode(xml_doc, "PCODE1", "PCODE2") + attributes["postcode_full"] = parse_postcode(string_or_nil(xml_doc, "Q14Postcode")) attributes["pcodenk"] = 0 if attributes["postcode_full"].present? # known if given attributes["soctenant"] = soctenant(attributes) attributes["ethnic_group2"] = nil # 23/24 variable @@ -134,7 +134,10 @@ module Imports # Required for our form invalidated questions (not present in import) attributes["previous_la_known"] = 1 if attributes["prevloc"].present? && attributes["ppostcode_full"].blank? - attributes["la_known"] = 1 if attributes["la"].present? && attributes["postcode_full"].blank? + if attributes["la"].present? && attributes["postcode_full"].blank? + attributes["la_known"] = 1 + attributes["is_la_inferred"] = false + end # Sets the log creator owner_id = meta_field_value(xml_doc, "owner-user-id").strip @@ -439,6 +442,12 @@ module Imports end end + def parse_postcode(postcode) + return if postcode.blank? + + UKPostcode.parse(postcode).to_s + end + def set_default_values(attributes) attributes["armedforcesspouse"] ||= 7 attributes["hhregres"] ||= 8 @@ -452,6 +461,7 @@ module Imports if [attributes["pregyrha"], attributes["pregla"], attributes["pregghb"], attributes["pregother"]].all?(&:blank?) attributes["pregblank"] = 1 end + attributes["pcodenk"] ||= 1 # buyer 1 characteristics attributes["age1_known"] ||= 1 diff --git a/spec/fixtures/imports/sales_logs/discounted_ownership_sales_log.xml b/spec/fixtures/imports/sales_logs/discounted_ownership_sales_log.xml index b697c4960..6be983074 100644 --- a/spec/fixtures/imports/sales_logs/discounted_ownership_sales_log.xml +++ b/spec/fixtures/imports/sales_logs/discounted_ownership_sales_log.xml @@ -33,7 +33,7 @@ 3 3 House 1 Purpose built - SW1A 1AA + GL519EX @@ -208,7 +208,7 @@ 3 Private tenant - GL51 9EX + GL519EX Cheltenham E07000078 diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index 7fd5de7de..d86193839 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -754,6 +754,52 @@ RSpec.describe Imports::SalesLogsImportService do expect(sales_log&.buy2livein).to eq(nil) end end + + context "when setting location fields" do + let(:sales_log_id) { "outright_sale_sales_log" } + + before do + allow(logger).to receive(:warn).and_return(nil) + end + + it "correctly sets LA if postcode is not given" do + sales_log_xml.at_xpath("//xmlns:Q14ONSLACode").content = "E07000142" + sales_log_xml.at_xpath("//xmlns:Q14Postcode").content = "" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.pcodenk).to eq(1) # postcode not known + expect(sales_log&.is_la_inferred).to eq(false) + expect(sales_log&.la_known).to eq(1) # la known + expect(sales_log&.la).to eq("E07000142") + expect(sales_log&.status).to eq("completed") + end + + it "correctly sets previous LA if postcode is not given" do + sales_log_xml.at_xpath("//xmlns:Q7ONSLACode").content = "E07000142" + sales_log_xml.at_xpath("//xmlns:Q7Postcode").content = "" + sales_log_xml.at_xpath("//xmlns:Q7UnknownPostcode").content = "" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.ppcodenk).to eq(1) # previous postcode not known + expect(sales_log&.is_previous_la_inferred).to eq(false) + expect(sales_log&.previous_la_known).to eq(1) # la known + expect(sales_log&.prevloc).to eq("E07000142") + expect(sales_log&.status).to eq("completed") + end + + it "correctly sets posctode if given" do + sales_log_xml.at_xpath("//xmlns:Q7Postcode").content = "GL519EX" + sales_log_xml.at_xpath("//xmlns:Q7UnknownPostcode").content = "" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.ppcodenk).to eq(0) + expect(sales_log&.ppostcode_full).to eq("GL51 9EX") + expect(sales_log&.status).to eq("completed") + end + end end end end