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 price_options to serializer_params. Fix broken examples.

Sebastian Palmabfab600855a6
diff --git a/api/app/controllers/spree/api/v2/base_controller.rb b/api/app/controllers/spree/api/v2/base_controller.rb
index 1c5c86d5b2..9e4615a68a 100644
--- a/api/app/controllers/spree/api/v2/base_controller.rb
+++ b/api/app/controllers/spree/api/v2/base_controller.rb
@@ -131,6 +131,7 @@ module Spree
{
currency: current_currency,
locale: current_locale,
+ price_options: current_price_options,
store: current_store,
user: spree_current_user,
image_transformation: params[:image_transformation],
diff --git a/api/app/serializers/spree/api/v2/base_serializer.rb b/api/app/serializers/spree/api/v2/base_serializer.rb
index bce162ea4b..a7bc0485cc 100644
--- a/api/app/serializers/spree/api/v2/base_serializer.rb
+++ b/api/app/serializers/spree/api/v2/base_serializer.rb
@@ -13,12 +13,19 @@ module Spree
opts = options.dup
params_cache_key = params.map do |param|
- next if param.nil? || param.last.nil?
+ value = param.last
+ next if param.nil? || value.nil?
- if param.last.respond_to?(:cache_key_with_version)
- param.last.cache_key_with_version
+ if value.respond_to?(:cache_key_with_version)
+ value.cache_key_with_version
+ elsif value.is_a?(Hash)
+ # covers the current_price_options cases:
+ # { price_options: { tax_zone: Spree::Zone... } } or
+ # { price_options: { tax_zone: nil } }
+ only_key = value.keys.first
+ value[only_key]&.cache_key_with_version
else
- param.last.to_s.downcase
+ value.to_s.downcase
end
end.compact.reject(&:blank?).join('-')
diff --git a/api/app/serializers/spree/api/v2/platform/price_serializer.rb b/api/app/serializers/spree/api/v2/platform/price_serializer.rb
index dc441093b4..ef65845217 100644
--- a/api/app/serializers/spree/api/v2/platform/price_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/price_serializer.rb
@@ -5,16 +5,12 @@ module Spree
class PriceSerializer < BaseSerializer
include ResourceSerializerConcern
- attribute :display_price_including_vat_for do |price|
- price.display_price_including_vat_for({})
+ attribute :display_price_including_vat_for do |price, params|
+ price.display_price_including_vat_for(params[:price_options].presence || {})
end
- attribute :display_compare_at_price do |price|
- price.display_price_including_vat_for({})
- end
-
- attribute :display_compare_at_price_including_vat_for do |price|
- price.display_compare_at_price_including_vat_for({})
+ attribute :display_compare_at_price_including_vat_for do |price, params|
+ price.display_compare_at_price_including_vat_for(params[:price_options].presence || {})
end
end
end
diff --git a/api/lib/spree/api/testing_support/v2/serializers_params.rb b/api/lib/spree/api/testing_support/v2/serializers_params.rb
index 58595b232d..3dcd385aaa 100644
--- a/api/lib/spree/api/testing_support/v2/serializers_params.rb
+++ b/api/lib/spree/api/testing_support/v2/serializers_params.rb
@@ -2,13 +2,15 @@ shared_context 'API v2 serializers params' do
let(:store) { Spree::Store.default || create(:store, default: true) }
let(:currency) { store.default_currency }
let(:locale) { store.default_locale }
+ let(:zone) { Spree::Zone.default_tax || create(:zone, default_tax: true) }
let(:serializer_params) do
{
store: store,
currency: currency,
user: nil,
- locale: locale
+ locale: locale,
+ price_options: { tax_zone: zone }
}
end
end
diff --git a/api/spec/controllers/spree/api/v2/base_controller_spec.rb b/api/spec/controllers/spree/api/v2/base_controller_spec.rb
index c3d4bbe959..d1ee52e468 100644
--- a/api/spec/controllers/spree/api/v2/base_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v2/base_controller_spec.rb
@@ -126,6 +126,7 @@ describe Spree::Api::V2::BaseController, type: :controller do
let(:currency) { store.default_currency }
let(:locale) { store.default_locale }
let(:user) { nil }
+ let(:price_options) { { tax_zone: Spree::Zone.new } }
shared_examples 'returns proper values' do
before do
@@ -134,15 +135,21 @@ describe Spree::Api::V2::BaseController, type: :controller do
allow(dummy_controller).to receive(:current_currency).and_return(currency)
allow(dummy_controller).to receive(:current_locale).and_return(locale)
allow(dummy_controller).to receive(:spree_current_user).and_return(user)
+ allow(dummy_controller).to receive(:current_price_options).and_return(price_options)
end
- it { expect(dummy_controller.send(:serializer_params).keys).to eq(%i[currency locale store user image_transformation taxon_image_transformation]) }
+ let(:serializer_params) { dummy_controller.send(:serializer_params) }
+
+ it 'contains the expected hash serializer keys' do
+ expect(serializer_params.keys).to match_array(%i[currency image_transformation locale price_options store taxon_image_transformation user])
+ end
it do
- expect(dummy_controller.send(:serializer_params)).to eq(
+ expect(serializer_params).to eq(
{
store: store,
currency: currency,
+ price_options: price_options,
user: user,
locale: locale,
image_transformation: nil,
diff --git a/api/spec/requests/spree/api/v2/caching_spec.rb b/api/spec/requests/spree/api/v2/caching_spec.rb
index 2af046053f..4f15942920 100644
--- a/api/spec/requests/spree/api/v2/caching_spec.rb
+++ b/api/spec/requests/spree/api/v2/caching_spec.rb
@@ -105,12 +105,15 @@ describe 'API v2 Caching spec', type: :request do
end
end
- context 'serialiers caching' do
+ context 'serializers caching' do
+ let!(:zone) { create(:zone, default_tax: true) }
let(:cache_store) { Spree::V2::Storefront::ProductSerializer.cache_store_instance }
let(:cache_entry) { cache_store.read(product, namespace: cache_namespace) }
context 'auto expiration' do
- let(:cache_namespace) { "jsonapi-serializer-usd-en-#{store.cache_key_with_version}" }
+ let(:cache_namespace) do
+ "jsonapi-serializer-usd-en-#{zone.cache_key_with_version}-#{store.cache_key_with_version}"
+ end
it 'auto expire cache after record being updated' do
get "/api/v2/storefront/products/#{product.id}"
@@ -137,7 +140,12 @@ describe 'API v2 Caching spec', type: :request do
let!(:user) { create(:user) }
let(:currency) { 'EUR' }
let(:locale) { 'de' }
- let(:cache_namespace) { "jsonapi-serializer-eur-de-#{store.cache_key_with_version}-#{user.cache_key_with_version}" }
+ let(:cache_namespace) do
+ "jsonapi-serializer-eur-de" \
+ "-#{zone.cache_key_with_version}" \
+ "-#{store.cache_key_with_version}" \
+ "-#{user.cache_key_with_version}"
+ end
it 'includes currency and signed user in the cache key' do
get "/api/v2/storefront/products/#{product.id}?currency=#{currency}&locale=#{locale}", headers: headers_bearer
diff --git a/api/spec/serializers/spree/api/v2/platform/price_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/price_serializer_spec.rb
new file mode 100644
index 0000000000..f2bffc7db0
--- /dev/null
+++ b/api/spec/serializers/spree/api/v2/platform/price_serializer_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Spree::Api::V2::Platform::PriceSerializer do
+ include_context 'API v2 serializers params'
+
+ subject { described_class.new(price, params: serializer_params) }
+
+ let(:price) { create(:price) }
+
+ it { expect(subject.serializable_hash).to be_kind_of(Hash) }
+
+ # serializable_hash is returned with a different key order,
+ # making the tests fail if is compared to a pre-defined hash
+ it do
+ serializable_hash = subject.serializable_hash
+ expect(serializable_hash[:data][:id]).to eq(price.id.to_s)
+ expect(serializable_hash[:data][:type]).to eq(:price)
+ expect(serializable_hash[:data][:attributes][:amount]).to eq(price.amount)
+ expect(serializable_hash[:data][:attributes][:currency]).to eq(price.currency)
+ expect(serializable_hash[:data][:attributes][:deleted_at]).to eq(price.deleted_at)
+ expect(serializable_hash[:data][:attributes][:created_at]).to eq(price.created_at)
+ expect(serializable_hash[:data][:attributes][:updated_at]).to eq(price.updated_at)
+ expect(serializable_hash[:data][:attributes][:compare_at_amount]).to eq(price.compare_at_amount)
+ expect(serializable_hash[:data][:attributes][:display_compare_at_price]).to eq(Spree::Money.new(0, currency: price.currency))
+ expect(serializable_hash[:data][:attributes][:display_amount]).to eq(price.display_amount)
+ expect(serializable_hash[:data][:attributes][:display_price]).to eq(price.display_price)
+ expect(serializable_hash[:data][:attributes][:display_compare_at_price_including_vat_for]).to eq(Spree::Money.new(price.compare_at_amount, currency: price.currency))
+ expect(serializable_hash[:data][:attributes][:display_compare_at_amount]).to eq(Spree::Money.new(0, currency: price.currency))
+ expect(serializable_hash[:data][:attributes][:display_price_including_vat_for]).to eq(Spree::Money.new(price.amount, currency: price.currency))
+ end
+end