Class: Spree::Api::V1::ShipmentsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/api/v1/shipments_controller.rb

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Methods inherited from BaseController

#content_type, #permitted_line_item_attributes

Methods included from ControllerSetup

included

Instance Method Details

#addObject



59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 59

def add
  quantity = params[:quantity].to_i

  Spree::Dependencies.cart_add_item_service.constantize.call(order: @shipment.order,
                                                             variant: variant,
                                                             quantity: quantity,
                                                             options: { shipment: @shipment })

  respond_with(@shipment, default_template: :show)
end

#createObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 21

def create
  @order = Spree::Order.find_by!(number: params.fetch(:shipment).fetch(:order_id))
  authorize! :show, @order
  authorize! :create, Shipment
  quantity = params[:quantity].to_i
  @shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))

  @line_item = Spree::Dependencies.cart_add_item_service.constantize.call(order: @order,
                                                                          variant: variant,
                                                                          quantity: quantity,
                                                                          options: { shipment: @shipment }).value

  respond_with(@shipment.reload, default_template: :show)
end

#mineObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 8

def mine
  if current_api_user.persisted?
    @shipments = Spree::Shipment.
                 reverse_chronological.
                 joins(:order).
                 where(spree_orders: { user_id: current_api_user.id }).
                 includes(mine_includes).
                 ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  else
    render 'spree/api/errors/unauthorized', status: :unauthorized
  end
end

#readyObject



43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 43

def ready
  unless @shipment.ready?
    if @shipment.can_ready?
      @shipment.ready!
    else
      render 'spree/api/v1/shipments/cannot_ready_shipment', status: 422 and return
    end
  end
  respond_with(@shipment, default_template: :show)
end

#removeObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 70

def remove
  quantity = if params.key?(:quantity)
               params[:quantity].to_i
             else
               @shipment.inventory_units_for(variant).sum(:quantity)
             end

  Spree::Dependencies.cart_remove_item_service.constantize.call(order: @shipment.order,
                                                                variant: variant,
                                                                quantity: quantity,
                                                                options: { shipment: @shipment })

  if @shipment.inventory_units.any?
    @shipment.reload
  else
    @shipment.destroy!
  end

  respond_with(@shipment, default_template: :show)
end

#shipObject



54
55
56
57
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 54

def ship
  @shipment.ship! unless @shipment.shipped?
  respond_with(@shipment, default_template: :show)
end

#transfer_to_locationObject



91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 91

def transfer_to_location
  @stock_location = Spree::StockLocation.find(params[:stock_location_id])

  unless @quantity > 0
    unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occured, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")
    return
  end

  @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
  render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
end

#transfer_to_shipmentObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 103

def transfer_to_shipment
  @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])

  error =
    if @quantity < 0 && @target_shipment == @original_shipment
      "#{Spree.t(:negative_quantity, scope: 'api')}, \n#{Spree.t('wrong_shipment_target', scope: 'api')}"
    elsif @target_shipment == @original_shipment
      Spree.t(:wrong_shipment_target, scope: 'api')
    elsif @quantity < 0
      Spree.t(:negative_quantity, scope: 'api')
    end

  if error
    unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occured, scope: 'api')} \n#{error}")
  else
    @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
    render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
  end
end

#updateObject



36
37
38
39
40
41
# File 'app/controllers/spree/api/v1/shipments_controller.rb', line 36

def update
  @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
  @shipment.update_attributes_and_order(shipment_params)

  respond_with(@shipment.reload, default_template: :show)
end