From fa8676b8eaec6031d9265f1613fce673c7835a81 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 23 Mar 2023 15:57:05 +0000 Subject: [PATCH] Lettings log import validations (#1463) * Clear import fields failing non_temp_accommodation validation * Clear joint tenancy if there is only 1 person in the household * Clear offered if the value is above the maximum of 20 * Display error message on ecstat * Remove ecstat if the income is too high for it --- .../validations/financial_validations.rb | 1 + .../validations/household_validations.rb | 2 +- .../validations/property_validations.rb | 2 +- app/models/validations/tenancy_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 21 ++++ config/locales/en.yml | 2 + .../0ead17cb-1668-442d-898c-0d52879ff592.xml | 1 + .../lettings_logs_import_service_spec.rb | 111 ++++++++++++++++++ 8 files changed, 139 insertions(+), 3 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 71371ec71..f39c17af7 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -25,6 +25,7 @@ module Validations::FinancialValidations if record.ecstat1 && record.weekly_net_income if record.weekly_net_income > 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) + record.errors.add :ecstat1, :over_hard_max, message: I18n.t("validations.financial.ecstat.over_hard_max", hard_max: record.applicable_income_range.hard_max) end if record.weekly_net_income < record.applicable_income_range.hard_min diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index afa7867ff..0b871fce0 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -63,7 +63,7 @@ module Validations::HouseholdValidations def validate_previous_housing_situation(record) if record.is_relet_to_temp_tenant? && !record.previous_tenancy_was_temporary? - record.errors.add :prevten, I18n.t("validations.household.prevten.non_temp_accommodation") + record.errors.add :prevten, :non_temp_accommodation, message: I18n.t("validations.household.prevten.non_temp_accommodation") end if record.age1.present? && record.age1 > 19 && record.previous_tenancy_was_foster_care? diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index 2e53b8385..dafe6f6dd 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -16,7 +16,7 @@ module Validations::PropertyValidations end if record.offered.negative? || record.offered > 20 - record.errors.add :offered, I18n.t("validations.property.offered.relet_number") + record.errors.add :offered, :over_20, message: I18n.t("validations.property.offered.relet_number") end end diff --git a/app/models/validations/tenancy_validations.rb b/app/models/validations/tenancy_validations.rb index e2ea108cf..5e1ec4a3c 100644 --- a/app/models/validations/tenancy_validations.rb +++ b/app/models/validations/tenancy_validations.rb @@ -43,7 +43,7 @@ module Validations::TenancyValidations return unless record.collection_start_year && record.joint if record.hhmemb == 1 && record.joint != 2 && record.collection_start_year >= 2022 - record.errors.add :joint, I18n.t("validations.tenancy.not_joint") + record.errors.add :joint, :not_joint_tenancy, message: I18n.t("validations.tenancy.not_joint") record.errors.add :hhmemb, I18n.t("validations.tenancy.joint_more_than_one_member") end end diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index 9e5de20ba..ecd32c51d 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -311,6 +311,27 @@ module Imports attributes.delete("prevten") attributes.delete("age1") save_lettings_log(attributes, previous_status) + elsif lettings_log.errors.of_kind?(:prevten, :non_temp_accommodation) + @logger.warn("Log #{lettings_log.old_id}: Removing vacancy reason and previous tenancy since this accommodation is not temporary") + @logs_overridden << lettings_log.old_id + attributes.delete("prevten") + attributes.delete("rsnvac") + save_lettings_log(attributes, previous_status) + elsif lettings_log.errors.of_kind?(:joint, :not_joint_tenancy) + @logger.warn("Log #{lettings_log.old_id}: Removing joint tenancy as there is only 1 person in the household") + @logs_overridden << lettings_log.old_id + attributes.delete("joint") + save_lettings_log(attributes, previous_status) + elsif lettings_log.errors.of_kind?(:offered, :over_20) + @logger.warn("Log #{lettings_log.old_id}: Removing offered as the value is above the maximum of 20") + @logs_overridden << lettings_log.old_id + attributes.delete("offered") + save_lettings_log(attributes, previous_status) + elsif lettings_log.errors.of_kind?(:earnings, :over_hard_max) + @logger.warn("Log #{lettings_log.old_id}: Removing working situation because income is too high for it") + @logs_overridden << lettings_log.old_id + attributes.delete("ecstat1") + 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/config/locales/en.yml b/config/locales/en.yml index 215a611b4..526dd8347 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -272,6 +272,8 @@ en: local_authority: general_needs: "Enter a value for the support charge between £0 and £60 per week if the landlord is a local authority and it is a general needs letting" supported_housing: "Enter a value for the support charge between £0 and £120 per week if the landlord is a local authority and it is a supported housing letting" + ecstat: + over_hard_max: "Net income of £%{hard_max} per week is too high for given the tenant’s working situation" brent: below_hard_min: "Rent is below the absolute minimum expected for a property of this type. Please check the rent, rent period, local authority and (if general needs) number of bedrooms" above_hard_max: "Rent is higher than the absolute maximum expected for a property of this type. Please check the rent, rent period, local authority and (if general needs) number of bedrooms" diff --git a/spec/fixtures/imports/logs/0ead17cb-1668-442d-898c-0d52879ff592.xml b/spec/fixtures/imports/logs/0ead17cb-1668-442d-898c-0d52879ff592.xml index c43bcf0bf..15f97ce5b 100644 --- a/spec/fixtures/imports/logs/0ead17cb-1668-442d-898c-0d52879ff592.xml +++ b/spec/fixtures/imports/logs/0ead17cb-1668-442d-898c-0d52879ff592.xml @@ -119,6 +119,7 @@ Yes 2 No + 2 No diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 5b56efdfb..89dd6e0c4 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -366,6 +366,81 @@ RSpec.describe Imports::LettingsLogsImportService do end end + context "and this is a non temporary acommodation" do + before do + lettings_log_xml.at_xpath("//xmlns:Q27").content = "9" + lettings_log_xml.at_xpath("//xmlns:Q11").content = "4" + lettings_log_xml.at_xpath("//xmlns:VDAY").content = "" + lettings_log_xml.at_xpath("//xmlns:VMONTH").content = "" + lettings_log_xml.at_xpath("//xmlns:VYEAR").content = "" + lettings_log_xml.at_xpath("//xmlns:MRCDAY").content = "" + lettings_log_xml.at_xpath("//xmlns:MRCMONTH").content = "" + lettings_log_xml.at_xpath("//xmlns:MRCYEAR").content = "" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing vacancy reason and previous tenancy since this accommodation is not temporary/) + 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.rsnvac).to be_nil + expect(lettings_log.prevten).to be_nil + end + end + + context "and the number the property was relet is over 20" do + before do + lettings_log_xml.at_xpath("//xmlns:Q20").content = "25" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing offered as the value is above the maximum of 20/) + 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.offered).to be_nil + end + end + + context "and income over the max" do + before do + lettings_log_xml.at_xpath("//xmlns:Q8Money").content = "25000" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing working situation because income is too high for it/) + 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.ecstat1).to be_nil + expect(lettings_log.earnings).to eq(25_000) + 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" @@ -560,5 +635,41 @@ RSpec.describe Imports::LettingsLogsImportService do .to change(OrganisationRelationship, :count).by(1) end end + + context "when this is a joint tenancy with 1 person in the household" do + let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + before do + lettings_log_xml.at_xpath("//xmlns:joint").content = "1" + lettings_log_xml.at_xpath("//xmlns:HHMEMB").content = "1" + lettings_log_xml.at_xpath("//xmlns:P2Age").content = "" + lettings_log_xml.at_xpath("//xmlns:P2Rel").content = "" + lettings_log_xml.at_xpath("//xmlns:P2Sex").content = "" + lettings_log_xml.at_xpath("//xmlns:P1Nat").content = "18" + lettings_log_xml.at_xpath("//xmlns:P2Eco").content = "" + lettings_log_xml.at_xpath("//xmlns:DAY").content = "2" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2022" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing joint tenancy as there is only 1 person in the household/) + 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.joint).to be_nil + expect(lettings_log.hhmemb).to eq(1) + end + end end end