spree_api 4.3.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/concerns/spree/api/v2/caching.rb +7 -3
  3. data/app/controllers/concerns/spree/api/v2/coupon_codes_helper.rb +29 -0
  4. data/app/controllers/concerns/spree/api/v2/number_resource.rb +11 -0
  5. data/app/controllers/concerns/spree/api/v2/platform/nested_set_reposition_concern.rb +37 -0
  6. data/app/controllers/concerns/spree/api/v2/platform/promotion_calculator_params.rb +17 -0
  7. data/app/controllers/concerns/spree/api/v2/platform/promotion_rule_params.rb +16 -0
  8. data/app/controllers/concerns/spree/api/v2/storefront/metadata_controller_concern.rb +18 -0
  9. data/app/controllers/spree/api/v1/checkouts_controller.rb +1 -1
  10. data/app/controllers/spree/api/v2/base_controller.rb +7 -5
  11. data/app/controllers/spree/api/v2/platform/adjustments_controller.rb +19 -0
  12. data/app/controllers/spree/api/v2/platform/classifications_controller.rb +1 -22
  13. data/app/controllers/spree/api/v2/platform/cms_pages_controller.rb +4 -0
  14. data/app/controllers/spree/api/v2/platform/cms_sections_controller.rb +13 -18
  15. data/app/controllers/spree/api/v2/platform/digital_links_controller.rb +25 -0
  16. data/app/controllers/spree/api/v2/platform/digitals_controller.rb +19 -0
  17. data/app/controllers/spree/api/v2/platform/line_items_controller.rb +59 -0
  18. data/app/controllers/spree/api/v2/platform/menu_items_controller.rb +5 -19
  19. data/app/controllers/spree/api/v2/platform/menus_controller.rb +0 -4
  20. data/app/controllers/spree/api/v2/platform/orders_controller.rb +163 -0
  21. data/app/controllers/spree/api/v2/platform/payment_methods_controller.rb +27 -0
  22. data/app/controllers/spree/api/v2/platform/payments_controller.rb +17 -0
  23. data/app/controllers/spree/api/v2/platform/promotion_actions_controller.rb +30 -0
  24. data/app/controllers/spree/api/v2/platform/promotion_categories_controller.rb +19 -0
  25. data/app/controllers/spree/api/v2/platform/promotion_rules_controller.rb +25 -0
  26. data/app/controllers/spree/api/v2/platform/promotions_controller.rb +31 -0
  27. data/app/controllers/spree/api/v2/platform/resource_controller.rb +47 -16
  28. data/app/controllers/spree/api/v2/platform/roles_controller.rb +15 -0
  29. data/app/controllers/spree/api/v2/platform/shipments_controller.rb +143 -0
  30. data/app/controllers/spree/api/v2/platform/shipping_categories_controller.rb +15 -0
  31. data/app/controllers/spree/api/v2/platform/shipping_methods_controller.rb +24 -0
  32. data/app/controllers/spree/api/v2/platform/states_controller.rb +19 -0
  33. data/app/controllers/spree/api/v2/platform/stock_items_controller.rb +19 -0
  34. data/app/controllers/spree/api/v2/platform/stock_locations_controller.rb +19 -0
  35. data/app/controllers/spree/api/v2/platform/store_credit_categories_controller.rb +15 -0
  36. data/app/controllers/spree/api/v2/platform/store_credit_types_controller.rb +15 -0
  37. data/app/controllers/spree/api/v2/platform/store_credits_controller.rb +19 -0
  38. data/app/controllers/spree/api/v2/platform/tax_categories_controller.rb +19 -0
  39. data/app/controllers/spree/api/v2/platform/tax_rates_controller.rb +23 -0
  40. data/app/controllers/spree/api/v2/platform/taxonomies_controller.rb +19 -0
  41. data/app/controllers/spree/api/v2/platform/taxons_controller.rb +25 -0
  42. data/app/controllers/spree/api/v2/platform/users_controller.rb +4 -0
  43. data/app/controllers/spree/api/v2/platform/variants_controller.rb +19 -0
  44. data/app/controllers/spree/api/v2/platform/webhooks/events_controller.rb +21 -0
  45. data/app/controllers/spree/api/v2/platform/webhooks/subscribers_controller.rb +21 -0
  46. data/app/controllers/spree/api/v2/platform/wished_items_controller.rb +19 -0
  47. data/app/controllers/spree/api/v2/platform/wishlists_controller.rb +19 -0
  48. data/app/controllers/spree/api/v2/platform/zones_controller.rb +19 -0
  49. data/app/controllers/spree/api/v2/resource_controller.rb +3 -3
  50. data/app/controllers/spree/api/v2/storefront/account/addresses_controller.rb +2 -2
  51. data/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb +4 -1
  52. data/app/controllers/spree/api/v2/storefront/cart_controller.rb +18 -28
  53. data/app/controllers/spree/api/v2/storefront/checkout_controller.rb +24 -0
  54. data/app/controllers/spree/api/v2/storefront/digitals_controller.rb +54 -0
  55. data/app/controllers/spree/api/v2/storefront/wishlists_controller.rb +171 -0
  56. data/app/helpers/spree/api/v2/collection_options_helpers.rb +1 -1
  57. data/app/jobs/spree/webhooks/subscribers/make_request_job.rb +17 -0
  58. data/app/models/concerns/spree/webhooks/has_webhooks.rb +60 -0
  59. data/app/models/spree/api/webhooks/order_decorator.rb +43 -0
  60. data/app/models/spree/api/webhooks/payment_decorator.rb +26 -0
  61. data/app/models/spree/api/webhooks/product_decorator.rb +27 -0
  62. data/app/models/spree/api/webhooks/shipment_decorator.rb +21 -0
  63. data/app/models/spree/api/webhooks/stock_item_decorator.rb +43 -0
  64. data/app/models/spree/api/webhooks/stock_movement_decorator.rb +52 -0
  65. data/app/models/spree/api/webhooks/variant_decorator.rb +26 -0
  66. data/app/models/spree/oauth_access_grant.rb +7 -0
  67. data/app/models/spree/oauth_access_token.rb +7 -0
  68. data/app/models/spree/oauth_application.rb +15 -0
  69. data/app/models/spree/webhooks/base.rb +11 -0
  70. data/app/models/spree/webhooks/event.rb +12 -0
  71. data/app/models/spree/webhooks/subscriber.rb +57 -0
  72. data/app/serializers/concerns/spree/api/v2/resource_serializer_concern.rb +19 -1
  73. data/app/serializers/spree/api/v2/base_serializer.rb +11 -4
  74. data/app/serializers/spree/api/v2/platform/address_serializer.rb +1 -1
  75. data/app/serializers/spree/api/v2/platform/adjustment_serializer.rb +20 -0
  76. data/app/serializers/spree/api/v2/platform/asset_serializer.rb +13 -0
  77. data/app/serializers/spree/api/v2/platform/calculator_serializer.rb +17 -0
  78. data/app/serializers/spree/api/v2/platform/classification_serializer.rb +1 -1
  79. data/app/serializers/spree/api/v2/platform/cms_page_serializer.rb +1 -1
  80. data/app/serializers/spree/api/v2/platform/cms_section_serializer.rb +8 -1
  81. data/app/serializers/spree/api/v2/platform/country_serializer.rb +1 -1
  82. data/app/serializers/spree/api/v2/platform/credit_card_serializer.rb +14 -0
  83. data/app/serializers/spree/api/v2/platform/customer_return_serializer.rb +17 -0
  84. data/app/serializers/spree/api/v2/platform/digital_link_serializer.rb +16 -0
  85. data/app/serializers/spree/api/v2/platform/digital_serializer.rb +30 -0
  86. data/app/serializers/spree/api/v2/platform/feature_page_serializer.rb +11 -0
  87. data/app/serializers/spree/api/v2/platform/homepage_serializer.rb +11 -0
  88. data/app/serializers/spree/api/v2/platform/inventory_unit_serializer.rb +19 -0
  89. data/app/serializers/spree/api/v2/platform/line_item_serializer.rb +19 -0
  90. data/app/serializers/spree/api/v2/platform/log_entry_serializer.rb +13 -0
  91. data/app/serializers/spree/api/v2/platform/menu_item_serializer.rb +1 -1
  92. data/app/serializers/spree/api/v2/platform/menu_serializer.rb +1 -1
  93. data/app/serializers/spree/api/v2/platform/option_type_serializer.rb +1 -1
  94. data/app/serializers/spree/api/v2/platform/option_value_serializer.rb +1 -1
  95. data/app/serializers/spree/api/v2/platform/order_promotion_serializer.rb +14 -0
  96. data/app/serializers/spree/api/v2/platform/order_serializer.rb +31 -0
  97. data/app/serializers/spree/api/v2/platform/payment_capture_event_serializer.rb +13 -0
  98. data/app/serializers/spree/api/v2/platform/payment_method_serializer.rb +18 -0
  99. data/app/serializers/spree/api/v2/platform/payment_serializer.rb +21 -0
  100. data/app/serializers/spree/api/v2/platform/price_serializer.rb +19 -0
  101. data/app/serializers/spree/api/v2/platform/product_property_serializer.rb +1 -1
  102. data/app/serializers/spree/api/v2/platform/product_serializer.rb +7 -3
  103. data/app/serializers/spree/api/v2/platform/promotion_action_line_item_serializer.rb +14 -0
  104. data/app/serializers/spree/api/v2/platform/promotion_action_serializer.rb +19 -0
  105. data/app/serializers/spree/api/v2/platform/promotion_category_serializer.rb +13 -0
  106. data/app/serializers/spree/api/v2/platform/promotion_rule_serializer.rb +21 -0
  107. data/app/serializers/spree/api/v2/platform/promotion_serializer.rb +17 -0
  108. data/app/serializers/spree/api/v2/platform/property_serializer.rb +11 -0
  109. data/app/serializers/spree/api/v2/platform/prototype_serializer.rb +15 -0
  110. data/app/serializers/spree/api/v2/platform/refund_reason_serializer.rb +11 -0
  111. data/app/serializers/spree/api/v2/platform/refund_serializer.rb +16 -0
  112. data/app/serializers/spree/api/v2/platform/reimbursement_credit_serializer.rb +10 -0
  113. data/app/serializers/spree/api/v2/platform/reimbursement_serializer.rb +18 -0
  114. data/app/serializers/spree/api/v2/platform/reimbursement_type_serializer.rb +11 -0
  115. data/app/serializers/spree/api/v2/platform/return_authorization_reason_serializer.rb +11 -0
  116. data/app/serializers/spree/api/v2/platform/return_authorization_serializer.rb +17 -0
  117. data/app/serializers/spree/api/v2/platform/return_item_serializer.rb +16 -0
  118. data/app/serializers/spree/api/v2/platform/role_serializer.rb +11 -0
  119. data/app/serializers/spree/api/v2/platform/shipment_serializer.rb +22 -0
  120. data/app/serializers/spree/api/v2/platform/shipping_category_serializer.rb +11 -0
  121. data/app/serializers/spree/api/v2/platform/shipping_method_serializer.rb +16 -0
  122. data/app/serializers/spree/api/v2/platform/shipping_rate_serializer.rb +15 -0
  123. data/app/serializers/spree/api/v2/platform/standard_page_serializer.rb +11 -0
  124. data/app/serializers/spree/api/v2/platform/state_change_serializer.rb +13 -0
  125. data/app/serializers/spree/api/v2/platform/state_serializer.rb +1 -1
  126. data/app/serializers/spree/api/v2/platform/stock_item_serializer.rb +1 -3
  127. data/app/serializers/spree/api/v2/platform/stock_location_serializer.rb +2 -4
  128. data/app/serializers/spree/api/v2/platform/stock_movement_serializer.rb +11 -0
  129. data/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb +15 -0
  130. data/app/serializers/spree/api/v2/platform/store_credit_category_serializer.rb +12 -0
  131. data/app/serializers/spree/api/v2/platform/store_credit_event_serializer.rb +14 -0
  132. data/app/serializers/spree/api/v2/platform/store_credit_serializer.rb +18 -0
  133. data/app/serializers/spree/api/v2/platform/store_credit_type_serializer.rb +12 -0
  134. data/app/serializers/spree/api/v2/platform/store_serializer.rb +1 -1
  135. data/app/serializers/spree/api/v2/platform/tax_category_serializer.rb +2 -2
  136. data/app/serializers/spree/api/v2/platform/tax_rate_serializer.rb +14 -0
  137. data/app/serializers/spree/api/v2/platform/taxon_serializer.rb +1 -1
  138. data/app/serializers/spree/api/v2/platform/taxonomy_serializer.rb +1 -1
  139. data/app/serializers/spree/api/v2/platform/user_serializer.rb +1 -1
  140. data/app/serializers/spree/api/v2/platform/variant_serializer.rb +3 -2
  141. data/app/serializers/spree/api/v2/platform/webhooks/event_serializer.rb +15 -0
  142. data/app/serializers/spree/api/v2/platform/webhooks/subscriber_serializer.rb +13 -0
  143. data/app/serializers/spree/api/v2/platform/wished_item_serializer.rb +29 -0
  144. data/app/serializers/spree/api/v2/platform/wishlist_serializer.rb +19 -0
  145. data/app/serializers/spree/api/v2/platform/zone_member_serializer.rb +13 -0
  146. data/app/serializers/spree/api/v2/platform/zone_serializer.rb +13 -0
  147. data/app/serializers/spree/v2/storefront/address_serializer.rb +1 -1
  148. data/app/serializers/spree/v2/storefront/cart_serializer.rb +1 -1
  149. data/app/serializers/spree/v2/storefront/cms_section_serializer.rb +5 -1
  150. data/app/serializers/spree/v2/storefront/credit_card_serializer.rb +1 -1
  151. data/app/serializers/spree/v2/storefront/digital_link_serializer.rb +11 -0
  152. data/app/serializers/spree/v2/storefront/estimated_shipping_rate_serializer.rb +2 -2
  153. data/app/serializers/spree/v2/storefront/line_item_serializer.rb +2 -1
  154. data/app/serializers/spree/v2/storefront/option_type_serializer.rb +1 -1
  155. data/app/serializers/spree/v2/storefront/option_value_serializer.rb +1 -1
  156. data/app/serializers/spree/v2/storefront/payment_method_serializer.rb +1 -1
  157. data/app/serializers/spree/v2/storefront/payment_serializer.rb +1 -1
  158. data/app/serializers/spree/v2/storefront/product_serializer.rb +2 -2
  159. data/app/serializers/spree/v2/storefront/promotion_serializer.rb +1 -1
  160. data/app/serializers/spree/v2/storefront/shipment_serializer.rb +2 -1
  161. data/app/serializers/spree/v2/storefront/store_credit_serializer.rb +1 -1
  162. data/app/serializers/spree/v2/storefront/store_serializer.rb +1 -1
  163. data/app/serializers/spree/v2/storefront/taxon_serializer.rb +1 -1
  164. data/app/serializers/spree/v2/storefront/taxonomy_serializer.rb +1 -1
  165. data/app/serializers/spree/v2/storefront/user_serializer.rb +1 -1
  166. data/app/serializers/spree/v2/storefront/variant_serializer.rb +1 -1
  167. data/app/serializers/spree/v2/storefront/wished_item_serializer.rb +29 -0
  168. data/app/serializers/spree/v2/storefront/wishlist_serializer.rb +17 -0
  169. data/app/services/spree/webhooks/subscribers/handle_request.rb +73 -0
  170. data/app/services/spree/webhooks/subscribers/make_request.rb +82 -0
  171. data/app/services/spree/webhooks/subscribers/queue_requests.rb +17 -0
  172. data/app/services/spree/webhooks.rb +13 -0
  173. data/config/i18n-tasks.yml +40 -0
  174. data/config/initializers/doorkeeper.rb +12 -12
  175. data/config/initializers/rabl.rb +2 -2
  176. data/config/locales/en.yml +29 -27
  177. data/config/routes.rb +83 -59
  178. data/db/migrate/20210902162826_create_spree_webhooks_tables.rb +16 -0
  179. data/db/migrate/20210919183228_enable_polymorphic_resource_owner.rb +21 -0
  180. data/db/migrate/20211025162826_create_spree_webhooks_events.rb +14 -0
  181. data/docs/oauth/index.yml +126 -33
  182. data/docs/v2/platform/index.yaml +19099 -1736
  183. data/docs/v2/storefront/index.yaml +14801 -14628
  184. data/{app/models/spree → lib/spree/api}/api_dependencies.rb +56 -4
  185. data/lib/spree/api/engine.rb +19 -3
  186. data/lib/spree/api/testing_support/factories/oauth_application_factory.rb +6 -0
  187. data/lib/spree/api/testing_support/factories/webhook_event_factory.rb +27 -0
  188. data/lib/spree/api/testing_support/factories/webhook_subscriber_factory.rb +13 -0
  189. data/lib/spree/api/testing_support/factories.rb +3 -0
  190. data/lib/spree/api/testing_support/helpers.rb +1 -1
  191. data/lib/spree/api/testing_support/jobs.rb +18 -0
  192. data/lib/spree/api/testing_support/matchers/webhooks.rb +67 -0
  193. data/lib/spree/api/testing_support/serializers.rb +25 -0
  194. data/lib/spree/api/testing_support/spree_webhooks.rb +9 -0
  195. data/lib/spree/api/testing_support/v2/base.rb +1 -1
  196. data/lib/spree/api/testing_support/v2/current_order.rb +34 -1
  197. data/lib/spree/api/testing_support/v2/platform_contexts.rb +101 -52
  198. data/lib/spree/api/testing_support/v2/serializers_params.rb +3 -1
  199. data/lib/spree/api.rb +1 -0
  200. data/spec/fixtures/files/icon_256x256.jpg +0 -0
  201. data/spree_api.gemspec +16 -15
  202. metadata +175 -28
  203. data/app/controllers/spree/api/errors_controller.rb +0 -9
