Browse Source

Reimport postcodes for 2022 (#1956)

pull/1961/head v0.3.64
kosiakkatrina 1 year ago committed by GitHub
parent
commit
e7b5d701cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      app/services/imports/lettings_logs_field_import_service.rb
  2. 4
      app/services/imports/logs_import_service.rb
  3. 2
      lib/tasks/data_import_field.rake
  4. 12
      spec/lib/tasks/data_import_field_spec.rb
  5. 120
      spec/services/imports/lettings_logs_field_import_service_spec.rb

25
app/services/imports/lettings_logs_field_import_service.rb

@ -30,6 +30,8 @@ module Imports
import_from(folder, :update_childrens_care_referral)
when "old_form_id"
import_from(folder, :update_old_form_id)
when "postcode_full"
import_from(folder, :update_postcode_full)
else
raise "Updating #{field} is not supported by the field import service"
end
@ -453,5 +455,28 @@ module Imports
@logger.warn("lettings log with old id #{old_id} not found")
end
end
def update_postcode_full(xml_doc)
return if meta_field_value(xml_doc, "form-name").include?("Sales")
old_id = meta_field_value(xml_doc, "document-id")
record = LettingsLog.find_by(old_id:)
return @logger.info("lettings log #{record.id} is not from 2022/23 collection period, skipping update") if record.collection_start_year != 2022
return @logger.warn("Could not find record matching legacy ID #{old_id}") if record.blank?
return @logger.info("lettings log #{record.id} has a value for postcode_full, skipping update") if record.postcode_full.present?
record.postcode_full = compose_postcode(xml_doc, "POSTCODE", "POSTCOD2")
if record.postcode_full.present?
record.postcode_known = postcode_known(record)
record.values_updated_at = Time.zone.now
if record.save
@logger.info("lettings log #{record.id} postcode_full value has been set to #{record.postcode_full}")
else
@logger.error("lettings log #{record.id} postcode_full value has not been set. Errors: #{record.errors.full_messages.join(', ')}")
end
else
@logger.info("lettings log #{record.id} is missing postcode_full, skipping update")
end
end
end
end

4
app/services/imports/logs_import_service.rb

@ -77,8 +77,8 @@ module Imports
end
def compose_postcode(xml_doc, outcode, incode)
outcode_value = string_or_nil(xml_doc, outcode)
incode_value = string_or_nil(xml_doc, incode)
outcode_value = string_or_nil(xml_doc, outcode)&.strip
incode_value = string_or_nil(xml_doc, incode)&.strip
if outcode_value.nil? || incode_value.nil? || !"#{outcode_value} #{incode_value}".match(POSTCODE_REGEXP)
nil
else

2
lib/tasks/data_import_field.rake

@ -7,7 +7,7 @@ namespace :core do
# We only allow a reduced list of known fields to be updatable
case field
when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless", "created_by", "sex_and_relat", "general_needs_referral", "person_details", "childrens_care_referral", "old_form_id"
when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason", "homeless", "created_by", "sex_and_relat", "general_needs_referral", "person_details", "childrens_care_referral", "old_form_id", "postcode_full"
s3_service = Storage::S3Service.new(PlatformHelper.is_paas? ? Configuration::PaasConfigurationService.new : Configuration::EnvConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"])
archive_io = s3_service.get_file_io(path)
archive_service = Storage::ArchiveService.new(archive_io)

12
spec/lib/tasks/data_import_field_spec.rb

@ -200,6 +200,18 @@ describe "data_import_field imports" do
end
end
context "and we update the postcode_full field" do
let(:field) { "postcode_full" }
it "updates the 2023 logs from the given XML file" do
expect(Storage::S3Service).to receive(:new).with(paas_config_service, instance_name)
expect(storage_service).to receive(:get_file_io).with("spec/fixtures/imports/logs")
expect(Imports::LettingsLogsFieldImportService).to receive(:new).with(archive_service)
expect(import_service).to receive(:update_field).with(field, "logs")
task.invoke(field, fixture_path)
end
end
it "raises an exception if no parameters are provided" do
expect { task.invoke }.to raise_error(/Usage/)
end

120
spec/services/imports/lettings_logs_field_import_service_spec.rb

@ -1450,4 +1450,124 @@ RSpec.describe Imports::LettingsLogsFieldImportService do
end
end
end
context "when updating postcode for 2022" do
let(:field) { "postcode_full" }
let(:lettings_log_id) { "00d2343e-d5fa-4c89-8400-ec3854b0f2b4" }
let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) }
before do
Timecop.freeze(2023, 5, 5)
Singleton.__init__(FormHandler)
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log_xml.at_xpath("//xmlns:POSTCODE").content = "A1"
lettings_log_xml.at_xpath("//xmlns:POSTCOD2").content = "1AA"
lettings_log_file.rewind
end
after do
Timecop.unfreeze
Singleton.__init__(FormHandler)
end
context "when the lettings log has no postcode_full and postcode known is nil" do
before do
lettings_log.update!(startdate: Time.zone.local(2022, 5, 5), postcode_known: nil, postcode_full: nil)
end
it "updates postcode known to yes and updates postcode_full" do
expect(logger).to receive(:info).with("lettings log #{lettings_log.id} postcode_full value has been set to A1 1AA")
import_service.send(:update_postcode_full, lettings_log_xml)
lettings_log.reload
expect(lettings_log.postcode_known).to eq(1)
expect(lettings_log.postcode_full).to eq("A1 1AA")
end
end
context "when the lettings log has no postcode_full and postcode known is no" do
before do
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(startdate: Time.zone.local(2022, 5, 5), postcode_known: 0, postcode_full: nil)
end
it "updates postcode known to yes and updates postcode_full if postcode is given" do
expect(logger).to receive(:info).with("lettings log #{lettings_log.id} postcode_full value has been set to A1 1AA")
import_service.send(:update_postcode_full, lettings_log_xml)
lettings_log.reload
expect(lettings_log.postcode_known).to eq(1)
expect(lettings_log.postcode_full).to eq("A1 1AA")
end
it "updates postcode known to yes and updates postcode_full if postcode is given and has trailing spaces" do
lettings_log_xml.at_xpath("//xmlns:POSTCODE").content = "A1"
lettings_log_xml.at_xpath("//xmlns:POSTCOD2").content = "1AA "
lettings_log_file.rewind
expect(logger).to receive(:info).with("lettings log #{lettings_log.id} postcode_full value has been set to A1 1AA")
import_service.send(:update_postcode_full, lettings_log_xml)
lettings_log.reload
expect(lettings_log.postcode_known).to eq(1)
expect(lettings_log.postcode_full).to eq("A1 1AA")
end
it "does not update postcode_full if postcode is not given" do
lettings_log_xml.at_xpath("//xmlns:POSTCODE").content = ""
lettings_log_xml.at_xpath("//xmlns:POSTCOD2").content = ""
lettings_log_file.rewind
expect(logger).to receive(:info).with("lettings log #{lettings_log.id} is missing postcode_full, skipping update")
import_service.send(:update_postcode_full, lettings_log_xml)
lettings_log.reload
expect(lettings_log.postcode_known).to eq(0)
expect(lettings_log.postcode_full).to be_nil
end
end
context "when the lettings log has a different postcode_full value" do
before do
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(startdate: Time.zone.local(2022, 5, 5), postcode_known: 1, postcode_full: "B1 1BB")
end
it "does not update the lettings_log postcode_full value" do
expect(logger).to receive(:info).with(/lettings log \d+ has a value for postcode_full, skipping update/)
expect { import_service.send(:update_postcode_full, lettings_log_xml) }
.not_to(change { lettings_log.reload.postcode_full })
end
end
context "when the lettings log has a validation error" do
before do
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(startdate: Time.zone.local(2022, 5, 5))
lettings_log.ppostcode_full = "123"
lettings_log.save!(validate: false)
end
it "does not update the lettings_log postcode_full value and prints the error" do
expect(logger).to receive(:error).with(/lettings log \d+ postcode_full value has not been set. Errors: Ppostcode full Enter a postcode in the correct format/)
expect { import_service.send(:update_postcode_full, lettings_log_xml) }
.not_to(change { lettings_log.reload.postcode_full })
end
end
context "when the lettings log is for 2023/24 year" do
let(:lettings_log_id) { "00d2343e-d5fa-4c89-8400-ec3854b0f2b4" }
before do
lettings_log_xml.at_xpath("//xmlns:UPRN").content = nil
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(startdate: Time.zone.local(2023, 4, 3), uprn: nil)
end
it "skips the log" do
expect(logger).to receive(:info).with(/lettings log \d+ is not from 2022\/23 collection period, skipping update/)
expect { import_service.send(:update_postcode_full, lettings_log_xml) }
.not_to(change { lettings_log.reload.postcode_full })
end
end
end
end

Loading…
Cancel
Save