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: resolved stock locations and reasons on order writes through the current store
Damian Legawiecd869ec901c04
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/orders/fulfillment_actions.rb b/spree/api/app/controllers/concerns/spree/api/v3/orders/fulfillment_actions.rbindex 3c700a5d9a..4f02bd8071 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/orders/fulfillment_actions.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/orders/fulfillment_actions.rb@@ -135,9 +135,17 @@ module Spreeend# The attributes handed to the update workflow. The workflow assigns- # ids raw, so a branch that must narrow one resolves it here first.+ # ids raw, so the stock location is resolved through the current+ # store first; a branch that must narrow it further overrides this.def update_attributes- permitted_params.to_h+ attributes = permitted_params.to_h++ if attributes['stock_location_id'].present?+ attributes['stock_location_id'] =+ current_store.stock_locations.find_by_prefix_id!(attributes['stock_location_id']).id+ end++ attributesend# What may be split out of this parcel, and where the split halfdiff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rbindex 10c55ee35f..c64d887d7d 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb@@ -18,7 +18,7 @@ module Spree## Editable fields only — status moves through the member actions.def update- if @resource.update(permitted_params)+ if @resource.update(update_attributes)render json: serialize_resource(@resource.reload)elserender_validation_error(@resource.errors)@@ -41,6 +41,19 @@ module Spreeprivate+ # The reason is resolved through the store rather than assigned+ # raw, so another store's reads as missing.+ def update_attributes+ attributes = permitted_params.to_h.except('reason_id')++ if params.key?(:reason_id)+ attributes['reason'] =+ (current_store.claim_reasons.find_by_prefix_id!(params[:reason_id]) if params[:reason_id].present?)+ end++ attributes+ end+# The whole store's catalogue: an operator may promise anything# the store sells as a replacement.def replacement_variant_for(variant_id)diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rbindex b2f46ca5e3..33e76d89a0 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb@@ -19,7 +19,7 @@ module Spree## Editable fields only — status moves through the member actions.def update- if @resource.update(permitted_params)+ if @resource.update(update_attributes)render json: serialize_resource(@resource.reload)elserender_validation_error(@resource.errors)@@ -42,6 +42,27 @@ module Spreeprivate+ # The ids are resolved through the store rather than assigned raw,+ # so another store's reason or warehouse reads as missing.+ def update_attributes+ attributes = permitted_params.to_h.except('reason_id', 'stock_location_id')++ if params.key?(:reason_id)+ attributes['reason'] =+ (current_store.return_reasons.find_by_prefix_id!(params[:reason_id]) if params[:reason_id].present?)+ end++ if params.key?(:stock_location_id)+ attributes['stock_location'] =+ if params[:stock_location_id].present?+ current_store.stock_locations.accessible_by(current_ability, :show).+ find_by_prefix_id!(params[:stock_location_id])+ end+ end++ attributes+ end+# The whole store's catalogue, and any warehouse the caller may# see: an operator exchanges into anything the store sells.def replacement_variant_for(variant_id)diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rbindex 45d00b2439..4639638129 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb@@ -113,7 +113,7 @@ module Spreeenddef find_stock_location!(id)- Spree::StockLocation.accessible_by(current_ability, :show).find_by_prefix_id!(id)+ current_store.stock_locations.accessible_by(current_ability, :show).find_by_prefix_id!(id)enddef stock_location_for_creatediff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rbindex ff76667399..2c423cc2f0 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb@@ -25,7 +25,7 @@ module Spree## Editable fields only — status moves through the member actions.def update- if @resource.update(permitted_params)+ if @resource.update(update_attributes)render json: serialize_resource(@resource.reload)elserender_validation_error(@resource.errors)@@ -55,6 +55,27 @@ module Spreeprivate+ # The ids are resolved through the store rather than assigned raw,+ # so another store's reason or warehouse reads as missing.+ def update_attributes+ attributes = permitted_params.to_h.except('reason_id', 'stock_location_id')++ if params.key?(:reason_id)+ attributes['reason'] =+ (current_store.return_reasons.find_by_prefix_id!(params[:reason_id]) if params[:reason_id].present?)+ end++ if params.key?(:stock_location_id)+ attributes['stock_location'] =+ if params[:stock_location_id].present?+ current_store.stock_locations.accessible_by(current_ability, :show).+ find_by_prefix_id!(params[:stock_location_id])+ end+ end++ attributes+ end+# Any of the store's warehouses the caller may see.def stock_location_for_createreturn nil if create_params[:stock_location_id].blank?diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rbindex a5f826c5d4..8b9ef618e3 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/claims_controller_spec.rb@@ -49,6 +49,17 @@ RSpec.describe Spree::Api::V3::Admin::Orders::ClaimsController, type: :controlleendend+ describe 'PATCH #update' do+ it "refuses another store's reason" do+ claim = create(:claim, order: order)++ patch :update, params: { order_id: order.prefixed_id, id: claim.prefixed_id,+ reason_id: create(:claim_reason, store: create(:store)).prefixed_id }, as: :json++ expect(response).to have_http_status(:not_found)+ end+ end+describe 'PATCH #approve' doit 'approves an open claim' doclaim = create(:claim, store: store, order: order)diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rbindex 9e719b68d8..1c3413eb37 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/exchanges_controller_spec.rb@@ -53,6 +53,22 @@ RSpec.describe Spree::Api::V3::Admin::Orders::ExchangesController, type: :controendend+ describe 'PATCH #update' do+ let(:exchange) { create(:exchange, order: order) }++ it "refuses another store's reason or warehouse" do+ other_store = create(:store)++ patch :update, params: { order_id: order.prefixed_id, id: exchange.prefixed_id,+ reason_id: create(:return_reason, store: other_store).prefixed_id }, as: :json+ expect(response).to have_http_status(:not_found)++ patch :update, params: { order_id: order.prefixed_id, id: exchange.prefixed_id,+ stock_location_id: create(:stock_location, store: other_store).prefixed_id }, as: :json+ expect(response).to have_http_status(:not_found)+ end+ end+describe 'PATCH #approve' doit 'approves a requested exchange' doexchange = create(:exchange, store: store, order: order)diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rbindex 499e011ce5..b1b6009596 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb@@ -9,6 +9,8 @@ RSpec.describe Spree::Api::V3::Admin::Orders::FulfillmentsController, type: :conlet!(:order) { create(:order_ready_to_ship, store: store) }let!(:shipment) { order.fulfillments.first }+ let(:foreign_stock_location) { create(:stock_location, store: create(:store)) }+before { request.headers.merge!(headers) }describe 'GET #index' do@@ -148,7 +150,29 @@ RSpec.describe Spree::Api::V3::Admin::Orders::FulfillmentsController, type: :conendend+ describe 'POST #create with a stock location of another store' do+ it 'is not found' do+ post :create, params: {+ order_id: order.prefixed_id,+ stock_location_id: foreign_stock_location.prefixed_id+ }, as: :json++ expect(response).to have_http_status(:not_found)+ end+ end+describe 'PATCH #update' do+ it 'refuses a stock location of another store' do+ patch :update, params: {+ order_id: order.prefixed_id,+ id: shipment.prefixed_id,+ stock_location_id: foreign_stock_location.prefixed_id+ }, as: :json++ expect(response).to have_http_status(:not_found)+ expect(shipment.reload.stock_location).not_to eq(foreign_stock_location)+ end+it 'updates fulfillment tracking' dopatch :update, params: {order_id: order.prefixed_id,@@ -360,5 +384,19 @@ RSpec.describe Spree::Api::V3::Admin::Orders::FulfillmentsController, type: :conexpect(response).to have_http_status(:ok), "Expected 200 but got #{response.status}: #{response.body}"expect(json_response['data']).to be_an(Array)end++ it 'refuses a stock location of another store' do+ variant = shipment.inventory_units.first.variant++ patch :split, params: {+ order_id: order.prefixed_id,+ id: shipment.prefixed_id,+ variant_id: variant.prefixed_id,+ quantity: 1,+ stock_location_id: foreign_stock_location.prefixed_id+ }, as: :json++ expect(response).to have_http_status(:not_found)+ endendenddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rbindex 3bf2f5a370..b4fec3e091 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/returns_controller_spec.rb@@ -71,6 +71,29 @@ RSpec.describe Spree::Api::V3::Admin::Orders::ReturnsController, type: :controllexpect(json_response['memo']).to eq('Updated')expect(json_response['status']).to eq('requested')end++ it "refuses another store's reason or warehouse" do+ return_record = create_return+ other_store = create(:store)++ patch :update, params: { order_id: order.prefixed_id, id: return_record.prefixed_id,+ reason_id: create(:return_reason, store: other_store).prefixed_id }, as: :json+ expect(response).to have_http_status(:not_found)++ patch :update, params: { order_id: order.prefixed_id, id: return_record.prefixed_id,+ stock_location_id: create(:stock_location, store: other_store).prefixed_id }, as: :json+ expect(response).to have_http_status(:not_found)+ end++ it "sets the store's own reason" do+ return_record = create_return+ reason = create(:return_reason, store: store)++ patch :update, params: { order_id: order.prefixed_id, id: return_record.prefixed_id, reason_id: reason.prefixed_id }, as: :json++ expect(response).to have_http_status(:ok)+ expect(return_record.reload.reason).to eq(reason)+ endenddescribe 'PATCH #approve' do