spree_api_v1 4.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +189 -0
  3. data/.gitignore +23 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +24 -0
  6. data/CHANGELOG.md +3 -0
  7. data/CODE_OF_CONDUCT.md +22 -0
  8. data/Gemfile +59 -0
  9. data/LICENSE +26 -0
  10. data/README.md +62 -0
  11. data/Rakefile +15 -0
  12. data/app/controllers/spree/api/v1/addresses_controller.rb +46 -0
  13. data/app/controllers/spree/api/v1/base_controller.rb +174 -0
  14. data/app/controllers/spree/api/v1/checkouts_controller.rb +106 -0
  15. data/app/controllers/spree/api/v1/classifications_controller.rb +21 -0
  16. data/app/controllers/spree/api/v1/countries_controller.rb +22 -0
  17. data/app/controllers/spree/api/v1/credit_cards_controller.rb +26 -0
  18. data/app/controllers/spree/api/v1/customer_returns_controller.rb +25 -0
  19. data/app/controllers/spree/api/v1/images_controller.rb +58 -0
  20. data/app/controllers/spree/api/v1/inventory_units_controller.rb +54 -0
  21. data/app/controllers/spree/api/v1/line_items_controller.rb +70 -0
  22. data/app/controllers/spree/api/v1/option_types_controller.rb +60 -0
  23. data/app/controllers/spree/api/v1/option_values_controller.rb +62 -0
  24. data/app/controllers/spree/api/v1/orders_controller.rb +160 -0
  25. data/app/controllers/spree/api/v1/payments_controller.rb +82 -0
  26. data/app/controllers/spree/api/v1/product_properties_controller.rb +73 -0
  27. data/app/controllers/spree/api/v1/products_controller.rb +131 -0
  28. data/app/controllers/spree/api/v1/promotions_controller.rb +30 -0
  29. data/app/controllers/spree/api/v1/properties_controller.rb +70 -0
  30. data/app/controllers/spree/api/v1/reimbursements_controller.rb +25 -0
  31. data/app/controllers/spree/api/v1/return_authorizations_controller.rb +70 -0
  32. data/app/controllers/spree/api/v1/shipments_controller.rb +196 -0
  33. data/app/controllers/spree/api/v1/states_controller.rb +36 -0
  34. data/app/controllers/spree/api/v1/stock_items_controller.rb +82 -0
  35. data/app/controllers/spree/api/v1/stock_locations_controller.rb +53 -0
  36. data/app/controllers/spree/api/v1/stock_movements_controller.rb +45 -0
  37. data/app/controllers/spree/api/v1/stores_controller.rb +56 -0
  38. data/app/controllers/spree/api/v1/taxonomies_controller.rb +67 -0
  39. data/app/controllers/spree/api/v1/taxons_controller.rb +100 -0
  40. data/app/controllers/spree/api/v1/users_controller.rb +97 -0
  41. data/app/controllers/spree/api/v1/variants_controller.rb +81 -0
  42. data/app/controllers/spree/api/v1/zones_controller.rb +55 -0
  43. data/app/helpers/spree/api/api_helpers.rb +190 -0
  44. data/app/models/spree/api_v1_configuration.rb +5 -0
  45. data/app/views/spree/api/errors/gateway_error.rabl +2 -0
  46. data/app/views/spree/api/errors/invalid_api_key.rabl +2 -0
  47. data/app/views/spree/api/errors/invalid_resource.rabl +3 -0
  48. data/app/views/spree/api/errors/must_specify_api_key.rabl +2 -0
  49. data/app/views/spree/api/errors/not_found.rabl +2 -0
  50. data/app/views/spree/api/errors/unauthorized.rabl +2 -0
  51. data/app/views/spree/api/v1/addresses/show.rabl +10 -0
  52. data/app/views/spree/api/v1/adjustments/show.rabl +4 -0
  53. data/app/views/spree/api/v1/countries/index.rabl +7 -0
  54. data/app/views/spree/api/v1/countries/show.rabl +5 -0
  55. data/app/views/spree/api/v1/credit_cards/index.rabl +7 -0
  56. data/app/views/spree/api/v1/credit_cards/show.rabl +3 -0
  57. data/app/views/spree/api/v1/customer_returns/index.rabl +7 -0
  58. data/app/views/spree/api/v1/images/index.rabl +4 -0
  59. data/app/views/spree/api/v1/images/new.rabl +3 -0
  60. data/app/views/spree/api/v1/images/show.rabl +6 -0
  61. data/app/views/spree/api/v1/inventory_units/show.rabl +2 -0
  62. data/app/views/spree/api/v1/line_items/new.rabl +3 -0
  63. data/app/views/spree/api/v1/line_items/show.rabl +14 -0
  64. data/app/views/spree/api/v1/option_types/index.rabl +3 -0
  65. data/app/views/spree/api/v1/option_types/new.rabl +3 -0
  66. data/app/views/spree/api/v1/option_types/show.rabl +5 -0
  67. data/app/views/spree/api/v1/option_values/index.rabl +3 -0
  68. data/app/views/spree/api/v1/option_values/new.rabl +3 -0
  69. data/app/views/spree/api/v1/option_values/show.rabl +2 -0
  70. data/app/views/spree/api/v1/orders/address.rabl +0 -0
  71. data/app/views/spree/api/v1/orders/canceled.rabl +0 -0
  72. data/app/views/spree/api/v1/orders/cart.rabl +0 -0
  73. data/app/views/spree/api/v1/orders/complete.rabl +0 -0
  74. data/app/views/spree/api/v1/orders/could_not_apply_coupon.rabl +2 -0
  75. data/app/views/spree/api/v1/orders/could_not_transition.rabl +3 -0
  76. data/app/views/spree/api/v1/orders/index.rabl +7 -0
  77. data/app/views/spree/api/v1/orders/insufficient_quantity.rabl +2 -0
  78. data/app/views/spree/api/v1/orders/invalid_shipping_method.rabl +2 -0
  79. data/app/views/spree/api/v1/orders/mine.rabl +9 -0
  80. data/app/views/spree/api/v1/orders/order.rabl +10 -0
  81. data/app/views/spree/api/v1/orders/payment.rabl +3 -0
  82. data/app/views/spree/api/v1/orders/show.rabl +51 -0
  83. data/app/views/spree/api/v1/payments/credit_over_limit.rabl +2 -0
  84. data/app/views/spree/api/v1/payments/index.rabl +7 -0
  85. data/app/views/spree/api/v1/payments/new.rabl +5 -0
  86. data/app/views/spree/api/v1/payments/show.rabl +2 -0
  87. data/app/views/spree/api/v1/payments/update_forbidden.rabl +2 -0
  88. data/app/views/spree/api/v1/product_properties/index.rabl +7 -0
  89. data/app/views/spree/api/v1/product_properties/new.rabl +2 -0
  90. data/app/views/spree/api/v1/product_properties/show.rabl +2 -0
  91. data/app/views/spree/api/v1/products/index.rabl +9 -0
  92. data/app/views/spree/api/v1/products/new.rabl +3 -0
  93. data/app/views/spree/api/v1/products/product.rabl +1 -0
  94. data/app/views/spree/api/v1/products/show.rabl +36 -0
  95. data/app/views/spree/api/v1/promotions/handler.rabl +5 -0
  96. data/app/views/spree/api/v1/promotions/show.rabl +2 -0
  97. data/app/views/spree/api/v1/properties/index.rabl +7 -0
  98. data/app/views/spree/api/v1/properties/new.rabl +2 -0
  99. data/app/views/spree/api/v1/properties/show.rabl +2 -0
  100. data/app/views/spree/api/v1/reimbursements/index.rabl +7 -0
  101. data/app/views/spree/api/v1/return_authorizations/index.rabl +7 -0
  102. data/app/views/spree/api/v1/return_authorizations/new.rabl +3 -0
  103. data/app/views/spree/api/v1/return_authorizations/show.rabl +2 -0
  104. data/app/views/spree/api/v1/shared/stock_location_required.rabl +2 -0
  105. data/app/views/spree/api/v1/shipments/big.rabl +48 -0
  106. data/app/views/spree/api/v1/shipments/cannot_ready_shipment.rabl +2 -0
  107. data/app/views/spree/api/v1/shipments/mine.rabl +9 -0
  108. data/app/views/spree/api/v1/shipments/show.rabl +32 -0
  109. data/app/views/spree/api/v1/shipments/small.rabl +37 -0
  110. data/app/views/spree/api/v1/shipping_rates/show.rabl +2 -0
  111. data/app/views/spree/api/v1/states/index.rabl +12 -0
  112. data/app/views/spree/api/v1/states/show.rabl +2 -0
  113. data/app/views/spree/api/v1/stock_items/index.rabl +7 -0
  114. data/app/views/spree/api/v1/stock_items/show.rabl +5 -0
  115. data/app/views/spree/api/v1/stock_locations/index.rabl +7 -0
  116. data/app/views/spree/api/v1/stock_locations/show.rabl +8 -0
  117. data/app/views/spree/api/v1/stock_movements/index.rabl +7 -0
  118. data/app/views/spree/api/v1/stock_movements/show.rabl +5 -0
  119. data/app/views/spree/api/v1/stores/index.rabl +4 -0
  120. data/app/views/spree/api/v1/stores/show.rabl +2 -0
  121. data/app/views/spree/api/v1/tags/index.rabl +9 -0
  122. data/app/views/spree/api/v1/taxonomies/index.rabl +7 -0
  123. data/app/views/spree/api/v1/taxonomies/jstree.rabl +7 -0
  124. data/app/views/spree/api/v1/taxonomies/nested.rabl +11 -0
  125. data/app/views/spree/api/v1/taxonomies/new.rabl +3 -0
  126. data/app/views/spree/api/v1/taxonomies/show.rabl +15 -0
  127. data/app/views/spree/api/v1/taxons/index.rabl +10 -0
  128. data/app/views/spree/api/v1/taxons/jstree.rabl +7 -0
  129. data/app/views/spree/api/v1/taxons/new.rabl +3 -0
  130. data/app/views/spree/api/v1/taxons/show.rabl +6 -0
  131. data/app/views/spree/api/v1/taxons/taxons.rabl +5 -0
  132. data/app/views/spree/api/v1/users/index.rabl +7 -0
  133. data/app/views/spree/api/v1/users/new.rabl +3 -0
  134. data/app/views/spree/api/v1/users/show.rabl +11 -0
  135. data/app/views/spree/api/v1/variants/big.rabl +14 -0
  136. data/app/views/spree/api/v1/variants/index.rabl +9 -0
  137. data/app/views/spree/api/v1/variants/new.rabl +2 -0
  138. data/app/views/spree/api/v1/variants/show.rabl +3 -0
  139. data/app/views/spree/api/v1/variants/small.rabl +18 -0
  140. data/app/views/spree/api/v1/zones/index.rabl +7 -0
  141. data/app/views/spree/api/v1/zones/show.rabl +6 -0
  142. data/config/initializers/rabl.rb +9 -0
  143. data/config/routes.rb +123 -0
  144. data/lib/spree/api_v1/controller_setup.rb +19 -0
  145. data/lib/spree/api_v1/engine.rb +27 -0
  146. data/lib/spree/api_v1/factories.rb +6 -0
  147. data/lib/spree/api_v1/responders/rabl_template.rb +28 -0
  148. data/lib/spree/api_v1/responders.rb +11 -0
  149. data/lib/spree/api_v1/testing_support/helpers.rb +44 -0
  150. data/lib/spree/api_v1/testing_support/setup.rb +16 -0
  151. data/lib/spree/api_v1/version.rb +9 -0
  152. data/lib/spree/api_v1.rb +9 -0
  153. data/lib/spree_api_v1.rb +3 -0
  154. data/script/rails +9 -0
  155. data/spec/fixtures/files/icon_256x256.jpg +0 -0
  156. data/spec/fixtures/thinking-cat.jpg +0 -0
  157. data/spree_api_v1.gemspec +26 -0
  158. metadata +261 -0