@@ -0,0 +1,27 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class PaymentMethodsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::PaymentMethod
10
+ end
11
+
12
+ def spree_permitted_attributes
13
+ preferred_attributes = []
14
+
15
+ if action_name == 'update'
16
+ resource.defined_preferences.each do |preference|
17
+ preferred_attributes << "preferred_#{preference}".to_sym
18
+ end
19
+ end
20
+
21
+ super + preferred_attributes
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class PaymentsController < ResourceController
6
+ include NumberResource
7
+
8
+ private
9
+
10
+ def model_class
11
+ Spree::Payment
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class PromotionActionsController < ResourceController
6
+ include ::Spree::Api::V2::Platform::PromotionCalculatorParams
7
+
8
+ private
9
+
10
+ def model_class
11
+ Spree::PromotionAction
12
+ end
13
+
14
+ def scope_includes
15
+ [:promotion]
16
+ end
17
+
18
+ def spree_permitted_attributes
19
+ conditional_params = action_name == 'update' ? [:id] : []
20
+
21
+ super + [{
22
+ promotion_action_line_items_attributes: Spree::PromotionActionLineItem.json_api_permitted_attributes.concat(conditional_params),
23
+ calculator_attributes: Spree::Calculator.json_api_permitted_attributes.concat(conditional_params, calculator_params)
24
+ }]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class PromotionCategoriesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::PromotionCategory
10
+ end
11
+
12
+ def scope_includes
13
+ [:promotions]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class PromotionRulesController < ResourceController
6
+ include ::Spree::Api::V2::Platform::PromotionRuleParams
7
+
8
+ private
9
+
10
+ def model_class
11
+ Spree::PromotionRule
12
+ end
13
+
14
+ def scope_includes
15
+ [:promotion]
16
+ end
17
+
18
+ def spree_permitted_attributes
19
+ super + rule_params
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class PromotionsController < ResourceController
6
+ include ::Spree::Api::V2::Platform::PromotionRuleParams
7
+ include ::Spree::Api::V2::Platform::PromotionCalculatorParams
8
+
9
+ private
10
+
11
+ def model_class
12
+ Spree::Promotion
13
+ end
14
+
15
+ def scope_includes
16
+ [:promotion_category, :promotion_rules, :promotion_actions]
17
+ end
18
+
19
+ def spree_permitted_attributes
20
+ conditional_params = action_name == 'update' ? [:id] : []
21
+
22
+ super + [{ promotion_actions_attributes: Spree::PromotionAction.json_api_permitted_attributes.concat(conditional_params) + [{
23
+ promotion_action_line_items_attributes: Spree::PromotionActionLineItem.json_api_permitted_attributes.concat(conditional_params),
24
+ calculator_attributes: Spree::Calculator.json_api_permitted_attributes.concat(conditional_params, calculator_params)
25
+ }], promotion_rules_attributes: Spree::PromotionRule.json_api_permitted_attributes.concat(conditional_params, rule_params) }]
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -3,21 +3,18 @@ module Spree
3
3
  module V2
