spree_api 3.6.6 → 3.7.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/spree/api/main.js +36 -0
  3. data/app/assets/javascripts/spree/api/storefront/cart.js +49 -0
  4. data/app/controllers/concerns/spree/api/v2/storefront/order_concern.rb +48 -0
  5. data/app/controllers/spree/api/errors_controller.rb +9 -0
  6. data/app/controllers/spree/api/v1/checkouts_controller.rb +12 -0
  7. data/app/controllers/spree/api/v1/customer_returns_controller.rb +1 -0
  8. data/app/controllers/spree/api/v1/line_items_controller.rb +11 -10
  9. data/app/controllers/spree/api/v1/orders_controller.rb +6 -6
  10. data/app/controllers/spree/api/v1/product_properties_controller.rb +1 -0
  11. data/app/controllers/spree/api/v1/products_controller.rb +6 -6
  12. data/app/controllers/spree/api/v1/promotions_controller.rb +1 -0
  13. data/app/controllers/spree/api/v1/reimbursements_controller.rb +1 -0
  14. data/app/controllers/spree/api/v1/shipments_controller.rb +12 -3
  15. data/app/controllers/spree/api/v1/variants_controller.rb +5 -3
  16. data/app/controllers/spree/api/v2/base_controller.rb +94 -0
  17. data/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb +55 -0
  18. data/app/controllers/spree/api/v2/storefront/account_controller.rb +33 -0
  19. data/app/controllers/spree/api/v2/storefront/cart_controller.rb +124 -0
  20. data/app/controllers/spree/api/v2/storefront/checkout_controller.rb +108 -0
  21. data/app/controllers/spree/api/v2/storefront/countries_controller.rb +57 -0
  22. data/app/controllers/spree/api/v2/storefront/products_controller.rb +87 -0
  23. data/app/controllers/spree/api/v2/storefront/taxons_controller.rb +82 -0
  24. data/app/helpers/spree/api/api_helpers.rb +1 -1
  25. data/app/helpers/spree/api/v2/collection_options_helpers.rb +37 -0
  26. data/app/models/doorkeeper/access_grant_decorator.rb +3 -0
  27. data/app/models/doorkeeper/access_token_decorator.rb +3 -0
  28. data/app/models/doorkeeper/application_decorator.rb +3 -0
  29. data/app/serializers/spree/v2/storefront/account/credit_card_serializer.rb +16 -0
  30. data/app/serializers/spree/v2/storefront/account_serializer.rb +29 -0
  31. data/app/serializers/spree/v2/storefront/address_serializer.rb +14 -0
  32. data/app/serializers/spree/v2/storefront/base_serializer.rb +9 -0
  33. data/app/serializers/spree/v2/storefront/cart_serializer.rb +40 -0
  34. data/app/serializers/spree/v2/storefront/country_serializer.rb +18 -0
  35. data/app/serializers/spree/v2/storefront/image_serializer.rb +11 -0
  36. data/app/serializers/spree/v2/storefront/line_item_serializer.rb +17 -0
  37. data/app/serializers/spree/v2/storefront/option_type_serializer.rb +13 -0
  38. data/app/serializers/spree/v2/storefront/option_value_serializer.rb +11 -0
  39. data/app/serializers/spree/v2/storefront/payment_method_serializer.rb +11 -0
  40. data/app/serializers/spree/v2/storefront/payment_serializer.rb +12 -0
  41. data/app/serializers/spree/v2/storefront/product_property_serializer.rb +14 -0
  42. data/app/serializers/spree/v2/storefront/product_serializer.rb +35 -0
  43. data/app/serializers/spree/v2/storefront/promotion_serializer.rb +12 -0
  44. data/app/serializers/spree/v2/storefront/shipment_serializer.rb +16 -0
  45. data/app/serializers/spree/v2/storefront/shipping_rate_serializer.rb +14 -0
  46. data/app/serializers/spree/v2/storefront/state_serializer.rb +11 -0
  47. data/app/serializers/spree/v2/storefront/taxon_image_serializer.rb +11 -0
  48. data/app/serializers/spree/v2/storefront/taxon_serializer.rb +28 -0
  49. data/app/serializers/spree/v2/storefront/taxonomy_serializer.rb +11 -0
  50. data/app/serializers/spree/v2/storefront/user_serializer.rb +11 -0
  51. data/app/serializers/spree/v2/storefront/variant_serializer.rb +21 -0
  52. data/app/views/spree/api/v1/orders/order.v1.rabl +1 -1
  53. data/config/initializers/doorkeeper.rb +20 -0
  54. data/config/locales/en.yml +4 -0
  55. data/config/routes.rb +51 -3
  56. data/db/migrate/20180320110726_create_doorkeeper_tables.rb +69 -0
  57. data/docs/oauth/index.yml +77 -0
  58. data/docs/v2/storefront/index.yaml +2444 -0
  59. data/lib/spree/api/engine.rb +9 -2
  60. data/lib/spree_api.rb +2 -0
  61. data/spree_api.gemspec +5 -1
  62. metadata +92 -7
