spree_api 4.3.0.rc3 → 4.4.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) 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 +5 -3
  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 +57 -29
  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/products_controller.rb +8 -0
  56. data/app/controllers/spree/api/v2/storefront/wishlists_controller.rb +171 -0
  57. data/app/helpers/spree/api/v2/collection_options_helpers.rb +1 -1
  58. data/app/jobs/spree/webhooks/subscribers/make_request_job.rb +17 -0
  59. data/app/models/concerns/spree/webhooks/has_webhooks.rb +58 -0
  60. data/app/models/spree/api/webhooks/order_decorator.rb +43 -0
  61. data/app/models/spree/api/webhooks/payment_decorator.rb +26 -0
  62. data/app/models/spree/api/webhooks/product_decorator.rb +27 -0
  63. data/app/models/spree/api/webhooks/shipment_decorator.rb +21 -0
  64. data/app/models/spree/api/webhooks/stock_item_decorator.rb +43 -0
  65. data/app/models/spree/api/webhooks/stock_movement_decorator.rb +52 -0
  66. data/app/models/spree/api/webhooks/variant_decorator.rb +26 -0
  67. data/app/models/spree/oauth_access_grant.rb +7 -0
  68. data/app/models/spree/oauth_access_token.rb +7 -0
  69. data/app/models/spree/oauth_application.rb +15 -0
  70. data/app/models/spree/webhooks/base.rb +11 -0
  71. data/app/models/spree/webhooks/event.rb +12 -0
  72. data/app/models/spree/webhooks/subscriber.rb +57 -0
  73. data/app/presenters/spree/api/products/filters_presenter.rb +39 -0
  74. data/app/serializers/concerns/spree/api/v2/resource_serializer_concern.rb +19 -1
  75. data/app/serializers/spree/api/v2/base_serializer.rb +11 -4
  76. data/app/serializers/spree/api/v2/platform/address_serializer.rb +1 -1
  77. data/app/serializers/spree/api/v2/platform/adjustment_serializer.rb +20 -0
  78. data/app/serializers/spree/api/v2/platform/asset_serializer.rb +13 -0
  79. data/app/serializers/spree/api/v2/platform/calculator_serializer.rb +17 -0
  80. data/app/serializers/spree/api/v2/platform/classification_serializer.rb +1 -1
  81. data/app/serializers/spree/api/v2/platform/cms_page_serializer.rb +1 -1
  82. data/app/serializers/spree/api/v2/platform/cms_section_serializer.rb +8 -1
  83. data/app/serializers/spree/api/v2/platform/country_serializer.rb +1 -1
  84. data/app/serializers/spree/api/v2/platform/credit_card_serializer.rb +14 -0
  85. data/app/serializers/spree/api/v2/platform/customer_return_serializer.rb +17 -0
  86. data/app/serializers/spree/api/v2/platform/digital_link_serializer.rb +16 -0
  87. data/app/serializers/spree/api/v2/platform/digital_serializer.rb +30 -0
  88. data/app/serializers/spree/api/v2/platform/feature_page_serializer.rb +11 -0
  89. data/app/serializers/spree/api/v2/platform/homepage_serializer.rb +11 -0
  90. data/app/serializers/spree/api/v2/platform/inventory_unit_serializer.rb +19 -0
  91. data/app/serializers/spree/api/v2/platform/line_item_serializer.rb +19 -0
  92. data/app/serializers/spree/api/v2/platform/log_entry_serializer.rb +13 -0
  93. data/app/serializers/spree/api/v2/platform/menu_item_serializer.rb +1 -1
  94. data/app/serializers/spree/api/v2/platform/menu_serializer.rb +1 -1
  95. data/app/serializers/spree/api/v2/platform/option_type_serializer.rb +1 -1
  96. data/app/serializers/spree/api/v2/platform/option_value_serializer.rb +1 -1
  97. data/app/serializers/spree/api/v2/platform/order_promotion_serializer.rb +14 -0
  98. data/app/serializers/spree/api/v2/platform/order_serializer.rb +31 -0
  99. data/app/serializers/spree/api/v2/platform/payment_capture_event_serializer.rb +13 -0
  100. data/app/serializers/spree/api/v2/platform/payment_method_serializer.rb +18 -0
  101. data/app/serializers/spree/api/v2/platform/payment_serializer.rb +21 -0
  102. data/app/serializers/spree/api/v2/platform/price_serializer.rb +19 -0
  103. data/app/serializers/spree/api/v2/platform/product_property_serializer.rb +1 -1
  104. data/app/serializers/spree/api/v2/platform/product_serializer.rb +7 -3
  105. data/app/serializers/spree/api/v2/platform/promotion_action_line_item_serializer.rb +14 -0
  106. data/app/serializers/spree/api/v2/platform/promotion_action_serializer.rb +19 -0
  107. data/app/serializers/spree/api/v2/platform/promotion_category_serializer.rb +13 -0
  108. data/app/serializers/spree/api/v2/platform/promotion_rule_serializer.rb +21 -0
  109. data/app/serializers/spree/api/v2/platform/promotion_serializer.rb +17 -0
  110. data/app/serializers/spree/api/v2/platform/property_serializer.rb +11 -0
  111. data/app/serializers/spree/api/v2/platform/prototype_serializer.rb +15 -0
  112. data/app/serializers/spree/api/v2/platform/refund_reason_serializer.rb +11 -0
  113. data/app/serializers/spree/api/v2/platform/refund_serializer.rb +16 -0
  114. data/app/serializers/spree/api/v2/platform/reimbursement_credit_serializer.rb +10 -0
  115. data/app/serializers/spree/api/v2/platform/reimbursement_serializer.rb +18 -0
  116. data/app/serializers/spree/api/v2/platform/reimbursement_type_serializer.rb +11 -0
  117. data/app/serializers/spree/api/v2/platform/return_authorization_reason_serializer.rb +11 -0
  118. data/app/serializers/spree/api/v2/platform/return_authorization_serializer.rb +17 -0
  119. data/app/serializers/spree/api/v2/platform/return_item_serializer.rb +16 -0
  120. data/app/serializers/spree/api/v2/platform/role_serializer.rb +11 -0
  121. data/app/serializers/spree/api/v2/platform/shipment_serializer.rb +22 -0
  122. data/app/serializers/spree/api/v2/platform/shipping_category_serializer.rb +11 -0
  123. data/app/serializers/spree/api/v2/platform/shipping_method_serializer.rb +16 -0
  124. data/app/serializers/spree/api/v2/platform/shipping_rate_serializer.rb +15 -0
  125. data/app/serializers/spree/api/v2/platform/standard_page_serializer.rb +11 -0
  126. data/app/serializers/spree/api/v2/platform/state_change_serializer.rb +13 -0
  127. data/app/serializers/spree/api/v2/platform/state_serializer.rb +1 -1
  128. data/app/serializers/spree/api/v2/platform/stock_item_serializer.rb +1 -3
  129. data/app/serializers/spree/api/v2/platform/stock_location_serializer.rb +2 -4
  130. data/app/serializers/spree/api/v2/platform/stock_movement_serializer.rb +11 -0
  131. data/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb +15 -0
  132. data/app/serializers/spree/api/v2/platform/store_credit_category_serializer.rb +12 -0
  133. data/app/serializers/spree/api/v2/platform/store_credit_event_serializer.rb +14 -0
  134. data/app/serializers/spree/api/v2/platform/store_credit_serializer.rb +18 -0
  135. data/app/serializers/spree/api/v2/platform/store_credit_type_serializer.rb +12 -0
  136. data/app/serializers/spree/api/v2/platform/store_serializer.rb +1 -1
  137. data/app/serializers/spree/api/v2/platform/tax_category_serializer.rb +2 -2
  138. data/app/serializers/spree/api/v2/platform/tax_rate_serializer.rb +14 -0
  139. data/app/serializers/spree/api/v2/platform/taxon_serializer.rb +1 -1
  140. data/app/serializers/spree/api/v2/platform/taxonomy_serializer.rb +1 -1
  141. data/app/serializers/spree/api/v2/platform/user_serializer.rb +1 -1
  142. data/app/serializers/spree/api/v2/platform/variant_serializer.rb +3 -2
  143. data/app/serializers/spree/api/v2/platform/webhooks/event_serializer.rb +15 -0
  144. data/app/serializers/spree/api/v2/platform/webhooks/subscriber_serializer.rb +13 -0
  145. data/app/serializers/spree/api/v2/platform/wished_item_serializer.rb +29 -0
  146. data/app/serializers/spree/api/v2/platform/wishlist_serializer.rb +19 -0
  147. data/app/serializers/spree/api/v2/platform/zone_member_serializer.rb +13 -0
  148. data/app/serializers/spree/api/v2/platform/zone_serializer.rb +13 -0
  149. data/app/serializers/spree/v2/storefront/address_serializer.rb +1 -1
  150. data/app/serializers/spree/v2/storefront/cart_serializer.rb +1 -1
  151. data/app/serializers/spree/v2/storefront/cms_section_serializer.rb +5 -1
  152. data/app/serializers/spree/v2/storefront/credit_card_serializer.rb +1 -1
  153. data/app/serializers/spree/v2/storefront/digital_link_serializer.rb +11 -0
  154. data/app/serializers/spree/v2/storefront/estimated_shipping_rate_serializer.rb +2 -2
  155. data/app/serializers/spree/v2/storefront/line_item_serializer.rb +2 -1
  156. data/app/serializers/spree/v2/storefront/option_type_serializer.rb +1 -1
  157. data/app/serializers/spree/v2/storefront/option_value_serializer.rb +1 -1
  158. data/app/serializers/spree/v2/storefront/payment_method_serializer.rb +1 -1
  159. data/app/serializers/spree/v2/storefront/payment_serializer.rb +1 -1
  160. data/app/serializers/spree/v2/storefront/product_serializer.rb +2 -2
  161. data/app/serializers/spree/v2/storefront/promotion_serializer.rb +1 -1
  162. data/app/serializers/spree/v2/storefront/shipment_serializer.rb +2 -1
  163. data/app/serializers/spree/v2/storefront/store_credit_serializer.rb +1 -1
  164. data/app/serializers/spree/v2/storefront/store_serializer.rb +1 -1
  165. data/app/serializers/spree/v2/storefront/taxon_serializer.rb +1 -1
  166. data/app/serializers/spree/v2/storefront/taxonomy_serializer.rb +1 -1
  167. data/app/serializers/spree/v2/storefront/user_serializer.rb +1 -1
  168. data/app/serializers/spree/v2/storefront/variant_serializer.rb +1 -1
  169. data/app/serializers/spree/v2/storefront/wished_item_serializer.rb +29 -0
  170. data/app/serializers/spree/v2/storefront/wishlist_serializer.rb +17 -0
  171. data/app/services/spree/webhooks/subscribers/handle_request.rb +73 -0
  172. data/app/services/spree/webhooks/subscribers/make_request.rb +82 -0
  173. data/app/services/spree/webhooks/subscribers/queue_requests.rb +17 -0
  174. data/app/services/spree/webhooks.rb +13 -0
  175. data/config/i18n-tasks.yml +40 -0
  176. data/config/initializers/doorkeeper.rb +12 -12
  177. data/config/initializers/rabl.rb +2 -2
  178. data/config/locales/en.yml +29 -27
  179. data/config/routes.rb +85 -59
  180. data/db/migrate/20210902162826_create_spree_webhooks_tables.rb +16 -0
  181. data/db/migrate/20210919183228_enable_polymorphic_resource_owner.rb +21 -0
  182. data/db/migrate/20211025162826_create_spree_webhooks_events.rb +14 -0
  183. data/docs/oauth/index.yml +126 -33
  184. data/docs/v2/platform/index.yaml +19182 -1717
  185. data/docs/v2/storefront/index.yaml +14823 -14588
  186. data/{app/models/spree → lib/spree/api}/api_dependencies.rb +60 -5
  187. data/lib/spree/api/engine.rb +24 -1
  188. data/lib/spree/api/testing_support/factories/oauth_application_factory.rb +6 -0
  189. data/lib/spree/api/testing_support/factories/webhook_event_factory.rb +27 -0
  190. data/lib/spree/api/testing_support/factories/webhook_subscriber_factory.rb +13 -0
  191. data/lib/spree/api/testing_support/factories.rb +3 -0
  192. data/lib/spree/api/testing_support/helpers.rb +1 -1
  193. data/lib/spree/api/testing_support/jobs.rb +18 -0
  194. data/lib/spree/api/testing_support/matchers/webhooks.rb +67 -0
  195. data/lib/spree/api/testing_support/serializers.rb +25 -0
  196. data/lib/spree/api/testing_support/spree_webhooks.rb +9 -0
  197. data/lib/spree/api/testing_support/v2/base.rb +1 -1
  198. data/lib/spree/api/testing_support/v2/current_order.rb +34 -1
  199. data/lib/spree/api/testing_support/v2/platform_contexts.rb +109 -50
  200. data/lib/spree/api/testing_support/v2/serializers_params.rb +3 -1
  201. data/lib/spree/api.rb +1 -0
  202. data/spec/fixtures/files/icon_256x256.jpg +0 -0
  203. data/spree_api.gemspec +16 -15
  204. metadata +176 -28
  205. data/app/controllers/spree/api/errors_controller.rb +0 -9
