From 2c2f91c524bea3062026cf084d537fd1c0a74e6f Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 18 Apr 2023 14:00:00 +0100 Subject: [PATCH] Import location fields for 2023/24 logs --- app/models/log.rb | 2 +- .../imports/lettings_logs_import_service.rb | 17 ++++ .../00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml | 5 + .../lettings_logs_import_service_spec.rb | 91 +++++++++++++++++++ 4 files changed, 114 insertions(+), 1 deletion(-) diff --git a/app/models/log.rb b/app/models/log.rb index fc125fd46..4ee748eda 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -48,7 +48,7 @@ class Log < ApplicationRecord service = UprnClient.new(uprn) service.call - return errors.add(:uprn, service.error) if service.error.present? + return errors.add(:uprn, :uprn_error, message: service.error) if service.error.present? presenter = UprnDataPresenter.new(service.result) diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index 01c71faef..702140454 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -202,6 +202,16 @@ module Imports attributes["first_time_property_let_as_social_housing"] = first_time_let(attributes["rsnvac"]) attributes["declaration"] = declaration(xml_doc) + if attributes["startdate"] >= Time.zone.local(2023, 4, 1) + 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") + end + set_partial_charges_to_zero(attributes) # Supported Housing fields @@ -333,6 +343,13 @@ module Imports return save_lettings_log(attributes, previous_status) end + if lettings_log.errors.of_kind?(:uprn, :uprn_error) + @logger.warn("Log #{lettings_log.old_id}: Setting uprn_known to no with error: #{lettings_log.errors[:uprn].join(', ')}") + @logs_overridden << lettings_log.old_id + attributes["uprn_known"] = 0 + return save_lettings_log(attributes, previous_status) + end + @logger.error("Log #{lettings_log.old_id}: Failed to import") lettings_log.errors.each do |error| @logger.error("Validation error: Field #{error.attribute}:") diff --git a/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml b/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml index 8bc6a935c..cd0e03d74 100644 --- a/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml +++ b/spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml @@ -131,6 +131,11 @@ <_2cYears/> + 12345678 + + + + SR8 3HF Durham E06000047 diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 4621afb2f..4fbd485c0 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -1151,5 +1151,96 @@ RSpec.describe Imports::LettingsLogsImportService do expect(lettings_log.hbrentshortfall).to be_nil end end + + context "when setting location fields for 23/24 logs" do + let(:lettings_log_id) { "00d2343e-d5fa-4c89-8400-ec3854b0f2b4" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + around do |example| + Timecop.freeze(Time.zone.local(2023, 4, 1)) do + Singleton.__init__(FormHandler) + example.run + end + Timecop.return + Singleton.__init__(FormHandler) + end + + before do + lettings_log_xml.at_xpath("//xmlns:DAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "4" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2023" + lettings_log_xml.at_xpath("//xmlns:UPRN").content = "123456781234" + lettings_log_xml.at_xpath("//xmlns:AddressLine1").content = "address 1" + lettings_log_xml.at_xpath("//xmlns:AddressLine2").content = "address 2" + lettings_log_xml.at_xpath("//xmlns:TownCity").content = "towncity" + lettings_log_xml.at_xpath("//xmlns:County").content = "county" + lettings_log_xml.at_xpath("//xmlns:POSTCODE").content = "A1" + lettings_log_xml.at_xpath("//xmlns:POSTCOD2").content = "1AA" + + body = { + results: [ + { + DPA: { + "POSTCODE": "LS16 6FT", + "POST_TOWN": "Westminster", + "PO_BOX_NUMBER": "321", + "DOUBLE_DEPENDENT_LOCALITY": "Double Dependent Locality", + }, + }, + ], + }.to_json + + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123456781234") + .to_return(status: 200, body:, headers: {}) + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123") + .to_return(status: 500, body: "{}", headers: {}) + + allow(logger).to receive(:warn).and_return(nil) + end + + it "correctly sets address if uprn is not given" do + lettings_log_xml.at_xpath("//xmlns:UPRN").content = "" + 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" 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(1) + expect(lettings_log&.uprn).to eq("123456781234") + expect(lettings_log&.address_line1).to eq("321") + expect(lettings_log&.address_line2).to eq("Double Dependent Locality") + expect(lettings_log&.town_or_city).to eq("Westminster") + expect(lettings_log&.postcode_full).to eq("LS16 6FT") + expect(lettings_log&.la).to eq("E08000035") + end + + it "correctly sets address and uprn if uprn is given but not recognised" do + lettings_log_xml.at_xpath("//xmlns:UPRN").content = "123" + + 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) + 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") + expect(lettings_log&.la).to eq("E06000047") + end + end end end