Browse Source

Check the error before clearing postcode known

pull/1385/head
Kat 3 years ago
parent
commit
cd8cfaac7c
  1. 4
      app/models/validations/sales/household_validations.rb
  2. 11
      app/services/imports/sales_logs_import_service.rb
  3. 5
      spec/services/imports/sales_logs_import_service_spec.rb

4
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

11
app/services/imports/sales_logs_import_service.rb

@ -192,7 +192,11 @@ module Imports
attributes.delete(error.attribute.to_s)
end
@logs_overridden << sales_log.old_id
reset_postcode_known(attributes)
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")
@ -208,11 +212,6 @@ module Imports
end
end
def reset_postcode_known(attributes)
attributes["pcodenk"] = attributes["postcode_full"].present? ? 0 : nil
attributes["ppcodenk"] = attributes["ppostcode_full"].present? ? 0 : nil
end
def compute_differences(sales_log, attributes)
differences = []
attributes.each do |key, value|

5
spec/services/imports/sales_logs_import_service_spec.rb

@ -406,8 +406,9 @@ RSpec.describe Imports::SalesLogsImportService do
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: Last settled accommodation and discounted ownership property postcodes must match/)
expect(logger).to receive(:warn).with(/Removing field ppostcode_full from log triggering validation: Last settled accommodation and discounted ownership property 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

Loading…
Cancel
Save