@@ -3,8 +3,29 @@ module Spree
3
3
  module V2
4
4
  module Platform
5
5
  class TaxonsController < ResourceController
6
+ include ::Spree::Api::V2::Platform::NestedSetRepositionConcern
7
+
6
8
  private
7
9
 
10
+ def successful_reposition_actions
11
+ reload_taxon_and_set_new_permalink(resource)
12
+ update_permalinks_on_child_taxons
13
+
14
+ render_serialized_payload { serialize_resource(resource) }
15
+ end
16
+
17
+ def reload_taxon_and_set_new_permalink(taxon)
18
+ taxon.reload
19
+ taxon.set_permalink
20
+ taxon.save!
21
+ end
22
+
23
+ def update_permalinks_on_child_taxons
24
+ resource.descendants.each do |taxon|
25
+ reload_taxon_and_set_new_permalink(taxon)
26
+ end
27
+ end
28
+
8
29
  def model_class
9
30
  Spree::Taxon
10
31
  end
@@ -23,6 +44,10 @@ module Spree
23
44
  def serializer_params
24
45
  super.merge(include_products: action_name == 'show')
25
46
  end
47
+
48
+ def spree_permitted_attributes
49
+ super + [:new_parent_id, :new_position_idx]
50
+ end
26
51
  end
