PageRenderTime 1615ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

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