diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index f9318ab0f..ec73b4abb 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -14,8 +14,8 @@ module Validations::Sales::HouseholdValidations return unless record.postcode_full && record.ppostcode_full && record.discounted_ownership_sale? unless record.postcode_full == record.ppostcode_full - record.errors.add :postcode_full, I18n.t("validations.household.postcode.discounted_ownership") - record.errors.add :ppostcode_full, I18n.t("validations.household.postcode.discounted_ownership") + record.errors.add :postcode_full, :postcodes_not_matching, message: I18n.t("validations.household.postcode.discounted_ownership") + record.errors.add :ppostcode_full, :postcodes_not_matching, message: I18n.t("validations.household.postcode.discounted_ownership") end end diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 6244e28e7..ed127bff2 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -192,6 +192,11 @@ module Imports attributes.delete(error.attribute.to_s) end @logs_overridden << sales_log.old_id + if sales_log.errors.of_kind?(:postcode_full, :postcodes_not_matching) + @logger.warn("Log #{sales_log.old_id}: Removing postcode known and previous postcode known as the postcodes are invalid") + attributes.delete("pcodenk") + attributes.delete("ppcodenk") + end save_sales_log(attributes, previous_status) else @logger.error("Log #{sales_log.old_id}: Failed to import") diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index b8d1c592f..2481cdce7 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -394,6 +394,37 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and it has an invalid record with invalid postcodes" do + let(:sales_log_id) { "discounted_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//meta:status").content = "submitted-invalid" + sales_log_xml.at_xpath("//xmlns:Q7Postcode").content = "A1 1AA" + sales_log_xml.at_xpath("//xmlns:Q14Postcode").content = "A1 2AA" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing field postcode_full from log triggering validation: Buyer's last accommodation and discounted ownership postcodes must match/) + expect(logger).to receive(:warn).with(/Removing field ppostcode_full from log triggering validation: Buyer's last accommodation and discounted ownership postcodes must match/) + expect(logger).to receive(:warn).with(/Removing field postcode_full from log triggering validation: postcodes_not_matching/) + expect(logger).to receive(:warn).with(/Removing field ppostcode_full from log triggering validation: postcodes_not_matching/) + expect(logger).to receive(:warn).with(/Removing postcode known and previous postcode known as the postcodes are 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.postcode_full).to be_nil + expect(sales_log.ppostcode_full).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" }