PageRenderTime 1631ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/api/app/controllers/spree/api/orders_controller.rb

https://github.com/slavix/spree
Ruby | 131 lines | 111 code | 19 blank | 1 comment | 15 complexity | e442ead55ff23a8edc5e898f965616ba MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. class OrdersController < Spree::Api::BaseController
  4. skip_before_filter :check_for_user_or_api_key, only: :apply_coupon_code
  5. skip_before_filter :authenticate_user, only: :apply_coupon_code
  6. before_filter :find_order, except: [:create, :mine, :index, :update]
  7. # Dynamically defines our stores checkout steps to ensure we check authorization on each step.
  8. Order.checkout_steps.keys.each do |step|
  9. define_method step do
  10. find_order
  11. authorize! :update, @order, params[:token]
  12. end
  13. end
  14. def cancel
  15. authorize! :update, @order, params[:token]
  16. @order.cancel!
  17. render :show
  18. end
  19. def create
  20. authorize! :create, Order
  21. order_user = if current_api_user.has_spree_role?('admin') && order_params[:user_id]
  22. Spree.user_class.find(order_params[:user_id])
  23. else
  24. current_api_user
  25. end
  26. @order = Spree::Core::Importer::Order.import(order_user, order_params)
  27. respond_with(@order, default_template: :show, status: 201)
  28. end
  29. def empty
  30. authorize! :update, @order, order_token
  31. @order.empty!
  32. render text: nil, status: 204
  33. end
  34. def index
  35. authorize! :index, Order
  36. @orders = Order.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  37. respond_with(@orders)
  38. end
  39. def show
  40. authorize! :show, @order, order_token
  41. respond_with(@order)
  42. end
  43. def update
  44. find_order(true)
  45. authorize! :update, @order, order_token
  46. if @order.contents.update_cart(order_params)
  47. user_id = params[:order][:user_id]
  48. if current_api_user.has_spree_role?('admin') && user_id
  49. @order.associate_user!(Spree.user_class.find(user_id))
  50. end
  51. respond_with(@order, default_template: :show)
  52. else
  53. invalid_resource!(@order)
  54. end
  55. end
  56. def mine
  57. if current_api_user.persisted?
  58. @orders = current_api_user.orders.reverse_chronological.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  59. else
  60. render "spree/api/errors/unauthorized", status: :unauthorized
  61. end
  62. end
  63. def apply_coupon_code
  64. find_order
  65. authorize! :update, @order, order_token
  66. @order.coupon_code = params[:coupon_code]
  67. @handler = PromotionHandler::Coupon.new(@order).apply
  68. status = @handler.successful? ? 200 : 422
  69. render "spree/api/promotions/handler", :status => status
  70. end
  71. private
  72. def order_params
  73. if params[:order]
  74. params[:order][:payments_attributes] = params[:order][:payments] if params[:order][:payments]
  75. params[:order][:shipments_attributes] = params[:order][:shipments] if params[:order][:shipments]
  76. params[:order][:line_items_attributes] = params[:order][:line_items] if params[:order][:line_items]
  77. params[:order][:ship_address_attributes] = params[:order][:ship_address] if params[:order][:ship_address]
  78. params[:order][:bill_address_attributes] = params[:order][:bill_address] if params[:order][:bill_address]
  79. params.require(:order).permit(permitted_order_attributes)
  80. else
  81. {}
  82. end
  83. end
  84. def permitted_order_attributes
  85. if current_api_user.has_spree_role? "admin"
  86. super << admin_order_attributes
  87. else
  88. super
  89. end
  90. end
  91. def permitted_shipment_attributes
  92. if current_api_user.has_spree_role? "admin"
  93. super << admin_shipment_attributes
  94. else
  95. super
  96. end
  97. end
  98. def admin_shipment_attributes
  99. [:shipping_method, :stock_location, :inventory_units => [:variant_id, :sku]]
  100. end
  101. def admin_order_attributes
  102. [:import, :number, :completed_at, :locked_at, :channel, :user_id]
  103. end
  104. def find_order(lock = false)
  105. @order = Spree::Order.lock(lock).find_by!(number: params[:id])
  106. end
  107. def order_id
  108. super || params[:id]
  109. end
  110. end
  111. end
  112. end