Class: Spree::Api::V3::Admin::StockTransfersController

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

Overview

Inventory movement between stock locations, or vendor → location for receives. Pass ‘source_location_id` for transfers; omit it to record an external receive.

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

#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

#createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/spree/api/v3/admin/stock_transfers_controller.rb', line 11

def create
  authorize!(:create, model_class)

  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',
      message: Spree.t('stock_transfer.errors.must_have_variant'),
      status: :unprocessable_content
    )
  end

  @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.persisted?
    render json: serialize_resource(@resource), status: :created
  else
    render_validation_error(@resource.errors)
  end
end