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.

Phase P4.2: Let a client label a stock correction

Łukasz Adamczyke109227a07e1
diff --git a/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb
index f4a75de9f5..f0f0cf8a89 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/stock_levels_controller.rb
@@ -62,8 +62,12 @@ module Spree
# Stock levels are auto-created against a (variant, stock_location)
# pair and never re-pointed, so update only touches the count and
# backorder flag — not the variant or location FKs.
+ #
+ # `reason` labels the correction in the stock history. It stays
+ # optional so an audit requirement never turns an existing endpoint
+ # into a 422; omitting it falls back to a translated default.
def permitted_params
- params.permit(:count_on_hand, :backorderable, metadata: {})
+ params.permit(:count_on_hand, :backorderable, :reason, metadata: {})
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb
index b3649ee957..836ac5ab95 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_controller_spec.rb
@@ -51,6 +51,14 @@ RSpec.describe Spree::Api::V3::Admin::StockLevelsController, type: :controller d
expect(movement.quantity).to eq(42 - count_before)
end
+ it 'stores a client-supplied reason on the movement' do
+ patch :update, params: { id: stock_level.prefixed_id, count_on_hand: 42, reason: 'Damaged in transit' }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(stock_level.reload.count_on_hand).to eq(42)
+ expect(stock_level.stock_movements.adjusted.last.reason).to eq('Damaged in transit')
+ end
+
it 'labels an unlabelled correction in English' do
patch :update, params: { id: stock_level.prefixed_id, count_on_hand: 42 }, as: :json