27
52
  end
28
53
  end
@@ -21,6 +21,10 @@ module Spree
21
21
  def model_param_name
22
22
  'user'
23
23
  end
24
+
25
+ def spree_permitted_attributes
26
+ super + [:password, :password_confirmation]
27
+ end
24
28
  end
25
29
  end
26
30
  end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class VariantsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::Variant
10
+ end
11
+
12
+ def spree_permitted_attributes
13
+ super + [:option_value_ids, :price, :currency]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ module Webhooks
6
+ class EventsController < ResourceController
7
+ private
8
+
9
+ def model_class
10
+ Spree::Webhooks::Event
11
+ end
12
+
13
+ def scope_includes
14
+ %i[subscriber]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ module Webhooks
6
+ class SubscribersController < ResourceController
7
+ private
8
+
9
+ def model_class
10
+ Spree::Webhooks::Subscriber
11
+ end
12
+
13
+ def spree_permitted_attributes
14
+ super + [{ subscriptions: [] }]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Platform
5
+ class WishedItemsController < ResourceController
6
+ private
7
+
8
+ def scope_includes
9
+ [:variant]
10
+ end
11
+
12
+ def model_class
13
+ Spree::WishedItem
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 WishlistsController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::Wishlist
10
+ end
11
+
12
+ def scope_includes
13
+ [:wished_items]
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 ZonesController < ResourceController
6
+ private
7
+
8
+ def model_class
9
+ Spree::Zone
10
+ end
11
+
12
+ def scope_includes
13
+ [:zone_members]
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -24,11 +24,11 @@ module Spree
24
24
  end
