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.

Added product sorting in Storefront/Platform API by created_at/updated_at/available_on (#11266)

Szymon Iwacz2b36ff5965f2
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 843ccc03a9..8f366c67b5 100644
--- a/api/app/controllers/spree/api/v2/platform/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/products_controller.rb
@@ -14,6 +14,10 @@ module Spree
def scope_includes
product_list_includes
end
+
+ def allowed_sort_attributes
+ super << :available_on
+ end
end
end
end
diff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
index 946e5b6d36..ffb252b1f8 100644
--- a/api/app/controllers/spree/api/v2/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
@@ -42,6 +42,10 @@ module Spree
def scope_includes
product_list_includes
end
+
+ def allowed_sort_attributes
+ super << :available_on
+ end
end
end
end
diff --git a/api/docs/v2/platform/index.yaml b/api/docs/v2/platform/index.yaml
index f4c03de280..8f0666c3d6 100644
--- a/api/docs/v2/platform/index.yaml
+++ b/api/docs/v2/platform/index.yaml
@@ -57,8 +57,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T13:45:53.182Z'
- updated_at: '2021-08-18T13:45:53.182Z'
+ created_at: '2021-08-20T11:02:26.158Z'
+ updated_at: '2021-08-20T11:02:26.158Z'
deleted_at:
label:
relationships:
@@ -85,8 +85,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T13:45:53.188Z'
- updated_at: '2021-08-18T13:45:53.188Z'
+ created_at: '2021-08-20T11:02:26.166Z'
+ updated_at: '2021-08-20T11:02:26.166Z'
deleted_at:
label:
relationships:
@@ -154,8 +154,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T13:45:53.540Z'
- updated_at: '2021-08-18T13:45:53.540Z'
+ created_at: '2021-08-20T11:02:26.535Z'
+ updated_at: '2021-08-20T11:02:26.535Z'
deleted_at:
label:
relationships:
@@ -243,8 +243,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T13:45:53.599Z'
- updated_at: '2021-08-18T13:45:53.599Z'
+ created_at: '2021-08-20T11:02:26.609Z'
+ updated_at: '2021-08-20T11:02:26.609Z'
deleted_at:
label:
relationships:
@@ -315,8 +315,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T13:45:53.677Z'
- updated_at: '2021-08-18T13:45:53.688Z'
+ created_at: '2021-08-20T11:02:26.706Z'
+ updated_at: '2021-08-20T11:02:26.721Z'
deleted_at:
label:
relationships:
@@ -439,8 +439,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T13:45:54.112Z'
- updated_at: '2021-08-18T13:45:54.112Z'
+ created_at: '2021-08-20T11:02:27.225Z'
+ updated_at: '2021-08-20T11:02:27.225Z'
relationships:
product:
data:
@@ -454,8 +454,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T13:45:54.187Z'
- updated_at: '2021-08-18T13:45:54.187Z'
+ created_at: '2021-08-20T11:02:27.336Z'
+ updated_at: '2021-08-20T11:02:27.336Z'
relationships:
product:
data:
@@ -510,8 +510,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T13:45:54.545Z'
- updated_at: '2021-08-18T13:45:54.545Z'
+ created_at: '2021-08-20T11:02:27.775Z'
+ updated_at: '2021-08-20T11:02:27.775Z'
relationships:
product:
data:
@@ -572,8 +572,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T13:45:54.675Z'
- updated_at: '2021-08-18T13:45:54.675Z'
+ created_at: '2021-08-20T11:02:27.921Z'
+ updated_at: '2021-08-20T11:02:27.921Z'
relationships:
product:
data:
@@ -631,8 +631,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T13:45:54.951Z'
- updated_at: '2021-08-18T13:45:54.951Z'
+ created_at: '2021-08-20T11:02:28.202Z'
+ updated_at: '2021-08-20T11:02:28.202Z'
relationships:
product:
data:
@@ -738,8 +738,8 @@ paths:
type: classification
attributes:
position: 2
- created_at: '2021-08-18T13:45:55.595Z'
- updated_at: '2021-08-18T13:45:55.614Z'
+ created_at: '2021-08-20T11:02:28.904Z'
+ updated_at: '2021-08-20T11:02:28.922Z'
relationships:
product:
data:
@@ -804,9 +804,9 @@ paths:
name: United States of America
numcode: 840
states_required: true
- updated_at: '2021-08-18T13:45:55.899Z'
+ updated_at: '2021-08-20T11:02:29.220Z'
zipcode_required: true
- created_at: '2021-08-18T13:45:55.899Z'
+ created_at: '2021-08-20T11:02:29.220Z'
relationships:
states:
data: []
@@ -819,9 +819,9 @@ paths:
name: NAME_2
numcode: 840
states_required: false
- updated_at: '2021-08-18T13:45:55.908Z'
+ updated_at: '2021-08-20T11:02:29.229Z'
zipcode_required: true
- created_at: '2021-08-18T13:45:55.908Z'
+ created_at: '2021-08-20T11:02:29.229Z'
relationships:
states:
data: []
@@ -834,9 +834,9 @@ paths:
name: NAME_3
numcode: 840
states_required: false
- updated_at: '2021-08-18T13:45:55.910Z'
+ updated_at: '2021-08-20T11:02:29.231Z'
zipcode_required: true
- created_at: '2021-08-18T13:45:55.910Z'
+ created_at: '2021-08-20T11:02:29.231Z'
relationships:
states:
data: []
@@ -889,9 +889,9 @@ paths:
name: NAME_6
numcode: 840
states_required: false
- updated_at: '2021-08-18T13:45:55.975Z'
+ updated_at: '2021-08-20T11:02:29.297Z'
zipcode_required: true
- created_at: '2021-08-18T13:45:55.975Z'
+ created_at: '2021-08-20T11:02:29.297Z'
relationships:
states:
data: []
@@ -957,8 +957,8 @@ paths:
name: foo-size-1
presentation: Size
position: 1
- created_at: '2021-08-18T13:45:56.048Z'
- updated_at: '2021-08-18T13:45:56.048Z'
+ created_at: '2021-08-20T11:02:29.373Z'
+ updated_at: '2021-08-20T11:02:29.373Z'
filterable: true
relationships:
option_values:
@@ -969,8 +969,8 @@ paths:
name: foo-size-2
presentation: Size
position: 2
- created_at: '2021-08-18T13:45:56.050Z'
- updated_at: '2021-08-18T13:45:56.050Z'
+ created_at: '2021-08-20T11:02:29.375Z'
+ updated_at: '2021-08-20T11:02:29.375Z'
filterable: true
relationships:
option_values:
@@ -1022,8 +1022,8 @@ paths:
name: foo-size-5
presentation: Size
position: 1
- created_at: '2021-08-18T13:45:56.126Z'
- updated_at: '2021-08-18T13:45:56.126Z'
+ created_at: '2021-08-20T11:02:29.453Z'
+ updated_at: '2021-08-20T11:02:29.453Z'
filterable: true
relationships:
option_values:
@@ -1081,8 +1081,8 @@ paths:
name: foo-size-6
presentation: Size
position: 1
- created_at: '2021-08-18T13:45:56.169Z'
- updated_at: '2021-08-18T13:45:56.169Z'
+ created_at: '2021-08-20T11:02:29.497Z'
+ updated_at: '2021-08-20T11:02:29.497Z'
filterable: true
relationships:
option_values:
@@ -1137,8 +1137,8 @@ paths:
name: Size-X
presentation: Size
position: 1
- created_at: '2021-08-18T13:45:56.247Z'
- updated_at: '2021-08-18T13:45:56.257Z'
+ created_at: '2021-08-20T11:02:29.580Z'
+ updated_at: '2021-08-20T11:02:29.591Z'
filterable: true
relationships:
option_values:
@@ -1252,8 +1252,8 @@ paths:
position: 1
name: Size-1
presentation: S
- created_at: '2021-08-18T13:45:56.421Z'
- updated_at: '2021-08-18T13:45:56.421Z'
+ created_at: '2021-08-20T11:02:29.758Z'
+ updated_at: '2021-08-20T11:02:29.758Z'
relationships:
option_type:
data:
@@ -1265,8 +1265,8 @@ paths:
position: 1
name: Size-2
presentation: S
- created_at: '2021-08-18T13:45:56.426Z'
- updated_at: '2021-08-18T13:45:56.426Z'
+ created_at: '2021-08-20T11:02:29.763Z'
+ updated_at: '2021-08-20T11:02:29.763Z'
relationships:
option_type:
data:
@@ -1319,8 +1319,8 @@ paths:
position: 1
name: Size-5
presentation: S
- created_at: '2021-08-18T13:45:56.507Z'
- updated_at: '2021-08-18T13:45:56.507Z'
+ created_at: '2021-08-20T11:02:29.842Z'
+ updated_at: '2021-08-20T11:02:29.842Z'
relationships:
option_type:
data:
@@ -1377,8 +1377,8 @@ paths:
position: 1
name: Size-6
presentation: S
- created_at: '2021-08-18T13:45:56.554Z'
- updated_at: '2021-08-18T13:45:56.554Z'
+ created_at: '2021-08-20T11:02:29.890Z'
+ updated_at: '2021-08-20T11:02:29.890Z'
relationships:
option_type:
data:
@@ -1434,8 +1434,8 @@ paths:
position: 1
name: M
presentation: S
- created_at: '2021-08-18T13:45:56.630Z'
- updated_at: '2021-08-18T13:45:56.640Z'
+ created_at: '2021-08-20T11:02:29.959Z'
+ updated_at: '2021-08-20T11:02:29.973Z'
relationships:
option_type:
data:
@@ -1553,8 +1553,8 @@ paths:
lft: 1
rgt: 6
description:
- created_at: '2021-08-18T13:45:56.837Z'
- updated_at: '2021-08-18T13:45:56.897Z'
+ created_at: '2021-08-20T11:02:30.161Z'
+ updated_at: '2021-08-20T11:02:30.221Z'
meta_title:
meta_description:
meta_keywords:
@@ -1588,8 +1588,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T13:45:56.860Z'
- updated_at: '2021-08-18T13:45:56.863Z'
+ created_at: '2021-08-20T11:02:30.183Z'
+ updated_at: '2021-08-20T11:02:30.187Z'
meta_title:
meta_description:
meta_keywords:
@@ -1623,8 +1623,8 @@ paths:
lft: 4
rgt: 5
description:
- created_at: '2021-08-18T13:45:56.884Z'
- updated_at: '2021-08-18T13:45:56.888Z'
+ created_at: '2021-08-20T11:02:30.208Z'
+ updated_at: '2021-08-20T11:02:30.212Z'
meta_title:
meta_description:
meta_keywords:
@@ -1699,8 +1699,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T13:45:57.054Z'
- updated_at: '2021-08-18T13:45:57.058Z'
+ created_at: '2021-08-20T11:02:30.393Z'
+ updated_at: '2021-08-20T11:02:30.397Z'
meta_title:
meta_description:
meta_keywords:
@@ -1779,8 +1779,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T13:45:57.148Z'
- updated_at: '2021-08-18T13:45:57.151Z'
+ created_at: '2021-08-20T11:02:30.484Z'
+ updated_at: '2021-08-20T11:02:30.488Z'
meta_title:
meta_description:
meta_keywords:
@@ -1860,8 +1860,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T13:45:57.300Z'
- updated_at: '2021-08-18T13:45:57.324Z'
+ created_at: '2021-08-20T11:02:30.652Z'
+ updated_at: '2021-08-20T11:02:30.675Z'
meta_title:
meta_description:
meta_keywords:
@@ -1992,9 +1992,9 @@ paths:
- id: '1'
type: user
attributes:
- created_at: '2021-08-18T13:45:57.670Z'
- updated_at: '2021-08-18T13:45:57.670Z'
+ created_at: '2021-08-20T11:02:31.076Z'
+ updated_at: '2021-08-20T11:02:31.076Z'
average_order_value: []
lifetime_value: []
store_credits: []
@@ -2006,9 +2006,9 @@ paths:
- id: '2'
type: user
attributes:
- created_at: '2021-08-18T13:45:57.680Z'
- updated_at: '2021-08-18T13:45:57.680Z'
+ created_at: '2021-08-20T11:02:31.087Z'
+ updated_at: '2021-08-20T11:02:31.087Z'
average_order_value: []
lifetime_value: []
store_credits: []
@@ -2020,9 +2020,9 @@ paths:
- id: '3'
type: user
attributes:
- created_at: '2021-08-18T13:45:57.682Z'
- updated_at: '2021-08-18T13:45:57.682Z'
+ created_at: '2021-08-20T11:02:31.090Z'
+ updated_at: '2021-08-20T11:02:31.090Z'
average_order_value: []
lifetime_value: []
store_credits: []
@@ -2075,9 +2075,9 @@ paths:
id: '2'
type: user
attributes:
- created_at: '2021-08-18T13:45:57.800Z'
- updated_at: '2021-08-18T13:45:57.800Z'
+ created_at: '2021-08-20T11:02:31.214Z'
+ updated_at: '2021-08-20T11:02:31.214Z'
average_order_value: []
lifetime_value: []
store_credits: []
@@ -2134,9 +2134,9 @@ paths:
id: '2'
type: user
attributes:
- created_at: '2021-08-18T13:45:57.881Z'
- updated_at: '2021-08-18T13:45:57.881Z'
+ created_at: '2021-08-20T11:02:31.290Z'
+ updated_at: '2021-08-20T11:02:31.290Z'
average_order_value: []
lifetime_value: []
store_credits: []
@@ -2193,8 +2193,8 @@ paths:
type: user
attributes:
- created_at: '2021-08-18T13:45:57.992Z'
- updated_at: '2021-08-18T13:45:58.002Z'
+ created_at: '2021-08-20T11:02:31.401Z'
+ updated_at: '2021-08-20T11:02:31.417Z'
average_order_value: []
lifetime_value: []
store_credits: []
@@ -2337,6 +2337,59 @@ components:
required:
- name
- presentation
+ product_params:
+ type: object
+ properties:
+ name:
+ type: string
+ description:
+ type: string
+ available_on:
+ type: string
+ discontinue_on:
+ type: string
+ permalink:
+ type: string
+ meta_description:
+ type: string
+ meta_keywords:
+ type: string
+ price:
+ type: string
+ sku:
+ type: string
+ deleted_at:
+ type: string
+ prototype_id:
+ type: string
+ option_values_hash:
+ type: string
+ weight:
+ type: string
+ height:
+ type: string
+ width:
+ type: string
+ depth:
+ type: string
+ shipping_category_id:
+ type: string
+ tax_category_id:
+ type: string
+ cost_currency:
+ type: string
+ cost_price:
+ type: string
+ compare_at_price:
+ type: string
+ option_type_ids:
+ type: string
+ taxon_ids:
+ type: string
+ required:
+ - name
+ - price
+ - shipping_category_id
user_params:
type: object
properties:
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index b831894687..6199bb0fa0 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -1208,13 +1208,15 @@ paths:
name: sort
schema:
type: string
- example: '-updated_at,price,-name'
+ example: '-updated_at,price,-name,created_at,-available_on'
description: |-
Sort products based on: <ul>
- <li>price (ascending/descenging)</li>
- <li>updated_at (ascending/descenging)</li>
- <li>name (ascending/descenging)</li>
- </ul> Use <q>-</q> sign to set descenging sort, eg. <q>-updated_at</q>
+ <li>name (ascending/descending)</li>
+ <li>price (ascending/descending)</li>
+ <li>available_on (ascending/descending)</li>
+ <li>created_at (ascending/descending)</li>
+ <li>updated_at (ascending/descending)</li>
+ </ul> Use <q>-</q> sign to set descending sort, eg. <q>-updated_at</q>
- $ref: '#/components/parameters/PageParam'
- $ref: '#/components/parameters/PerPageParam'
- $ref: '#/components/parameters/ProductIncludeParam'
diff --git a/api/spec/requests/spree/api/v2/platform/products_spec.rb b/api/spec/requests/spree/api/v2/platform/products_spec.rb
new file mode 100644
index 0000000000..103857fc2b
--- /dev/null
+++ b/api/spec/requests/spree/api/v2/platform/products_spec.rb
@@ -0,0 +1,280 @@
+require 'swagger_helper'
+
+describe 'API V2 Platform Products Spec' do
+ include_context 'Platform API v2'
+
+ let(:bearer_token) { { 'Authorization' => valid_authorization } }
+
+ let!(:products) { create_list(:product, 5, stores: [store]) }
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:taxon) { taxonomy.root }
+ let(:product_with_taxon) { create(:product, taxons: [taxon], stores: [store]) }
+ let(:product_with_name) { create(:product, name: 'Test Product', stores: [store]) }
+ let(:product_with_price) { create(:product, price: 13.44, stores: [store]) }
+ let!(:option_type) { create(:option_type) }
+ let!(:option_value) { create(:option_value, option_type: option_type) }
+ let(:product_with_option) { create(:product, option_types: [option_type], stores: [store]) }
+ let!(:variant) { create(:variant, product: product_with_option, option_values: [option_value]) }
+ let(:product) { create(:product, stores: [store]) }
+ let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day, stores: [store]) }
+ let!(:discontinued_product) { create(:product, discontinue_on: Time.current - 1.day, stores: [store]) }
+ let!(:not_available_product) { create(:product, available_on: nil, stores: [store]) }
+ let!(:in_stock_product) { create(:product_in_stock, stores: [store]) }
+ let!(:not_backorderable_product) { create(:product_in_stock, :without_backorder, stores: [store]) }
+ let!(:property) { create(:property) }
+ let!(:new_property) { create(:property) }
+ let!(:product_with_property) { create(:product, properties: [property], stores: [store]) }
+ let!(:product_property) { create(:product_property, property: new_property, product: product_with_property, value: 'Some Value') }
+
+ describe 'products#index' do
+ context 'with no params' do
+ before { get '/api/v2/platform/products', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns all products' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].first).to have_type('product')
+ end
+ end
+
+ context 'when product associated with two stores' do
+ let!(:new_store_taxonomy) { create(:taxonomy, store: store) }
+ let(:store2) { create(:store) }
+ let(:taxonomy2) { create(:taxonomy, store: store2) }
+ let!(:taxon2) { taxonomy2.root }
+
+ before do
+ product_with_taxon.stores << store2
+ product_with_taxon.taxons << taxon2
+ end
+
+ shared_examples 'should not return not related taxon' do
+ it do
+ expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{ 'id' => new_store_taxonomy.id.to_s, 'type' => 'taxon' }])
+ end
+ end
+ end
+
+ context 'current store' do
+ let(:store_2) { create(:store) }
+ let!(:product_from_another_store) { create(:product, stores: [store_2]) }
+
+ before { get '/api/v2/platform/products', headers: bearer_token }
+
+ it 'returns products from this store only' do
+ expect(json_response['data'].count).to eq store.products.count
+ product_ids = json_response['data'].map(&:first).map(&:last)
+
+ expect(product_ids).not_to include(product_from_another_store.id)
+ expect(product_ids).to eq(store.products.ids.map(&:to_s))
+ end
+ end
+
+ context 'sort products' do
+ context 'sorting by price' do
+ context 'ascending order' do
+ before { get '/api/v2/platform/products?sort=price', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by price' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.joins(master: :prices).select("#{store.products.table_name}.*, #{Spree::Price.table_name}.amount").distinct.order("#{Spree::Price.table_name}.amount").map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/platform/products?sort=-price', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by price with descending order' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.joins(master: :prices).select("#{store.products.table_name}.*, #{Spree::Price.table_name}.amount").distinct.order("#{Spree::Price.table_name}.amount DESC").map(&:id).map(&:to_s)
+ end
+ end
+ end
+
+ context 'sorting by name' do
+ context 'A-Z' do
+ before { get '/api/v2/platform/products?sort=name', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by name' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(:name).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'Z-A' do
+ before { get '/api/v2/platform/products?sort=-name', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by name with descending order' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(name: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
+
+ context 'sorting by updated_at' do
+ context 'ascending order' do
+ before { get '/api/v2/platform/products?sort=updated_at', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by updated_at' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(:updated_at).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/platform/products?sort=-updated_at', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by updated_at with descending order' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(updated_at: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
+
+ context 'sorting by created_at' do
+ context 'ascending order' do
+ before { get '/api/v2/platform/products?sort=created_at', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by created_at' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(:created_at).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/platform/products?sort=-created_at', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by created_at with descending order' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(created_at: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
+
+ context 'sorting by available_on' do
+ context 'ascending order' do
+ before { get '/api/v2/platform/products?sort=available_on', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by available_on' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(:available_on).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/platform/products?sort=-available_on', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by available_on with descending order' do
+ expect(json_response['data'].count).to eq store.products.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.order(available_on: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
+ end
+
+ context 'paginate products' do
+ context 'with specified pagination params' do
+ context 'when per_page is between 1 and default value' do
+ before { get '/api/v2/platform/products?page=1&per_page=2', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns the default number of products' do
+ expect(json_response['data'].count).to eq 2
+ end
+
+ it 'returns proper meta data' do
+ expect(json_response['meta']['count']).to eq 2
+ expect(json_response['meta']['total_count']).to eq store.products.count
+ end
+
+ it 'returns proper links data' do
+ expect(json_response['links']['self']).to include('/api/v2/platform/products?page=1&per_page=2')
+ expect(json_response['links']['next']).to include('/api/v2/platform/products?page=2&per_page=2')
+ expect(json_response['links']['prev']).to include('/api/v2/platform/products?page=1&per_page=2')
+ end
+ end
+
+ context 'when per_page is above the default value' do
+ before { get '/api/v2/platform/products?page=1&per_page=8', headers: bearer_token }
+
+ it 'returns the per_page number of products' do
+ expect(json_response['data'].count).to eq 8
+ end
+ end
+
+ context 'when per_page is less than 0' do
+ before { get '/api/v2/platform/products?page=1&per_page=-1', headers: bearer_token }
+
+ it 'returns the max or default number of products' do
+ expect(json_response['data'].count).to eq 11
+ end
+ end
+
+ context 'when per_page is equal 0' do
+ before { get '/api/v2/platform/products?page=1&per_page=0', headers: bearer_token }
+
+ it 'returns the max or default number of products' do
+ expect(json_response['data'].count).to eq 11
+ end
+ end
+ end
+
+ context 'without specified pagination params' do
+ before { get '/api/v2/platform/products', headers: bearer_token }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns specified amount products' do
+ expect(json_response['data'].count).to eq store.products.count
+ end
+
+ it 'returns proper meta data' do
+ expect(json_response['meta']['count']).to eq json_response['data'].count
+ expect(json_response['meta']['total_count']).to eq store.products.count
+ end
+
+ it 'returns proper links data' do
+ expect(json_response['links']['self']).to include('/api/v2/platform/products')
+ expect(json_response['links']['next']).to include('/api/v2/platform/products?page=1')
+ expect(json_response['links']['prev']).to include('/api/v2/platform/products?page=1')
+ end
+ end
+ end
+
+ context 'fetch products by curency param' do
+ before { store.update(supported_currencies: 'USD,EUR,GBP') }
+
+ context 'with default currency' do
+ before { get '/api/v2/platform/products?currency=USD', headers: bearer_token }
+
+ it 'returns products' do
+ expect(json_response['data']).not_to be_empty
+ expect(json_response['data'][0]['attributes']['currency']).to eq 'USD'
+ expect(json_response['data'].count).to eq store.products.count
+ end
+ end
+ end
+ end
+end
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 068f3ba559..02244e1e95 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -401,6 +401,54 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
end
+
+ context 'sorting by created_at' do
+ context 'ascending order' do
+ before { get '/api/v2/storefront/products?sort=created_at' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by created_at' do
+ expect(json_response['data'].count).to eq store.products.available.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.available.order(:created_at).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/storefront/products?sort=-created_at' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by created_at with descending order' do
+ expect(json_response['data'].count).to eq store.products.available.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.available.order(created_at: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
+
+ context 'sorting by available_on' do
+ context 'ascending order' do
+ before { get '/api/v2/storefront/products?sort=available_on' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by available_on' do
+ expect(json_response['data'].count).to eq store.products.available.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.available.order(:available_on).map(&:id).map(&:to_s)
+ end
+ end
+
+ context 'descending order' do
+ before { get '/api/v2/storefront/products?sort=-available_on' }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products sorted by available_on with descending order' do
+ expect(json_response['data'].count).to eq store.products.available.count
+ expect(json_response['data'].pluck(:id)).to eq store.products.available.order(available_on: :desc).map(&:id).map(&:to_s)
+ end
+ end
+ end
end
context 'paginate products' do
diff --git a/api/spec/swagger_helper.rb b/api/spec/swagger_helper.rb
index 4f097025b4..6ebdaa3b70 100644
--- a/api/spec/swagger_helper.rb
+++ b/api/spec/swagger_helper.rb
@@ -77,7 +77,7 @@ RSpec.configure do |config|
required: %w[name presentation]
},
option_value_params: {
- type: :object,
+ type: :object,
properties: {
name: { type: :string },
presentation: { type: :string },
@@ -85,6 +85,35 @@ RSpec.configure do |config|
},
required: %w[name presentation]
},
+ product_params: {
+ type: :object,
+ properties: {
+ name: { type: :string },
+ description: { type: :string },
+ available_on: { type: :string },
+ discontinue_on: { type: :string },
+ permalink: { type: :string },
+ meta_description: { type: :string },
+ meta_keywords: { type: :string },
+ price: { type: :string },
+ sku: { type: :string },
+ deleted_at: { type: :string },
+ prototype_id: { type: :string },
+ option_values_hash: { type: :string },
+ weight: { type: :string },
+ height: { type: :string },
+ width: { type: :string },
+ depth: { type: :string },
+ shipping_category_id: { type: :string },
+ tax_category_id: { type: :string },
+ cost_currency: { type: :string },
+ cost_price: { type: :string },
+ compare_at_price: { type: :string },
+ option_type_ids: { type: :string },
+ taxon_ids: { type: :string }
+ },
+ required: %w[name price shipping_category_id]
+ },
user_params: {
type: :object,
properties: {