Browse Source

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
pull/1342/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
bbbc7bcc11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/validations/financial_validations.rb
  2. 2
      app/models/validations/sales/financial_validations.rb
  3. 6
      app/services/imports/lettings_logs_import_service.rb
  4. 5
      app/services/imports/sales_logs_import_service.rb
  5. 42
      spec/services/imports/lettings_logs_import_service_spec.rb
  6. 24
      spec/services/imports/sales_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

2
app/models/validations/sales/financial_validations.rb

@ -37,7 +37,7 @@ module Validations::Sales::FinancialValidations
end end
def validate_mortgage(record) 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 end
def validate_cash_discount(record) def validate_cash_discount(record)

6
app/services/imports/lettings_logs_import_service.rb

@ -332,6 +332,12 @@ 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")
attributes.delete("hbrentshortfall")
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|

5
app/services/imports/sales_logs_import_service.rb

@ -225,6 +225,11 @@ module Imports
attributes.delete("postcode_full") attributes.delete("postcode_full")
attributes["pcodenk"] = attributes["la"].present? ? 1 : nil attributes["pcodenk"] = attributes["la"].present? ? 1 : nil
save_sales_log(attributes, previous_status) 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 else
@logger.error("Log #{sales_log.old_id}: Failed to import") @logger.error("Log #{sales_log.old_id}: Failed to import")
sales_log.errors.each do |error| sales_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 be_nil
end
end
end end
end end

24
spec/services/imports/sales_logs_import_service_spec.rb

@ -637,6 +637,30 @@ RSpec.describe Imports::SalesLogsImportService do
end end
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 context "and it has an invalid income" do
let(:sales_log_id) { "shared_ownership_sales_log" } let(:sales_log_id) { "shared_ownership_sales_log" }

Loading…
Cancel
Save