Browse Source

Import location fields for 2023/24 logs

pull/1560/head
Kat 3 years ago
parent
commit
2c2f91c524
  1. 2
      app/models/log.rb
  2. 17
      app/services/imports/lettings_logs_import_service.rb
  3. 5
      spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml
  4. 91
      spec/services/imports/lettings_logs_import_service_spec.rb

2
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)

17
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}:")

5
spec/fixtures/imports/logs/00d2343e-d5fa-4c89-8400-ec3854b0f2b4.xml vendored

@ -131,6 +131,11 @@
<_2cYears/>
</Group>
<Group>
<UPRN>12345678</UPRN>
<AddressLine1/>
<AddressLine2/>
<TownCity/>
<County/>
<Q28pc override-field="">SR8 3HF</Q28pc>
<Q28Auth>Durham</Q28Auth>
<Q28ONS>E06000047</Q28ONS>

91
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

Loading…
Cancel
Save