25
25
 
26
26
  def allowed_sort_attributes
27
- default_sort_atributes
27
+ default_sort_attributes
28
28
  end
29
29
 
30
- def default_sort_atributes
31
- [:id, :name, :number, :position, :updated_at, :created_at]
30
+ def default_sort_attributes
31
+ [:id, :name, :slug, :number, :position, :updated_at, :created_at, :deleted_at]
32
32
  end
33
33
 
34
34
  def scope(skip_cancancan: false)
@@ -4,6 +4,8 @@ module Spree
4
4
  module Storefront
5
5
  module Account
6
6
  class AddressesController < ::Spree::Api::V2::ResourceController
7
+ include Spree::BaseHelper
8
+
7
9
  before_action :require_spree_current_user
8
10
 
9
11
  def create
@@ -37,7 +39,7 @@ module Spree
37
39
  end
38
40
 
39
41
  def scope
40
- super.where(user: spree_current_user).not_deleted
42
+ super.where(user: spree_current_user, country: available_countries).not_deleted
41
43
  end
42
44
 
43
45
  def model_class
@@ -57,11 +59,11 @@ module Spree
57
59
  end
58
60
 
59
61
  def create_service
60
- Spree::Api::Dependencies.storefront_account_create_address_service.constantize
62
+ Spree::Api::Dependencies.storefront_address_create_service.constantize
61
63
  end
