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
Let only one caller at a time ship or cancel a transfer
Damian Legawiec3691d8e7b3b4
diff --git a/spree/core/app/models/concerns/spree/receivable_item.rb b/spree/core/app/models/concerns/spree/receivable_item.rbindex 8f8e9ca54f..23a811d13e 100644--- a/spree/core/app/models/concerns/spree/receivable_item.rb+++ b/spree/core/app/models/concerns/spree/receivable_item.rb@@ -67,7 +67,7 @@ module Spree# caller sent: a receive of twelve against ten shipped is a miscount at# the receiving dock, not a retrospective error in what left.def received_does_not_exceed_expected- return if quantity_received.blank? || quantity_expected.zero?+ return if quantity_received.blank?return if quantity_received.to_i <= quantity_expectederrors.add(:quantity_received, :cannot_exceed_expected,diff --git a/spree/core/app/workflows/spree/stock_transfers/cancel.rb b/spree/core/app/workflows/spree/stock_transfers/cancel.rbindex 912f91d0c2..01731b8fd9 100644--- a/spree/core/app/workflows/spree/stock_transfers/cancel.rb+++ b/spree/core/app/workflows/spree/stock_transfers/cancel.rb@@ -25,10 +25,15 @@ module Spreedef perform(stock_transfer:, on_in_transit: nil, reason: nil, canceler: nil)super- step :ensure_cancelable- run_hooks :validate+ # One cancellation at a time per document, and no receive alongside it.+ # Each line's outstanding quantity is what is still in flight, so a+ # receive committing between reading it and restocking the source+ # credits the same units twice — once onto the destination shelf, once+ # back onto the source. Locked document-then-level, as `Receive` does.+ stock_transfer.with_lock do+ step :ensure_cancelable+ run_hooks :validate- ApplicationRecord.transaction dostep :resolve_in_flight_unitsstep :mark_canceledend@@ -53,7 +58,10 @@ module Spreedef resolve_in_flight_unitsreturn unless stock_transfer.in_flight?- stock_transfer.items.each do |item|+ # Read the running totals under the lock: an association loaded before+ # it holds the quantities as they were before any receive that has+ # since committed, and those are what `outstanding` subtracts from.+ stock_transfer.items.reload.each do |item|next unless item.outstanding.positive?on_in_transit == 'restock' ? restock(item) : write_off(item)diff --git a/spree/core/app/workflows/spree/stock_transfers/mark_in_transit.rb b/spree/core/app/workflows/spree/stock_transfers/mark_in_transit.rbindex d7db079f28..bf2f92e58b 100644--- a/spree/core/app/workflows/spree/stock_transfers/mark_in_transit.rb+++ b/spree/core/app/workflows/spree/stock_transfers/mark_in_transit.rb@@ -18,11 +18,17 @@ module Spreedef perform(stock_transfer:, force: false)super- step :ensure_shippable- step :ensure_source_has_stock unless force- run_hooks :validate+ # One departure at a time per document. The status check is what stops+ # a transfer shipping twice, so reading it outside the lock lets two+ # callers — a double-clicked button, a retried request — both see+ # `draft` and both write a full set of `shipped` movements, taking the+ # source shelf down twice for one trip. Locked document-then-level, the+ # same order `Receive` uses, so the two cannot deadlock.+ stock_transfer.with_lock do+ step :ensure_shippable+ step :ensure_source_has_stock unless force+ run_hooks :validate- ApplicationRecord.transaction dorun_hooks :before_unstockstep :write_shipped_movementsstep :mark_in_transit