4
4
  module Platform
5
5
  class ResourceController < ::Spree::Api::V2::ResourceController
6
- READ_ACTIONS = %i[show index]
7
- WRITE_ACTIONS = %i[create update destroy]
8
-
9
6
  # doorkeeper scopes usage: https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
10
- before_action -> { doorkeeper_authorize! :read, :admin }, only: READ_ACTIONS
11
- before_action -> { doorkeeper_authorize! :write, :admin }, only: WRITE_ACTIONS
7
+ before_action :validate_token_client
8
+ before_action -> { doorkeeper_authorize! :read, :admin }
9
+ before_action -> { doorkeeper_authorize! :write, :admin }, if: :write_request?
12
10
 
13
11
  # optional authorization if using a user token instead of app token
14
- before_action :authorize_spree_user, only: WRITE_ACTIONS
12
+ before_action :authorize_spree_user
15
13
 
16
- # index and show acrtions are defined in Spree::Api::V2::ResourceController
14
+ # index and show actions are defined in Spree::Api::V2::ResourceController
17
15
 
18
16
  def create
19
17
  resource = model_class.new(permitted_resource_params)
20
-
21
18
  ensure_current_store(resource)
22
19
 
23
20
  if resource.save
@@ -28,8 +25,10 @@ module Spree
28
25
  end