62
64
 
63
65
  def update_service
64
- Spree::Api::Dependencies.storefront_account_update_address_service.constantize
66
+ Spree::Api::Dependencies.storefront_address_update_service.constantize
65
67
  end
66
68
 
67
69
  def address_params
@@ -23,7 +23,10 @@ module Spree
23
23
  end
24
24
 
25
25
  def scope
26
- super.where(user: spree_current_user, payment_method: current_store.payment_methods.available_on_front_end)
26
+ super.not_expired.not_removed.where(
27
+ user: spree_current_user,
28
+ payment_method: current_store.payment_methods.available_on_front_end
29
+ )
27
30
  end
28
31
 
29
32
  def collection_serializer
@@ -3,21 +3,29 @@ module Spree
3
3
  module V2
4
4
  module Storefront
5
5
  class CartController < ::Spree::Api::V2::BaseController
6
- include Spree::Api::V2::Storefront::OrderConcern
7
- before_action :ensure_order, except: :create
6
+ include OrderConcern
7
+ include CouponCodesHelper
8
+ include Spree::Api::V2::Storefront::MetadataControllerConcern
9
+
10
+ before_action :ensure_valid_metadata, only: %i[create add_item]
11
+ before_action :ensure_order, except: %i[create associate]
8
12
  before_action :load_variant, only: :add_item
13
+ before_action :require_spree_current_user, only: :associate
14
+
9
15
 
10
16
  def create
11
17
  spree_authorize! :create, Spree::Order
12
18
 
