From ecdfab1c8880d765e84d12eaf983328d9a13b9fe Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 9 May 2023 15:46:37 +0100 Subject: [PATCH] Update more validations upon import (#1609) * Clear invalid format previous postcode * Fix import with income 2 outside of london validation * Remove mscharge if it is under minimum * Clear frombeds if it's out of range --- .../local_authority_validations.rb | 2 +- .../sales/financial_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 4 +- .../imports/sales_logs_import_service.rb | 9 ++ .../imports/sales_logs_import_service_spec.rb | 103 ++++++++++++++++++ 5 files changed, 116 insertions(+), 4 deletions(-) diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index c895880bd..3bdd79166 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -3,7 +3,7 @@ module Validations::LocalAuthorityValidations postcode = record.ppostcode_full if record.previous_postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) error_message = I18n.t("validations.postcode") - record.errors.add :ppostcode_full, error_message + record.errors.add :ppostcode_full, :wrong_format, message: error_message end end end diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 8e54758bd..02fceeadc 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -20,7 +20,7 @@ module Validations::Sales::FinancialValidations if record.london_property? && record.income2 > 90_000 relevant_fields.each { |field| record.errors.add field, :over_hard_max_for_london, message: I18n.t("validations.financial.income.over_hard_max_for_london") } elsif record.property_not_in_london? && record.income2 > 80_000 - relevant_fields.each { |field| record.errors.add field, I18n.t("validations.financial.income.over_hard_max_for_outside_london") } + relevant_fields.each { |field| record.errors.add field, :over_hard_max_for_outside_london, message: I18n.t("validations.financial.income.over_hard_max_for_outside_london") } end end diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index b1487a0b7..75d7f1737 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -467,11 +467,11 @@ module Imports def previous_postcode_known(xml_doc, previous_postcode, prevloc) previous_postcode_known = string_or_nil(xml_doc, "Q12bnot") if previous_postcode_known == "Temporary_or_Unknown" || (previous_postcode.nil? && prevloc.present?) - 0 + 0 # not known elsif previous_postcode.nil? nil else - 1 + 1 # known end end diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 565c16512..6e2c11bf3 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -225,10 +225,13 @@ module Imports %i[exdate over_a_year_from_saledate] => %w[exdate], %i[income1 over_hard_max_for_outside_london] => %w[income1], %i[income1 over_hard_max_for_london] => %w[income1], + %i[income2 over_hard_max_for_outside_london] => %w[income2], %i[income2 over_hard_max_for_london] => %w[income2], %i[equity over_max] => %w[equity], %i[equity under_min] => %w[equity], + %i[mscharge under_min] => %w[mscharge has_mscharge], %i[mortgage cannot_be_0] => %w[mortgage], + %i[frombeds outside_the_range] => %w[frombeds], } errors.each do |(error, fields)| @@ -249,6 +252,12 @@ module Imports attributes.delete("postcode_full") attributes["pcodenk"] = attributes["la"].present? ? 1 : nil save_sales_log(attributes, previous_status) + elsif sales_log.errors.of_kind?(:ppostcode_full, :wrong_format) + @logger.warn("Log #{sales_log.old_id}: Removing previous postcode as the postcode is invalid") + @logs_overridden << sales_log.old_id + attributes.delete("ppostcode_full") + attributes["ppcodenk"] = attributes["prevloc"].present? ? 1 : nil + save_sales_log(attributes, previous_status) elsif sales_log.errors.of_kind?(:uprn, :uprn_error) @logger.warn("Log #{sales_log.old_id}: Setting uprn_known to no with error: #{sales_log.errors[:uprn].join(', ')}") @logs_overridden << sales_log.old_id diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index f169dbd78..eb7051a81 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -595,6 +595,33 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and it has a record with previous postcode in invalid format" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q7Postcode").content = "L3132AF" + sales_log_xml.at_xpath("//xmlns:Q7ONSLACode").content = "E07000223" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Log shared_ownership_sales_log: Removing previous postcode as the postcode is invalid/) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .not_to raise_error + end + + it "clears out the invalid answers and sets correct prevloc" 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.ppostcode_full).to be_nil + expect(sales_log.ppcodenk).to eq(1) # not known + expect(sales_log.prevloc).to eq("E07000223") # not known + end + end + context "and setup field has validation error in incomplete log" do let(:sales_log_id) { "shared_ownership_sales_log" } @@ -768,6 +795,32 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and it has an invalid income 2" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q2Person1Income").content = "0" + sales_log_xml.at_xpath("//xmlns:Q2Person2Income").content = "95000" + sales_log_xml.at_xpath("//xmlns:Q14ONSLACode").content = "E07000223" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing income2 with error: Combined income must be £80,000 or lower for properties outside London local authorities, Income must be £80,000 or lower for properties outside London local authority/) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .not_to raise_error + end + + it "clears out the invalid answers" 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.income2).to be_nil + end + end + context "and it has an invalid income 1 for london" do let(:sales_log_id) { "shared_ownership_sales_log" } @@ -818,6 +871,56 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and it has an invalid mscharge" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q29MonthlyCharges").content = "0.1" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing mscharge with error: Monthly leasehold charges must be at least £1/) + expect(logger).to receive(:warn).with(/Removing has_mscharge with error: Monthly leasehold charges must be at least £1/) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .not_to raise_error + end + + it "clears out the invalid answers" 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.mscharge).to be_nil + expect(sales_log.has_mscharge).to be_nil + end + end + + context "and it has an invalid frombeds" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q20Bedrooms").content = "0" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing frombeds with error: Number of bedrooms in previous property must be between 1 and 6/) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .not_to raise_error + end + + it "clears out the invalid answers" 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.frombeds).to be_nil + end + end + context "when inferring default answers for completed sales logs" do context "when the armedforcesspouse is not answered" do let(:sales_log_id) { "discounted_ownership_sales_log" }