Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
run_hooks :validate
- ApplicationRecord.transaction do
+ moved = ApplicationRecord.transaction do
+ next false if already_in?('active')
+
step :mark_active
run_hooks :after_activate
+ true
end
- product.publish_event('product.activated')
+ product.publish_event('product.activated') if moved
success(product)
end
diff --git a/spree/core/app/workflows/spree/products/archive.rb b/spree/core/app/workflows/spree/products/archive.rb
index 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 Spree
run_hooks :validate
- ApplicationRecord.transaction do
+ moved = ApplicationRecord.transaction do
+ next false if already_in?('archived')
+
step :mark_archived
step :withdraw_submission
run_hooks :after_archive
+ true
end
- product.publish_event('product.archived')
+ product.publish_event('product.archived') if moved
success(product)
end
diff --git a/spree/core/app/workflows/spree/products/draft.rb b/spree/core/app/workflows/spree/products/draft.rb
index 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 Spree
run_hooks :validate
- ApplicationRecord.transaction do
+ moved = ApplicationRecord.transaction do
+ next false if already_in?('draft')
+
step :mark_draft
step :withdraw_submission
run_hooks :after_draft
+ true
end
# 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 moved
success(product)
end
diff --git a/spree/core/app/workflows/spree/products/status_transition.rb b/spree/core/app/workflows/spree/products/status_transition.rb
new file mode 100644
index 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
+end
diff --git a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
index 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' do
described_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' do
before { Spree.hooks.clear! }
after { Spree.hooks.clear! }
@@ -202,6 +236,14 @@ RSpec.describe 'Spree::Products status workflows' do
described_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')
+ end
end
describe Spree::Products::Draft do
@@ -210,5 +252,13 @@ RSpec.describe 'Spree::Products status workflows' do
it 'returns the product to draft' do
expect { 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')
+ end
end
end