Browse Source

Clear tshortfall if it fails validation

pull/1472/head
Kat 3 years ago
parent
commit
64afd42e36
  1. 2
      app/models/validations/financial_validations.rb
  2. 5
      app/services/imports/lettings_logs_import_service.rb
  3. 42
      spec/services/imports/lettings_logs_import_service_spec.rb

2
app/models/validations/financial_validations.rb

@ -4,7 +4,7 @@ module Validations::FinancialValidations
# or 'validate_' to run on submit as well # or 'validate_' to run on submit as well
def validate_outstanding_rent_amount(record) def validate_outstanding_rent_amount(record)
if !record.has_hbrentshortfall? && record.tshortfall.present? 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
end end

5
app/services/imports/lettings_logs_import_service.rb

@ -332,6 +332,11 @@ module Imports
@logs_overridden << lettings_log.old_id @logs_overridden << lettings_log.old_id
attributes.delete("ecstat1") attributes.delete("ecstat1")
save_lettings_log(attributes, previous_status) 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 else
@logger.error("Log #{lettings_log.old_id}: Failed to import") @logger.error("Log #{lettings_log.old_id}: Failed to import")
lettings_log.errors.each do |error| lettings_log.errors.each do |error|

42
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) expect(lettings_log.hhmemb).to eq(1)
end end
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
end end

Loading…
Cancel
Save