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: 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.rb
index 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 Spree
end
# 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
+
+ attributes
end
# What may be split out of this parcel, and where the split half
diff --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.rb
index 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)
else
render_validation_error(@resource.errors)
@@ -41,6 +41,19 @@ module Spree
private
+ # 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.rb
index 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)
else
render_validation_error(@resource.errors)
@@ -42,6 +42,27 @@ module Spree
private
+ # 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.rb
index 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 Spree
end
def 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)
end
def stock_location_for_create
diff --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.rb
index 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)
else
render_validation_error(@resource.errors)
@@ -55,6 +55,27 @@ module Spree
private
+ # 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_create
return 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.rb
index 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: :controlle
end
end
+ 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' do
it 'approves an open claim' do
claim = 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.rb
index 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: :contro
end
end
+ 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' do
it 'approves a requested exchange' do
exchange = 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.rb
index 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: :con
let!(: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: :con
end
end
+ 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' do
patch :update, params: {
order_id: order.prefixed_id,
@@ -360,5 +384,19 @@ RSpec.describe Spree::Api::V3::Admin::Orders::FulfillmentsController, type: :con
expect(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)
+ end
end
end
diff --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.rb
index 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: :controll
expect(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)
+ end
end
describe 'PATCH #approve' do