Class: Spree::Api::V3::Admin::Orders::FulfillmentsController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb

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

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

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#cancelObject

PATCH /api/v3/admin/orders/:order_id/fulfillments/:id/cancel



38
39
40
41
42
43
44
45
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 38

def cancel
  with_order_lock do
    @resource.cancel!
    render json: serialize_resource(@resource.reload)
  rescue StateMachines::InvalidTransition => e
    render_service_error(e.message)
  end
end

#fulfillObject

PATCH /api/v3/admin/orders/:order_id/fulfillments/:id/fulfill



28
29
30
31
32
33
34
35
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 28

def fulfill
  with_order_lock do
    @resource.ship!
    render json: serialize_resource(@resource.reload)
  rescue StateMachines::InvalidTransition => e
    render_service_error(e.message)
  end
end

#resumeObject

PATCH /api/v3/admin/orders/:order_id/fulfillments/:id/resume



48
49
50
51
52
53
54
55
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 48

def resume
  with_order_lock do
    @resource.resume!
    render json: serialize_resource(@resource.reload)
  rescue StateMachines::InvalidTransition => e
    render_service_error(e.message)
  end
end

#splitObject

PATCH /api/v3/admin/orders/:order_id/fulfillments/:id/split



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 58

def split
  with_order_lock do
    variant = Spree::Variant.find_by_prefix_id!(params[:variant_id])
    quantity = params[:quantity].to_i

    stock_location = if params[:stock_location_id].present?
                       Spree::StockLocation.find_by_prefix_id!(params[:stock_location_id])
                     else
                       @resource.stock_location
                     end

    fulfilment_changer = @resource.transfer_to_location(variant, quantity, stock_location)

    if fulfilment_changer.run!
      fulfillments = @order.reload.shipments
      render json: {
        data: fulfillments.map { |s| serialize_resource(s) }
      }
    else
      render_validation_error(fulfilment_changer.errors)
    end
  end
end

#updateObject

PATCH /api/v3/admin/orders/:order_id/fulfillments/:id



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 12

def update
  with_order_lock do
    result = Spree.shipment_update_service.call(
      shipment: @resource,
      shipment_attributes: permitted_params.to_h
    )

    if result.success?
      render json: serialize_resource(@resource.reload)
    else
      render_result_error(result)
    end
  end
end