29
26
 
30
27
  def update
31
- if resource.update(permitted_resource_params)
32
- ensure_current_store(resource)
28
+ resource.assign_attributes(permitted_resource_params)
29
+ ensure_current_store(resource)
30
+
31
+ if resource.save
33
32
  render_serialized_payload { serialize_resource(resource) }
34
33
  else
35
34
  render_error_payload(resource.errors)
@@ -47,14 +46,15 @@ module Spree
47
46
  protected
48
47
 
49
48
  def resource_serializer
50
- "Spree::Api::V2::Platform::#{model_class.to_s.demodulize}Serializer".constantize
49
+ serializer_base_name = model_class.to_s.sub('Spree::', '')
50
+ "Spree::Api::V2::Platform::#{serializer_base_name}Serializer".constantize
51
51
  end
52
52
 
53
53
  def collection_serializer
54
54
  resource_serializer
55
55
  end
56
56
 
57
- # overwiting to utilize ransack gem for filtering
57
+ # overwriting to utilize ransack gem for filtering
58
58
  # https://github.com/activerecord-hackery/ransack#search-matchers
59
59
  def collection
60
60
  @collection ||= scope.ransack(params[:filter]).result
@@ -74,22 +74,35 @@ module Spree
74
74
  return nil if doorkeeper_token.resource_owner_id.nil?
