From bbbc7bcc114b387536a08289b59b1271756333f5 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 28 Mar 2023 09:18:57 +0100 Subject: [PATCH] Clear mortgage and outstanding amount when importing (#1472) * Clear mortgage if it's 0 * Clear tshortfall if it fails validation * Remove hbrentshortfall because we want to set the log back in progress --- .../validations/financial_validations.rb | 2 +- .../sales/financial_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 6 +++ .../imports/sales_logs_import_service.rb | 5 +++ .../lettings_logs_import_service_spec.rb | 42 +++++++++++++++++++ .../imports/sales_logs_import_service_spec.rb | 24 +++++++++++ 6 files changed, 79 insertions(+), 2 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index fc1c44585..c68343ae1 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/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 8f81aa117..2cd06b9ba 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -37,7 +37,7 @@ module Validations::Sales::FinancialValidations end def validate_mortgage(record) - record.errors.add :mortgage, I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero? + record.errors.add :mortgage, :cannot_be_0, message: I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero? end def validate_cash_discount(record) diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index ecd32c51d..9bbb15723 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -332,6 +332,12 @@ 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") + attributes.delete("hbrentshortfall") + 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/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index e6f735b3e..4d26639da 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -225,6 +225,11 @@ module Imports attributes.delete("postcode_full") attributes["pcodenk"] = attributes["la"].present? ? 1 : nil save_sales_log(attributes, previous_status) + elsif sales_log.errors.of_kind?(:mortgage, :cannot_be_0) + @logger.warn("Log #{sales_log.old_id}: Removing mortgage because it cannot be 0") + @logs_overridden << sales_log.old_id + attributes.delete("mortgage") + save_sales_log(attributes, previous_status) else @logger.error("Log #{sales_log.old_id}: Failed to import") sales_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..c9f794336 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 be_nil + end + end end end diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index f0168ba11..3ab0c4a97 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -637,6 +637,30 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "when mortgage value is 0" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:CALCMORT").content = "0" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing mortgage because it cannot be 0/) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .not_to raise_error + end + + it "clears out the referral answer" do + allow(logger).to receive(:warn) + + sales_log_service.send(:create_log, sales_log_xml) + sales_log = SalesLog.find_by(old_id: sales_log_id) + + expect(sales_log).not_to be_nil + expect(sales_log.mortgage).to be_nil + end + end + context "and it has an invalid income" do let(:sales_log_id) { "shared_ownership_sales_log" }