From 84a6fc6308de8ba021aeb128fea39b32df5816d4 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 20 Mar 2023 17:30:50 +0000 Subject: [PATCH] Clear postcode known if postcode is getting cleared (#1447) --- .../imports/sales_logs_import_service.rb | 2 ++ .../imports/sales_logs_import_service_spec.rb | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 3dc3c6d27..e548b8c72 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -191,6 +191,8 @@ module Imports sales_log.errors.each do |error| @logger.warn("Log #{sales_log.old_id}: Removing field #{error.attribute} from log triggering validation: #{error.type}") attributes.delete(error.attribute.to_s) + attributes.delete("pcodenk") if error.attribute == :postcode_full + attributes.delete("ppcodenk") if error.attribute == :ppostcode_full end @logs_overridden << sales_log.old_id save_sales_log(attributes, previous_status) diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index f0fe93588..d74b24ce2 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -448,6 +448,32 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and it has invalid postcodes" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//meta:status").content = "submitted-invalid" + sales_log_xml.at_xpath("//xmlns:Q14Postcode").content = "2AA" # postcode + 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 { 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.postcode_full).to be_nil + end + end + context "and it has an invalid record with invalid contracts exchange date" do let(:sales_log_id) { "shared_ownership_sales_log" }