Class: Spree::Api::V3::Admin::PriceListsController

Inherits:
ResourceController show all
Defined in:
app/controllers/spree/api/v3/admin/price_lists_controller.rb

Overview

Admin CRUD for ‘Spree::PriceList`, plus the lifecycle transitions (`activate` / `deactivate`) and the spreadsheet’s data feed (‘prices`).

Everything writable on the list — name, schedule, match policy, product membership (‘product_ids: […]`), nested rules (`rules: […]`), and individual price overrides (`prices: […]`) — flows through the regular PATCH payload, so the SPA saves the entire editor in one round-trip. No separate add_products / remove_products / add_rule / bulk_update_prices endpoints.

Scoped under the ‘products` API-key scope — price lists are a product/pricing concern; we don’t introduce a separate ‘read_price_lists` scope.

Constant Summary

Constants included from ScopedAuthorization

ScopedAuthorization::READ_ACTIONS

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods inherited from ResourceController

#create, #destroy, #index, #show, #update

Methods included from Spree::Api::V3::ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#activateObject

PATCH /api/v3/admin/price_lists/:id/activate

State transition: draft|inactive → active (or → scheduled when ‘starts_at` is in the future). Mirrors the old Rails admin’s “Activate” button which automatically scheduled future lists.



49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/spree/api/v3/admin/price_lists_controller.rb', line 49

def activate
  authorize! :update, @resource
  event = @resource.starts_at.present? && @resource.starts_at.future? ? :schedule : :activate

  if @resource.send(event)
    render json: serialize_resource(@resource)
  else
    render_validation_error(@resource.errors)
  end
end

#deactivateObject

PATCH /api/v3/admin/price_lists/:id/deactivate



61
62
63
64
65
66
67
68
69
# File 'app/controllers/spree/api/v3/admin/price_lists_controller.rb', line 61

def deactivate
  authorize! :update, @resource

  if @resource.deactivate
    render json: serialize_resource(@resource)
  else
    render_validation_error(@resource.errors)
  end
end

#price_rule_typesObject

GET /api/v3/admin/price_lists/price_rule_types

Returns ‘[{ type, label, description, preference_schema }]` for every registered subclass in `Spree.pricing.rules`. The SPA uses this to build the “Add rule” picker + render a generic preferences form per subclass. Rules themselves are not a separate REST resource — they ride along on the price list’s PATCH body via ‘rules: […]`.



39
40
41
42
# File 'app/controllers/spree/api/v3/admin/price_lists_controller.rb', line 39

def price_rule_types
  authorize! :read, Spree::PriceRule
  render json: { data: Spree::PriceRule.subclasses_with_preference_schema }
end

#pricesObject

GET /api/v3/admin/price_lists/:id/prices

The spreadsheet editor’s data source. Returns every Price row in this list (filtered by ‘?currency=`), eager-loading `variant.product` + option values so each cell can render product name, variant options and SKU without N+1.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/spree/api/v3/admin/price_lists_controller.rb', line 77

def prices
  authorize! :read, @resource
  currency = params[:currency].presence || current_store.default_currency
  prices = @resource.prices
                    .includes(variant: [:product, { option_values: :option_type }])
                    .where(currency: currency)
                    .joins(variant: :product)
                    .order(Arel.sql("#{Spree::Product.table_name}.name ASC"))
                    .order(Arel.sql("#{Spree::Variant.table_name}.position ASC"))

  render json: {
    data: prices.map { |p| serialize_price(p) },
    meta: { currency: currency, count: prices.size }
  }
end