13
- order_params = {
19
+ create_cart_params = {
14
20
  user: spree_current_user,
15
21
  store: current_store,
16
- currency: current_currency
22
+ currency: current_currency,
23
+ public_metadata: add_item_params[:public_metadata],
24
+ private_metadata: add_item_params[:private_metadata],
17
25
  }
18
26
 
19
27
  order = spree_current_order if spree_current_order.present?
20
- order ||= create_service.call(order_params).value
28
+ order ||= create_service.call(create_cart_params).value
21
29
 
22
30
  render_serialized_payload(201) { serialize_resource(order) }
23
31
  end
@@ -29,8 +37,10 @@ module Spree
29
37
  result = add_item_service.call(
30
38
  order: spree_current_order,
31
39
  variant: @variant,
32
- quantity: params[:quantity],
33
- options: params[:options]
40
+ quantity: add_item_params[:quantity],
41
+ public_metadata: add_item_params[:public_metadata],
42
+ private_metadata: add_item_params[:private_metadata],
43
+ options: add_item_params[:options]
34
44
  )
35
45
 
36
46
  render_order(result)
@@ -105,7 +115,7 @@ module Spree
105
115
 
106
116
  coupon_codes = select_coupon_codes
107
117
 
108
- return render_error_payload(Spree.t('v2.cart.no_coupon_code', scope: 'api')) if coupon_codes.empty?
118
+ return render_error_payload(I18n.t('spree.api.v2.cart.no_coupon_code')) if coupon_codes.empty?
109
119
 
110
120
  result_errors = coupon_codes.count > 1 ? select_errors(coupon_codes) : select_error(coupon_codes)
111
121
 
@@ -128,6 +138,34 @@ module Spree
128
138
  end
129
139
  end
130
140
 
141
+ def associate
142
+ guest_order_token = params[:guest_order_token]
143
+ guest_order = ::Spree::Api::Dependencies.storefront_current_order_finder.constantize.new.execute(
144
+ store: current_store,
145
+ user: nil,
146
+ token: guest_order_token,
147
+ currency: current_currency
148
+ )
149
+
150
+ spree_authorize! :update, guest_order, guest_order_token
151
+
152
+ result = associate_service.call(guest_order: guest_order, user: spree_current_user)
153
+
154
+ if result.success?
155
+ render_serialized_payload { serialize_resource(guest_order) }
156
+ else
157
+ render_error_payload(result.error)
158
+ end
159
+ end
160
+
161
+ def change_currency
162
+ spree_authorize! :update, spree_current_order, order_token
163
+
164
+ result = change_currency_service.call(order: spree_current_order, new_currency: params[:new_currency])
165
+
166
+ render_order(result)
167
+ end
168
+
131
169
  private
132
170
 
133
171
  def resource_serializer
@@ -166,12 +204,20 @@ module Spree
166
204
  Spree::Api::Dependencies.storefront_cart_estimate_shipping_rates_service.constantize
167
205
  end
168
206
 
207
+ def associate_service
208
+ Spree::Api::Dependencies.storefront_cart_associate_service.constantize
209
+ end
210
+
211
+ def change_currency_service
212
+ Spree::Api::Dependencies.storefront_cart_change_currency_service.constantize
213
+ end
214
+
169
215
  def line_item
170
216
  @line_item ||= spree_current_order.line_items.find(params[:line_item_id])
171
217
  end
172
218
 
173
219
  def load_variant
174
- @variant = current_store.variants.find(params[:variant_id])
220
+ @variant = current_store.variants.find(add_item_params[:variant_id])
175
221
  end
176
222
 
177
223
  def render_error_item_quantity
@@ -189,26 +235,8 @@ module Spree
189
235
  ).serializable_hash
190
236
  end
191
237
 
192
- def select_coupon_codes
193
- params[:coupon_code].present? ? [params[:coupon_code]] : check_coupon_codes
194
- end
195
-
196
- def check_coupon_codes
197
- spree_current_order.promotions.coupons.map(&:code)
198
- end
199
-
200
- def select_error(coupon_codes)
201
- result = coupon_handler.new(spree_current_order).remove(coupon_codes.first)
202
- result.error
203
- end
204
-
205
- def select_errors(coupon_codes)
206
- results = []
207
- coupon_codes.each do |coupon_code|
208
- results << coupon_handler.new(spree_current_order).remove(coupon_code)
209
- end
210
-
211
- results.select(&:error)
238
+ def add_item_params
239
+ params.permit(:quantity, :variant_id, public_metadata: {}, private_metadata: {}, options: {})
212
240
  end
213
241
  end
214
242
  end
@@ -44,6 +44,22 @@ module Spree
44
44
  render_order(result)
45
45
  end
46
46
 
47
+ def create_payment
48
+ result = create_payment_service.call(order: spree_current_order, params: params)
49
+
50
+ if result.success?
51
+ render_serialized_payload(201) { serialize_resource(spree_current_order.reload) }
52
+ else
53
+ render_error_payload(result.error)
54
+ end
55
+ end
56
+
57
+ def select_shipping_method
58
+ result = select_shipping_method_service.call(order: spree_current_order, params: params)
59
+
60
+ render_order(result)
61
+ end
62
+
47
63
  def add_store_credit
48
64
  spree_authorize! :update, spree_current_order, order_token
49
65
 
@@ -118,6 +134,14 @@ module Spree
118
134
  Spree::Api::Dependencies.storefront_shipment_serializer.constantize
119
135
  end
120
136
 
137
+ def create_payment_service
138
+ Spree::Api::Dependencies.storefront_payment_create_service.constantize
139
+ end
140
+
141
+ def select_shipping_method_service
142
+ Spree::Api::Dependencies.storefront_checkout_select_shipping_method_service.constantize
143
+ end
144
+
121
145
  def serialize_payment_methods(payment_methods)
122
146
  payment_methods_serializer.new(payment_methods, params: serializer_params).serializable_hash
