Browse Source

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
pull/1611/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
ecdfab1c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/validations/local_authority_validations.rb
  2. 2
      app/models/validations/sales/financial_validations.rb
  3. 4
      app/services/imports/lettings_logs_import_service.rb
  4. 9
      app/services/imports/sales_logs_import_service.rb
  5. 103
      spec/services/imports/sales_logs_import_service_spec.rb

2
app/models/validations/local_authority_validations.rb

@ -3,7 +3,7 @@ module Validations::LocalAuthorityValidations
postcode = record.ppostcode_full postcode = record.ppostcode_full
if record.previous_postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) if record.previous_postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP))
error_message = I18n.t("validations.postcode") 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 end
end end

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

@ -20,7 +20,7 @@ module Validations::Sales::FinancialValidations
if record.london_property? && record.income2 > 90_000 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") } 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 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
end end

4
app/services/imports/lettings_logs_import_service.rb

@ -467,11 +467,11 @@ module Imports
def previous_postcode_known(xml_doc, previous_postcode, prevloc) def previous_postcode_known(xml_doc, previous_postcode, prevloc)
previous_postcode_known = string_or_nil(xml_doc, "Q12bnot") previous_postcode_known = string_or_nil(xml_doc, "Q12bnot")
if previous_postcode_known == "Temporary_or_Unknown" || (previous_postcode.nil? && prevloc.present?) if previous_postcode_known == "Temporary_or_Unknown" || (previous_postcode.nil? && prevloc.present?)
0 0 # not known
elsif previous_postcode.nil? elsif previous_postcode.nil?
nil nil
else else
1 1 # known
end end
end end

9
app/services/imports/sales_logs_import_service.rb

@ -225,10 +225,13 @@ module Imports
%i[exdate over_a_year_from_saledate] => %w[exdate], %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_outside_london] => %w[income1],
%i[income1 over_hard_max_for_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[income2 over_hard_max_for_london] => %w[income2],
%i[equity over_max] => %w[equity], %i[equity over_max] => %w[equity],
%i[equity under_min] => %w[equity], %i[equity under_min] => %w[equity],
%i[mscharge under_min] => %w[mscharge has_mscharge],
%i[mortgage cannot_be_0] => %w[mortgage], %i[mortgage cannot_be_0] => %w[mortgage],
%i[frombeds outside_the_range] => %w[frombeds],
} }
errors.each do |(error, fields)| errors.each do |(error, fields)|
@ -249,6 +252,12 @@ 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?(: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) 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(', ')}") @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 @logs_overridden << sales_log.old_id

103
spec/services/imports/sales_logs_import_service_spec.rb

@ -595,6 +595,33 @@ RSpec.describe Imports::SalesLogsImportService do
end end
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 context "and setup field has validation error in incomplete log" do
let(:sales_log_id) { "shared_ownership_sales_log" } let(:sales_log_id) { "shared_ownership_sales_log" }
@ -768,6 +795,32 @@ RSpec.describe Imports::SalesLogsImportService do
end end
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 context "and it has an invalid income 1 for london" do
let(:sales_log_id) { "shared_ownership_sales_log" } let(:sales_log_id) { "shared_ownership_sales_log" }
@ -818,6 +871,56 @@ RSpec.describe Imports::SalesLogsImportService do
end end
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 inferring default answers for completed sales logs" do
context "when the armedforcesspouse is not answered" do context "when the armedforcesspouse is not answered" do
let(:sales_log_id) { "discounted_ownership_sales_log" } let(:sales_log_id) { "discounted_ownership_sales_log" }

Loading…
Cancel
Save