From 9c2de581e43eabfd7d16c643fba501c033497b83 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 28 Mar 2023 12:32:41 +0100 Subject: [PATCH] Clear tshortfall if it is not positive --- .../validations/financial_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 6 +++ .../lettings_logs_import_service_spec.rb | 41 +++++++++++++++---- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index e6ce85c32..622adc117 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -79,7 +79,7 @@ module Validations::FinancialValidations record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") record.errors.add :brent, I18n.t("validations.financial.rent.less_than_shortfall") elsif record.wtshortfall < 0.01 - record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.must_be_positive") + record.errors.add :tshortfall, :must_be_positive, message: I18n.t("validations.financial.tshortfall.must_be_positive") end end diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index 786dae8f9..a374112d0 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -367,6 +367,12 @@ module Imports attributes.delete("supcharg") attributes.delete("tcharge") save_lettings_log(attributes, previous_status) + elsif lettings_log.errors.of_kind?(:tshortfall, :must_be_positive) + @logger.warn("Log #{lettings_log.old_id}: Removing tshortfall, because it is not positive/") + @logs_overridden << lettings_log.old_id + attributes.delete("tshortfall") + attributes.delete("tshortfall_known") + 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 2b0980461..c98d2cc58 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -384,7 +384,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the vacancy reason answer" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -407,7 +407,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the number offered answer" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -429,7 +429,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the working situation answer" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -453,7 +453,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the age answer" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -476,7 +476,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the bedrooms answer" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -506,7 +506,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the charges answers" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -530,7 +530,7 @@ RSpec.describe Imports::LettingsLogsImportService do .not_to raise_error end - it "clears out the referral answer" do + it "clears out the charges answers" do allow(logger).to receive(:warn) lettings_log_service.send(:create_log, lettings_log_xml) @@ -545,6 +545,33 @@ RSpec.describe Imports::LettingsLogsImportService do end end + context "and tshortfall is not positive" do + let(:lettings_log_id) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" } + + before do + lettings_log_xml.at_xpath("//xmlns:Q18d").content = "1" + lettings_log_xml.at_xpath("//xmlns:Q6Ben").content = "1" + lettings_log_xml.at_xpath("//xmlns:Q18dyes").content = "0" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing tshortfall, because it is not positive/) + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "clears out the tshortfall 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.tshortfall_known).to be_nil + end + end + context "and the net income soft validation is triggered (net_income_value_check)" do before do lettings_log_xml.at_xpath("//xmlns:Q8a").content = "1 Weekly"