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.

API v2 include Stock Location relationship in Checkout Shipping Rates endpoint

Damian Legawiecedc78381dd82
diff --git a/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb b/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
index e87bed1d39..11c8056eeb 100644
--- a/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
@@ -125,7 +125,7 @@ module Spree
def serialize_shipping_rates(shipments)
shipping_rates_serializer.new(
shipments,
- include: [:shipping_rates],
+ include: [:shipping_rates, :stock_location],
params: { show_rates: true }
).serializable_hash
end
diff --git a/api/app/serializers/spree/v2/storefront/shipment_serializer.rb b/api/app/serializers/spree/v2/storefront/shipment_serializer.rb
index d7a2c8cf4b..3b4f6a1d60 100644
--- a/api/app/serializers/spree/v2/storefront/shipment_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/shipment_serializer.rb
@@ -10,6 +10,8 @@ module Spree
attribute :free, &:free?
has_many :shipping_rates, if: proc { |_record, params| params&.dig(:show_rates) }
+
+ belongs_to :stock_location
end
end
end
diff --git a/api/app/serializers/spree/v2/storefront/stock_location_serializer.rb b/api/app/serializers/spree/v2/storefront/stock_location_serializer.rb
new file mode 100644
index 0000000000..4eaa967491
--- /dev/null
+++ b/api/app/serializers/spree/v2/storefront/stock_location_serializer.rb
@@ -0,0 +1,11 @@
+module Spree
+ module V2
+ module Storefront
+ class StockLocationSerializer < BaseSerializer
+ set_type :stock_location
+
+ attributes :name
+ end
+ end
+ end
+end
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index 328706ae64..71726829ef 100644
--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
@@ -607,7 +607,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
expect(json_response['data'][0]).to have_type('shipment')
expect(json_response['data'][0]).to have_relationships(:shipping_rates)
expect(json_response['included']).to be_present
- expect(json_response['included'].size).to eq(shipment.shipping_rates.count)
+ expect(json_response['included'].size).to eq(shipment.shipping_rates.count + 1)
shipment.shipping_rates.each do |shipping_rate|
expect(json_response['included']).to include(have_type('shipping_rate').and have_id(shipping_rate.id.to_s))
end
@@ -623,6 +623,9 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
expect(json_response['included'][0]).to have_attribute(:shipping_method_id).with_value(shipping_method.id)
expect(json_response['included'][0]).to have_attribute(:selected).with_value(shipping_rate.selected)
expect(json_response['included'][0]).to have_attribute(:free).with_value(shipping_rate.free?)
+
+ expect(json_response['included']).to include(have_type('stock_location').and have_id(shipment.stock_location_id.to_s))
+ expect(json_response['included']).to include(have_type('stock_location').and have_attribute(:name).with_value(shipment.stock_location.name))
end
end