diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index 066c55798..c41837f5d 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -99,7 +99,7 @@ module Imports attributes["prevten"] = unsafe_string_as_integer(xml_doc, "Q11") attributes["prevloc"] = string_or_nil(xml_doc, "Q12aONS") attributes["ppostcode_full"] = compose_postcode(xml_doc, "PPOSTC1", "PPOSTC2") - attributes["previous_postcode_known"] = previous_postcode_known(xml_doc, attributes["ppostcode_full"]) + attributes["previous_postcode_known"] = previous_postcode_known(xml_doc, attributes["ppostcode_full"], attributes["prevloc"]) attributes["layear"] = unsafe_string_as_integer(xml_doc, "Q12c") attributes["waityear"] = unsafe_string_as_integer(xml_doc, "Q12d") attributes["homeless"] = unsafe_string_as_integer(xml_doc, "Q13") @@ -375,9 +375,9 @@ module Imports end end - def previous_postcode_known(xml_doc, previous_postcode) + def previous_postcode_known(xml_doc, previous_postcode, prevloc) previous_postcode_known = string_or_nil(xml_doc, "Q12bnot") - if previous_postcode_known == "Temporary_or_Unknown" + if previous_postcode_known == "Temporary_or_Unknown" || (previous_postcode.nil? && prevloc.present?) 0 elsif previous_postcode.nil? nil @@ -386,10 +386,11 @@ module Imports end end + POSTCODE_REGEXP = Validations::PropertyValidations::POSTCODE_REGEXP def compose_postcode(xml_doc, outcode, incode) outcode_value = string_or_nil(xml_doc, outcode) incode_value = string_or_nil(xml_doc, incode) - if outcode_value.nil? || incode_value.nil? + if outcode_value.nil? || incode_value.nil? || !"#{outcode_value}#{incode_value}".match(POSTCODE_REGEXP) nil else "#{outcode_value}#{incode_value}" 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 8fc8cf37d..d34952c31 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 @@ -524,9 +524,9 @@ - LS8 - 3HX - LS16 - 6FT + SR7 + 0A5 + TS27 + 5B7