Class: Spree::Api::ShipmentsController

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

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Instance Method Details

#addObject



79
80
81
82
83
84
# File 'app/controllers/spree/api/shipments_controller.rb', line 79

def add
  quantity = params[:quantity].to_i

  @shipment.order.contents.add(variant, quantity, { shipment: @shipment })
  respond_with(@shipment, default_template: :show)
end

#createObject



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

def create
  authorize! :create, Shipment
  quantity = params[:quantity].to_i
  @shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))
  @order.contents.add(variant, quantity, { shipment: @shipment })

  @shipment.save!

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

#estimated_ratesObject



27
28
29
30
31
# File 'app/controllers/spree/api/shipments_controller.rb', line 27

def estimated_rates
  authorize! :update, @shipment
  estimator = Spree::Config.stock.estimator_class.new
  @shipping_rates = estimator.shipping_rates(@shipment.to_package, false)
end

#mineObject



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

def mine
  if current_api_user
    @shipments = Spree::Shipment
      .reverse_chronological
      .joins(:order)
      .where(spree_orders: { user_id: current_api_user.id })
      .includes(mine_includes)
      .ransack(params[:q]).result

    @shipments = paginate(@shipments)
  else
    render "spree/api/errors/unauthorized", status: :unauthorized
  end
end

#readyObject



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

def ready
  unless @shipment.ready?
    if @shipment.can_ready?
      @shipment.ready!
    else
      logger.error("cannot_ready_shipment shipment_state=#{@shipment.state}")
      render('spree/api/shipments/cannot_ready_shipment', status: 422) && return
    end
  end
  respond_with(@shipment, default_template: :show)
end

#removeObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/spree/api/shipments_controller.rb', line 86

def remove
  quantity = params[:quantity].to_i

  if @shipment.shipped? || @shipment.canceled?
    @shipment.errors.add(:base, :cannot_remove_items_shipment_state, state: @shipment.state)
    invalid_resource!(@shipment)
  else
    @shipment.order.contents.remove(variant, quantity, { shipment: @shipment })
    @shipment.reload if @shipment.persisted?
    respond_with(@shipment, default_template: :show)
  end
end

#select_shipping_methodObject



33
34
35
36
37
38
39
# File 'app/controllers/spree/api/shipments_controller.rb', line 33

def select_shipping_method
  authorize! :update, @shipment
  shipping_method = Spree::ShippingMethod.find(params.require(:shipping_method_id))
  @shipment.select_shipping_method(shipping_method)
  @order.recalculate
  respond_with(@shipment, default_template: :show)
end

#shipObject



70
71
72
73
74
75
76
77
# File 'app/controllers/spree/api/shipments_controller.rb', line 70

def ship
  authorize! :ship, @shipment
  unless @shipment.shipped?
    @shipment.suppress_mailer = (params[:send_mailer] == 'false')
    @shipment.ship!
  end
  respond_with(@shipment, default_template: :show)
end

#transfer_to_locationObject



99
100
101
102
103
# File 'app/controllers/spree/api/shipments_controller.rb', line 99

def transfer_to_location
  @desired_stock_location = Spree::StockLocation.find(params[:stock_location_id])
  @desired_shipment = @original_shipment.order.shipments.build(stock_location: @desired_stock_location)
  transfer_to_shipment
end

#transfer_to_shipmentObject



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

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

  fulfilment_changer = Spree::FulfilmentChanger.new(
    current_shipment: @original_shipment,
    desired_shipment: @desired_shipment,
    variant: @variant,
    quantity: @quantity
  )

  if fulfilment_changer.run!
    render json: { success: true, message: t('spree.api.shipment.transfer_success') }, status: :accepted
  else
    render json: { success: false, message: fulfilment_changer.errors.full_messages.to_sentence }, status: :accepted
  end
end

#updateObject



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

def update
  @shipment.update_attributes_and_order(shipment_params)

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