Browse Source

Blank out incompatible data when importing from CDS. (#1357)

This covers the following errors:

- Where the income is 0, set earnings and income to blank and set incref to refused
- Removing invalid tenancylength and tenancy values where tenancylength is invalid
- Removing prevten and age1 where incompatible
pull/1359/head
James Rose 2 years ago committed by GitHub
parent
commit
cb2c13ed7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/validations/financial_validations.rb
  2. 2
      app/models/validations/household_validations.rb
  3. 21
      app/services/imports/lettings_logs_import_service.rb
  4. 75
      spec/services/imports/lettings_logs_import_service_spec.rb

4
app/models/validations/financial_validations.rb

@ -24,11 +24,11 @@ module Validations::FinancialValidations
def validate_net_income(record) def validate_net_income(record)
if record.ecstat1 && record.weekly_net_income if record.ecstat1 && record.weekly_net_income
if record.weekly_net_income > record.applicable_income_range.hard_max if record.weekly_net_income > record.applicable_income_range.hard_max
record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_max", hard_max: record.applicable_income_range.hard_max) record.errors.add :earnings, :over_hard_max, message: I18n.t("validations.financial.earnings.over_hard_max", hard_max: record.applicable_income_range.hard_max)
end end
if record.weekly_net_income < record.applicable_income_range.hard_min if record.weekly_net_income < record.applicable_income_range.hard_min
record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_min", hard_min: record.applicable_income_range.hard_min) record.errors.add :earnings, :under_hard_min, message: I18n.t("validations.financial.earnings.under_hard_min", hard_min: record.applicable_income_range.hard_min)
end end
end end

2
app/models/validations/household_validations.rb

@ -67,7 +67,7 @@ module Validations::HouseholdValidations
end end
if record.age1.present? && record.age1 > 19 && record.previous_tenancy_was_foster_care? if record.age1.present? && record.age1 > 19 && record.previous_tenancy_was_foster_care?
record.errors.add :prevten, I18n.t("validations.household.prevten.over_20_foster_care") record.errors.add :prevten, :over_20_foster_care, message: I18n.t("validations.household.prevten.over_20_foster_care")
record.errors.add :age1, I18n.t("validations.household.age.lead.over_20") record.errors.add :age1, I18n.t("validations.household.age.lead.over_20")
end end

21
app/services/imports/lettings_logs_import_service.rb

@ -287,6 +287,27 @@ module Imports
@logs_overridden << lettings_log.old_id @logs_overridden << lettings_log.old_id
attributes.delete("referral") attributes.delete("referral")
save_lettings_log(attributes, previous_status) save_lettings_log(attributes, previous_status)
elsif lettings_log.errors.of_kind?(:earnings, :under_hard_min)
@logger.warn("Log #{lettings_log.old_id}: Where the income is 0, set earnings and income to blank and set incref to refused")
@logs_overridden << lettings_log.old_id
attributes.delete("earnings")
attributes.delete("incfreq")
attributes["incref"] = 1
attributes["net_income_known"] = 2
save_lettings_log(attributes, previous_status)
elsif lettings_log.errors.include?(:tenancylength) && lettings_log.errors.include?(:tenancy)
@logger.warn("Log #{lettings_log.old_id}: Removing tenancylength as invalid")
@logs_overridden << lettings_log.old_id
attributes.delete("tenancylength")
attributes.delete("tenancy")
save_lettings_log(attributes, previous_status)
elsif lettings_log.errors.of_kind?(:prevten, :over_20_foster_care)
@logger.warn("Log #{lettings_log.old_id}: Removing age1 and prevten as incompatible")
@logs_overridden << lettings_log.old_id
attributes.delete("prevten")
attributes.delete("age1")
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|

75
spec/services/imports/lettings_logs_import_service_spec.rb

@ -205,6 +205,81 @@ RSpec.describe Imports::LettingsLogsImportService do
end end
end end
context "and it has zero earnings" do
before do
lettings_log_xml.at_xpath("//meta:status").content = "submitted"
lettings_log_xml.at_xpath("//xmlns:Q8Money").content = 0
end
it "intercepts the relevant validation error" do
expect(logger).to receive(:warn).with(/Where the income is 0, set earnings and income to blank and set incref to refused/)
expect { lettings_log_service.send(:create_log, lettings_log_xml) }
.not_to raise_error
end
it "clears out the invalid answers" 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.earnings).to be_nil
expect(lettings_log.incref).to eq(1)
expect(lettings_log.net_income_known).to eq(2)
end
end
context "and an invalid tenancy length for tenancy type" do
before do
lettings_log_xml.at_xpath("//meta:status").content = "submitted"
lettings_log_xml.at_xpath("//xmlns:_2cYears").content = "1"
lettings_log_xml.at_xpath("//xmlns:Q2b").content = "4"
end
it "intercepts the relevant validation error" do
expect(logger).to receive(:warn).with(/Removing tenancylength as invalid/)
expect { lettings_log_service.send(:create_log, lettings_log_xml) }
.not_to raise_error
end
it "clears out the invalid answers" 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.tenancylength).to be_nil
expect(lettings_log.tenancy).to be_nil
end
end
context "and an lead tenant must be under 20 if childrens home or foster care" do
before do
lettings_log_xml.at_xpath("//meta:status").content = "submitted"
lettings_log_xml.at_xpath("//xmlns:Q11").content = "13"
lettings_log_xml.at_xpath("//xmlns:P1Age").content = "22"
end
it "intercepts the relevant validation error" do
expect(logger).to receive(:warn).with(/Removing age1 and prevten as incompatible/)
expect { lettings_log_service.send(:create_log, lettings_log_xml) }
.not_to raise_error
end
it "clears out the invalid answers" 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.age1).to be_nil
expect(lettings_log.prevten).to be_nil
end
end
context "and this is an internal transfer from a non social housing" do context "and this is an internal transfer from a non social housing" do
before do before do
lettings_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home" lettings_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home"

Loading…
Cancel
Save