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: a refused order edit no longer saves part of the changes (#14753)

Łukasz Adamczyk07b7aa2c921f
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
index 692660eee0..8109f700a5 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
@@ -685,6 +685,25 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller do
expect(order.reload.line_items).to contain_exactly(kept)
expect(kept.reload.quantity).to eq(3)
end
+
+ it 'applies none of the changes when one line is refused' do
+ unavailable = create(:variant)
+ unavailable.stock_levels.update_all(count_on_hand: 0, backorderable: false)
+
+ patch :update, params: {
+ id: order.prefixed_id,
+ items: [
+ { variant_id: kept.variant.prefixed_id, quantity: 3 },
+ { variant_id: removed.variant.prefixed_id, quantity: 0 },
+ { variant_id: unavailable.prefixed_id, quantity: 5 }
+ ]
+ }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(response.body).to include('is not available')
+ expect(order.reload.line_items).to contain_exactly(kept, removed)
+ expect(kept.reload.quantity).to eq(1)
+ end
end
# Staff assigning the business an order is for. A buyer with several
diff --git a/spree/core/app/services/spree/orders/update.rb b/spree/core/app/services/spree/orders/update.rb
index dc644b766b..21144e94bd 100644
--- a/spree/core/app/services/spree/orders/update.rb
+++ b/spree/core/app/services/spree/orders/update.rb
@@ -15,7 +15,10 @@ module Spree
items_param = @params.delete(:items)
address_params = extract_address_params
- ApplicationRecord.transaction do
+ # requires_new: the rescue below sits outside the block, so joining a
+ # caller's open transaction (the API's order lock) would let it commit
+ # the half-applied edit.
+ ApplicationRecord.transaction(requires_new: true) do
ship_address_id_before = @order.ship_address_id
assign_addresses(address_params)
diff --git a/spree/core/spec/services/spree/orders/update_spec.rb b/spree/core/spec/services/spree/orders/update_spec.rb
index 43c80a53c8..6cc802f7e5 100644
--- a/spree/core/spec/services/spree/orders/update_spec.rb
+++ b/spree/core/spec/services/spree/orders/update_spec.rb
@@ -89,6 +89,14 @@ module Spree
expect(order.reload.email).not_to eq('[email protected]')
expect(order.line_items.count).to eq(0)
end
+
+ it 'rolls back the entire update when the caller holds the order lock' do
+ result = nil
+ order.with_lock { result = described_class.call(order: order, params: params) }
+
+ expect(result).to be_failure
+ expect(order.reload.email).not_to eq('[email protected]')
+ end
end
context 'with invalid variant in items' do