diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 61772be93..8f81aa117 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -98,10 +98,10 @@ module Validations::Sales::FinancialValidations if record.equity < range.min record.errors.add :type, I18n.t("validations.financial.equity.under_min", min_equity: range.min) - record.errors.add :equity, I18n.t("validations.financial.equity.under_min", min_equity: range.min) + record.errors.add :equity, :under_min, message: I18n.t("validations.financial.equity.under_min", min_equity: range.min) elsif record.equity > range.max record.errors.add :type, I18n.t("validations.financial.equity.over_max", max_equity: range.max) - record.errors.add :equity, I18n.t("validations.financial.equity.over_max", max_equity: range.max) + record.errors.add :equity, :over_max, message: I18n.t("validations.financial.equity.over_max", max_equity: range.max) end end diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index 32a8b22bd..e0f93515c 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -38,7 +38,7 @@ module Validations::SharedValidations postcode = record.postcode_full if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) error_message = I18n.t("validations.postcode") - record.errors.add :postcode_full, error_message + record.errors.add :postcode_full, :wrong_format, message: error_message end end diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index c2844e019..e6f735b3e 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -214,6 +214,17 @@ module Imports @logs_overridden << sales_log.old_id attributes.delete("income1") save_sales_log(attributes, previous_status) + elsif sales_log.errors.of_kind?(:equity, :over_max) || sales_log.errors.of_kind?(:equity, :under_min) + @logger.warn("Log #{sales_log.old_id}: Removing equity as the equity is invalid") + @logs_overridden << sales_log.old_id + attributes.delete("equity") + save_sales_log(attributes, previous_status) + elsif sales_log.errors.of_kind?(:postcode_full, :wrong_format) + @logger.warn("Log #{sales_log.old_id}: Removing postcode as the postcode is invalid") + @logs_overridden << sales_log.old_id + attributes.delete("postcode_full") + attributes["pcodenk"] = attributes["la"].present? ? 1 : nil + save_sales_log(attributes, previous_status) else @logger.error("Log #{sales_log.old_id}: Failed to import") sales_log.errors.each do |error| @@ -544,7 +555,7 @@ module Imports end # other household members characteristics - (2..attributes["hhmemb"]).each do |index| + (2..[attributes["hhmemb"], 6].min).each do |index| attributes["age#{index}_known"] ||= 1 attributes["sex#{index}"] ||= "R" attributes["ecstat#{index}"] ||= 10 diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index e41ffabaf..f0168ba11 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -439,6 +439,81 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and it has a record with invalid equity" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q23Equity").content = "78" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Log shared_ownership_sales_log: Removing equity as the equity is invalid/) + 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.equity).to be_nil + end + end + + context "and it has a record with invalid equity (under_min)" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q23Equity").content = "20" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Log shared_ownership_sales_log: Removing equity as the equity is invalid/) + 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.equity).to be_nil + end + end + + context "and it has a record with postcode in invalid format" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:Q14Postcode").content = "L3132AF" + sales_log_xml.at_xpath("//xmlns:Q14ONSLACode").content = "E07000223" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Log shared_ownership_sales_log: Removing 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 la" 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.postcode_full).to be_nil + expect(sales_log.pcodenk).to eq(1) # not known + expect(sales_log.la).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" } @@ -501,7 +576,7 @@ RSpec.describe Imports::SalesLogsImportService do end it "intercepts the relevant validation error" do - expect(logger).to receive(:warn).with(/Enter a postcode in the correct format, for example AA1 1AA/) + expect(logger).to receive(:warn).with(/Removing field postcode_full from log triggering validation: wrong_format/) expect { sales_log_service.send(:create_log, sales_log_xml) } .not_to raise_error end @@ -518,6 +593,20 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "when there is information about 7 people" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:P7Age").content = "22" + sales_log_xml.at_xpath("//xmlns:LiveInOther").content = "10" + end + + it "does not try to save information about person 7" do + expect { sales_log_service.send(:create_log, sales_log_xml) } + .not_to raise_error + end + end + context "and it has an invalid record with invalid contracts exchange date" do let(:sales_log_id) { "shared_ownership_sales_log" }