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.

Simplify serializers

Damian Legawiecaa6d2ba435fd
diff --git a/api/.claude/settings.local.json b/api/.claude/settings.local.json
deleted file mode 100644
index f4b2128360..0000000000
--- a/api/.claude/settings.local.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "permissions": {
- "allow": [
- "Read(//Users/damian/github/spree/**)"
- ],
- "deny": [],
- "ask": []
- }
-}
diff --git a/api/app/controllers/spree/api/v3/base_controller.rb b/api/app/controllers/spree/api/v3/base_controller.rb
index ff7d9ed5b1..b5c865975b 100644
--- a/api/app/controllers/spree/api/v3/base_controller.rb
+++ b/api/app/controllers/spree/api/v3/base_controller.rb
@@ -11,6 +11,8 @@ module Spree
include Spree::Api::V3::Authentication
include Spree::Api::V3::ErrorHandler
include Spree::Api::V3::HttpCaching
+ include Spree::Api::V3::ResourceSerializer
+ include Pagy::Method
# Optional authentication by default
before_action :authenticate_user
diff --git a/api/app/controllers/spree/api/v3/resource_controller.rb b/api/app/controllers/spree/api/v3/resource_controller.rb
index 2d6229775f..0b31199ad6 100644
--- a/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -2,9 +2,6 @@ module Spree
module Api
module V3
class ResourceController < ::Spree::Api::V3::BaseController
- include Spree::Api::V3::ResourceSerializer
- include Pagy::Method
-
before_action :set_resource, only: [:show, :update, :destroy]
# GET /api/v3/resource
diff --git a/api/app/controllers/spree/api/v3/storefront/countries_controller.rb b/api/app/controllers/spree/api/v3/storefront/countries_controller.rb
index 1fc31501e0..8be67f9a7b 100644
--- a/api/app/controllers/spree/api/v3/storefront/countries_controller.rb
+++ b/api/app/controllers/spree/api/v3/storefront/countries_controller.rb
@@ -3,8 +3,6 @@ module Spree
module V3
module Storefront
class CountriesController < ResourceController
- # Public endpoint - no authentication required
-
protected
def scope
@@ -18,11 +16,6 @@ module Spree
def serializer_class
Spree.api.v3_storefront_country_serializer
end
-
- # Not needed for index/show
- def permitted_params
- {}
- end
end
end
end
diff --git a/api/app/controllers/spree/api/v3/storefront/coupon_codes_controller.rb b/api/app/controllers/spree/api/v3/storefront/coupon_codes_controller.rb
index 26c420190a..e4396a9255 100644
--- a/api/app/controllers/spree/api/v3/storefront/coupon_codes_controller.rb
+++ b/api/app/controllers/spree/api/v3/storefront/coupon_codes_controller.rb
@@ -13,7 +13,7 @@ module Spree
@order.coupon_code = params[:coupon_code]
if @order.save
- render json: serialize_order, status: :created
+ render json: serialize_resource(@order), status: :created
else
render_errors(@order.errors)
end
@@ -29,22 +29,6 @@ module Spree
render_errors(@order.errors)
end
end
-
- protected
-
- def serialize_order
- serializer_class.new(@order, params: serializer_params).to_h
- end
-
- def serializer_params
- {
- currency: current_currency,
- store: current_store,
- user: current_user,
- locale: current_locale,
- includes: requested_includes
- }
- end
end
end
end
diff --git a/api/app/controllers/spree/api/v3/storefront/customers_controller.rb b/api/app/controllers/spree/api/v3/storefront/customers_controller.rb
index 753dafc666..fa10a1aca1 100644
--- a/api/app/controllers/spree/api/v3/storefront/customers_controller.rb
+++ b/api/app/controllers/spree/api/v3/storefront/customers_controller.rb
@@ -12,7 +12,7 @@ module Spree
# PATCH /api/v3/storefront/customers/me
def update
- if current_user.update(customer_params)
+ if current_user.update(permitted_params)
render json: serialize_resource(current_user)
else
render_errors(current_user.errors)
@@ -29,15 +29,7 @@ module Spree
Spree.api.v3_storefront_user_serializer
end
- def serializer_params
- {
- store: current_store,
- locale: current_locale,
- includes: include_tree
- }
- end
-
- def customer_params
+ def permitted_params
params.require(:customer).permit(Spree::PermittedAttributes.user_attributes)
end
end
diff --git a/api/app/controllers/spree/api/v3/storefront/line_items_controller.rb b/api/app/controllers/spree/api/v3/storefront/line_items_controller.rb
index 6ca6e4899e..72de19c097 100644
--- a/api/app/controllers/spree/api/v3/storefront/line_items_controller.rb
+++ b/api/app/controllers/spree/api/v3/storefront/line_items_controller.rb
@@ -9,20 +9,13 @@ module Spree
before_action :authorize_order_access!
skip_before_action :set_resource
- # GET /api/v3/storefront/orders/:order_id/line_items
- def index
- render json: {
- data: serialize_collection(@order.line_items)
- }
- end
-
# POST /api/v3/storefront/orders/:order_id/line_items
def create
- result = add_item_service.call(
+ result = Spree.cart_add_item_service.call(
order: @order,
variant: variant,
- quantity: line_item_params[:quantity] || 1,
- options: line_item_params[:options] || {}
+ quantity: permitted_params[:quantity] || 1,
+ options: permitted_params[:options] || {}
)
if result.success?
@@ -33,21 +26,15 @@ module Spree
end
end
- # GET /api/v3/storefront/orders/:order_id/line_items/:id
- def show
- @line_item = @order.line_items.find(params[:id])
- render json: serialize_resource(@line_item)
- end
-
# PATCH /api/v3/storefront/orders/:order_id/line_items/:id
def update
@line_item = @order.line_items.find(params[:id])
- if line_item_params[:quantity].present?
- result = set_item_quantity_service.call(
+ if permitted_params[:quantity].present?
+ result = Spree.cart_set_item_quantity_service.call(
order: @order,
line_item: @line_item,
- quantity: line_item_params[:quantity]
+ quantity: permitted_params[:quantity]
)
if result.success?
@@ -64,7 +51,7 @@ module Spree
def destroy
@line_item = @order.line_items.find(params[:id])
- remove_line_item_service.call(
+ Spree.cart_remove_line_item_service.call(
order: @order,
line_item: @line_item
)
@@ -75,7 +62,11 @@ module Spree
protected
def variant
- Spree::Variant.find(line_item_params[:variant_id])
+ current_store.variants.accessible_by(current_ability).find(line_item_params[:variant_id])
+ end
+
+ def scope
+ @order.line_items
end
def model_class
@@ -87,24 +78,8 @@ module Spree
end
def permitted_params
- line_item_params
- end
-
- def line_item_params
params.require(:line_item).permit(Spree::PermittedAttributes.line_item_attributes + [{ options: {} }])
end
-
- def add_item_service
- Spree::Api::Dependencies.storefront_cart_add_item_service.constantize
- end
-
- def set_item_quantity_service
- Spree::Api::Dependencies.storefront_cart_set_item_quantity_service.constantize
- end
-
- def remove_line_item_service
- Spree::Api::Dependencies.storefront_cart_remove_line_item_service.constantize
- end
end
end
end
diff --git a/api/app/controllers/spree/api/v3/storefront/payment_methods_controller.rb b/api/app/controllers/spree/api/v3/storefront/payment_methods_controller.rb
deleted file mode 100644
index f3c5218fbf..0000000000
--- a/api/app/controllers/spree/api/v3/storefront/payment_methods_controller.rb
+++ /dev/null
@@ -1,46 +0,0 @@
-module Spree
- module Api
- module V3
- module Storefront
- class PaymentMethodsController < BaseController
- include Spree::Api::V3::OrderConcern
-
- before_action :set_order
- before_action :authorize_order_access!
-
- # GET /api/v3/storefront/orders/:order_id/payment_methods
- def index
- @payment_methods = available_payment_methods
-
- render json: {
- data: serialize_collection(@payment_methods)
- }
- end
-
- protected
-
- def available_payment_methods
- @order.available_payment_methods
- end
-
- def serialize_collection(collection)
- collection.map { |item| serializer_class.new(item, params: serializer_params).to_h }
- end
-
- def serializer_class
- Spree.api.v3_storefront_payment_method_serializer
- end
-
- def serializer_params
- {
- currency: current_currency,
- store: current_store,
- locale: current_locale,
- includes: include_tree
- }
- end
- end
- end
- end
- end
-end
diff --git a/api/app/controllers/spree/api/v3/storefront/payments_controller.rb b/api/app/controllers/spree/api/v3/storefront/payments_controller.rb
deleted file mode 100644
index 15698f5f0c..0000000000
--- a/api/app/controllers/spree/api/v3/storefront/payments_controller.rb
+++ /dev/null
@@ -1,57 +0,0 @@
-module Spree
- module Api
- module V3
- module Storefront
- class PaymentsController < ResourceController
- include Spree::Api::V3::OrderConcern
-
- before_action :set_order
- before_action :authorize_order_access!
- before_action :set_payment, only: [:show]
-
- # GET /api/v3/storefront/orders/:order_id/payments
- def index
- render json: {
- data: serialize_collection(@order.payments.valid)
- }
- end
-
- # GET /api/v3/storefront/orders/:order_id/payments/:id
- def show
- render json: serialize_resource(@payment)
- end
-
- # POST /api/v3/storefront/orders/:order_id/payments
- def create
- @payment = @order.payments.build(payment_params)
- authorize_resource!(@payment, :create)
-
- if @payment.save
- render json: serialize_resource(@payment), status: :created
- else
- render_errors(@payment.errors)
- end
- end
-
- protected
-
- def set_payment
- @payment = @order.payments.valid.find(params[:id])
- end
-
- def model_class
- Spree::Payment
- end
-
- def serializer_class
- Spree.api.v3_storefront_payment_serializer
- end
-
- def permitted_params
- params.require(:payment).permit(Spree::PermittedAttributes.payment_attributes)
- end
- end
- end
- end
- end
-end
diff --git a/api/app/controllers/spree/api/v3/storefront/posts_controller.rb b/api/app/controllers/spree/api/v3/storefront/posts_controller.rb
deleted file mode 100644
index 78e2facfa1..0000000000
--- a/api/app/controllers/spree/api/v3/storefront/posts_controller.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-module Spree
- module Api
- module V3
- module Storefront
- class PostsController < ResourceController
- # Public endpoint - no authentication required
-
- protected
-
- def scope
- Spree::Post.published.for_store(current_store).accessible_by(current_ability, :show)
- end
-
- def model_class
- Spree::Post
- end
-
- # Simple inline serializer for posts
- Class.new do
- attr_reader :resource
-
- def initialize(resource, context = {})
- @resource = resource
-
- def as_json
- {
- id: resource.id,
- title: resource.title,
- slug: resource.slug,
- content: resource.content,
- published_at: resource.published_at,
- created_at: resource.created_at,
- updated_at: resource.updated_at
- }
- end
- end
- end
-
- # Not needed for index/show
- def permitted_params
- {}
- end
- end
- end
- end
- end
-end
diff --git a/api/app/controllers/spree/api/v3/storefront/products_controller.rb b/api/app/controllers/spree/api/v3/storefront/products_controller.rb
index 4a6fdbffa0..f89cc38793 100644
--- a/api/app/controllers/spree/api/v3/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v3/storefront/products_controller.rb
@@ -23,11 +23,6 @@ module Spree
variants: [:prices, {option_values: :option_type }, { stock_items: :stock_location }]
]
end
-
- # Not needed for index/show, but required by ResourceController
- def permitted_params
- {}
- end
end
end
end
diff --git a/api/app/controllers/spree/api/v3/storefront/shipping_methods_controller.rb b/api/app/controllers/spree/api/v3/storefront/shipping_methods_controller.rb
deleted file mode 100644
index a0f0dd51d2..0000000000
--- a/api/app/controllers/spree/api/v3/storefront/shipping_methods_controller.rb
+++ /dev/null
@@ -1,46 +0,0 @@
-module Spree
- module Api
- module V3
- module Storefront
- class ShippingMethodsController < BaseController
- include Spree::Api::V3::OrderConcern
-
- before_action :set_order
- before_action :authorize_order_access!
-
- # GET /api/v3/storefront/orders/:order_id/shipping_methods
- def index
- @shipping_methods = available_shipping_methods
-
- render json: {
- data: serialize_collection(@shipping_methods)
- }
- end
-
- protected
-
- def available_shipping_methods
- @order.available_shipping_methods
- end
-
- def serialize_collection(collection)
- collection.map { |item| serializer_class.new(item, params: serializer_params).to_h }
- end
-
- def serializer_class
- Spree.api.v3_storefront_shipping_method_serializer
- end
-
- def serializer_params
- {
- currency: current_currency,
- store: current_store,
- locale: current_locale,
- includes: include_tree
- }
- end
- end
- end
- end
- end
-end
diff --git a/api/app/controllers/spree/api/v3/storefront/taxonomies_controller.rb b/api/app/controllers/spree/api/v3/storefront/taxonomies_controller.rb
new file mode 100644
index 0000000000..f9d3205155
--- /dev/null
+++ b/api/app/controllers/spree/api/v3/storefront/taxonomies_controller.rb
@@ -0,0 +1,19 @@
+module Spree
+ module Api
+ module V3
+ module Storefront
+ class TaxonomiesController < ResourceController
+ protected
+
+ def model_class
+ Spree::Taxonomy
+ end
+
+ def serializer_class
+ Spree.api.v3_storefront_taxonomy_serializer
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v3/storefront/wishlist_items_controller.rb b/api/app/controllers/spree/api/v3/storefront/wishlist_items_controller.rb
index 7bb5dcaf11..1f88da89eb 100644
--- a/api/app/controllers/spree/api/v3/storefront/wishlist_items_controller.rb
+++ b/api/app/controllers/spree/api/v3/storefront/wishlist_items_controller.rb
@@ -6,47 +6,16 @@ module Spree
before_action :require_authentication!
before_action :set_wishlist
- # POST /api/v3/storefront/wishlists/:wishlist_id/items
- def create
- @item = @wishlist.wished_items.build(permitted_params)
-
- if @item.save
- render json: serialize_resource(@item), status: :created
- else
- render_errors(@item.errors)
- end
- end
-
- # GET /api/v3/storefront/wishlists/:wishlist_id/items/:id
- def show
- @item = @wishlist.wished_items.find(params[:id])
- render json: serialize_resource(@item)
- end
-
- # PATCH /api/v3/storefront/wishlists/:wishlist_id/items/:id
- def update
- @item = @wishlist.wished_items.find(params[:id])
-
- if @item.update(permitted_params)
- render json: serialize_resource(@item)
- else
- render_errors(@item.errors)
- end
- end
-
- # DELETE /api/v3/storefront/wishlists/:wishlist_id/items/:id
- def destroy
- @item = @wishlist.wished_items.find(params[:id])
- @item.destroy
- head :no_content
- end
-
protected
def set_wishlist
@wishlist = current_user.wishlists.find(params[:wishlist_id])
end
+ def scope
+ @wishlist.wished_items
+ end
+
def model_class
Spree::WishedItem
end
diff --git a/api/app/serializers/spree/api/v3/address_serializer.rb b/api/app/serializers/spree/api/v3/address_serializer.rb
index 50231d1fff..34d1566a8c 100644
--- a/api/app/serializers/spree/api/v3/address_serializer.rb
+++ b/api/app/serializers/spree/api/v3/address_serializer.rb
@@ -3,17 +3,8 @@ module Spree
module V3
class AddressSerializer < BaseSerializer
attributes :id, :firstname, :lastname, :full_name, :address1, :address2,
- :city, :zipcode, :phone, :company, :state_id, :state_name,
- :state_text, :country_id,
- created_at: :iso8601, updated_at: :iso8601
-
- attribute :country_name do |address|
- address.country&.name
- end
-
- attribute :country_iso do |address|
- address.country&.iso
- end
+ :city, :zipcode, :phone, :company, :state_id,
+ :state_text, :country_id, :country_iso, :country_name
end
end
end
diff --git a/api/app/serializers/spree/api/v3/image_serializer.rb b/api/app/serializers/spree/api/v3/image_serializer.rb
index 326a1af631..b7c33c7dbf 100644
--- a/api/app/serializers/spree/api/v3/image_serializer.rb
+++ b/api/app/serializers/spree/api/v3/image_serializer.rb
@@ -8,36 +8,29 @@ module Spree
image_url(image)
end
- attribute :large_url do |image|
- image_url(image, size: [1200, 1200])
+ # Dynamically define attributes for each configured image variant
+ # Uses named variants from Spree::Config.product_image_variant_sizes
+ # (e.g., mini, small, medium, large, xlarge)
+ Spree::Config.product_image_variant_sizes.each_key do |variant_name|
+ attribute :"#{variant_name}_url" do |image|
+ variant_url(image, variant_name)
+ end
end
- attribute :medium_url do |image|
- image_url(image, size: [600, 600])
- end
+ private
- attribute :small_url do |image|
- image_url(image, size: [300, 300])
- end
+ def image_url(image)
+ return nil unless image&.attachment&.attached?
- attribute :thumbnail_url do |image|
- image_url(image, size: [150, 150])
+ Rails.application.routes.url_helpers.cdn_image_url(image.attachment)
end
- private
-
- def image_url(image, size: nil)
- unless image&.attachment&.attached?
- return nil
- end
+ def variant_url(image, variant_name)
+ return nil unless image&.attachment&.attached?
- if size
- Rails.application.routes.url_helpers.cdn_image_url(
- image.attachment.variant(resize_to_limit: size)
- )
- else
- Rails.application.routes.url_helpers.cdn_image_url(image.attachment)
- end
+ Rails.application.routes.url_helpers.cdn_image_url(
+ image.attachment.variant(variant_name)
+ )
end
end
end
diff --git a/api/app/serializers/spree/api/v3/line_item_serializer.rb b/api/app/serializers/spree/api/v3/line_item_serializer.rb
index 01d5ccfabb..77d57332ec 100644
--- a/api/app/serializers/spree/api/v3/line_item_serializer.rb
+++ b/api/app/serializers/spree/api/v3/line_item_serializer.rb
@@ -2,88 +2,18 @@ module Spree
module Api
module V3
class LineItemSerializer < BaseSerializer
- attributes :id, :variant_id, :quantity, :currency
-
- attribute :price do |line_item|
- line_item.price.to_f
- end
-
- attribute :display_price do |line_item|
- line_item.display_price.to_s
- end
-
- attribute :total do |line_item|
- line_item.total.to_f
- end
-
- attribute :display_total do |line_item|
- line_item.display_total.to_s
- end
-
- attribute :adjustment_total do |line_item|
- line_item.adjustment_total.to_f
- end
-
- attribute :display_adjustment_total do |line_item|
- line_item.display_adjustment_total.to_s
- end
-
- attribute :promo_total do |line_item|
- line_item.promo_total.to_f
- end
-
- attribute :display_promo_total do |line_item|
- line_item.display_promo_total.to_s
- end
-
- attribute :included_tax_total do |line_item|
- line_item.included_tax_total.to_f
- end
-
- attribute :display_included_tax_total do |line_item|
- line_item.display_included_tax_total.to_s
- end
-
- attribute :additional_tax_total do |line_item|
- line_item.additional_tax_total.to_f
- end
-
- attribute :display_additional_tax_total do |line_item|
- line_item.display_additional_tax_total.to_s
- end
-
- attribute :pre_tax_amount do |line_item|
- line_item.pre_tax_amount.to_f
- end
-
- attribute :display_pre_tax_amount do |line_item|
- line_item.display_pre_tax_amount.to_s
- end
-
- attribute :compare_at_amount do |line_item|
- line_item.compare_at_amount.to_f
- end
-
- attribute :display_compare_at_amount do |line_item|
- line_item.display_compare_at_amount.to_s
- end
-
- attribute :discounted_amount do |line_item|
- line_item.discounted_amount.to_f
- end
-
- attribute :display_discounted_amount do |line_item|
- line_item.display_discounted_amount.to_s
- end
-
- # Conditional associations
- one :variant,
- resource: Spree.api.v3_storefront_variant_serializer,
- if: proc { params[:includes]&.include?('variant') }
-
- one :product,
- resource: Spree.api.v3_storefront_product_serializer,
- if: proc { params[:includes]&.include?('product') }
+ attributes :id, :variant_id, :quantity, :name, :slug, :options_text,
+ :price, :display_price, :total, :display_total,
+ :adjustment_total, :display_adjustment_total,
+ :additional_tax_total, :display_additional_tax_total,
+ :included_tax_total, :display_included_tax_total,
+ :promo_total, :display_promo_total,
+ :pre_tax_amount, :display_pre_tax_amount,
+ :discounted_amount, :display_discounted_amount,
+ :compare_at_amount, :display_compare_at_amount
+
+ many :images, resource: Spree.api.v3_storefront_image_serializer
+ many :option_values, resource: Spree.api.v3_storefront_option_value_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/option_type_serializer.rb b/api/app/serializers/spree/api/v3/option_type_serializer.rb
index d525be1751..5fbbb6175d 100644
--- a/api/app/serializers/spree/api/v3/option_type_serializer.rb
+++ b/api/app/serializers/spree/api/v3/option_type_serializer.rb
@@ -3,10 +3,6 @@ module Spree
module V3
class OptionTypeSerializer < BaseSerializer
attributes :id, :name, :presentation, :position
-
- many :option_values,
- resource: Spree.api.v3_storefront_option_value_serializer,
- if: proc { params[:includes]&.include?('option_values') }
end
end
end
diff --git a/api/app/serializers/spree/api/v3/order_serializer.rb b/api/app/serializers/spree/api/v3/order_serializer.rb
index 7e3ac9cf8d..6df5c68b90 100644
--- a/api/app/serializers/spree/api/v3/order_serializer.rb
+++ b/api/app/serializers/spree/api/v3/order_serializer.rb
@@ -3,75 +3,14 @@ module Spree
module V3
class OrderSerializer < BaseSerializer
attributes :id, :number, :state, :token, :email, :special_instructions,
- :currency, :item_count, :shipment_state, :payment_state
-
- # Totals
- attribute :item_total do |order|
- order.item_total.to_f
- end
-
- attribute :display_item_total do |order|
- order.display_item_total.to_s
- end
-
- attribute :ship_total do |order|
- order.ship_total.to_f
- end
-
- attribute :display_ship_total do |order|
- order.display_ship_total.to_s
- end
-
- attribute :adjustment_total do |order|
- order.adjustment_total.to_f
- end
-
- attribute :display_adjustment_total do |order|
- order.display_adjustment_total.to_s
- end
-
- attribute :promo_total do |order|
- order.promo_total.to_f
- end
-
- attribute :display_promo_total do |order|
- order.display_promo_total.to_s
- end
-
- attribute :tax_total do |order|
- order.tax_total.to_f
- end
-
- attribute :display_tax_total do |order|
- order.display_tax_total.to_s
- end
-
- attribute :included_tax_total do |order|
- order.included_tax_total.to_f
- end
-
- attribute :display_included_tax_total do |order|
- order.display_included_tax_total.to_s
- end
-
- attribute :additional_tax_total do |order|
- order.additional_tax_total.to_f
- end
-
- attribute :display_additional_tax_total do |order|
- order.display_additional_tax_total.to_s
- end
-
- attribute :total do |order|
- order.total.to_f
- end
-
- attribute :display_total do |order|
- order.display_total.to_s
- end
+ :currency, :item_count, :shipment_state, :payment_state,
+ :item_total, :display_item_total, :ship_total, :display_ship_total,
+ :adjustment_total, :display_adjustment_total, :promo_total, :display_promo_total,
+ :tax_total, :display_tax_total, :included_tax_total, :display_included_tax_total,
+ :additional_tax_total, :display_additional_tax_total, :total, :display_total
# Timestamps
- attributes completed_at: :iso8601, created_at: :iso8601, updated_at: :iso8601
+ attributes completed_at: :iso8601
# Conditional associations
many :line_items,
@@ -84,21 +23,18 @@ module Spree
many :payments,
resource: Spree.api.v3_storefront_payment_serializer,
- if: proc { params[:includes]&.include?('payments') } do |order|
- order.payments.valid
- end
+ if: proc { params[:includes]&.include?('payments') }
- one :billing_address,
+ one :bill_address,
resource: Spree.api.v3_storefront_address_serializer,
- if: proc { params[:includes]&.include?('billing_address') } do |order|
- order.bill_address
- end
+ if: proc { params[:includes]&.include?('bill_address') }
- one :shipping_address,
+ one :ship_address,
resource: Spree.api.v3_storefront_address_serializer,
- if: proc { params[:includes]&.include?('shipping_address') } do |order|
- order.ship_address
- end
+ if: proc { params[:includes]&.include?('ship_address') }
+
+ many :payment_methods,
+ resource: Spree.api.v3_storefront_payment_method_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/payment_method_serializer.rb b/api/app/serializers/spree/api/v3/payment_method_serializer.rb
index 2092057a43..41329a58ea 100644
--- a/api/app/serializers/spree/api/v3/payment_method_serializer.rb
+++ b/api/app/serializers/spree/api/v3/payment_method_serializer.rb
@@ -2,8 +2,7 @@ module Spree
module Api
module V3
class PaymentMethodSerializer < BaseSerializer
- attributes :id, :name, :description, :type, :active,
- created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :name, :description, :type
end
end
end
diff --git a/api/app/serializers/spree/api/v3/payment_serializer.rb b/api/app/serializers/spree/api/v3/payment_serializer.rb
index da1284f7c4..ea6eaaf7c8 100644
--- a/api/app/serializers/spree/api/v3/payment_serializer.rb
+++ b/api/app/serializers/spree/api/v3/payment_serializer.rb
@@ -2,21 +2,9 @@ module Spree
module Api
module V3
class PaymentSerializer < BaseSerializer
- attributes :id, :state, :payment_method_id, :response_code, :number,
- :cvv_response_code, :cvv_response_message,
- created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :state, :payment_method_id, :response_code, :number, :amount, :display_amount
- attribute :amount do |payment|
- payment.amount.to_f
- end
-
- attribute :display_amount do |payment|
- payment.display_amount.to_s
- end
-
- attribute :payment_method_name do |payment|
- payment.payment_method&.name
- end
+ one :payment_method, resource: Spree.api.v3_storefront_payment_method_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/product_serializer.rb b/api/app/serializers/spree/api/v3/product_serializer.rb
index 832ec75328..7c7bd54c2b 100644
--- a/api/app/serializers/spree/api/v3/product_serializer.rb
+++ b/api/app/serializers/spree/api/v3/product_serializer.rb
@@ -4,7 +4,7 @@ module Spree
class ProductSerializer < BaseSerializer
attributes :id, :name, :description, :slug, :sku, :barcode,
:meta_description, :meta_keywords,
- available_on: :iso8601, created_at: :iso8601, updated_at: :iso8601
+ available_on: :iso8601
attribute :purchasable do |product|
product.purchasable?
diff --git a/api/app/serializers/spree/api/v3/shipment_serializer.rb b/api/app/serializers/spree/api/v3/shipment_serializer.rb
index 7bfc50f771..4a382cbb47 100644
--- a/api/app/serializers/spree/api/v3/shipment_serializer.rb
+++ b/api/app/serializers/spree/api/v3/shipment_serializer.rb
@@ -2,39 +2,15 @@ module Spree
module Api
module V3
class ShipmentSerializer < BaseSerializer
- attributes :id, :number, :state, :tracking,
- shipped_at: :iso8601, created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :number, :state, :tracking, :tracking_url, :shipped_at,
+ :cost, :display_cost, :adjustment_total, :display_adjustment_total,
+ :additional_tax_total, :display_additional_tax_total, :promo_total, :display_promo_total,
+ :included_tax_total, :display_included_tax_total, :total, :display_total
- attribute :cost do |shipment|
- shipment.cost.to_f
- end
+ one :shipping_method, resource: Spree.api.v3_storefront_shipping_method_serializer
+ one :stock_location, resource: Spree.api.v3_storefront_stock_location_serializer
- attribute :display_cost do |shipment|
- shipment.display_cost.to_s
- end
-
- attribute :shipping_method do |shipment|
- next unless shipment.shipping_method
-
- {
- id: shipment.shipping_method.id,
- name: shipment.shipping_method.name,
- code: shipment.shipping_method.code
- }
- end
-
- attribute :stock_location do |shipment|
- next unless shipment.stock_location
-
- {
- id: shipment.stock_location.id,
- name: shipment.stock_location.name
- }
- end
-
- many :shipping_rates,
- resource: Spree.api.v3_storefront_shipping_rate_serializer,
- if: proc { params[:includes]&.include?('shipping_rates') }
+ many :shipping_rates, resource: Spree.api.v3_storefront_shipping_rate_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/shipping_method_serializer.rb b/api/app/serializers/spree/api/v3/shipping_method_serializer.rb
index 6a025a775f..a49df30b54 100644
--- a/api/app/serializers/spree/api/v3/shipping_method_serializer.rb
+++ b/api/app/serializers/spree/api/v3/shipping_method_serializer.rb
@@ -2,8 +2,7 @@ module Spree
module Api
module V3
class ShippingMethodSerializer < BaseSerializer
- attributes :id, :name, :code, :tracking_url, :admin_name,
- created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :name, :code
end
end
end
diff --git a/api/app/serializers/spree/api/v3/shipping_rate_serializer.rb b/api/app/serializers/spree/api/v3/shipping_rate_serializer.rb
index c1ea936d0e..1437dc8614 100644
--- a/api/app/serializers/spree/api/v3/shipping_rate_serializer.rb
+++ b/api/app/serializers/spree/api/v3/shipping_rate_serializer.rb
@@ -2,8 +2,7 @@ module Spree
module Api
module V3
class ShippingRateSerializer < BaseSerializer
- attributes :id, :name, :selected, :shipping_method_id,
- created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :name, :selected, :shipping_method_id
attribute :cost do |shipping_rate|
shipping_rate.cost.to_f
@@ -13,9 +12,7 @@ module Spree
shipping_rate.display_cost.to_s
end
- attribute :shipping_method_code do |shipping_rate|
- shipping_rate.shipping_method&.code
- end
+ one :shipping_method, resource: Spree.api.v3_storefront_shipping_method_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/stock_location_serializer.rb b/api/app/serializers/spree/api/v3/stock_location_serializer.rb
new file mode 100644
index 0000000000..c70405e4f0
--- /dev/null
+++ b/api/app/serializers/spree/api/v3/stock_location_serializer.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Api
+ module V3
+ class StockLocationSerializer < BaseSerializer
+ attributes :id, :name, :address1, :city, :zipcode, :country_id, :country_iso, :country_name, :state_id, :state_text
+ end
+ end
+ end
+end
diff --git a/api/app/serializers/spree/api/v3/store_serializer.rb b/api/app/serializers/spree/api/v3/store_serializer.rb
index e3f26597b3..2dd06c4bc8 100644
--- a/api/app/serializers/spree/api/v3/store_serializer.rb
+++ b/api/app/serializers/spree/api/v3/store_serializer.rb
@@ -4,16 +4,33 @@ module Spree
class StoreSerializer < BaseSerializer
attributes :id, :name, :url, :meta_description, :meta_keywords, :seo_title,
:default_currency, :code, :default, :facebook, :twitter,
- :instagram, :customer_support_email, :default_locale,
- created_at: :iso8601, updated_at: :iso8601
+ :instagram, :customer_support_email, :default_locale
attribute :supported_currencies do |store|
store.supported_currencies_list.map(&:iso_code)
end
+ attribute :favicon_image_url do |store|
+ image_url_for(store.favicon_image)
+ end
+
+ attribute :logo_image_url do |store|
+ image_url_for(store.logo)
+ end
+
+ attribute :social_image_url do |store|
+ image_url_for(store.social_image)
+ end
+
attribute :supported_locales do |store|
store.supported_locales_list
end
+
+ many :payment_methods,
+ proc { |payment_methods, _params|
+ payment_methods.select { |pm| pm.available_on_front_end? && pm.active? }
+ },
+ resource: Spree.api.v3_storefront_payment_method_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/taxon_serializer.rb b/api/app/serializers/spree/api/v3/taxon_serializer.rb
index a292ce5ac4..b915accf9d 100644
--- a/api/app/serializers/spree/api/v3/taxon_serializer.rb
+++ b/api/app/serializers/spree/api/v3/taxon_serializer.rb
@@ -4,8 +4,7 @@ module Spree
class TaxonSerializer < BaseSerializer
attributes :id, :name, :permalink, :position, :lft, :rgt, :depth,
:meta_title, :meta_description, :meta_keywords,
- :parent_id, :taxonomy_id, :children_count,
- created_at: :iso8601, updated_at: :iso8601
+ :parent_id, :taxonomy_id, :children_count
attribute :description do |taxon|
taxon.description.to_plain_text
@@ -43,10 +42,6 @@ module Spree
many :children,
resource: Spree.api.v3_storefront_taxon_serializer,
if: proc { params[:includes]&.include?('children') }
-
- one :taxonomy,
- resource: Spree.api.v3_storefront_taxonomy_serializer,
- if: proc { params[:includes]&.include?('taxonomy') }
end
end
end
diff --git a/api/app/serializers/spree/api/v3/taxonomy_serializer.rb b/api/app/serializers/spree/api/v3/taxonomy_serializer.rb
index 1f706e7a50..ed9512ace5 100644
--- a/api/app/serializers/spree/api/v3/taxonomy_serializer.rb
+++ b/api/app/serializers/spree/api/v3/taxonomy_serializer.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
class TaxonomySerializer < BaseSerializer
- attributes :id, :name
+ attributes :id, :name, :position
# Conditional associations
one :root,
diff --git a/api/app/serializers/spree/api/v3/user_serializer.rb b/api/app/serializers/spree/api/v3/user_serializer.rb
index 9155535ff0..542a84f490 100644
--- a/api/app/serializers/spree/api/v3/user_serializer.rb
+++ b/api/app/serializers/spree/api/v3/user_serializer.rb
@@ -2,8 +2,11 @@ module Spree
module Api
module V3
class UserSerializer < BaseSerializer
- attributes :id, :email, :first_name, :last_name,
- created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :email, :first_name, :last_name
+
+ many :addresses, resource: Spree.api.v3_storefront_address_serializer
+ one :default_billing_address, resource: Spree.api.v3_storefront_address_serializer
+ one :default_shipping_address, resource: Spree.api.v3_storefront_address_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/variant_serializer.rb b/api/app/serializers/spree/api/v3/variant_serializer.rb
index 895e29d466..84a8275b22 100644
--- a/api/app/serializers/spree/api/v3/variant_serializer.rb
+++ b/api/app/serializers/spree/api/v3/variant_serializer.rb
@@ -60,9 +60,7 @@ module Spree
resource: Spree.api.v3_storefront_image_serializer,
if: proc { params[:includes]&.include?('images') }
- many :option_values,
- resource: Spree.api.v3_storefront_option_value_serializer,
- if: proc { params[:includes]&.include?('option_values') }
+ many :option_values, resource: Spree.api.v3_storefront_option_value_serializer
private
diff --git a/api/app/serializers/spree/api/v3/wished_item_serializer.rb b/api/app/serializers/spree/api/v3/wished_item_serializer.rb
index d784682c1f..5225baaac9 100644
--- a/api/app/serializers/spree/api/v3/wished_item_serializer.rb
+++ b/api/app/serializers/spree/api/v3/wished_item_serializer.rb
@@ -2,11 +2,9 @@ module Spree
module Api
module V3
class WishedItemSerializer < BaseSerializer
- attributes :id, :variant_id, :wishlist_id, :quantity, created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :variant_id, :wishlist_id, :quantity
- one :variant,
- resource: Spree.api.v3_storefront_variant_serializer,
- if: proc { params[:includes]&.include?('variant') }
+ one :variant, resource: Spree.api.v3_storefront_variant_serializer
end
end
end
diff --git a/api/app/serializers/spree/api/v3/wishlist_serializer.rb b/api/app/serializers/spree/api/v3/wishlist_serializer.rb
index dd81d77eba..9267a5eebf 100644
--- a/api/app/serializers/spree/api/v3/wishlist_serializer.rb
+++ b/api/app/serializers/spree/api/v3/wishlist_serializer.rb
@@ -2,7 +2,7 @@ module Spree
module Api
module V3
class WishlistSerializer < BaseSerializer
- attributes :id, :name, :token, created_at: :iso8601, updated_at: :iso8601
+ attributes :id, :name, :token
attribute :is_default do |wishlist|
wishlist.is_default?
@@ -12,12 +12,10 @@ module Spree
wishlist.is_private?
end
- many :items,
+ many :wished_items,
key: :items,
resource: Spree.api.v3_storefront_wished_item_serializer,
- if: proc { params[:includes]&.include?('items') } do |wishlist|
- wishlist.wished_items
- end
+ if: proc { params[:includes]&.include?('items') }
end
end
end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index cb6c946a33..b969d0e4bc 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -220,30 +220,24 @@ Spree::Core::Engine.add_routes do
namespace :v3 do
namespace :storefront do
- # ===== AUTHENTICATION =====
- # Public endpoints for login/register, JWT required for refresh
+ # Authentication
post 'auth/login', to: 'auth#create'
post 'auth/register', to: 'auth#register'
post 'auth/refresh', to: 'auth#refresh'
post 'auth/oauth/callback', to: 'auth#oauth_callback'
- # ===== STORE & CONFIGURATION (Public) =====
+ # Store
get 'store', to: 'stores#current'
# Geography
resources :countries, only: [:index, :show]
- # ===== CATALOG (Public) =====
- # Products with Ransack filtering
- # GET /products?q[name_cont]=shirt&q[s]=price+asc
+ # Catalog
resources :products, only: [:index, :show]
+ resources :taxonomies, only: [:index, :show]
+ resources :taxons, only: [:show]
- # Taxons (categories)
- resources :taxons, only: [:index, :show]
-
- # ===== ORDERS (Public create, token or JWT for access) =====
- # POST /orders - Create order (returns order_token for guests)
- # Access via X-Order-Token header OR JWT
+ # Orders
resources :orders do
# State transitions
member do
@@ -254,40 +248,23 @@ Spree::Core::Engine.add_routes do
end
# Nested resources - all require order access
- resources :line_items, only: [:index, :create, :show, :update, :destroy]
- resources :payments, only: [:index, :create, :show]
- resources :shipments, only: [:index, :show, :update]
+ resources :line_items, only: [:create, :show, :update, :destroy]
resources :coupon_codes, only: [:create, :destroy]
-
- # Available methods for order
- resources :shipping_methods, only: [:index]
- resources :payment_methods, only: [:index]
end
- # ===== CUSTOMER (JWT Required) =====
+ # Customer
get 'customers/me', to: 'customers#show'
patch 'customers/me', to: 'customers#update'
-
resources :addresses, only: [:index, :show, :create, :update, :destroy], path: 'customers/me/addresses'
resources :credit_cards, only: [:index, :show, :destroy], path: 'customers/me/credit_cards'
resources :payment_sources, only: [:index, :show, :destroy], path: 'customers/me/payment_sources'
- # ===== WISHLISTS (JWT Required) =====
+ # Wishlists
resources :wishlists do
- # Wishlist items as nested resource
resources :items, only: [:create, :update, :destroy], controller: 'wishlist_items'
end
- # ===== CMS & CONTENT (Public) =====
- resources :posts, only: [:index, :show]
- resources :policies, only: [:index, :show]
-
- # Pages (new page builder)
- resources :pages, only: [:index, :show] do
- resources :sections, only: [:index, :show]
- end
-
- # ===== DIGITAL DOWNLOADS =====
+ # Digital Downloads
# Access via token in URL
get 'digitals/:token', to: 'digitals#download', as: :digital_download
end
diff --git a/api/lib/spree/api/dependencies.rb b/api/lib/spree/api/dependencies.rb
index b66c3c9b5f..ac822ec8ce 100644
--- a/api/lib/spree/api/dependencies.rb
+++ b/api/lib/spree/api/dependencies.rb
@@ -95,7 +95,6 @@ module Spree
v3_storefront_product_serializer: 'Spree::Api::V3::ProductSerializer',
v3_storefront_variant_serializer: 'Spree::Api::V3::VariantSerializer',
v3_storefront_image_serializer: 'Spree::Api::V3::ImageSerializer',
- v3_storefront_taxon_serializer: 'Spree::Api::V3::TaxonSerializer',
v3_storefront_option_type_serializer: 'Spree::Api::V3::OptionTypeSerializer',
v3_storefront_option_value_serializer: 'Spree::Api::V3::OptionValueSerializer',
v3_storefront_order_serializer: 'Spree::Api::V3::OrderSerializer',
@@ -113,7 +112,9 @@ module Spree
v3_storefront_payment_method_serializer: 'Spree::Api::V3::PaymentMethodSerializer',
v3_storefront_shipping_method_serializer: 'Spree::Api::V3::ShippingMethodSerializer',
v3_storefront_shipping_rate_serializer: 'Spree::Api::V3::ShippingRateSerializer',
+ v3_storefront_stock_location_serializer: 'Spree::Api::V3::StockLocationSerializer',
v3_storefront_taxonomy_serializer: 'Spree::Api::V3::TaxonomySerializer',
+ v3_storefront_taxon_serializer: 'Spree::Api::V3::TaxonSerializer',
# platform serializers
platform_metafield_serializer: 'Spree::Api::V2::Platform::MetafieldSerializer',
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index f86209c837..1b141881ac 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -47,8 +47,8 @@ module Spree
after_create :update_tax_charge
- delegate :name, :description, :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, to: :variant
- delegate :brand, :category, to: :product
+ delegate :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, :images, :option_values, to: :variant
+ delegate :name, :description, :brand, :category, to: :product
delegate :tax_zone, to: :order
delegate :digital?, :can_supply?, to: :variant
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index a30789daba..5bbd4f2312 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -555,6 +555,10 @@ module Spree
payments.valid.completed.size == payments.valid.size && payments.valid.sum(:amount) >= total
end
+ def payment_methods
+ @payment_methods ||= store.payment_methods.active.available_on_front_end.select { |pm| pm.available_for_order?(self) }
+ end
+
def available_payment_methods(store = nil)
Spree::Deprecation.warn('`Order#available_payment_methods` is deprecated and will be removed in Spree 5.5. Use `collect_frontend_payment_methods` instead.')