123
147
  end
@@ -0,0 +1,54 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class DigitalsController < ::Spree::Api::V2::ResourceController
6
+ def download
7
+ if attachment.present?
8
+ if digital_link.authorize!
9
+ if defined?(ActiveStorage::Service::DiskService) && ActiveStorage::Blob.service.instance_of?(ActiveStorage::Service::DiskService)
10
+ # The asset is hosted on disk, use send_file.
11
+
12
+ send_file(
13
+ ActiveStorage::Blob.service.path_for(attachment.key),
14
+ filename: attachment.filename.to_s,
15
+ type: attachment.content_type.to_s,
16
+ status: :ok
17
+ ) and return
18
+
19
+ else
20
+ # The asset is hosted on a 3rd party service, use an expiring url with disposition: 'attachment'.
21
+
22
+ redirect_to attachment.url(
23
+ expires_in: current_store.digital_asset_link_expire_time.seconds,
24
+ disposition: 'attachment',
25
+ host: current_store.formatted_url
26
+ ) and return
27
+
28
+ end
29
+ end
30
+ else
31
+ Rails.logger.error I18n.t('spree.api.v2.digitals.missing_file')
32
+ end
33
+
34
+ render json: { error: I18n.t('spree.api.v2.digitals.unauthorized') }, status: 403
35
+ end
36
+
37
+ private
38
+
39
+ def model_class
40
+ Spree::Digital
41
+ end
42
+
43
+ def digital_link
44
+ @digital_link ||= DigitalLink.find_by!(token: params[:token])
45
+ end
46
+
47
+ def attachment
48
+ @attachment ||= digital_link.digital.try(:attachment) if digital_link.present?
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -46,6 +46,14 @@ module Spree
46
46
  def allowed_sort_attributes
47
47
  super << :available_on
48
48
  end
49
+
50
+ def collection_meta(collection)
51
+ super(collection).merge(filters: filters_meta)
52
+ end
53
+
54
+ def filters_meta
55
+ Spree::Api::Products::FiltersPresenter.new(current_store, current_currency, params).to_h
56
+ end
49
57
  end
50
58
  end
51
59
  end
