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

#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



69
70
71
72
73
74
75
76
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 69

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

#createObject

POST /api/v3/admin/orders/:order_id/fulfillments

Manually registers a fulfillment on a completed order (external carrier / 3PL sync), bypassing order routing. Moves the requested line item quantities out of their routed fulfillments; when items is omitted, everything not yet shipped is moved. An explicit cost persists only with status: 'shipped' — pending fulfillments are re-priced by the rate engine.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 19

def create
  authorize!(:create, Spree::Shipment)

  with_order_lock do
    result = Spree.fulfillment_create_service.call(
      order: @order,
      stock_location: stock_location_for_create,
      items: items_for_create,
      tracking: create_params[:tracking],
      delivery_method: delivery_method_for_create,
      cost: create_params[:cost],
      status: create_params[:status],
      metadata: create_params[:metadata]&.to_h
    )

    if result.success?
      render json: serialize_resource(result.value), status: :created
    else
      render_result_error(result)
    end
  end
end

#fulfillObject

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



59
60
61
62
63
64
65
66
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 59

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



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

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb', line 89

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

    stock_location = if params[:stock_location_id].present?
                       find_stock_location!(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



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

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