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.

Add changes and specs for products listed by currency param

Przemek Kosakowski011cd767e329
diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rb
index 838aca264d..96dd4ac21c 100644
--- a/api/app/controllers/spree/api/v2/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/resource_controller.rb
@@ -24,6 +24,7 @@ module Spree
def serialize_resource(resource)
resource_serializer.new(
resource,
+ params: { currency: current_currency, store: current_store, user: spree_current_user },
include: resource_includes,
fields: sparse_fields
).serializable_hash
diff --git a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
index 3da8befe40..f29ab62f95 100644
--- a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
@@ -440,6 +440,29 @@ describe 'API V2 Storefront Cart Spec', type: :request do
expect(json_response['data']).to have_attribute(:currency).with_value('EUR')
end
end
+
+ context 'as a signed user' do
+ context 'with valid currency param' do
+ include_context 'creates order with line item'
+ it_behaves_like 'showing the cart'
+
+ it 'includes the requested currency' do
+ get '/api/v2/storefront/cart', headers: headers, params: { currency: 'USD' }
+ expect(json_response['data']).to have_attribute(:currency).with_value('USD')
+ end
+ end
+
+ context 'with invalid currency param' do
+ include_context 'creates order with line item'
+ it_behaves_like 'showing the cart'
+
+ it 'includes the requested currency' do
+ get '/api/v2/storefront/cart', headers: headers, params: { currency: 'PLN' }
+
+ expect(json_response['data']['attributes']['currency']).to eq store.default_currency
+ end
+ end
+ end
end
context 'with option: include' do
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 0b652994b2..0f42c0d526 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -233,9 +233,52 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
end
+
+ context 'fetch products by curency param' do
+ let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR,GBP', default_currency: 'USD' ) }
+
+ context 'with supported currency' do
+ before { get '/api/v2/storefront/products?currency=USD' }
+
+ it 'return products' do
+ expect(json_response['data']).not_to eq []
+ expect(json_response['data'][0]['attributes']['currency']).to eq 'USD'
+ end
+ end
+
+ context 'without supported currency' do
+ before { get '/api/v2/storefront/products?currency=PLN' }
+
+ it 'return empty result' do
+ expect(json_response['data']).to eq []
+ end
+ end
+
+ end
end
describe 'products#show' do
+ context 'with supported currency param' do
+ before { get "/api/v2/storefront/products/#{product.slug}?currency=USD" }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'return product with supported currency' do
+ expect(json_response['data']).not_to be []
+ expect(json_response['data']['attributes']['slug']).to eq product.slug
+ end
+ end
+
+ context 'without supported currency param' do
+ before { get "/api/v2/storefront/products/#{product.slug}?currency=PLN" }
+
+ it 'return product with supported currency' do
+ expect(json_response['data']).not_to be []
+ expect(json_response['data']['attributes']['slug']).to eq product.slug
+ expect(json_response['data']['attributes']['currency']).to eq store.default_currency
+ end
+ end
+
context 'with non-existing product' do
before { get '/api/v2/storefront/products/example' }
diff --git a/core/app/finders/spree/orders/find_current.rb b/core/app/finders/spree/orders/find_current.rb
index a08dddeb00..06c627d3d1 100644
--- a/core/app/finders/spree/orders/find_current.rb
+++ b/core/app/finders/spree/orders/find_current.rb
@@ -9,7 +9,7 @@ module Spree
return order unless order.nil?
return if user.nil?
- incomplete_orders.order(created_at: :desc).find_by(store: store, user: user)
+ incomplete_orders.order(created_at: :desc).find_by(store: store, user: user, currency: params[:currency])
end
private
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index dc6ccf4813..dde191c10e 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -22,6 +22,7 @@ module Spree
products = by_ids(scope)
products = by_skus(products)
products = by_price(products)
+ products = by_currency(products)
products = by_taxons(products)
products = by_concat_taxons(products)
products = by_name(products)
@@ -51,6 +52,10 @@ module Spree
price.present?
end
+ def currency?
+ currency.present?
+ end
+
def taxons?
taxons.present?
end
@@ -94,7 +99,7 @@ module Spree
def by_price(products)
return products unless price?
- products.joins(master: :default_price).
+ products.joins(master: :prices).
where(
spree_prices: {
amount: price.min..price.max,
@@ -103,6 +108,12 @@ module Spree
)
end
+ def by_currency(products)
+ return products unless currency?
+
+ products.joins(master: :prices).where(spree_prices: { currency: currency })
+ end
+
def by_taxons(products)
return products unless taxons?