From 64afd42e36cdceb4707a7276748fa852c0f888d3 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 27 Mar 2023 09:02:05 +0100 Subject: [PATCH] Clear tshortfall if it fails validation --- .../validations/financial_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 5 +++ .../lettings_logs_import_service_spec.rb | 42 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index f39c17af7..500a9b44e 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -4,7 +4,7 @@ module Validations::FinancialValidations # or 'validate_' to run on submit as well def validate_outstanding_rent_amount(record) if !record.has_hbrentshortfall? && record.tshortfall.present? - record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.outstanding_amount_not_required") + record.errors.add :tshortfall, :no_outstanding_charges, message: I18n.t("validations.financial.tshortfall.outstanding_amount_not_required") end end diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index ecd32c51d..48f9ce811 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -332,6 +332,11 @@ module Imports @logs_overridden << lettings_log.old_id attributes.delete("ecstat1") save_lettings_log(attributes, previous_status) + elsif lettings_log.errors.of_kind?(:tshortfall, :no_outstanding_charges) + @logger.warn("Log #{lettings_log.old_id}: Removing tshortfall as there are no outstanding charges") + @logs_overridden << lettings_log.old_id + attributes.delete("tshortfall") + save_lettings_log(attributes, previous_status) else @logger.error("Log #{lettings_log.old_id}: Failed to import") lettings_log.errors.each do |error| diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 89dd6e0c4..a6dd8f356 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -671,5 +671,47 @@ RSpec.describe Imports::LettingsLogsImportService do expect(lettings_log.hhmemb).to eq(1) end end + + context "when there are no outstanding charges but outstanding amount is given" do + let(:lettings_log_id) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + before do + FormHandler.instance.use_fake_forms! + Singleton.__init__(FormHandler) + + lettings_log_xml.at_xpath("//xmlns:DAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2022" + lettings_log_xml.at_xpath("//xmlns:P1Nat").content = "18" + lettings_log_xml.at_xpath("//xmlns:Q18d").content = "2" + lettings_log_xml.at_xpath("//xmlns:Q18dyes").content = "20" + lettings_log_xml.at_xpath("//xmlns:_2cYears").content = "" + lettings_log_xml.at_xpath("//xmlns:Inj").content = "" + lettings_log_xml.at_xpath("//xmlns:LeftAF").content = "" + end + + after do + FormHandler.instance.use_real_forms! + Singleton.__init__(FormHandler) + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing tshortfall as there are no outstanding charges/) + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "clears out the referral answer" 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.tshortfall).to be_nil + expect(lettings_log.hbrentshortfall).to eq(2) + end + end end end