Browse Source

Blank out all invalid fields for in-progress logs (#972)

Only import fields from the old CORE service that are logically valid. If a log is `saved` or `submitted-invalid` then it might have fields that are logically invalid at import. We can safely blank these out for the user to re-input.
pull/977/head
James Rose 2 years ago committed by GitHub
parent
commit
5511ededeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      app/services/imports/lettings_logs_import_service.rb
  2. 26
      spec/services/imports/lettings_logs_import_service_spec.rb

22
app/services/imports/lettings_logs_import_service.rb

@ -247,12 +247,12 @@ module Imports
apply_date_consistency!(attributes) apply_date_consistency!(attributes)
apply_household_consistency!(attributes) apply_household_consistency!(attributes)
lettings_log = save_lettings_log(attributes) lettings_log = save_lettings_log(attributes, previous_status)
compute_differences(lettings_log, attributes) compute_differences(lettings_log, attributes)
check_status_completed(lettings_log, previous_status) unless @logs_overridden.include?(lettings_log.old_id) check_status_completed(lettings_log, previous_status) unless @logs_overridden.include?(lettings_log.old_id)
end end
def save_lettings_log(attributes) def save_lettings_log(attributes, previous_status)
lettings_log = LettingsLog.new(attributes) lettings_log = LettingsLog.new(attributes)
begin begin
lettings_log.save! lettings_log.save!
@ -264,21 +264,29 @@ module Imports
record.update!(attributes) record.update!(attributes)
record record
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
rescue_validation_or_raise(lettings_log, attributes, e) rescue_validation_or_raise(lettings_log, attributes, previous_status, e)
end end
end end
def rescue_validation_or_raise(lettings_log, attributes, exception) def rescue_validation_or_raise(lettings_log, attributes, previous_status, exception)
if lettings_log.errors.of_kind?(:referral, :internal_transfer_non_social_housing) # Blank out all invalid fields for in-progress logs
if %w[saved submitted-invalid].include?(previous_status)
lettings_log.errors.each do |error|
@logger.warn("Log #{lettings_log.old_id}: Removing field #{error.attribute} from log triggering validation: #{error.type}")
attributes.delete(error.attribute.to_s)
end
@logs_overridden << lettings_log.old_id
save_lettings_log(attributes, previous_status)
elsif lettings_log.errors.of_kind?(:referral, :internal_transfer_non_social_housing)
@logger.warn("Log #{lettings_log.old_id}: Removing internal transfer referral since previous tenancy is a non social housing") @logger.warn("Log #{lettings_log.old_id}: Removing internal transfer referral since previous tenancy is a non social housing")
@logs_overridden << lettings_log.old_id @logs_overridden << lettings_log.old_id
attributes.delete("referral") attributes.delete("referral")
save_lettings_log(attributes) save_lettings_log(attributes, previous_status)
elsif lettings_log.errors.of_kind?(:referral, :internal_transfer_fixed_or_lifetime) elsif lettings_log.errors.of_kind?(:referral, :internal_transfer_fixed_or_lifetime)
@logger.warn("Log #{lettings_log.old_id}: Removing internal transfer referral since previous tenancy is fixed terms or lifetime") @logger.warn("Log #{lettings_log.old_id}: Removing internal transfer referral since previous tenancy is fixed terms or lifetime")
@logs_overridden << lettings_log.old_id @logs_overridden << lettings_log.old_id
attributes.delete("referral") attributes.delete("referral")
save_lettings_log(attributes) save_lettings_log(attributes, previous_status)
else else
raise exception raise exception
end end

26
spec/services/imports/lettings_logs_import_service_spec.rb

@ -170,6 +170,32 @@ RSpec.describe Imports::LettingsLogsImportService do
end end
end end
context "and this is an internal transfer that is in-progress with invalid answers" do
before do
lettings_log_xml.at_xpath("//meta:status").content = "submitted-invalid"
lettings_log_xml.at_xpath("//xmlns:P2Age").content = 999
end
it "intercepts the relevant validation error" do
expect(logger).to receive(:warn).with(/Removing field age2 from log triggering validation: Answer cannot be over 16/)
expect(logger).to receive(:warn).with(/Removing field age2 from log triggering validation: Person 2’s age must be between/)
expect(logger).to receive(:warn).with(/Removing field ecstat2 from log triggering validation: Answer cannot be ‘child under 16’/)
expect { lettings_log_service.send(:create_log, lettings_log_xml) }
.not_to raise_error
end
it "clears out the invalid answers" do
allow(logger).to receive(:warn)
lettings_log_service.send(:create_log, lettings_log_xml)
lettings_log = LettingsLog.find_by(old_id: lettings_log_id)
expect(lettings_log).not_to be_nil
expect(lettings_log.age2).to be_nil
expect(lettings_log.ecstat2).to be_nil
end
end
context "and this is an internal transfer from a non social housing" do context "and this is an internal transfer from a non social housing" do
before do before do
lettings_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home" lettings_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home"

Loading…
Cancel
Save