75
75
  return @spree_current_user if @spree_current_user
76
76
 
77
- @spree_current_user ||= Spree.user_class.find_by(id: doorkeeper_token.resource_owner_id)
77
+ @spree_current_user ||= doorkeeper_token.resource_owner
78
78
  end
79
79
 
80
80
  def access_denied(exception)
81
81
  access_denied_401(exception)
82
82
  end
83
83
 
84
+ def validate_token_client
85
+ return if doorkeeper_token.nil?
86
+
87
+ raise Doorkeeper::Errors::DoorkeeperError if doorkeeper_token.application.nil?
88
+ end
89
+
84
90
  # if using a user oAuth token we need to check CanCanCan abilities
85
91
  # defined in https://github.com/spree/spree/blob/master/core/app/models/spree/ability.rb
86
92
  def authorize_spree_user
87
93
  return if spree_current_user.nil?
88
94
 
89
- if action_name == 'create'
95
+ case action_name
96
+ when 'create'
90
97
  spree_authorize! :create, model_class
98
+ when 'destroy'
99
+ spree_authorize! :destroy, resource
100
+ when 'index'
101
+ spree_authorize! :read, model_class
102
+ when 'show'
103
+ spree_authorize! :read, resource
91
104
  else
92
- spree_authorize! action_name, resource
105
+ spree_authorize! :update, resource
93
106
  end
