Class: Spree::Api::V3::Admin::ProductsController

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

Constant Summary

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 included from BulkOperations

#bulk_add_tags, #bulk_remove_tags

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

#bulk_add_to_categoriesObject

POST /api/v3/admin/products/bulk_add_to_categories Body: { ids: […], category_ids: […] }



54
55
56
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 54

def bulk_add_to_categories
  apply_categories(Spree::Taxons::AddProducts)
end

#bulk_add_to_channelsObject

POST /api/v3/admin/products/bulk_add_to_channels Body: { ids: […], channel_ids: […] }



66
67
68
69
70
71
72
73
74
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 66

def bulk_add_to_channels
  authorize! :update, model_class

  channels = scoped_channels
  product_ids = bulk_collection.distinct.ids
  channels.find_each { |channel| channel.add_products(product_ids) }

  render json: { product_count: product_ids.size, channel_count: channels.size }
end

#bulk_destroyObject

DELETE /api/v3/admin/products/bulk_destroy Body: { ids: […] }



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 90

def bulk_destroy
  authorize! :destroy, model_class

  # Scope by `:destroy` rather than reusing `bulk_collection`
  # (which is `:update`-scoped). Otherwise an admin with update
  # rights but no destroy rights could soft-delete records.
  destroy_scope = model_class.for_store(current_store)
                             .accessible_by(current_ability, :destroy)
                             .where(id: decode_ids(params[:ids]))
  destroyed = destroy_scope.count(&:destroy)

  render json: { product_count: destroyed }
end

#bulk_remove_from_categoriesObject

POST /api/v3/admin/products/bulk_remove_from_categories Body: { ids: […], category_ids: […] }



60
61
62
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 60

def bulk_remove_from_categories
  apply_categories(Spree::Taxons::RemoveProducts)
end

#bulk_remove_from_channelsObject

POST /api/v3/admin/products/bulk_remove_from_channels Body: { ids: […], channel_ids: […] }



78
79
80
81
82
83
84
85
86
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 78

def bulk_remove_from_channels
  authorize! :update, model_class

  channels = scoped_channels
  product_ids = bulk_collection.distinct.ids
  removed = channels.sum { |channel| channel.remove_products(product_ids) }

  render json: { product_count: product_ids.size, channel_count: channels.size, removed: removed }
end

#bulk_status_updateObject

POST /api/v3/admin/products/bulk_status_update Body: { ids: […], status: ‘draft’ | ‘active’ | ‘archived’ }



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 34

def bulk_status_update
  authorize! :update, model_class

  unless Spree::Product::STATUSES.include?(params[:status].to_s)
    return render_error(
      code: 'invalid_status',
      message: Spree.t(:invalid_status, scope: 'errors.messages', default: 'Invalid status'),
      status: :unprocessable_content
    )
  end

  count = bulk_collection.update_all(status: params[:status], updated_at: Time.current)
  # `update_all` skips `after_commit`, so the search index won't refresh on its own.
  bulk_collection.each(&:enqueue_search_index)

  render json: { product_count: count, status: params[:status] }
end

#cloneObject

POST /api/v3/admin/products/:id/clone



20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/spree/api/v3/admin/products_controller.rb', line 20

def clone
  @resource = find_resource
  authorize!(:create, @resource)

  result = @resource.duplicate
  if result.success?
    render json: serialize_resource(result.value), status: :created
  else
    render_service_error(result.error)
  end
end