From b756900cf6637e8f9cd79a728422bcc6696f593f Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 24 Aug 2023 11:06:08 +0100 Subject: [PATCH] Prioritise addresses over UPRN --- .../imports/lettings_logs_import_service.rb | 10 +++++++--- .../lettings_logs_import_service_spec.rb | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index f1a86c023..ecbcaf9d9 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -200,13 +200,13 @@ module Imports attributes["first_time_property_let_as_social_housing"] = first_time_let(attributes["rsnvac"]) attributes["declaration"] = declaration(xml_doc) - attributes["uprn"] = string_or_nil(xml_doc, "UPRN") - attributes["uprn_known"] = attributes["uprn"].present? ? 1 : 0 - attributes["uprn_confirmed"] = attributes["uprn"].present? ? 1 : 0 attributes["address_line1"] = string_or_nil(xml_doc, "AddressLine1") attributes["address_line2"] = string_or_nil(xml_doc, "AddressLine2") attributes["town_or_city"] = string_or_nil(xml_doc, "TownCity") attributes["county"] = string_or_nil(xml_doc, "County") + attributes["uprn"] = address_given?(attributes) ? nil : string_or_nil(xml_doc, "UPRN") + attributes["uprn_known"] = attributes["uprn"].present? ? 1 : 0 + attributes["uprn_confirmed"] = attributes["uprn"].present? ? 1 : 0 set_partial_charges_to_zero(attributes) @@ -650,5 +650,9 @@ module Imports OrganisationRelationship.find_or_create_by!(parent_organisation_id:, child_organisation_id:) end + + def address_given?(attributes) + attributes["address_line1"].present? && attributes["town_or_city"].present? + end end end diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 8986710d9..5512bc49e 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -1531,7 +1531,24 @@ RSpec.describe Imports::LettingsLogsImportService do expect(lettings_log&.postcode_full).to eq("A1 1AA") end - it "correctly sets address and uprn if uprn is given" do + it "prioritises address and doesn't set UPRN if both address and UPRN is given" do + lettings_log_service.send(:create_log, lettings_log_xml) + + lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + expect(lettings_log&.uprn_known).to eq(0) # no + expect(lettings_log&.uprn).to be_nil + expect(lettings_log&.address_line1).to eq("address 1") + expect(lettings_log&.address_line2).to eq("address 2") + expect(lettings_log&.town_or_city).to eq("towncity") + expect(lettings_log&.county).to eq("county") + expect(lettings_log&.postcode_full).to eq("A1 1AA") + end + + it "correctly sets address and uprn if uprn is given and address isn't given" do + lettings_log_xml.at_xpath("//xmlns:AddressLine1").content = "" + lettings_log_xml.at_xpath("//xmlns:AddressLine2").content = "" + lettings_log_xml.at_xpath("//xmlns:TownCity").content = "" + lettings_log_xml.at_xpath("//xmlns:County").content = "" lettings_log_service.send(:create_log, lettings_log_xml) lettings_log = LettingsLog.find_by(old_id: lettings_log_id)