94
107
  end
95
108
 
@@ -98,7 +111,21 @@ module Spree
98
111
  end
99
112
 
100
113
  def spree_permitted_attributes
101
- Spree::PermittedAttributes.try("#{model_param_name}_attributes") || {}
114
+ store_ids = if model_class.method_defined?(:stores)
115
+ [{ store_ids: [] }]
116
+ else
117
+ []
118
+ end
119
+
120
+ model_class.json_api_permitted_attributes + store_ids + metadata_params
121
+ end
122
+
123
+ def metadata_params
124
+ if model_class.include?(Metadata)
125
+ [{ public_metadata: {}, private_metadata: {} }]
126
+ else
127
+ []
128
+ end
102
129
  end
103
130
 
104
131
  def permitted_resource_params
@@ -108,6 +135,10 @@ module Spree
108
135
  def allowed_sort_attributes
109
136
  (super << spree_permitted_attributes).uniq.compact
110
137
  end
138
+
139
+ def write_request?
140
+ %w[put patch post delete].include?(request.request_method.downcase)
141
+ end
111
142
  end
112
143
  end
113
144
  end
@@ -0,0 +1,15 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class RolesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::Role
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,143 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class ShipmentsController < ResourceController
6
+ include NumberResource
7
+
8
+ before_action :load_variant, only: %i[transfer_to_location transfer_to_shipment]
9
+
10
+ SHIPMENT_STATES = %w[ready ship cancel resume pend]
11
+
12
+ def create
13
+ # FIXME: support saving metadata
14
+ result = create_service.call(
15
+ store: current_store,
16
+ shipment_attributes: params.require(:shipment).permit(
17
+ :order_id, :stock_location_id, :variant_id, :quantity
18
+ )
19
+ )
20
+ render_result(result, 201)
21
+ end
22
+
23
+ def update
24
+ result = update_service.call(shipment: resource, shipment_attributes: permitted_resource_params)
25
+ render_result(result)
26
+ end
27
+
28
+ SHIPMENT_STATES.each do |state|
29
+ define_method state do
30
+ result = change_state_service.call(shipment: resource, state: state)
31
+ render_result(result)
32
+ end
33
+ end
34
+
35
+ def add_item
36
+ result = add_item_service.call(
37
+ shipment: resource,
38
+ variant_id: params.dig(:shipment, :variant_id),
39
+ quantity: params.dig(:shipment, :quantity)
40
+ )
41
+ render_result(result)
42
+ end
43
+
44
+ def remove_item
45
+ result = remove_item_service.call(
46
+ shipment: resource,
47
+ variant_id: params.dig(:shipment, :variant_id),
48
+ quantity: params.dig(:shipment, :quantity)
49
+ )
50
+
51
+ if result.success?
52
+ if result.value == :shipment_deleted
53
+ head 204
54
+ else
55
+ render_serialized_payload { serialize_resource(result.value) }
56
+ end
57
+ else
58
+ render_error_payload(result.error)
59
+ end
60
+ end
61
+
62
+ def transfer_to_location
63
+ stock_location = Spree::StockLocation.find(params.dig(:shipment, :stock_location_id))
64
+ quantity = params.dig(:shipment, :quantity)&.to_i || 1
65
+
66
+ unless quantity > 0
67
+ render_error_payload("#{I18n.t('spree.api.shipment_transfer_errors_occurred')} \n #{I18n.t('spree.api.negative_quantity')}")
68
+ return
69
+ end
70
+
71
+ transfer = resource.transfer_to_location(@variant, quantity, stock_location)
72
+ if transfer.valid? && transfer.run!
73
+ render json: { message: I18n.t('spree.api.shipment_transfer_success') }, status: 201
74
+ else
75
+ render_error_payload(transfer.errors)
76
+ end
77
+ end
78
+
79
+ def transfer_to_shipment
80
+ target_shipment = Spree::Shipment.find_by!(number: params.dig(:shipment, :target_shipment_number))
81
+ quantity = params.dig(:shipment, :quantity)&.to_i || 1
82
+
83
+ error =
84
+ if quantity < 0 && target_shipment == resource
85
+ "#{I18n.t('spree.api.negative_quantity')}, \n#{I18n.t('spree.api.wrong_shipment_target')}"
86
+ elsif target_shipment == resource
87
+ I18n.t('spree.api.wrong_shipment_target')
88
+ elsif quantity < 0
89
+ I18n.t('spree.api.negative_quantity')
90
+ end
91
+
92
+ if error
93
+ render_error_payload("#{I18n.t('spree.api.shipment_transfer_errors_occurred')} \n#{error}")
94
+ else
95
+ transfer = resource.transfer_to_shipment(@variant, quantity, target_shipment)
96
+ if transfer.valid? && transfer.run!
97
+ render json: { message: I18n.t('spree.api.shipment_transfer_success') }, status: 201
98
+ else
99
+ render_error_payload(transfer.errors)
100
+ end
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def model_class
107
+ Spree::Shipment
108
+ end
109
+
110
+ def load_variant
111
+ @variant = current_store.variants.find_by(id: params.dig(:shipment, :variant_id))
112
+ end
113
+
114
+ def spree_permitted_attributes
115
+ Spree::Shipment.json_api_permitted_attributes + [
116
+ :selected_shipping_rate_id
117
+ ]
118
+ end
119
+
120
+ def create_service
121
+ Spree::Api::Dependencies.platform_shipment_create_service.constantize
122
+ end
123
+
124
+ def update_service
125
+ Spree::Api::Dependencies.platform_shipment_update_service.constantize
126
+ end
127
+
128
+ def change_state_service
129
+ Spree::Api::Dependencies.platform_shipment_change_state_service.constantize
130
+ end
131
+
132
+ def add_item_service
133
+ Spree::Api::Dependencies.platform_shipment_add_item_service.constantize
134
+ end
135
+
136
+ def remove_item_service
137
+ Spree::Api::Dependencies.platform_shipment_remove_item_service.constantize
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,15 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class ShippingCategoriesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::ShippingCategory
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class ShippingMethodsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::ShippingMethod
10
+ end
11
+
12
+ def spree_permitted_attributes
13
+ super + [
14
+ {
15
+ shipping_category_ids: [],
16
+ calculator_attributes: {}
17
+ }
18
+ ]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class StatesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::State
10
+ end
11
+
12
+ def scope_includes
13
+ [:country]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class StockItemsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::StockItem
10
+ end
11
+
12
+ def scope_includes
13
+ [:variant, :stock_location]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class StockLocationsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::StockLocation
10
+ end
11
+
12
+ def scope_includes
13
+ [:country]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class StoreCreditCategoriesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::StoreCreditCategory
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class StoreCreditTypesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::StoreCreditType
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class StoreCreditsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::StoreCredit
10
+ end
11
+
12
+ def scope_includes
13
+ [:user, :created_by, :category, :credit_type]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class TaxCategoriesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::TaxCategory
10
+ end
11
+
12
+ def scope_includes
13
+ [:tax_rates]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class TaxRatesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::TaxRate
10
+ end
11
+
12
+ def scope_includes
13
+ [:zone, :tax_category]
14
+ end
15
+
16
+ def spree_permitted_attributes
17
+ super + [calculator_attributes: Spree::Calculator.json_api_permitted_attributes]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class TaxonomiesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::Taxonomy
10
+ end
11
+
12
+ def scope_includes
13
+ [:taxons, :root]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end