@@ -0,0 +1,171 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class WishlistsController < ::Spree::Api::V2::ResourceController
6
+ before_action :require_spree_current_user, except: [:show]
7
+ before_action :ensure_valid_quantity, only: [:add_item, :set_item_quantity]
8
+
9
+ def show
10
+ spree_authorize! :show, resource
11
+ super
12
+ end
13
+
14
+ def create
15
+ spree_authorize! :create, Spree::Wishlist
16
+
17
+ @wishlist = spree_current_user.wishlists.new(wishlist_attributes)
18
+
19
+ ensure_current_store(@wishlist)
20
+
21
+ @wishlist.save
22
+
23
+ if @wishlist.persisted?
24
+ render_serialized_payload(201) { serialize_resource(@wishlist) }
25
+ else
26
+ render_error_payload(@wishlist.errors.full_messages.to_sentence)
27
+ end
28
+ end
29
+
30
+ def update
31
+ authorize! :update, resource
32
+
33
+ resource.update wishlist_attributes
34
+
35
+ if resource.errors.empty?
36
+ render_serialized_payload { serialize_resource(resource) }
37
+ else
38
+ render_error_payload(resource.errors.full_messages.to_sentence)
39
+ end
40
+ end
41
+
42
+ def destroy
43
+ authorize! :destroy, resource
44
+
45
+ if resource.destroy
46
+ head 204
47
+ else
48
+ render_error_payload(I18n.t('spree.api.v2.wishlist.errors.the_wishlist_could_not_be_destroyed'))
49
+ end
50
+ end
51
+
52
+ def default
53
+ spree_authorize! :create, Spree::Wishlist
54
+
55
+ @default_wishlist = spree_current_user.default_wishlist_for_store(current_store)
56
+
57
+ render_serialized_payload { serialize_resource(@default_wishlist) }
58
+ end
59
+
60
+ def add_item
61
+ spree_authorize! :create, Spree::WishedItem
62
+
63
+ if resource.wished_items.present? && resource.wished_items.detect { |wv| wv.variant_id.to_s == params[:variant_id].to_s }.present?
64
+ @wished_item = resource.wished_items.detect { |wi| wi.variant_id.to_s == params[:variant_id].to_s }
65
+ @wished_item.quantity = params[:quantity]
66
+ else
67
+ @wished_item = Spree::WishedItem.new(params.permit(:quantity, :variant_id))
68
+ @wished_item.wishlist = resource
69
+ @wished_item.save
70
+ end
71
+
72
+ resource.reload
73
+
74
+ if @wished_item.persisted?
75
+ render_serialized_payload { serialize_wished_item(@wished_item) }
76
+ else
77
+ render_error_payload(resource.errors.full_messages.to_sentence)
78
+ end
79
+ end
80
+
81
+ def set_item_quantity
82
+ spree_authorize! :update, wished_item
83
+
84
+ wished_item.update(params.permit(:quantity))
85
+
86
+ if wished_item.errors.empty?
87
+ render_serialized_payload { serialize_wished_item(wished_item) }
88
+ else
89
+ render_error_payload(resource.errors.full_messages.to_sentence)
90
+ end
91
+ end
92
+
93
+ def remove_item
94
+ spree_authorize! :destroy, wished_item
95
+
96
+ if wished_item.destroy
97
+ render_serialized_payload { serialize_wished_item(wished_item) }
98
+ else
99
+ render_error_payload(resource.errors.full_messages.to_sentence)
100
+ end
101
+ end
102
+
103
+ private
104
+
105
+ def scope(skip_cancancan: true)
106
+ if action_name == 'show'
107
+ super
108
+ else
109
+ super.where(user: spree_current_user)
110
+ end
111
+ end
112
+
113
+ def resource
114
+ @resource ||= scope.find_by(token: params[:id])
115
+ end
116
+
117
+ def model_class
118
+ Spree::Wishlist
119
+ end
120
+
121
+ def resource_serializer
122
+ ::Spree::V2::Storefront::WishlistSerializer
123
+ end
124
+
125
+ def collection_serializer
126
+ resource_serializer
127
+ end
128
+
129
+ def wishlist_attributes
130
+ params.require(:wishlist).permit(permitted_wishlist_attributes)
131
+ end
132
+
133
+ def wished_item_attributes
134
+ params.permit(permitted_wished_item_attributes)
135
+ end
136
+
137
+ def wished_item
138
+ @wished_item ||= resource.wished_items.find(params[:item_id])
139
+ end
140
+
141
+ def serialize_wished_item(wished_item)
142
+ ::Spree::V2::Storefront::WishedItemSerializer.new(
143
+ wished_item,
144
+ params: serializer_params,
145
+ include: resource_includes,
146
+ fields: sparse_fields
147
+ ).serializable_hash
148
+ end
149
+
150
+ def serializer_params
151
+ super.merge(is_variant_included: params[:is_variant_included])
152
+ end
153
+
154
+ def render_error_item_quantity
155
+ render json: { error: I18n.t('spree.api.v2.wishlist.wrong_quantity') }, status: 422
156
+ end
157
+
158
+ def ensure_valid_quantity
159
+ return render_error_item_quantity if params[:quantity].present? && params[:quantity].to_i <= 0
160
+
161
+ params[:quantity] = if params[:quantity].present?
162
+ params[:quantity].to_i
163
+ else
164
+ 1
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -23,7 +23,7 @@ module Spree
23
23
  # leaving this method in public scope so it's still possible to modify
24
24
  # those params to support non-standard non-JSON API parameters
25
25
  def collection_permitted_params
26
- params.permit(:format, :page, :per_page, :sort, :include, :fields, filter: {})
26
+ params.permit(:format, :page, :per_page, :sort, :include, fields: {}, filter: {})
27
27
  end
28
28
 
29
29
  private
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Webhooks
3
+ module Subscribers
4
+ class MakeRequestJob < Spree::BaseJob
5
+ queue_as :spree_webhooks
6
+
7
+ def perform(webhook_payload_body, event_name, subscriber)
8
+ Spree::Webhooks::Subscribers::HandleRequest.new(
9
+ event_name: event_name,
10
+ subscriber: subscriber,
11
+ webhook_payload_body: webhook_payload_body
12
+ ).call
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end