From cb2c13ed7a2e9c6d18529a9e4fbfa7ebd1415eb1 Mon Sep 17 00:00:00 2001 From: James Rose Date: Wed, 1 Mar 2023 09:07:23 +0000 Subject: [PATCH] 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 --- .../validations/financial_validations.rb | 4 +- .../validations/household_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 21 ++++++ .../lettings_logs_import_service_spec.rb | 75 +++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index a5430bbea..175b0b1bd 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -24,11 +24,11 @@ module Validations::FinancialValidations def validate_net_income(record) if record.ecstat1 && record.weekly_net_income 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 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 diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index 4ffd0b1f8..afa7867ff 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -67,7 +67,7 @@ module Validations::HouseholdValidations end 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") end diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index a474f0c45..3aa73db4b 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -287,6 +287,27 @@ module Imports @logs_overridden << lettings_log.old_id attributes.delete("referral") 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 @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 7850008a1..e3f04b74c 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -205,6 +205,81 @@ RSpec.describe Imports::LettingsLogsImportService do 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 before do lettings_log_xml.at_xpath("//xmlns:Q11").content = "9 Residential care home"