Browse Source

Add reimport reason update_field case (#1896)

pull/1908/head
kosiakkatrina 1 year ago committed by GitHub
parent
commit
f59c67b651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      app/services/imports/lettings_logs_field_import_service.rb
  2. 2
      lib/tasks/data_import_field.rake
  3. 12
      spec/lib/tasks/data_import_field_spec.rb
  4. 70
      spec/services/imports/lettings_logs_field_import_service_spec.rb

27
app/services/imports/lettings_logs_field_import_service.rb

@ -14,6 +14,8 @@ module Imports
import_from(folder, :update_creation_method)
when "address"
import_from(folder, :update_address)
when "reason"
import_from(folder, :update_reason)
else
raise "Updating #{field} is not supported by the field import service"
end
@ -176,5 +178,30 @@ module Imports
1
end
end
def update_reason(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:)
if record.present?
if record.reason.present?
@logger.info("lettings log #{record.id} has a value for reason, skipping update")
else
reason = unsafe_string_as_integer(xml_doc, "Q9a")
reasonother = string_or_nil(xml_doc, "Q9aa")
if reason == 20 && reasonother.blank?
@logger.info("lettings log #{record.id}'s reason is other but other reason is not provided, skipping update")
else
record.update!(reason:, reasonother:)
@logger.info("lettings log #{record.id}'s reason value has been set to #{reason}")
@logger.info("lettings log #{record.id}'s reasonother value has been set to #{reasonother}") if record.reasonother.present?
end
end
else
@logger.warn("lettings log with old id #{old_id} not found")
end
end
end
end

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"
when "tenancycode", "major_repairs", "lettings_allocation", "offered", "address", "reason"
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

@ -104,6 +104,18 @@ describe "data_import_field imports" do
end
end
context "and we update the reason field" do
let(:field) { "reason" }
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

70
spec/services/imports/lettings_logs_field_import_service_spec.rb

@ -471,4 +471,74 @@ RSpec.describe Imports::LettingsLogsFieldImportService do
end
end
end
context "when updating reason" do
let(:field) { "reason" }
context "when the lettings log has no reason value" do
let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) }
before do
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(reason: nil)
lettings_log_xml.at_xpath("//xmlns:Q9a").content = "47"
end
it "updates the lettings_log reason value" do
expect(logger).to receive(:info).with(/lettings log \d+'s reason value has been set to 47/)
expect { import_service.send(:update_reason, lettings_log_xml) }
.to(change { lettings_log.reload.reason }.from(nil).to(47))
end
end
context "when the lettings log has a different reason value" do
let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) }
before do
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(reason: 18)
lettings_log_xml.at_xpath("//xmlns:Q9a").content = "47"
end
it "does not update the lettings_log reason value" do
expect(logger).to receive(:info).with(/lettings log \d+ has a value for reason, skipping update/)
expect { import_service.send(:update_reason, lettings_log_xml) }
.not_to(change { lettings_log.reload.reason })
end
end
context "when the new value is 'other'" do
let(:lettings_log) { LettingsLog.find_by(old_id: lettings_log_id) }
before do
Imports::LettingsLogsImportService.new(storage_service, logger).create_logs(fixture_directory)
lettings_log_file.rewind
lettings_log.update!(reason: nil)
lettings_log_xml.at_xpath("//xmlns:Q9a").content = "20"
end
context "and other value is given" do
before do
lettings_log_xml.at_xpath("//xmlns:Q9aa").content = "other"
end
it "updates the lettings_log reason value" do
expect(logger).to receive(:info).with(/lettings log \d+'s reason value has been set to 20/)
expect(logger).to receive(:info).with(/lettings log \d+'s reasonother value has been set to other/)
expect { import_service.send(:update_reason, lettings_log_xml) }
.to(change { lettings_log.reload.reason }.from(nil).to(20))
end
end
context "and other value is not given" do
it "does not update the lettings_log reason value" do
expect(logger).to receive(:info).with(/lettings log \d+'s reason is other but other reason is not provided, skipping update/)
expect { import_service.send(:update_reason, lettings_log_xml) }
.not_to(change { lettings_log.reload.reason })
end
end
end
end
end

Loading…
Cancel
Save