Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Discard a verdict for a number the buyer no longer holds
Łukasz Adamczykfaa5e0b4d733
diff --git a/spree/core/app/services/spree/tax_identifiers/validate.rb b/spree/core/app/services/spree/tax_identifiers/validate.rbindex 235d5fa14e..5bd7486440 100644--- a/spree/core/app/services/spree/tax_identifiers/validate.rb+++ b/spree/core/app/services/spree/tax_identifiers/validate.rb@@ -10,6 +10,11 @@ module Spree# @param tax_identifier [Spree::TaxIdentifier]# @return [Spree::TaxIdentifier]def call(tax_identifier:)+ # What this run is answering about. The buyer can change the number while+ # a check is in flight, and a verdict about the old one must not be+ # written over the new one — see #persist.+ @checked = tax_identifier.slice(:kind, :value)+validator = Spree.tax_identifier_validators[tax_identifier.kind]# Narrow race: the validator was deregistered between enqueue and run.@@ -34,11 +39,20 @@ module Spreeprivate+ # Re-read before writing. A slower check for a number the buyer has since+ # replaced would otherwise stamp its verdict onto the new one — reporting+ # a registration as verified when nothing has verified it, which is what+ # decides reverse charge. The row is already `pending` from the write that+ # replaced it, and its own check is on the way.def persist(tax_identifier, result)return failure(tax_identifier, result.errors.full_messages.to_sentence) unless result.valid?- tax_identifier.update_columns(result.to_columns.merge(updated_at: Time.current))- success(tax_identifier)+ current = tax_identifier.class.find_by(id: tax_identifier.id)+ return success(tax_identifier) if current.nil?+ return success(tax_identifier) if current.slice(:kind, :value) != @checked++ current.update_columns(result.to_columns.merge(updated_at: Time.current))+ success(current)enddef unsupported_resultdiff --git a/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb b/spree/core/spec/services/spree/tax_identifiers/validate_spec.rbindex 6814276ec3..0379677962 100644--- a/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb+++ b/spree/core/spec/services/spree/tax_identifiers/validate_spec.rb@@ -101,4 +101,28 @@ describe Spree::TaxIdentifiers::Validate doexpect(tax_identifier.validation_evidence['message']).to include('boom')endend++ # The buyer can replace the number while a check is in flight. A verdict about+ # the number they no longer hold must not land on the one they do — it would+ # report a registration as verified when nothing verified it, and that is what+ # decides reverse charge.+ context 'when the number changes while the check runs' do+ before do+ register(Class.new(Spree::TaxIdentifiers::Validator::Base) do+ def call(tax_identifier:)+ # Stands in for the buyer editing the number mid-check.+ tax_identifier.class.where(id: tax_identifier.id).update_all(value: 'DE222222222')+ Spree::TaxIdentifiers::ValidationResult.new(status: 'verified', checked_at: Time.current)+ end+ end)+ end++ it 'discards the verdict rather than writing it onto the new number' do+ described_class.call(tax_identifier: tax_identifier)++ row = tax_identifier.reload+ expect(row.value).to eq('DE222222222')+ expect(row.validation_status).not_to eq('verified')+ end+ endend