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 API V2 Platform products by price sorting (#11276)

Szymon Iwacz11a3ea821f5a
diff --git a/api/app/controllers/spree/api/v2/platform/products_controller.rb b/api/app/controllers/spree/api/v2/platform/products_controller.rb
index 8f366c67b5..e808b1d209 100644
--- a/api/app/controllers/spree/api/v2/platform/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/products_controller.rb
@@ -18,6 +18,14 @@ module Spree
def allowed_sort_attributes
super << :available_on
end
+
+ def sorted_collection
+ collection_sorter.new(collection, current_currency, params, allowed_sort_attributes).call
+ end
+
+ def collection_sorter
+ Spree::Api::Dependencies.platform_products_sorter.constantize
+ end
end
end
end
diff --git a/api/app/models/spree/api_dependencies.rb b/api/app/models/spree/api_dependencies.rb
index 590a05bdad..ec5cb39bb0 100644
--- a/api/app/models/spree/api_dependencies.rb
+++ b/api/app/models/spree/api_dependencies.rb
@@ -19,7 +19,7 @@ module Spree
:storefront_store_serializer, :storefront_address_serializer, :storefront_order_serializer,
:storefront_account_create_address_service, :storefront_account_update_address_service, :storefront_address_finder,
:storefront_account_create_service, :storefront_account_update_service, :storefront_collection_sorter, :error_handler,
- :storefront_cart_empty_service, :storefront_cart_destroy_service, :storefront_credit_cards_destroy_service
+ :storefront_cart_empty_service, :storefront_cart_destroy_service, :storefront_credit_cards_destroy_service, :platform_products_sorter
].freeze
attr_accessor *INJECTION_POINTS
@@ -85,6 +85,7 @@ module Spree
@storefront_collection_sorter = Spree::Dependencies.collection_sorter
@storefront_order_sorter = Spree::Dependencies.collection_sorter
@storefront_products_sorter = Spree::Dependencies.products_sorter
+ @platform_products_sorter = Spree::Dependencies.products_sorter
# paginators
@storefront_collection_paginator = Spree::Dependencies.collection_paginator
diff --git a/api/spec/requests/spree/api/v2/platform/products_spec.rb b/api/spec/requests/spree/api/v2/platform/products_spec.rb
index 00c1503ec4..57308978d7 100644
--- a/api/spec/requests/spree/api/v2/platform/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/platform/products_spec.rb
@@ -73,6 +73,8 @@ describe 'API V2 Platform Products Spec' do
context 'sort products' do
context 'sorting by price' do
+ before { store.products.each_with_index { |p, i| p.update(price: p.price + i) } }
+
context 'ascending order' do
before { get '/api/v2/platform/products?sort=price', headers: bearer_token }