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.

Sprint 3 cleanup: drop redundant overrides, use existing Spree primitives

Damian Legawiec60262c3b7fbc
diff --git a/packages/admin-sdk/src/params.ts b/packages/admin-sdk/src/params.ts
index 834c410297..3e89965f30 100644
--- a/packages/admin-sdk/src/params.ts
+++ b/packages/admin-sdk/src/params.ts
@@ -463,14 +463,20 @@ export interface TaxCategoryUpdateParams {
is_default?: boolean
}
+/**
+ * Where the merchant exposes a payment method — `front_end` shows it on the
+ * storefront, `back_end` only in the admin (e.g. manual check capture),
+ * `both` everywhere.
+ */
+export type PaymentMethodDisplayOn = 'both' | 'front_end' | 'back_end'
+
export interface PaymentMethodCreateParams {
/** Fully-qualified STI subclass name, e.g. 'Spree::PaymentMethod::Check'. */
type: string
name: string
description?: string | null
active?: boolean
- /** 'front_end' | 'back_end' | 'both'. */
- display_on?: string
+ display_on?: PaymentMethodDisplayOn
auto_capture?: boolean | null
position?: number
metadata?: Record<string, unknown>
@@ -480,7 +486,7 @@ export interface PaymentMethodUpdateParams {
name?: string
description?: string | null
active?: boolean
- display_on?: string
+ display_on?: PaymentMethodDisplayOn
auto_capture?: boolean | null
position?: number
metadata?: Record<string, unknown>
diff --git a/spree/api/app/controllers/spree/api/v3/admin/payment_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/payment_methods_controller.rb
index a560149c4c..c6a563a2c8 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/payment_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/payment_methods_controller.rb
@@ -6,12 +6,12 @@ module Spree
scoped_resource :settings
def create
- klass = resolve_subclass(params[:type])
- return render_unknown_type if klass.nil?
+ klass = resolve_subclass(permitted_params[:type])
+ return render_unknown_type unless klass
- @resource = klass.new(permitted_params)
- authorize_resource!(@resource, :create)
+ @resource = klass.new(permitted_params.except(:type))
@resource.stores = [current_store] if @resource.stores.empty?
+ authorize_resource!(@resource, :create)
if @resource.save
render json: serialize_resource(@resource), status: :created
@@ -30,16 +30,12 @@ module Spree
Spree.api.admin_payment_method_serializer
end
- def scope
- current_store.payment_methods.accessible_by(current_ability, :show)
- end
-
- def permitted_params
- params.permit(:name, :description, :active, :display_on, :auto_capture, :position, metadata: {})
- end
-
private
+ # Looks up `type` against `Spree::PaymentMethod.providers` (the
+ # registered allowlist) so callers can't constantize arbitrary
+ # classes. Returns `Spree::PaymentMethod` itself when type is blank
+ # so the model layer can surface its own validation error.
def resolve_subclass(type_name)
return Spree::PaymentMethod if type_name.blank?
diff --git a/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb
index 5088a37b05..c0295e4fcf 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/stock_items_controller.rb
@@ -2,11 +2,9 @@ module Spree
module Api
module V3
module Admin
- # Lightweight `index`/`show`/`update`/`destroy` on `Spree::StockItem`
- # for inventory adjustments against existing variant/location
- # pairings. Stock items are auto-created when a variant lands at a
- # stock location, so there's no `create` route — use the variants
- # / stock-locations endpoints for that flow.
+ # Stock items are auto-created when a variant lands at a stock
+ # location, so there's deliberately no `create` route — use the
+ # variants / stock-locations endpoints for that flow.
class StockItemsController < ResourceController
scoped_resource :settings
@@ -23,10 +21,6 @@ module Spree
def collection_includes
[:stock_location, :variant]
end
-
- def permitted_params
- params.permit(:count_on_hand, :backorderable, metadata: {})
- end
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb
index 855668870b..8e0836e6a2 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/stock_transfers_controller.rb
@@ -2,29 +2,20 @@ module Spree
module Api
module V3
module Admin
- # Inventory movement between stock locations, or external →
- # location for receives. Pass `source_location_id` for transfers,
- # omit it for receives (vendor stock arriving). Each request body
- # carries a `variants` array of `{ variant_id, quantity }` pairs;
- # the model fans them out across `stock_movements` and adjusts the
- # source/destination `count_on_hand` atomically.
+ # Inventory movement between stock locations, or vendor → location
+ # for receives. Pass `source_location_id` for transfers; omit it to
+ # record an external receive.
class StockTransfersController < ResourceController
scoped_resource :settings
def create
authorize!(:create, model_class)
- destination = find_stock_location!(params[:destination_location_id])
- return if destination.nil?
-
- source = nil
- if params[:source_location_id].present?
- source = find_stock_location!(params[:source_location_id])
- return if source.nil?
- end
-
- variants_map = build_variants_map(params[:variants])
+ destination = Spree::StockLocation.find_by_prefix_id!(params[:destination_location_id])
+ source = params[:source_location_id].present? ?
+ Spree::StockLocation.find_by_prefix_id!(params[:source_location_id]) : nil
+ variants_map = build_variants_map
if variants_map.empty?
return render_error(
code: 'invalid_variants',
@@ -33,13 +24,11 @@ module Spree
)
end
- @resource = Spree::StockTransfer.new(
- source_location: source,
- destination_location: destination,
- reference: params[:reference]
- )
+ @resource = source ?
+ Spree::StockTransfer.new(reference: params[:reference]).tap { |t| t.transfer(source, destination, variants_map) } :
+ Spree::StockTransfer.new(reference: params[:reference]).tap { |t| t.receive(destination, variants_map) }
- if @resource.transfer(source, destination, variants_map)
+ if @resource.persisted?
render json: serialize_resource(@resource), status: :created
else
render_validation_error(@resource.errors)
@@ -62,36 +51,21 @@ module Spree
private
- def find_stock_location!(prefixed_id)
- location = Spree::StockLocation.find_by_prefix_id(prefixed_id)
- return location if location
-
- render_error(
- code: 'stock_location_not_found',
- message: Spree.t(:resource_not_found, scope: :api),
- status: :not_found
- )
- nil
- end
-
- # Resolves the request's `[{ variant_id, quantity }]` payload into
- # the `{ variant => quantity }` hash `StockTransfer#transfer`
- # expects. Variants the merchant doesn't have access to (or that
- # don't exist) are simply dropped — the model layer's
- # `stock_movements_not_empty` validation surfaces a 422 if the
- # final hash ends up empty.
- def build_variants_map(input)
- entries = input.respond_to?(:each) ? input.to_a : []
-
- entries.each_with_object({}) do |entry, acc|
- hash = entry.respond_to?(:to_unsafe_h) ? entry.to_unsafe_h : entry
- next unless hash.is_a?(Hash)
-
- variant = Spree::Variant.find_by_prefix_id(hash[:variant_id] || hash['variant_id'])
- quantity = (hash[:quantity] || hash['quantity']).to_i
- next if variant.nil? || quantity.zero?
+ # Variants the merchant doesn't have access to are dropped silently;
+ # if the resulting map is empty the action surfaces a 422
+ # `invalid_variants` so callers can distinguish "nothing supplied"
+ # from "all variants were rejected." A single SELECT covers any
+ # number of variants instead of N round-trips.
+ def build_variants_map
+ entries = params.permit(variants: [:variant_id, :quantity]).fetch(:variants, [])
+ quantities_by_id = entries.each_with_object({}) do |entry, hash|
+ decoded = Spree::PrefixedId.decode_prefixed_id(entry[:variant_id])
+ hash[decoded.to_i] = entry[:quantity].to_i if decoded
+ end
- acc[variant] = quantity
+ Spree::Variant.where(id: quantities_by_id.keys).each_with_object({}) do |variant, acc|
+ quantity = quantities_by_id[variant.id]
+ acc[variant] = quantity if quantity&.positive?
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/tax_categories_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/tax_categories_controller.rb
index 10a134dd14..df08636ab7 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/tax_categories_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/tax_categories_controller.rb
@@ -14,10 +14,6 @@ module Spree
def serializer_class
Spree.api.admin_tax_category_serializer
end
-
- def permitted_params
- params.permit(:name, :tax_code, :description, :is_default)
- end
end
end
end
diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rb
index b5a0e11ff5..478a439537 100644
--- a/spree/core/lib/spree/permitted_attributes.rb
+++ b/spree/core/lib/spree/permitted_attributes.rb
@@ -168,7 +168,7 @@ module Spree
@@payment_attributes = [:amount, :payment_method_id, :payment_method]
- @@payment_method_attributes = [:name, :type, :description, :active, :display_on, :auto_capture, :position]
+ @@payment_method_attributes = [:name, :type, :description, :active, :display_on, :auto_capture, :position, { metadata: {} }]
@@payment_session_attributes = [:amount, :payment_method_id, { external_data: {} }]
@@ -250,7 +250,7 @@ module Spree
:gateway_payment_profile_id, :last_digits, :name, :encrypted_data
]
- @@stock_item_attributes = [:variant_id, :stock_location_id, :backorderable, :count_on_hand]
+ @@stock_item_attributes = [:variant_id, :stock_location_id, :backorderable, :count_on_hand, { metadata: {} }]
@@stock_location_attributes = [
:name, :active, :address1, :address2, :city, :zipcode, :company,