diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index 618e16e7b..1f67c12e1 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -104,8 +104,8 @@ module Validations::HouseholdValidations # 29 Prison / Approved Probation Hostel if record.is_internal_transfer? && [3, 4, 7, 9, 10, 13, 14, 19, 21, 23, 24, 25, 26, 27, 28, 29].include?(record.prevten) label = record.form.get_question("prevten", record).present? ? record.form.get_question("prevten", record).label_from_value(record.prevten) : "" - record.errors.add :prevten, I18n.t("validations.household.prevten.internal_transfer", prevten: label) - record.errors.add :referral, I18n.t("validations.household.referral.prevten_invalid", prevten: label) + record.errors.add :prevten, :internal_transfer_non_social_housing, message: I18n.t("validations.household.prevten.internal_transfer", prevten: label) + record.errors.add :referral, :internal_transfer_non_social_housing, message: I18n.t("validations.household.referral.prevten_invalid", prevten: label) end end diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index 6b670ce81..3d2a5e1fb 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -1,5 +1,11 @@ module Imports class CaseLogsImportService < ImportService + def initialize(storage_service, logger = Rails.logger) + @logs_with_discrepancies = Set.new + @logs_overridden = Set.new + super + end + def create_logs(folder) import_from(folder, :create_log) if @logs_with_discrepancies.count.positive? @@ -193,19 +199,35 @@ module Imports apply_date_consistency!(attributes) apply_household_consistency!(attributes) - case_log = CaseLog.new(attributes) - save_case_log(case_log, attributes) + case_log = save_case_log(attributes) compute_differences(case_log, attributes) - check_status_completed(case_log, previous_status) + check_status_completed(case_log, previous_status) unless @logs_overridden.include?(case_log.old_id) end - def save_case_log(case_log, attributes) - case_log.save! - rescue ActiveRecord::RecordNotUnique - unless attributes["old_id"].nil? - record = CaseLog.find_by(old_id: attributes["old_id"]) - @logger.info "Updating case log #{case_log.id} with legacy ID #{attributes['old_id']}" + def save_case_log(attributes) + case_log = CaseLog.new(attributes) + begin + case_log.save! + case_log + rescue ActiveRecord::RecordNotUnique + legacy_id = attributes["old_id"] + record = CaseLog.find_by(old_id: legacy_id) + @logger.info "Updating case log #{record.id} with legacy ID #{legacy_id}" record.update!(attributes) + record + rescue ActiveRecord::RecordInvalid => e + rescue_validation_or_raise(case_log, attributes, e) + end + end + + def rescue_validation_or_raise(case_log, attributes, exception) + if case_log.errors.of_kind?(:referral, :internal_transfer_non_social_housing) + @logger.warn("Log #{case_log.old_id}: Removing internal transfer referral since previous tenancy is a non social housing") + @logs_overridden << case_log.old_id + attributes.delete("referral") + save_case_log(attributes) + else + raise exception end end diff --git a/spec/services/imports/case_logs_import_service_spec.rb b/spec/services/imports/case_logs_import_service_spec.rb index 797a42e16..db20cf117 100644 --- a/spec/services/imports/case_logs_import_service_spec.rb +++ b/spec/services/imports/case_logs_import_service_spec.rb @@ -156,5 +156,28 @@ RSpec.describe Imports::CaseLogsImportService do end end end + + context "and this is an internal transfer from a non social housing" do + before do + case_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home" + case_log_xml.at_xpath("//xmlns:Q16").content = "1 Internal Transfer" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing internal transfer referral since previous tenancy is a non social housing/) + expect { case_log_service.send(:create_log, case_log_xml) } + .not_to raise_error + end + + it "clears out the referral answer" do + allow(logger).to receive(:warn) + + case_log_service.send(:create_log, case_log_xml) + case_log = CaseLog.find_by(old_id: case_log_id) + + expect(case_log).not_to be_nil + expect(case_log.referral).to be_nil + end + end end end