@@ -0,0 +1,55 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ module Account
6
+ class CreditCardsController < ::Spree::Api::V2::BaseController
7
+ before_action :require_spree_current_user
8
+
9
+ def index
10
+ render_serialized_payload { serialize_collection(resource) }
11
+ end
12
+
13
+ def show
14
+ render_serialized_payload { serialize_resource(resource) }
15
+ end
16
+
17
+ private
18
+
19
+ def resource
20
+ dependencies[:resource_finder].new.execute(scope: scope, params: params)
21
+ end
22
+
23
+ def dependencies
24
+ {
25
+ resource_finder: Spree::CreditCards::Find,
26
+ collection_serializer: Spree::V2::Storefront::Account::CreditCardSerializer,
27
+ resource_serializer: Spree::V2::Storefront::Account::CreditCardSerializer
28
+ }
29
+ end
30
+
31
+ def serialize_collection(collection)
32
+ dependencies[:collection_serializer].new(
33
+ collection,
34
+ include: resource_includes,
35
+ fields: sparse_fields
36
+ ).serializable_hash
37
+ end
38
+
39
+ def serialize_resource(resource)
40
+ dependencies[:resource_serializer].new(
41
+ resource,
42
+ include: resource_includes,
43
+ fields: sparse_fields
44
+ ).serializable_hash
45
+ end
46
+
47
+ def scope
48
+ spree_current_user.credit_cards.accessible_by(current_ability, :read)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,33 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class AccountController < ::Spree::Api::V2::BaseController
6
+ before_action :require_spree_current_user
7
+
8
+ def show
9
+ render_serialized_payload { serialize_resource(resource) }
10
+ end
11
+
12
+ private
13
+
14
+ def resource
15
+ spree_current_user
16
+ end
17
+
18
+ def serialize_resource(resource)
19
+ dependencies[:resource_serializer].new(
20
+ resource,
21
+ include: resource_includes,
22
+ fields: sparse_fields
23
+ ).serializable_hash
24
+ end
25
+
26
+ def dependencies
27
+ { resource_serializer: Spree::V2::Storefront::AccountSerializer }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,124 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class CartController < ::Spree::Api::V2::BaseController
6
+ include Spree::Api::V2::Storefront::OrderConcern
7
+ before_action :ensure_order, except: :create
8
+
9
+ def create
10
+ spree_authorize! :create, Spree::Order
11
+
12
+ order_params = {
13
+ user: spree_current_user,
14
+ store: spree_current_store,
15
+ currency: current_currency
16
+ }
17
+
18
+ order = spree_current_order if spree_current_order.present?
19
+ order ||= dependencies[:create_cart].call(order_params).value
20
+
21
+ render_serialized_payload(201) { serialize_order(order) }
22
+ end
23
+
24
+ def add_item
25
+ variant = Spree::Variant.find(params[:variant_id])
26
+
27
+ spree_authorize! :update, spree_current_order, order_token
28
+ spree_authorize! :show, variant
29
+
30
+ result = dependencies[:add_item_to_cart].call(
31
+ order: spree_current_order,
32
+ variant: variant,
33
+ quantity: params[:quantity],
34
+ options: params[:options]
35
+ )
36
+
37
+ render_order(result)
38
+ end
39
+
40
+ def remove_line_item
41
+ spree_authorize! :update, spree_current_order, order_token
42
+
43
+ dependencies[:remove_item_from_cart].call(
44
+ order: spree_current_order,
45
+ line_item: line_item
46
+ )
47
+
48
+ render_serialized_payload { serialized_current_order }
49
+ end
50
+
51
+ def empty
52
+ spree_authorize! :update, spree_current_order, order_token
53
+
54
+ spree_current_order.empty!
55
+
56
+ render_serialized_payload { serialized_current_order }
57
+ end
58
+
59
+ def set_quantity
60
+ return render_error_item_quantity unless params[:quantity].to_i > 0
61
+
62
+ spree_authorize! :update, spree_current_order, order_token
63
+
64
+ result = dependencies[:set_item_quantity].call(order: spree_current_order, line_item: line_item, quantity: params[:quantity])
65
+
66
+ render_order(result)
67
+ end
68
+
69
+ def show
70
+ spree_authorize! :show, spree_current_order, order_token
71
+
72
+ render_serialized_payload { serialized_current_order }
73
+ end
74
+
75
+ def apply_coupon_code
76
+ spree_authorize! :update, spree_current_order, order_token
77
+
78
+ spree_current_order.coupon_code = params[:coupon_code]
79
+ result = dependencies[:coupon_handler].new(spree_current_order).apply
80
+
81
+ if result.error.blank?
82
+ render_serialized_payload { serialized_current_order }
83
+ else
84
+ render_error_payload(result.error)
85
+ end
86
+ end
87
+
88
+ def remove_coupon_code
89
+ spree_authorize! :update, spree_current_order, order_token
90
+
91
+ result = dependencies[:coupon_handler].new(spree_current_order).remove(params[:coupon_code])
92
+
93
+ if result.error.blank?
94
+ render_serialized_payload { serialized_current_order }
95
+ else
96
+ render_error_payload(result.error)
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def dependencies
103
+ {
104
+ create_cart: Spree::Cart::Create,
105
+ add_item_to_cart: Spree::Cart::AddItem,
106
+ remove_item_from_cart: Spree::Cart::RemoveLineItem,
107
+ cart_serializer: Spree::V2::Storefront::CartSerializer,
108
+ set_item_quantity: Spree::Cart::SetQuantity,
109
+ coupon_handler: Spree::PromotionHandler::Coupon
110
+ }
111
+ end
112
+
113
+ def line_item
114
+ @line_item ||= spree_current_order.line_items.find(params[:line_item_id])
115
+ end
116
+
117
+ def render_error_item_quantity
118
+ render json: { error: I18n.t(:wrong_quantity, scope: 'spree.api.v2.cart') }, status: 422
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,108 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class CheckoutController < ::Spree::Api::V2::BaseController
6
+ include Spree::Api::V2::Storefront::OrderConcern
7
+ before_action :ensure_order
8
+
9
+ def next
10
+ spree_authorize! :update, spree_current_order, order_token
11
+
12
+ result = dependencies[:next_state_handler].call(order: spree_current_order)
13
+
14
+ render_order(result)
15
+ end
16
+
17
+ def advance
18
+ spree_authorize! :update, spree_current_order, order_token
19
+
20
+ result = dependencies[:advance_handler].call(order: spree_current_order)
21
+
22
+ render_order(result)
23
+ end
24
+
25
+ def complete
26
+ spree_authorize! :update, spree_current_order, order_token
27
+
28
+ result = dependencies[:completer].call(order: spree_current_order)
29
+
30
+ render_order(result)
31
+ end
32
+
33
+ def update
34
+ spree_authorize! :update, spree_current_order, order_token
35
+
36
+ result = dependencies[:updater].call(
37
+ order: spree_current_order,
38
+ params: params,
39
+ permitted_attributes: dependencies[:permitted_attributes],
40
+ request_env: request.headers.env
41
+ )
42
+
43
+ render_order(result)
44
+ end
45
+
46
+ def add_store_credit
47
+ spree_authorize! :update, spree_current_order, order_token
48
+
49
+ result = dependencies[:add_store_credit_handler].call(
50
+ order: spree_current_order,
51
+ amount: params[:amount].try(:to_f)
52
+ )
53
+
54
+ render_order(result)
55
+ end
56
+
57
+ def remove_store_credit
58
+ spree_authorize! :update, spree_current_order, order_token
59
+
60
+ result = dependencies[:remove_store_credit_handler].call(order: spree_current_order)
61
+ render_order(result)
62
+ end
63
+
64
+ def shipping_rates
65
+ result = dependencies[:shipping_rates_getter].call(order: spree_current_order)
66
+
67
+ render_serialized_payload { serialize_shipping_rates(result.value) }
68
+ end
69
+
70
+ def payment_methods
71
+ render_serialized_payload { serialize_payment_methods(spree_current_order.available_payment_methods) }
72
+ end
73
+
74
+ private
75
+
76
+ def dependencies
77
+ {
78
+ next_state_handler: Spree::Checkout::Next,
79
+ advance_handler: Spree::Checkout::Advance,
80
+ add_store_credit_handler: Spree::Checkout::AddStoreCredit,
81
+ remove_store_credit_handler: Spree::Checkout::RemoveStoreCredit,
82
+ completer: Spree::Checkout::Complete,
83
+ updater: Spree::Checkout::Update,
84
+ cart_serializer: Spree::V2::Storefront::CartSerializer,
85
+ payment_methods_serializer: Spree::V2::Storefront::PaymentMethodSerializer,
86
+ shipping_rates_getter: Spree::Checkout::GetShippingRates,
87
+ shipping_rates_serializer: Spree::V2::Storefront::ShipmentSerializer,
88
+ # defined in https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/strong_parameters.rb#L19
89
+ permitted_attributes: permitted_checkout_attributes
90
+ }
91
+ end
92
+
93
+ def serialize_payment_methods(payment_methods)
94
+ dependencies[:payment_methods_serializer].new(payment_methods).serializable_hash
95
+ end
96
+
97
+ def serialize_shipping_rates(shipments)
98
+ dependencies[:shipping_rates_serializer].new(
99
+ shipments,
100
+ include: [:shipping_rates],
101
+ params: { show_rates: true }
102
+ ).serializable_hash
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,57 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class CountriesController < ::Spree::Api::V2::BaseController
6
+ include Spree::Api::V2::CollectionOptionsHelpers
7
+
8
+ def index
9
+ render_serialized_payload { serialize_collection(collection) }
10
+ end
11
+
12
+ def show
13
+ render_serialized_payload { serialize_resource(resource) }
14
+ end
15
+
16
+ private
17
+
18
+ def serialize_collection(collection)
19
+ dependencies[:collection_serializer].new(collection).serializable_hash
20
+ end
21
+
22
+ def serialize_resource(resource)
23
+ dependencies[:resource_serializer].new(
24
+ resource,
25
+ include: resource_includes,
26
+ fields: sparse_fields,
27
+ params: { include_states: true }
28
+ ).serializable_hash
29
+ end
30
+
31
+ def collection
32
+ dependencies[:collection_finder].new(scope, params).call
33
+ end
34
+
35
+ def resource
36
+ return scope.default if params[:iso] == 'default'
37
+
38
+ scope.find_by(iso: params[:iso]&.upcase) ||
39
+ scope.find_by(iso3: params[:iso]&.upcase)
40
+ end
41
+
42
+ def dependencies
43
+ {
44
+ collection_serializer: Spree::V2::Storefront::CountrySerializer,
45
+ resource_serializer: Spree::V2::Storefront::CountrySerializer,
46
+ collection_finder: Spree::Countries::Find
47
+ }
48
+ end
49
+
50
+ def scope
51
+ Spree::Country.accessible_by(current_ability, :read)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,87 @@
1
+ module Spree
2
+ module Api
3
+ module V2
4
+ module Storefront
5
+ class ProductsController < ::Spree::Api::V2::BaseController
6
+ include Spree::Api::V2::CollectionOptionsHelpers
7
+
8
+ def index
9
+ render_serialized_payload { serialize_collection(paginated_collection) }
10
+ end
11
+
12
+ def show
13
+ render_serialized_payload { serialize_resource(resource) }
14
+ end
15
+
16
+ private
17
+
18
+ def serialize_collection(collection)
19
+ dependencies[:collection_serializer].new(
20
+ collection,
21
+ collection_options(collection)
22
+ ).serializable_hash
23
+ end
24
+
25
+ def serialize_resource(resource)
26
+ dependencies[:resource_serializer].new(
27
+ resource,
28
+ include: resource_includes,
29
+ fields: sparse_fields
30
+ ).serializable_hash
31
+ end
32
+
33
+ def paginated_collection
34
+ dependencies[:collection_paginator].new(sorted_collection, params).call
35
+ end
36
+
37
+ def sorted_collection
38
+ dependencies[:collection_sorter].new(collection, params, current_currency).call
39
+ end
40
+
41
+ def collection
42
+ dependencies[:collection_finder].new(scope: scope, params: params, current_currency: current_currency).execute
43
+ end
44
+
45
+ def resource
46
+ scope.find_by(slug: params[:id]) || scope.find(params[:id])
47
+ end
48
+
49
+ def dependencies
50
+ {
51
+ collection_sorter: Spree::Products::Sort,
52
+ collection_finder: Spree::Products::Find,
53
+ collection_paginator: Spree::Shared::Paginate,
54
+ collection_serializer: Spree::V2::Storefront::ProductSerializer,
55
+ resource_serializer: Spree::V2::Storefront::ProductSerializer
56
+ }
57
+ end
58
+
59
+ def collection_options(collection)
60
+ {
61
+ links: collection_links(collection),
62
+ meta: collection_meta(collection),
63
+ include: resource_includes,
64
+ fields: sparse_fields
65
+ }
66
+ end
67
+
68
+ def scope
69
+ Spree::Product.accessible_by(current_ability, :read).includes(scope_includes)
70
+ end
71
+
72
+ def scope_includes
73
+ {
74
+ master: :default_price,
75
+ variants: [],
76
+ variant_images: [],
77
+ taxons: [],
78
+ product_properties: :property,
79
+ option_types: :option_values,
80
+ variants_including_master: %i[default_price option_values]
81
+ }
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end