Browse Source

CLDC-1261: Remove invalid referral answer during import (#626)

pull/628/head
Stéphane Meny 3 years ago committed by GitHub
parent
commit
560d9061c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/validations/household_validations.rb
  2. 36
      app/services/imports/case_logs_import_service.rb
  3. 23
      spec/services/imports/case_logs_import_service_spec.rb

4
app/models/validations/household_validations.rb

@ -104,8 +104,8 @@ module Validations::HouseholdValidations
# 29 Prison / Approved Probation Hostel # 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) 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) : "" 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 :prevten, :internal_transfer_non_social_housing, message: 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 :referral, :internal_transfer_non_social_housing, message: I18n.t("validations.household.referral.prevten_invalid", prevten: label)
end end
end end

36
app/services/imports/case_logs_import_service.rb

@ -1,5 +1,11 @@
module Imports module Imports
class CaseLogsImportService < ImportService 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) def create_logs(folder)
import_from(folder, :create_log) import_from(folder, :create_log)
if @logs_with_discrepancies.count.positive? if @logs_with_discrepancies.count.positive?
@ -193,19 +199,35 @@ module Imports
apply_date_consistency!(attributes) apply_date_consistency!(attributes)
apply_household_consistency!(attributes) apply_household_consistency!(attributes)
case_log = CaseLog.new(attributes) case_log = save_case_log(attributes)
save_case_log(case_log, attributes)
compute_differences(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 end
def save_case_log(case_log, attributes) def save_case_log(attributes)
case_log = CaseLog.new(attributes)
begin
case_log.save! case_log.save!
case_log
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
unless attributes["old_id"].nil? legacy_id = attributes["old_id"]
record = CaseLog.find_by(old_id: attributes["old_id"]) record = CaseLog.find_by(old_id: legacy_id)
@logger.info "Updating case log #{case_log.id} with legacy ID #{attributes['old_id']}" @logger.info "Updating case log #{record.id} with legacy ID #{legacy_id}"
record.update!(attributes) 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
end end

23
spec/services/imports/case_logs_import_service_spec.rb

@ -156,5 +156,28 @@ RSpec.describe Imports::CaseLogsImportService do
end end
end 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
end end

Loading…
Cancel
Save