@@ -0,0 +1,82 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class PaymentsController < Spree::Api::V1::BaseController
5
+ before_action :find_order
6
+ before_action :find_payment, only: [:update, :show, :authorize, :purchase, :capture, :void]
7
+
8
+ def index
9
+ @payments = @order.payments.ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
10
+ respond_with(@payments)
11
+ end
12
+
13
+ def new
14
+ @payment_methods = Spree::PaymentMethod.available
15
+ respond_with(@payment_methods)
16
+ end
17
+
18
+ def create
19
+ @order.validate_payments_attributes([payment_params])
20
+ @payment = @order.payments.build(payment_params)
21
+ if @payment.save
22
+ respond_with(@payment, status: 201, default_template: :show)
23
+ else
24
+ invalid_resource!(@payment)
25
+ end
26
+ end
27
+
28
+ def update
29
+ authorize! params[:action], @payment
30
+ if !@payment.editable?
31
+ render 'update_forbidden', status: 403
32
+ elsif @payment.update(payment_params)
33
+ respond_with(@payment, default_template: :show)
34
+ else
35
+ invalid_resource!(@payment)
36
+ end
37
+ end
38
+
39
+ def show
40
+ respond_with(@payment)
41
+ end
42
+
43
+ def authorize
44
+ perform_payment_action(:authorize)
45
+ end
46
+
47
+ def capture
48
+ perform_payment_action(:capture)
49
+ end
50
+
51
+ def purchase
52
+ perform_payment_action(:purchase)
53
+ end
54
+
55
+ def void
56
+ perform_payment_action(:void_transaction)
57
+ end
58
+
59
+ private
60
+
61
+ def find_order
62
+ @order = Spree::Order.find_by!(number: order_id)
63
+ authorize! :show, @order, order_token
64
+ end
65
+
66
+ def find_payment
67
+ @payment = @order.payments.find_by!(number: params[:id])
68
+ end
69
+
70
+ def perform_payment_action(action, *args)
71
+ authorize! action, Payment
72
+ @payment.send("#{action}!", *args)
73
+ respond_with(@payment, default_template: :show)
74
+ end
75
+
76
+ def payment_params
77
+ params.require(:payment).permit(permitted_payment_attributes)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,73 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ProductPropertiesController < Spree::Api::V1::BaseController
5
+ before_action :find_product, :authorize_product!
6
+ before_action :product_property, only: [:show, :update, :destroy]
7
+
8
+ def index
9
+ @product_properties = @product.product_properties.accessible_by(current_ability).
10
+ ransack(params[:q]).result.
11
+ page(params[:page]).per(params[:per_page])
12
+ respond_with(@product_properties)
13
+ end
14
+
15
+ def show
16
+ respond_with(@product_property)
17
+ end
18
+
19
+ def new; end
20
+
21
+ def create
22
+ authorize! :create, ProductProperty
23
+ @product_property = @product.product_properties.new(product_property_params)
24
+ if @product_property.save
25
+ respond_with(@product_property, status: 201, default_template: :show)
26
+ else
27
+ invalid_resource!(@product_property)
28
+ end
29
+ end
30
+
31
+ def update
32
+ authorize! :update, @product_property
33
+
34
+ if @product_property.update(product_property_params)
35
+ respond_with(@product_property, status: 200, default_template: :show)
36
+ else
37
+ invalid_resource!(@product_property)
38
+ end
39
+ end
40
+
41
+ def destroy
42
+ authorize! :destroy, @product_property
43
+ @product_property.destroy
44
+ respond_with(@product_property, status: 204)
45
+ end
46
+
47
+ private
48
+
49
+ def find_product
50
+ super(params[:product_id])
51
+ end
52
+
53
+ def authorize_product!
54
+ authorize! :show, @product
55
+ end
56
+
57
+ def product_property
58
+ if @product
59
+ @product_property ||= @product.product_properties.find_by(id: params[:id])
60
+ @product_property ||= @product.product_properties.includes(:property).where(spree_properties: { name: params[:id] }).first
61
+ raise ActiveRecord::RecordNotFound unless @product_property
62
+
63
+ authorize! :show, @product_property
64
+ end
65
+ end
66
+
67
+ def product_property_params
68
+ params.require(:product_property).permit(permitted_product_properties_attributes)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,131 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ProductsController < Spree::Api::V1::BaseController
5
+ before_action :find_product, only: [:update, :show, :destroy]
6
+
7
+ def index
8
+ @products = if params[:ids]
9
+ product_scope.where(id: params[:ids].split(',').flatten)
10
+ else
11
+ product_scope.ransack(params[:q]).result
12
+ end
13
+
14
+ @products = @products.distinct.page(params[:page]).per(params[:per_page])
15
+ expires_in 15.minutes, public: true
16
+ headers['Surrogate-Control'] = "max-age=#{15.minutes}"
17
+ respond_with(@products)
18
+ end
19
+
20
+ def show
21
+ expires_in 15.minutes, public: true
22
+ headers['Surrogate-Control'] = "max-age=#{15.minutes}"
23
+ headers['Surrogate-Key'] = 'product_id=1'
24
+ respond_with(@product)
25
+ end
26
+
27
+ # Takes besides the products attributes either an array of variants or
28
+ # an array of option types.
29
+ #
30
+ # By submitting an array of variants the option types will be created
31
+ # using the *name* key in options hash. e.g
32
+ #
33
+ # product: {
34
+ # ...
35
+ # variants: {
36
+ # price: 19.99,
37
+ # sku: "hey_you",
38
+ # options: [
39
+ # { name: "size", value: "small" },
40
+ # { name: "color", value: "black" }
41
+ # ]
42
+ # }
43
+ # }
44
+ #
45
+ # Or just pass in the option types hash:
46
+ #
47
+ # product: {
48
+ # ...
49
+ # option_types: ['size', 'color']
50
+ # }
51
+ #
52
+ # By passing the shipping category name you can fetch or create that
53
+ # shipping category on the fly. e.g.
54
+ #
55
+ # product: {
56
+ # ...
57
+ # shipping_category: "Free Shipping Items"
58
+ # }
59
+ #
60
+ def new; end
61
+
62
+ def create
63
+ authorize! :create, Product
64
+ params[:product][:available_on] ||= Time.current
65
+ set_up_shipping_category
66
+
67
+ options = { store: current_store, variants_attrs: variants_params, options_attrs: option_types_params }
68
+ @product = Core::Importer::Product.new(nil, product_params, options).create
69
+
70
+ if @product.persisted?
71
+ respond_with(@product, status: 201, default_template: :show)
72
+ else
73
+ invalid_resource!(@product)
74
+ end
75
+ end
76
+
77
+ def update
78
+ authorize! :update, @product
79
+
80
+ options = { store: current_store, variants_attrs: variants_params, options_attrs: option_types_params }
81
+ @product = Core::Importer::Product.new(@product, product_params, options).update
82
+
83
+ if @product.errors.empty?
84
+ respond_with(@product.reload, status: 200, default_template: :show)
85
+ else
86
+ invalid_resource!(@product)
87
+ end
88
+ end
89
+
90
+ def destroy
91
+ authorize! :destroy, @product
92
+ @product.destroy
93
+ respond_with(@product, status: 204)
94
+ end
95
+
96
+ private
97
+
98
+ def product_params
99
+ params.require(:product).permit(permitted_product_attributes)
100
+ end
101
+
102
+ def variants_params
103
+ variants_key = if params[:product].key? :variants
104
+ :variants
105
+ else
106
+ :variants_attributes
107
+ end
108
+
109
+ params.require(:product).permit(
110
+ variants_key => [permitted_variant_attributes, :id]
111
+ ).delete(variants_key) || []
112
+ end
113
+
114
+ def option_types_params
115
+ params[:product].fetch(:option_types, [])
116
+ end
117
+
118
+ def find_product
119
+ super(params[:id])
120
+ end
121
+
122
+ def set_up_shipping_category
123
+ if shipping_category = params[:product].delete(:shipping_category)
124
+ id = ShippingCategory.find_or_create_by(name: shipping_category).id
125
+ params[:product][:shipping_category_id] = id
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,30 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class PromotionsController < Spree::Api::V1::BaseController
5
+ before_action :requires_admin
6
+ before_action :load_promotion
7
+
8
+ def show
9
+ if @promotion
10
+ respond_with(@promotion, default_template: :show)
11
+ else
12
+ raise ActiveRecord::RecordNotFound
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def requires_admin
19
+ return if @current_user_roles.include?('admin')
20
+
21
+ unauthorized and return
22
+ end
23
+
24
+ def load_promotion
25
+ @promotion = Spree::Promotion.find_by(id: params[:id]) || Spree::Promotion.with_coupon_code(params[:id])
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,70 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class PropertiesController < Spree::Api::V1::BaseController
5
+ before_action :find_property, only: [:show, :update, :destroy]
6
+
7
+ def index
8
+ @properties = Spree::Property.accessible_by(current_ability)
9
+
10
+ @properties = if params[:ids]
11
+ @properties.where(id: params[:ids].split(',').flatten)
12
+ else
13
+ @properties.ransack(params[:q]).result
14
+ end
15
+
16
+ @properties = @properties.page(params[:page]).per(params[:per_page])
17
+ respond_with(@properties)
18
+ end
19
+
20
+ def show
21
+ respond_with(@property)
22
+ end
23
+
24
+ def new; end
25
+
26
+ def create
27
+ authorize! :create, Property
28
+ @property = Spree::Property.new(property_params)
29
+ if @property.save
30
+ respond_with(@property, status: 201, default_template: :show)
31
+ else
32
+ invalid_resource!(@property)
33
+ end
34
+ end
35
+
36
+ def update
37
+ if @property
38
+ authorize! :update, @property
39
+ @property.update(property_params)
40
+ respond_with(@property, status: 200, default_template: :show)
41
+ else
42
+ invalid_resource!(@property)
43
+ end
44
+ end
45
+
46
+ def destroy
47
+ if @property
48
+ authorize! :destroy, @property
49
+ @property.destroy
50
+ respond_with(@property, status: 204)
51
+ else
52
+ invalid_resource!(@property)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def find_property
59
+ @property = Spree::Property.accessible_by(current_ability, :show).find(params[:id])
60
+ rescue ActiveRecord::RecordNotFound
61
+ @property = Spree::Property.accessible_by(current_ability, :show).find_by!(name: params[:id])
62
+ end
63
+
64
+ def property_params
65
+ params.require(:property).permit(permitted_property_attributes)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ReimbursementsController < Spree::Api::V1::BaseController
5
+ def index
6
+ collection(Spree::Reimbursement)
7
+ respond_with(@collection)
8
+ end
9
+
10
+ private
11
+
12
+ def collection(resource)
13
+ return @collection if @collection.present?
14
+
15
+ params[:q] ||= {}
16
+
17
+ @collection = resource.all
18
+ # @search needs to be defined as this is passed to search_form_for
19
+ @search = @collection.ransack(params[:q])
20
+ @collection = @search.result.order(created_at: :desc).page(params[:page]).per(params[:per_page])
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,70 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ReturnAuthorizationsController < Spree::Api::V1::BaseController
5
+ def create
6
+ authorize! :create, ReturnAuthorization
7
+ @return_authorization = order.return_authorizations.build(return_authorization_params)
8
+ if @return_authorization.save
9
+ respond_with(@return_authorization, status: 201, default_template: :show)
10
+ else
11
+ invalid_resource!(@return_authorization)
12
+ end
13
+ end
14
+
15
+ def destroy
16
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :destroy).find(params[:id])
17
+ @return_authorization.destroy
18
+ respond_with(@return_authorization, status: 204)
19
+ end
20
+
21
+ def index
22
+ authorize! :admin, ReturnAuthorization
23
+ @return_authorizations = order.return_authorizations.accessible_by(current_ability).
24
+ ransack(params[:q]).result.
25
+ page(params[:page]).per(params[:per_page])
26
+ respond_with(@return_authorizations)
27
+ end
28
+
29
+ def new
30
+ authorize! :admin, ReturnAuthorization
31
+ end
32
+
33
+ def show
34
+ authorize! :admin, ReturnAuthorization
35
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :show).find(params[:id])
36
+ respond_with(@return_authorization)
37
+ end
38
+
39
+ def update
40
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
41
+ if @return_authorization.update(return_authorization_params)
42
+ respond_with(@return_authorization, default_template: :show)
43
+ else
44
+ invalid_resource!(@return_authorization)
45
+ end
46
+ end
47
+
48
+ def cancel
49
+ @return_authorization = order.return_authorizations.accessible_by(current_ability, :update).find(params[:id])
50
+ if @return_authorization.cancel
51
+ respond_with @return_authorization, default_template: :show
52
+ else
53
+ invalid_resource!(@return_authorization)
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def order
60
+ @order ||= Spree::Order.find_by!(number: order_id)
61
+ authorize! :show, @order
62
+ end
63
+
64
+ def return_authorization_params
65
+ params.require(:return_authorization).permit(permitted_return_authorization_attributes)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,196 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ShipmentsController < Spree::Api::V1::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
+
8
+ def mine
9
+ if current_api_user.persisted?
10
+ @shipments = Spree::Shipment.
11
+ reverse_chronological.
12
+ joins(:order).
13
+ where(spree_orders: { user_id: current_api_user.id }).
14
+ includes(mine_includes).
15
+ ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
16
+ else
17
+ render 'spree/api/errors/unauthorized', status: :unauthorized
18
+ end
19
+ end
20
+
21
+ def create
22
+ @order = Spree::Order.find_by!(number: params.fetch(:shipment).fetch(:order_id))
23
+ authorize! :show, @order
24
+ authorize! :create, Shipment
25
+ quantity = params[:quantity].to_i
26
+ @shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))
27
+
28
+ @line_item = Spree::Dependencies.cart_add_item_service.constantize.call(order: @order,
29
+ variant: variant,
30
+ quantity: quantity,
31
+ options: { shipment: @shipment }).value
32
+
33
+ respond_with(@shipment.reload, default_template: :show)
34
+ end
35
+
36
+ def update
37
+ @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
38
+ @shipment.update_attributes_and_order(shipment_params)
39
+
40
+ respond_with(@shipment.reload, default_template: :show)
41
+ end
42
+
43
+ def ready
44
+ unless @shipment.ready?
45
+ if @shipment.can_ready?
46
+ @shipment.ready!
47
+ else
48
+ render 'spree/api/v1/shipments/cannot_ready_shipment', status: 422 and return
49
+ end
50
+ end
51
+ respond_with(@shipment, default_template: :show)
52
+ end
53
+
54
+ def ship
55
+ @shipment.ship! unless @shipment.shipped?
56
+ respond_with(@shipment, default_template: :show)
57
+ end
58
+
59
+ def add
60
+ quantity = params[:quantity].to_i
61
+
62
+ Spree::Dependencies.cart_add_item_service.constantize.call(order: @shipment.order,
63
+ variant: variant,
64
+ quantity: quantity,
65
+ options: { shipment: @shipment })
66
+
67
+ respond_with(@shipment, default_template: :show)
68
+ end
69
+
70
+ def remove
71
+ quantity = if params.key?(:quantity)
72
+ params[:quantity].to_i
73
+ else
74
+ @shipment.inventory_units_for(variant).sum(:quantity)
75
+ end
76
+
77
+ Spree::Dependencies.cart_remove_item_service.constantize.call(order: @shipment.order,
78
+ variant: variant,
79
+ quantity: quantity,
80
+ options: { shipment: @shipment })
81
+
82
+ if @shipment.inventory_units.any?
83
+ @shipment.reload
84
+ else
85
+ @shipment.destroy!
86
+ end
87
+
88
+ respond_with(@shipment, default_template: :show)
89
+ end
90
+
91
+ def transfer_to_location
92
+ @stock_location = Spree::StockLocation.find(params[:stock_location_id])
93
+
94
+ unless @quantity > 0
95
+ unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")
96
+ return
97
+ end
98
+
99
+ transfer = @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
100
+ if transfer.valid?
101
+ transfer.run!
102
+ render json: { message: Spree.t(:shipment_transfer_success) }, status: 201
103
+ else
104
+ render json: { message: transfer.errors.full_messages.to_sentence }, status: 422
105
+ end
106
+ end
107
+
108
+ def transfer_to_shipment
109
+ @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])
110
+
111
+ error =
112
+ if @quantity < 0 && @target_shipment == @original_shipment
113
+ "#{Spree.t(:negative_quantity, scope: 'api')}, \n#{Spree.t('wrong_shipment_target', scope: 'api')}"
114
+ elsif @target_shipment == @original_shipment
115
+ Spree.t(:wrong_shipment_target, scope: 'api')
116
+ elsif @quantity < 0
117
+ Spree.t(:negative_quantity, scope: 'api')
118
+ end
119
+
120
+ if error
121
+ unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occurred, scope: 'api')} \n#{error}")
122
+ else
123
+ transfer = @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
124
+ if transfer.valid?
125
+ transfer.run!
126
+ render json: { message: Spree.t(:shipment_transfer_success) }, status: 201
127
+ else
128
+ render json: { message: transfer.errors.full_messages }, status: 422
129
+ end
130
+ end
131
+ end
132
+
133
+ private
134
+
135
+ def load_transfer_params
136
+ @original_shipment = Spree::Shipment.find_by!(number: params[:original_shipment_number])
137
+ @variant = Spree::Variant.find(params[:variant_id])
138
+ @quantity = params[:quantity].to_i
139
+ authorize! :show, @original_shipment
140
+ authorize! :create, Shipment
141
+ end
142
+
143
+ def find_and_update_shipment
144
+ @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
145
+ @shipment.update(shipment_params)
146
+ @shipment.reload
147
+ end
148
+
149
+ def shipment_params
150
+ if params[:shipment] && !params[:shipment].empty?
151
+ params.require(:shipment).permit(permitted_shipment_attributes)
152
+ else
153
+ {}
154
+ end
155
+ end
156
+
157
+ def variant
158
+ @variant ||= Spree::Variant.unscoped.find(params.fetch(:variant_id))
159
+ end
160
+
161
+ def mine_includes
162
+ {
163
+ order: {
164
+ bill_address: {
165
+ state: {},
166
+ country: {}
167
+ },
168
+ ship_address: {
169
+ state: {},
170
+ country: {}
171
+ },
172
+ adjustments: {},
173
+ payments: {
174
+ order: {},
175
+ payment_method: {}
176
+ }
177
+ },
178
+ inventory_units: {
179
+ line_item: {
180
+ product: {},
181
+ variant: {}
182
+ },
183
+ variant: {
184
+ product: {},
185
+ default_price: {},
186
+ option_values: {
187
+ option_type: {}
188
+ }
189
+ }
190
+ }
191
+ }
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end