diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index e8c59518b..c7721b666 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -247,12 +247,12 @@ module Imports apply_date_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) check_status_completed(lettings_log, previous_status) unless @logs_overridden.include?(lettings_log.old_id) end - def save_lettings_log(attributes) + def save_lettings_log(attributes, previous_status) lettings_log = LettingsLog.new(attributes) begin lettings_log.save! @@ -264,21 +264,29 @@ module Imports record.update!(attributes) record rescue ActiveRecord::RecordInvalid => e - rescue_validation_or_raise(lettings_log, attributes, e) + rescue_validation_or_raise(lettings_log, attributes, previous_status, e) end end - def rescue_validation_or_raise(lettings_log, attributes, exception) - if lettings_log.errors.of_kind?(:referral, :internal_transfer_non_social_housing) + def rescue_validation_or_raise(lettings_log, attributes, previous_status, exception) + # 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") @logs_overridden << lettings_log.old_id 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) @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 attributes.delete("referral") - save_lettings_log(attributes) + save_lettings_log(attributes, previous_status) else raise exception end diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index c74726216..576249971 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -170,6 +170,32 @@ RSpec.describe Imports::LettingsLogsImportService do 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 before do lettings_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home"