/api/app/controllers/spree/api/shipments_controller.rb

https://github.com/pdamer/spree-1 · Ruby · 149 lines · 127 code · 22 blank · 0 comment · 5 complexity · a9d37548ac27fef90c9d17e24be2fa21 MD5 · raw file

  1. module Spree
  2. module Api
  3. class ShipmentsController < Spree::Api::BaseController
  4. before_action :find_and_update_shipment, only: [:ship, :ready, :add, :remove]
  5. before_action :load_transfer_params, only: [:transfer_to_location, :transfer_to_shipment]
  6. def mine
  7. if current_api_user.persisted?
  8. @shipments = Spree::Shipment
  9. .reverse_chronological
  10. .joins(:order)
  11. .where(spree_orders: {user_id: current_api_user.id})
  12. .includes(mine_includes)
  13. .ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  14. else
  15. render "spree/api/errors/unauthorized", status: :unauthorized
  16. end
  17. end
  18. def create
  19. @order = Spree::Order.find_by!(number: params.fetch(:shipment).fetch(:order_id))
  20. authorize! :read, @order
  21. authorize! :create, Shipment
  22. quantity = params[:quantity].to_i
  23. @shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))
  24. @order.contents.add(variant, quantity, {shipment: @shipment})
  25. @shipment.save!
  26. respond_with(@shipment.reload, default_template: :show)
  27. end
  28. def update
  29. @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
  30. @shipment.update_attributes_and_order(shipment_params)
  31. respond_with(@shipment.reload, default_template: :show)
  32. end
  33. def ready
  34. unless @shipment.ready?
  35. if @shipment.can_ready?
  36. @shipment.ready!
  37. else
  38. render 'spree/api/shipments/cannot_ready_shipment', status: 422 and return
  39. end
  40. end
  41. respond_with(@shipment, default_template: :show)
  42. end
  43. def ship
  44. unless @shipment.shipped?
  45. @shipment.ship!
  46. end
  47. respond_with(@shipment, default_template: :show)
  48. end
  49. def add
  50. quantity = params[:quantity].to_i
  51. @shipment.order.contents.add(variant, quantity, {shipment: @shipment})
  52. respond_with(@shipment, default_template: :show)
  53. end
  54. def remove
  55. quantity = params[:quantity].to_i
  56. @shipment.order.contents.remove(variant, quantity, {shipment: @shipment})
  57. @shipment.reload if @shipment.persisted?
  58. respond_with(@shipment, default_template: :show)
  59. end
  60. def transfer_to_location
  61. @stock_location = Spree::StockLocation.find(params[:stock_location_id])
  62. @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
  63. render json: {success: true, message: Spree.t(:shipment_transfer_success)}, status: 201
  64. end
  65. def transfer_to_shipment
  66. @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])
  67. @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
  68. render json: {success: true, message: Spree.t(:shipment_transfer_success)}, status: 201
  69. end
  70. private
  71. def load_transfer_params
  72. @original_shipment = Spree::Shipment.where(number: params[:original_shipment_number]).first
  73. @variant = Spree::Variant.find(params[:variant_id])
  74. @quantity = params[:quantity].to_i
  75. authorize! :read, @original_shipment
  76. authorize! :create, Shipment
  77. end
  78. def find_and_update_shipment
  79. @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
  80. @shipment.update_attributes(shipment_params)
  81. @shipment.reload
  82. end
  83. def shipment_params
  84. if params[:shipment] && !params[:shipment].empty?
  85. params.require(:shipment).permit(permitted_shipment_attributes)
  86. else
  87. {}
  88. end
  89. end
  90. def variant
  91. @variant ||= Spree::Variant.unscoped.find(params.fetch(:variant_id))
  92. end
  93. def mine_includes
  94. {
  95. order: {
  96. bill_address: {
  97. state: {},
  98. country: {},
  99. },
  100. ship_address: {
  101. state: {},
  102. country: {},
  103. },
  104. adjustments: {},
  105. payments: {
  106. order: {},
  107. payment_method: {},
  108. },
  109. },
  110. inventory_units: {
  111. line_item: {
  112. product: {},
  113. variant: {},
  114. },
  115. variant: {
  116. product: {},
  117. default_price: {},
  118. option_values: {
  119. option_type: {},
  120. },
  121. },
  122. },
  123. }
  124. end
  125. end
  126. end
  127. end