Class: Spree::Api::OrdersController

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

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_user

Instance Method Summary collapse

Instance Method Details

#apply_coupon_codeObject



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

def apply_coupon_code
  Spree::Deprecation.warn('This method is deprecated. Please use `Spree::Api::CouponCodesController#create` endpoint instead.')

  authorize! :update, @order, order_token
  @order.coupon_code = params[:coupon_code]
  @handler = PromotionHandler::Coupon.new(@order).apply
  if @handler.successful?
    render "spree/api/promotions/handler", status: 200
  else
    logger.error("apply_coupon_code_error=#{@handler.error.inspect}")
    render "spree/api/promotions/handler", status: 422
  end
end

#cancelObject



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

def cancel
  authorize! :update, @order, params[:token]
  @order.canceled_by(current_api_user)
  respond_with(@order, default_template: :show)
end

#createObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/spree/api/orders_controller.rb', line 33

def create
  authorize! :create, Order

  if can?(:admin, Order)
    @order = Spree::Core::Importer::Order.import(determine_order_user, order_params)
    respond_with(@order, default_template: :show, status: 201)
  else
    @order = Spree::Order.create!(user: current_api_user, store: current_store)
    if @order.contents.update_cart order_params
      respond_with(@order, default_template: :show, status: 201)
    else
      invalid_resource!(@order)
    end
  end
end

#currentObject



91
92
93
94
95
96
97
# File 'app/controllers/spree/api/orders_controller.rb', line 91

def current
  if current_api_user && @order = current_api_user.last_incomplete_spree_order(store: current_store)
    respond_with(@order, default_template: :show, locals: { root_object: @order })
  else
    head :no_content
  end
end

#emptyObject



49
50
51
52
53
# File 'app/controllers/spree/api/orders_controller.rb', line 49

def empty
  authorize! :update, @order, order_token
  @order.empty!
  render plain: nil, status: 204
end

#indexObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/spree/api/orders_controller.rb', line 55

def index
  authorize! :index, Order
  orders_includes = [
    :user,
    :payments,
    :adjustments,
    :line_items
  ]
  @orders = paginate(
    Spree::Order
      .ransack(params[:q])
      .result
      .includes(orders_includes)
  )
  respond_with(@orders)
end

#mineObject



99
100
101
102
103
104
105
106
# File 'app/controllers/spree/api/orders_controller.rb', line 99

def mine
  if current_api_user
    @orders = current_api_user.orders.by_store(current_store).reverse_chronological.ransack(params[:q]).result
    @orders = paginate(@orders)
  else
    render "spree/api/errors/unauthorized", status: :unauthorized
  end
end

#showObject



72
73
74
75
# File 'app/controllers/spree/api/orders_controller.rb', line 72

def show
  authorize! :show, @order, order_token
  respond_with(@order)
end

#updateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/spree/api/orders_controller.rb', line 77

def update
  authorize! :update, @order, order_token

  if @order.contents.update_cart(order_params)
    user_id = params[:order][:user_id]
    if can?(:admin, @order) && user_id
      @order.associate_user!(Spree.user_class.find(user_id))
    end
    respond_with(@order, default_template: :show)
  else
    invalid_resource!(@order)
  end
end