Class: Spree::Api::V2::Platform::ShipmentsController

Inherits:
ResourceController show all
Includes:
NumberResource
Defined in:
app/controllers/spree/api/v2/platform/shipments_controller.rb

Constant Summary collapse

SHIPMENT_STATES =
%w[ready ship cancel resume pend]

Instance Method Summary collapse

Methods included from NumberResource

#resource

Methods inherited from ResourceController

#destroy

Methods inherited from ResourceController

#index, #show

Methods included from Caching

#collection_cache_key, #collection_cache_opts

Methods included from CollectionOptionsHelpers

#collection_links, #collection_meta, #collection_permitted_params

Methods inherited from BaseController

#content_type

Instance Method Details

#add_itemObject



35
36
37
38
39
40
41
42
# File 'app/controllers/spree/api/v2/platform/shipments_controller.rb', line 35

def add_item
  result = add_item_service.call(
    shipment: resource,
    variant_id: params.dig(:shipment, :variant_id),
    quantity: params.dig(:shipment, :quantity)
  )
  render_result(result)
end

#createObject



12
13
14
15
16
17
18
19
20
21
# File 'app/controllers/spree/api/v2/platform/shipments_controller.rb', line 12

def create
  # FIXME: support saving metadata
  result = create_service.call(
    store: current_store,
    shipment_attributes: params.require(:shipment).permit(
      :order_id, :stock_location_id, :variant_id, :quantity
    )
  )
  render_result(result, 201)
end

#remove_itemObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/spree/api/v2/platform/shipments_controller.rb', line 44

def remove_item
  result = remove_item_service.call(
    shipment: resource,
    variant_id: params.dig(:shipment, :variant_id),
    quantity: params.dig(:shipment, :quantity)
  )

  if result.success?
    if result.value == :shipment_deleted
      head 204
    else
      render_serialized_payload { serialize_resource(result.value) }
    end
  else
    render_error_payload(result.error)
  end
end

#transfer_to_locationObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/spree/api/v2/platform/shipments_controller.rb', line 62

def transfer_to_location
  stock_location = Spree::StockLocation.find(params.dig(:shipment, :stock_location_id))
  quantity = params.dig(:shipment, :quantity)&.to_i || 1

  unless quantity > 0
    render_error_payload("#{I18n.t('spree.api.shipment_transfer_errors_occurred')} \n #{I18n.t('spree.api.negative_quantity')}")
    return
  end

  transfer = resource.transfer_to_location(@variant, quantity, stock_location)
  if transfer.valid? && transfer.run!
    render json: { message: I18n.t('spree.api.shipment_transfer_success') }, status: 201
  else
    render_error_payload(transfer.errors)
  end
end

#transfer_to_shipmentObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/spree/api/v2/platform/shipments_controller.rb', line 79

def transfer_to_shipment
  target_shipment = Spree::Shipment.find_by!(number: params.dig(:shipment, :target_shipment_number))
  quantity = params.dig(:shipment, :quantity)&.to_i || 1

  error =
    if quantity < 0 && target_shipment == resource
      "#{I18n.t('spree.api.negative_quantity')}, \n#{I18n.t('spree.api.wrong_shipment_target')}"
    elsif target_shipment == resource
      I18n.t('spree.api.wrong_shipment_target')
    elsif quantity < 0
      I18n.t('spree.api.negative_quantity')
    end

  if error
    render_error_payload("#{I18n.t('spree.api.shipment_transfer_errors_occurred')} \n#{error}")
  else
    transfer = resource.transfer_to_shipment(@variant, quantity, target_shipment)
    if transfer.valid? && transfer.run!
      render json: { message: I18n.t('spree.api.shipment_transfer_success') }, status: 201
    else
      render_error_payload(transfer.errors)
    end
  end
end

#updateObject



23
24
25
26
# File 'app/controllers/spree/api/v2/platform/shipments_controller.rb', line 23

def update
  result = update_service.call(shipment: resource, shipment_attributes: permitted_resource_params)
  render_result(result)
end