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
Fix: Published a product status event once for concurrent moves (V-3779) (#15097)
Damian Legawiecb60273d5b198
diff --git a/spree/core/app/workflows/spree/products/activate.rb b/spree/core/app/workflows/spree/products/activate.rbindex 97b0d5a921..3247d21281 100644--- a/spree/core/app/workflows/spree/products/activate.rb+++ b/spree/core/app/workflows/spree/products/activate.rb@@ -5,6 +5,8 @@ module Spree# after_transition, so a handler can veto the change before anything is# written and see the product it applies to.class Activate < Spree::Workflow+ include StatusTransition+hooks :validate, :after_activate# @param product [Spree::Product]@@ -14,12 +16,15 @@ module Spreerun_hooks :validate- ApplicationRecord.transaction do+ moved = ApplicationRecord.transaction do+ next false if already_in?('active')+step :mark_activerun_hooks :after_activate+ trueend- product.publish_event('product.activated')+ product.publish_event('product.activated') if movedsuccess(product)enddiff --git a/spree/core/app/workflows/spree/products/archive.rb b/spree/core/app/workflows/spree/products/archive.rbindex 2feea9602c..c453faf7e3 100644--- a/spree/core/app/workflows/spree/products/archive.rb+++ b/spree/core/app/workflows/spree/products/archive.rb@@ -3,6 +3,8 @@ module Spree# Takes a product off sale for good. Replaces the `archive` state machine# event.class Archive < Spree::Workflow+ include StatusTransition+hooks :validate, :after_archive# @param product [Spree::Product]@@ -12,13 +14,16 @@ module Spreerun_hooks :validate- ApplicationRecord.transaction do+ moved = ApplicationRecord.transaction do+ next false if already_in?('archived')+step :mark_archivedstep :withdraw_submissionrun_hooks :after_archive+ trueend- product.publish_event('product.archived')+ product.publish_event('product.archived') if movedsuccess(product)enddiff --git a/spree/core/app/workflows/spree/products/draft.rb b/spree/core/app/workflows/spree/products/draft.rbindex 51cff0941d..e9453d3f0f 100644--- a/spree/core/app/workflows/spree/products/draft.rb+++ b/spree/core/app/workflows/spree/products/draft.rb@@ -3,6 +3,8 @@ module Spree# Returns a product to draft, hiding it from the storefront while it is# worked on. Replaces the `draft` state machine event.class Draft < Spree::Workflow+ include StatusTransition+hooks :validate, :after_draft# @param product [Spree::Product]@@ -12,17 +14,20 @@ module Spreerun_hooks :validate- ApplicationRecord.transaction do+ moved = ApplicationRecord.transaction do+ next false if already_in?('draft')+step :mark_draftstep :withdraw_submissionrun_hooks :after_draft+ trueend# Its siblings (Activate, Archive, Propose, Approve, Reject) all# announce themselves, and this one now carries a marketplace meaning# too: taking a listing back withdraws an open submission, which a# seller's integration has the same reason to hear about.- product.publish_event('product.drafted')+ product.publish_event('product.drafted') if movedsuccess(product)enddiff --git a/spree/core/app/workflows/spree/products/status_transition.rb b/spree/core/app/workflows/spree/products/status_transition.rbnew file mode 100644index 0000000000..59b6574b64--- /dev/null+++ b/spree/core/app/workflows/spree/products/status_transition.rb@@ -0,0 +1,24 @@+module Spree+ module Products+ # What the status workflows share: whether the product is already in the+ # status a move would put it in, read under a row lock inside the+ # workflow's own transaction.+ #+ # Two requests for the same move otherwise both read the old status, both+ # write the new one and both publish the event, so every webhook+ # subscriber hears of one change twice. With the lock the second waits,+ # finds the move made, and changes nothing.+ #+ # The status is read by query rather than by reloading the product, so a+ # caller's unsaved edits on the record are kept.+ module StatusTransition+ private++ # @param status [String]+ # @return [Boolean]+ def already_in?(status)+ product.class.where(id: product.id).lock.pick(:status) == status+ end+ end+ end+enddiff --git a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb b/spree/core/spec/workflows/spree/products/status_transitions_spec.rbindex 1bbe3ca5ca..ed958f4f5d 100644--- a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb+++ b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb@@ -165,6 +165,40 @@ RSpec.describe 'Spree::Products status workflows' dodescribed_class.call(product: product)end+ # A repeated or concurrent request finds the move made and changes+ # nothing, so subscribers hear of one activation once.+ context 'when the product is already active' do+ let(:product) { create(:product, status: 'active', store: store) }++ it 'succeeds without publishing again', events: true do+ allow(product).to receive(:publish_event)++ expect(described_class.call(product: product)).to be_success+ expect(product).not_to have_received(:publish_event).with('product.activated')+ end++ it 'runs no hook' do+ Spree.hooks.clear!+ seen = []+ Spree.hooks.register('products.activate.after_activate') { |workflow| seen << workflow.product }++ described_class.call(product: product)++ expect(seen).to be_empty+ ensure+ Spree.hooks.clear!+ end++ # Read by query, so a caller's in-memory record is not reloaded.+ it 'keeps unsaved edits on the record' do+ product.name = 'Renamed, not saved'++ described_class.call(product: product)++ expect(product.name).to eq('Renamed, not saved')+ end+ end+context 'hooks' dobefore { Spree.hooks.clear! }after { Spree.hooks.clear! }@@ -202,6 +236,14 @@ RSpec.describe 'Spree::Products status workflows' dodescribed_class.call(product: product)end++ it 'publishes nothing for a product already archived', events: true do+ archived = create(:product, status: 'archived', store: store)+ allow(archived).to receive(:publish_event)++ expect(described_class.call(product: archived)).to be_success+ expect(archived).not_to have_received(:publish_event).with('product.archived')+ endenddescribe Spree::Products::Draft do@@ -210,5 +252,13 @@ RSpec.describe 'Spree::Products status workflows' doit 'returns the product to draft' doexpect { described_class.call(product: product) }.to change { product.reload.status }.from('active').to('draft')end++ it 'publishes nothing for a product already in draft', events: true do+ draft = create(:product, status: 'draft', store: store)+ allow(draft).to receive(:publish_event)++ expect(described_class.call(product: draft)).to be_success+ expect(draft).not_to have_received(:publish_event).with('product.drafted')+ endendend