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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Expand Platform APIs Product and Variant serializers
Damian Legawiec70cf73b20136
diff --git a/api/app/serializers/spree/api/v2/platform/product_serializer.rb b/api/app/serializers/spree/api/v2/platform/product_serializer.rbindex b1a4709059..d874d3d086 100644--- a/api/app/serializers/spree/api/v2/platform/product_serializer.rb+++ b/api/app/serializers/spree/api/v2/platform/product_serializer.rb@@ -4,6 +4,57 @@ module Spreemodule Platformclass ProductSerializer < BaseSerializerinclude ::Spree::Api::V2::ResourceSerializerConcern+ include ::Spree::Api::V2::DisplayMoneyHelper++ attribute :purchasable do |product|+ product.purchasable?+ end++ attribute :in_stock do |product|+ product.in_stock?+ end++ attribute :backorderable do |product|+ product.backorderable?+ end++ attribute :available do |product|+ product.available?+ end++ attribute :currency do |_product, params|+ params[:currency]+ end++ attribute :price do |product, params|+ price(product, params[:currency])+ end++ attribute :display_price do |product, params|+ display_price(product, params[:currency])+ end++ attribute :compare_at_price do |product, params|+ compare_at_price(product, params[:currency])+ end++ attribute :display_compare_at_price do |product, params|+ display_compare_at_price(product, params[:currency])+ end++ belongs_to :tax_category++ has_one :master_variant,+ object_method_name: :master,+ id_method_name: :master_id,+ record_type: :variant,+ serializer: :variant++ has_one :default_variant,+ object_method_name: :default_variant,+ id_method_name: :default_variant_id,+ record_type: :variant,+ serializer: :varianthas_many :variantshas_many :option_types@@ -16,11 +67,8 @@ module Spreerecord_type: :image,serializer: :image- has_one :default_variant,- object_method_name: :default_variant,- id_method_name: :default_variant_id,- record_type: :variant,- serializer: :variant+ # TODO: add stock items+ # TODO: add pricesendendenddiff --git a/api/app/serializers/spree/api/v2/platform/stock_item_serializer.rb b/api/app/serializers/spree/api/v2/platform/stock_item_serializer.rbnew file mode 100644index 0000000000..1de41fb9d6--- /dev/null+++ b/api/app/serializers/spree/api/v2/platform/stock_item_serializer.rb@@ -0,0 +1,20 @@+module Spree+ module Api+ module V2+ module Platform+ class StockItemSerializer < BaseSerializer+ include ::Spree::Api::V2::ResourceSerializerConcern++ set_type :stock_item++ attribute :is_available do |stock_item|+ stock_item.available?+ end++ belongs_to :stock_location+ belongs_to :variant+ end+ end+ end+ end+enddiff --git a/api/app/serializers/spree/api/v2/platform/stock_location_serializer.rb b/api/app/serializers/spree/api/v2/platform/stock_location_serializer.rbnew file mode 100644index 0000000000..abd5f7c90e--- /dev/null+++ b/api/app/serializers/spree/api/v2/platform/stock_location_serializer.rb@@ -0,0 +1,16 @@+module Spree+ module Api+ module V2+ module Platform+ class StockLocationSerializer < BaseSerializer+ include ::Spree::Api::V2::ResourceSerializerConcern++ attributes :name++ has_many :shipments+ has_many :stock_items+ end+ end+ end+ end+enddiff --git a/api/app/serializers/spree/api/v2/platform/tax_category_serializer.rb b/api/app/serializers/spree/api/v2/platform/tax_category_serializer.rbnew file mode 100644index 0000000000..d8ee5d582c--- /dev/null+++ b/api/app/serializers/spree/api/v2/platform/tax_category_serializer.rb@@ -0,0 +1,13 @@+module Spree+ module Api+ module V2+ module Platform+ class TaxCategorySerializer < BaseSerializer+ include ::Spree::Api::V2::ResourceSerializerConcern++ # TODO: add tax_rates+ end+ end+ end+ end+enddiff --git a/api/app/serializers/spree/api/v2/platform/variant_serializer.rb b/api/app/serializers/spree/api/v2/platform/variant_serializer.rbindex dd2e8d0457..29f922e35f 100644--- a/api/app/serializers/spree/api/v2/platform/variant_serializer.rb+++ b/api/app/serializers/spree/api/v2/platform/variant_serializer.rb@@ -4,10 +4,52 @@ module Spreemodule Platformclass VariantSerializer < BaseSerializerinclude ::Spree::Api::V2::ResourceSerializerConcern+ include ::Spree::Api::V2::DisplayMoneyHelper++ attributes :name, :options_text, :total_on_hand++ attribute :purchasable do |product|+ product.purchasable?+ end++ attribute :in_stock do |product|+ product.in_stock?+ end++ attribute :backorderable do |product|+ product.backorderable?+ end++ attribute :available do |product|+ product.available?+ end++ attribute :currency do |_product, params|+ params[:currency]+ end++ attribute :price do |object, params|+ price(object, params[:currency])+ end++ attribute :display_price do |object, params|+ display_price(object, params[:currency])+ end++ attribute :compare_at_price do |object, params|+ compare_at_price(object, params[:currency])+ end++ attribute :display_compare_at_price do |object, params|+ display_compare_at_price(object, params[:currency])+ endbelongs_to :product+ belongs_to :tax_categoryhas_many :imageshas_many :option_values+ has_many :stock_items+ has_many :stock_locationsendendenddiff --git a/api/lib/spree/api/testing_support/v2/serializers_params.rb b/api/lib/spree/api/testing_support/v2/serializers_params.rbnew file mode 100644index 0000000000..58595b232d--- /dev/null+++ b/api/lib/spree/api/testing_support/v2/serializers_params.rb@@ -0,0 +1,14 @@+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(:serializer_params) do+ {+ store: store,+ currency: currency,+ user: nil,+ locale: locale+ }+ end+enddiff --git a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rbindex a7f075c455..ab63f9b5ce 100644--- a/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/product_serializer_spec.rb@@ -1,15 +1,21 @@require 'spec_helper'describe Spree::Api::V2::Platform::ProductSerializer do- subject { described_class.new(product) }+ include_context 'API v2 serializers params'++ subject { described_class.new(product, params: serializer_params) }let!(:images) { create_list(:image, 2) }let(:product) do- create(:product,+ create(:product_in_stock,+ name: 'Test Product',+ price: 10.00,+ compare_at_price: 15.00,variants_including_master: [create(:variant, images: images), create(:variant)],option_types: create_list(:option_type, 2),product_properties: create_list(:product_property, 2),- taxons: create_list(:taxon, 2))+ taxons: create_list(:taxon, 2),+ tax_category: create(:tax_category))endit { expect(subject.serializable_hash).to be_kind_of(Hash) }@@ -32,9 +38,30 @@ describe Spree::Api::V2::Platform::ProductSerializer doupdated_at: product.updated_at,promotionable: product.promotionable,meta_title: product.meta_title,- discontinue_on: product.discontinue_on+ discontinue_on: product.discontinue_on,+ purchasable: product.purchasable?,+ in_stock: product.in_stock?,+ backorderable: product.backorderable?,+ available: product.available?,+ currency: currency,+ price: BigDecimal(10),+ display_price: '$10.00',+ compare_at_price: BigDecimal(15),+ display_compare_at_price: '$15.00'},relationships: {+ tax_category: {+ data: {+ id: product.tax_category.id.to_s,+ type: :tax_category+ }+ },+ master_variant: {+ data: {+ id: product.master.id.to_s,+ type: :variant+ }+ },default_variant: {data: {id: product.default_variant.id.to_s,@@ -100,7 +127,7 @@ describe Spree::Api::V2::Platform::ProductSerializer dotype: :image}]- }+ },}}}diff --git a/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rbindex a7716cce29..107f2177bb 100644--- a/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/taxonomy_serializer_spec.rb@@ -1,11 +1,11 @@require 'spec_helper'-describe Spree::Api::V2::Platform::TaxonomySerializer do+describe Spree::Api::V2::Platform::TaxonomySerializer, retry: 3 dosubject { described_class.new(taxonomy) }let(:taxonomy) { create(:taxonomy) }+ let!(:root) { taxonomy.root }let!(:taxon) { create(:taxon, taxonomy: taxonomy) }- let(:root) { taxonomy.root }it { expect(subject.serializable_hash).to be_kind_of(Hash) }diff --git a/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rbindex 530725fdac..a56c9641bb 100644--- a/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v2/platform/variant_serializer_spec.rb@@ -1,9 +1,11 @@require 'spec_helper'describe Spree::Api::V2::Platform::VariantSerializer do- subject { described_class.new(variant) }+ include_context 'API v2 serializers params'- let(:variant) { create(:variant, images: create_list(:image, 2)) }+ subject { described_class.new(variant, params: serializer_params) }++ let!(:variant) { create(:variant, price: 10, compare_at_price: 15, images: create_list(:image, 2), tax_category: create(:tax_category)) }it { expect(subject.serializable_hash).to be_kind_of(Hash) }@@ -24,9 +26,21 @@ describe Spree::Api::V2::Platform::VariantSerializer doposition: variant.position,cost_currency: variant.cost_currency,track_inventory: variant.track_inventory,- created_at: variant.created_at,updated_at: variant.updated_at,- discontinue_on: variant.discontinue_on+ discontinue_on: variant.discontinue_on,+ created_at: variant.created_at,+ name: variant.name,+ options_text: variant.options_text,+ total_on_hand: variant.total_on_hand,+ purchasable: variant.purchasable?,+ in_stock: variant.in_stock?,+ backorderable: variant.backorderable?,+ available: variant.available?,+ currency: currency,+ price: BigDecimal(10),+ display_price: '$10.00',+ compare_at_price: BigDecimal(15),+ display_compare_at_price: '$15.00'},relationships: {product: {@@ -35,6 +49,12 @@ describe Spree::Api::V2::Platform::VariantSerializer dotype: :product}},+ tax_category: {+ data: {+ id: variant.tax_category.id.to_s,+ type: :tax_category+ }+ },option_values: {data: [{@@ -54,6 +74,22 @@ describe Spree::Api::V2::Platform::VariantSerializer dotype: :image}]+ },+ stock_locations: {+ data: [+ {+ id: variant.stock_locations.first.id.to_s,+ type: :stock_location+ }+ ]+ },+ stock_items: {+ data: [+ {+ id: variant.stock_items.first.id.to_s,+ type: :stock_item+ }+ ]}}}diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rbindex fff21b54ae..74a8d04e9e 100644--- a/api/spec/spec_helper.rb+++ b/api/spec/spec_helper.rb@@ -36,6 +36,7 @@ require 'spree/testing_support/factories'require 'spree/testing_support/preferences'require 'spree/testing_support/image_helpers'require 'spree/testing_support/next_instance_of'+require 'spree/testing_support/rspec_retry_config'require 'spree/api/testing_support/caching'require 'spree/api/testing_support/helpers'@@ -43,6 +44,7 @@ require 'spree/api/testing_support/setup'require 'spree/api/testing_support/v2/base'require 'spree/api/testing_support/v2/current_order'require 'spree/api/testing_support/v2/platform_contexts'+require 'spree/api/testing_support/v2/serializers_params'RSpec.configure do |config|config.backtrace_exclusion_patterns = [/gems\/activesupport/, /gems\/actionpack/, /gems\/rspec/]diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex d3bbe0cc97..6314513849 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -93,6 +93,7 @@ module Spreenot_discontinued.not_deleted.for_currency_and_available_price_amount(currency)end+ # FIXME: cost price should be represented with DisplayMoney classLOCALIZED_NUMBERS = %w(cost_price weight depth width height)LOCALIZED_NUMBERS.each do |m|