spree_legacy_api_v2 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +47 -0
- data/Rakefile +23 -0
- data/app/controllers/concerns/spree/api/v2/caching.rb +40 -0
- data/app/controllers/concerns/spree/api/v2/coupon_codes_helper.rb +29 -0
- data/app/controllers/concerns/spree/api/v2/number_resource.rb +11 -0
- data/app/controllers/concerns/spree/api/v2/platform/nested_set_reposition_concern.rb +37 -0
- data/app/controllers/concerns/spree/api/v2/platform/promotion_calculator_params.rb +17 -0
- data/app/controllers/concerns/spree/api/v2/platform/promotion_rule_params.rb +16 -0
- data/app/controllers/concerns/spree/api/v2/product_list_includes.rb +21 -0
- data/app/controllers/concerns/spree/api/v2/storefront/metadata_controller_concern.rb +18 -0
- data/app/controllers/concerns/spree/api/v2/storefront/order_concern.rb +49 -0
- data/app/controllers/spree/admin/oauth_applications_controller.rb +23 -0
- data/app/controllers/spree/api/v2/base_controller.rb +233 -0
- data/app/controllers/spree/api/v2/data_feeds/google_controller.rb +24 -0
- data/app/controllers/spree/api/v2/platform/addresses_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/adjustments_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/classifications_controller.rb +26 -0
- data/app/controllers/spree/api/v2/platform/countries_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/data_feeds_controller.rb +19 -0
- data/app/controllers/spree/api/v2/platform/digital_links_controller.rb +29 -0
- data/app/controllers/spree/api/v2/platform/digitals_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/gift_cards_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/line_items_controller.rb +63 -0
- data/app/controllers/spree/api/v2/platform/option_types_controller.rb +19 -0
- data/app/controllers/spree/api/v2/platform/option_values_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/orders_controller.rb +167 -0
- data/app/controllers/spree/api/v2/platform/payment_methods_controller.rb +31 -0
- data/app/controllers/spree/api/v2/platform/payments_controller.rb +21 -0
- data/app/controllers/spree/api/v2/platform/products_controller.rb +41 -0
- data/app/controllers/spree/api/v2/platform/promotion_actions_controller.rb +34 -0
- data/app/controllers/spree/api/v2/platform/promotion_categories_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/promotion_rules_controller.rb +29 -0
- data/app/controllers/spree/api/v2/platform/promotions_controller.rb +35 -0
- data/app/controllers/spree/api/v2/platform/resource_controller.rb +154 -0
- data/app/controllers/spree/api/v2/platform/roles_controller.rb +19 -0
- data/app/controllers/spree/api/v2/platform/shipments_controller.rb +147 -0
- data/app/controllers/spree/api/v2/platform/shipping_categories_controller.rb +19 -0
- data/app/controllers/spree/api/v2/platform/shipping_methods_controller.rb +28 -0
- data/app/controllers/spree/api/v2/platform/states_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/stock_items_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/stock_locations_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/store_credit_categories_controller.rb +19 -0
- data/app/controllers/spree/api/v2/platform/store_credit_types_controller.rb +19 -0
- data/app/controllers/spree/api/v2/platform/store_credits_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/tax_categories_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/tax_rates_controller.rb +27 -0
- data/app/controllers/spree/api/v2/platform/taxonomies_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/taxons_controller.rb +59 -0
- data/app/controllers/spree/api/v2/platform/users_controller.rb +37 -0
- data/app/controllers/spree/api/v2/platform/variants_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/wished_items_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/wishlists_controller.rb +23 -0
- data/app/controllers/spree/api/v2/platform/zones_controller.rb +23 -0
- data/app/controllers/spree/api/v2/resource_controller.rb +79 -0
- data/app/controllers/spree/api/v2/storefront/account/addresses_controller.rb +77 -0
- data/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb +52 -0
- data/app/controllers/spree/api/v2/storefront/account/orders_controller.rb +50 -0
- data/app/controllers/spree/api/v2/storefront/account_controller.rb +52 -0
- data/app/controllers/spree/api/v2/storefront/cart_controller.rb +246 -0
- data/app/controllers/spree/api/v2/storefront/checkout_controller.rb +183 -0
- data/app/controllers/spree/api/v2/storefront/countries_controller.rb +57 -0
- data/app/controllers/spree/api/v2/storefront/digitals_controller.rb +58 -0
- data/app/controllers/spree/api/v2/storefront/order_status_controller.rb +34 -0
- data/app/controllers/spree/api/v2/storefront/policies_controller.rb +31 -0
- data/app/controllers/spree/api/v2/storefront/post_categories_controller.rb +35 -0
- data/app/controllers/spree/api/v2/storefront/posts_controller.rb +51 -0
- data/app/controllers/spree/api/v2/storefront/products_controller.rb +66 -0
- data/app/controllers/spree/api/v2/storefront/stores_controller.rb +27 -0
- data/app/controllers/spree/api/v2/storefront/taxons_controller.rb +51 -0
- data/app/controllers/spree/api/v2/storefront/variants_controller.rb +41 -0
- data/app/controllers/spree/api/v2/storefront/wishlists_controller.rb +214 -0
- data/app/helpers/spree/api/v2/collection_options_helpers.rb +46 -0
- data/app/helpers/spree/api/v2/display_money_helper.rb +63 -0
- data/app/helpers/spree/api/v2/store_media_serializer_images_concern.rb +30 -0
- data/app/models/concerns/spree/user_api_authentication.rb +19 -0
- data/app/models/concerns/spree/user_api_methods.rb +7 -0
- data/app/models/spree/oauth_access_grant.rb +7 -0
- data/app/models/spree/oauth_access_token.rb +7 -0
- data/app/models/spree/oauth_application.rb +22 -0
- data/app/paginators/spree/api/paginate.rb +68 -0
- data/app/presenters/spree/api/products/filters_presenter.rb +39 -0
- data/app/serializers/concerns/spree/api/v2/image_transformation_concern.rb +15 -0
- data/app/serializers/concerns/spree/api/v2/public_metafields_concern.rb +15 -0
- data/app/serializers/concerns/spree/api/v2/resource_serializer_concern.rb +42 -0
- data/app/serializers/concerns/spree/api/v2/taxon_image_transformation_concern.rb +15 -0
- data/app/serializers/spree/api/v2/base_serializer.rb +43 -0
- data/app/serializers/spree/api/v2/platform/address_serializer.rb +15 -0
- data/app/serializers/spree/api/v2/platform/adjustment_serializer.rb +20 -0
- data/app/serializers/spree/api/v2/platform/admin_user_serializer.rb +15 -0
- data/app/serializers/spree/api/v2/platform/asset_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/base_serializer.rb +10 -0
- data/app/serializers/spree/api/v2/platform/calculator_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/classification_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/country_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/credit_card_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/customer_return_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/data_feed_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/digital_link_serializer.rb +16 -0
- data/app/serializers/spree/api/v2/platform/digital_serializer.rb +29 -0
- data/app/serializers/spree/api/v2/platform/gift_card_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/image_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/inventory_unit_serializer.rb +19 -0
- data/app/serializers/spree/api/v2/platform/line_item_serializer.rb +19 -0
- data/app/serializers/spree/api/v2/platform/log_entry_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/metafield_serializer.rb +21 -0
- data/app/serializers/spree/api/v2/platform/option_type_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/option_value_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/order_promotion_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/order_serializer.rb +31 -0
- data/app/serializers/spree/api/v2/platform/payment_capture_event_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/payment_method_serializer.rb +18 -0
- data/app/serializers/spree/api/v2/platform/payment_serializer.rb +22 -0
- data/app/serializers/spree/api/v2/platform/payment_source_serializer.rb +12 -0
- data/app/serializers/spree/api/v2/platform/price_serializer.rb +19 -0
- data/app/serializers/spree/api/v2/platform/product_property_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/product_serializer.rb +90 -0
- data/app/serializers/spree/api/v2/platform/promotion_action_line_item_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/promotion_action_serializer.rb +19 -0
- data/app/serializers/spree/api/v2/platform/promotion_category_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/promotion_rule_serializer.rb +21 -0
- data/app/serializers/spree/api/v2/platform/promotion_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/property_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/prototype_serializer.rb +15 -0
- data/app/serializers/spree/api/v2/platform/refund_reason_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/refund_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/reimbursement_credit_serializer.rb +10 -0
- data/app/serializers/spree/api/v2/platform/reimbursement_serializer.rb +19 -0
- data/app/serializers/spree/api/v2/platform/reimbursement_type_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/return_authorization_reason_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/return_authorization_serializer.rb +17 -0
- data/app/serializers/spree/api/v2/platform/return_item_serializer.rb +16 -0
- data/app/serializers/spree/api/v2/platform/role_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/shipment_serializer.rb +22 -0
- data/app/serializers/spree/api/v2/platform/shipping_category_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/shipping_method_serializer.rb +16 -0
- data/app/serializers/spree/api/v2/platform/shipping_rate_serializer.rb +15 -0
- data/app/serializers/spree/api/v2/platform/state_change_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/state_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/stock_item_serializer.rb +18 -0
- data/app/serializers/spree/api/v2/platform/stock_location_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/stock_movement_serializer.rb +11 -0
- data/app/serializers/spree/api/v2/platform/stock_transfer_serializer.rb +15 -0
- data/app/serializers/spree/api/v2/platform/store_credit_category_serializer.rb +12 -0
- data/app/serializers/spree/api/v2/platform/store_credit_event_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/store_credit_serializer.rb +18 -0
- data/app/serializers/spree/api/v2/platform/store_credit_type_serializer.rb +12 -0
- data/app/serializers/spree/api/v2/platform/store_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/tax_category_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/tax_rate_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/taxon_image_serializer.rb +15 -0
- data/app/serializers/spree/api/v2/platform/taxon_serializer.rb +47 -0
- data/app/serializers/spree/api/v2/platform/taxonomy_serializer.rb +14 -0
- data/app/serializers/spree/api/v2/platform/user_serializer.rb +37 -0
- data/app/serializers/spree/api/v2/platform/variant_serializer.rb +66 -0
- data/app/serializers/spree/api/v2/platform/wished_item_serializer.rb +29 -0
- data/app/serializers/spree/api/v2/platform/wishlist_serializer.rb +19 -0
- data/app/serializers/spree/api/v2/platform/zone_member_serializer.rb +13 -0
- data/app/serializers/spree/api/v2/platform/zone_serializer.rb +13 -0
- data/app/serializers/spree/v2/storefront/address_serializer.rb +22 -0
- data/app/serializers/spree/v2/storefront/base_serializer.rb +10 -0
- data/app/serializers/spree/v2/storefront/cart_serializer.rb +67 -0
- data/app/serializers/spree/v2/storefront/country_serializer.rb +24 -0
- data/app/serializers/spree/v2/storefront/credit_card_serializer.rb +15 -0
- data/app/serializers/spree/v2/storefront/digital_link_serializer.rb +11 -0
- data/app/serializers/spree/v2/storefront/estimated_shipping_rate_serializer.rb +35 -0
- data/app/serializers/spree/v2/storefront/gift_card_serializer.rb +16 -0
- data/app/serializers/spree/v2/storefront/image_serializer.rb +13 -0
- data/app/serializers/spree/v2/storefront/line_item_serializer.rb +23 -0
- data/app/serializers/spree/v2/storefront/metafield_serializer.rb +19 -0
- data/app/serializers/spree/v2/storefront/option_type_serializer.rb +15 -0
- data/app/serializers/spree/v2/storefront/option_value_serializer.rb +13 -0
- data/app/serializers/spree/v2/storefront/order_promotion_serializer.rb +14 -0
- data/app/serializers/spree/v2/storefront/order_serializer.rb +11 -0
- data/app/serializers/spree/v2/storefront/payment_method_serializer.rb +17 -0
- data/app/serializers/spree/v2/storefront/payment_serializer.rb +20 -0
- data/app/serializers/spree/v2/storefront/payment_source_serializer.rb +12 -0
- data/app/serializers/spree/v2/storefront/policy_serializer.rb +19 -0
- data/app/serializers/spree/v2/storefront/post_category_serializer.rb +23 -0
- data/app/serializers/spree/v2/storefront/post_serializer.rb +51 -0
- data/app/serializers/spree/v2/storefront/product_property_serializer.rb +19 -0
- data/app/serializers/spree/v2/storefront/product_serializer.rb +92 -0
- data/app/serializers/spree/v2/storefront/shipment_serializer.rb +26 -0
- data/app/serializers/spree/v2/storefront/shipping_category_serializer.rb +10 -0
- data/app/serializers/spree/v2/storefront/shipping_method_serializer.rb +12 -0
- data/app/serializers/spree/v2/storefront/shipping_rate_serializer.rb +26 -0
- data/app/serializers/spree/v2/storefront/state_serializer.rb +11 -0
- data/app/serializers/spree/v2/storefront/stock_location_serializer.rb +11 -0
- data/app/serializers/spree/v2/storefront/store_credit_category_serializer.rb +11 -0
- data/app/serializers/spree/v2/storefront/store_credit_event_serializer.rb +15 -0
- data/app/serializers/spree/v2/storefront/store_credit_serializer.rb +19 -0
- data/app/serializers/spree/v2/storefront/store_credit_type_serializer.rb +11 -0
- data/app/serializers/spree/v2/storefront/store_serializer.rb +18 -0
- data/app/serializers/spree/v2/storefront/taxon_image_serializer.rb +13 -0
- data/app/serializers/spree/v2/storefront/taxon_serializer.rb +55 -0
- data/app/serializers/spree/v2/storefront/taxonomy_serializer.rb +13 -0
- data/app/serializers/spree/v2/storefront/user_serializer.rb +33 -0
- data/app/serializers/spree/v2/storefront/variant_serializer.rb +58 -0
- data/app/serializers/spree/v2/storefront/wished_item_serializer.rb +29 -0
- data/app/serializers/spree/v2/storefront/wishlist_serializer.rb +17 -0
- data/app/views/spree/admin/oauth_applications/_form.html.erb +6 -0
- data/app/views/spree/admin/oauth_applications/_table_header.html.erb +7 -0
- data/app/views/spree/admin/oauth_applications/_table_row.html.erb +34 -0
- data/app/views/spree/admin/oauth_applications/create.turbo_stream.erb +31 -0
- data/app/views/spree/admin/oauth_applications/edit.html.erb +1 -0
- data/app/views/spree/admin/oauth_applications/index.html.erb +21 -0
- data/app/views/spree/admin/oauth_applications/new.html.erb +1 -0
- data/config/initializers/doorkeeper.rb +50 -0
- data/config/initializers/json_api_mime_types.rb +8 -0
- data/config/initializers/user_class_extensions.rb +7 -0
- data/config/routes.rb +219 -0
- data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
- data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
- data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
- data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +7 -0
- data/db/migrate/20180320110726_create_doorkeeper_tables.rb +69 -0
- data/db/migrate/20210727102516_change_integer_id_columns_type.rb +9 -0
- data/db/migrate/20210919183228_enable_polymorphic_resource_owner.rb +21 -0
- data/lib/generators/spree_legacy_api_v2/install/install_generator.rb +23 -0
- data/lib/spree_legacy_api_v2/engine.rb +77 -0
- data/lib/spree_legacy_api_v2/testing_support/factories/oauth_application_factory.rb +7 -0
- data/lib/spree_legacy_api_v2/testing_support/factories.rb +1 -0
- data/lib/spree_legacy_api_v2/testing_support/serializers.rb +15 -0
- data/lib/spree_legacy_api_v2/testing_support/v2/base.rb +13 -0
- data/lib/spree_legacy_api_v2/testing_support/v2/current_order.rb +116 -0
- data/lib/spree_legacy_api_v2/testing_support/v2/platform_contexts.rb +272 -0
- data/lib/spree_legacy_api_v2/testing_support/v2/serializers_params.rb +16 -0
- data/lib/spree_legacy_api_v2/version.rb +9 -0
- data/lib/spree_legacy_api_v2.rb +7 -0
- metadata +359 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Api
|
|
3
|
+
module V2
|
|
4
|
+
module Platform
|
|
5
|
+
class ResourceController < ::Spree::Api::V2::ResourceController
|
|
6
|
+
# doorkeeper scopes usage: https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
|
|
7
|
+
before_action :validate_token_client
|
|
8
|
+
before_action -> { doorkeeper_authorize! :read, :admin }
|
|
9
|
+
before_action -> { doorkeeper_authorize! :write, :admin }, if: :write_request?
|
|
10
|
+
|
|
11
|
+
# optional authorization if using a user token instead of app token
|
|
12
|
+
before_action :authorize_spree_user
|
|
13
|
+
|
|
14
|
+
# index and show actions are defined in Spree::Api::V2::ResourceController
|
|
15
|
+
|
|
16
|
+
def create
|
|
17
|
+
resource = model_class.new(permitted_resource_params)
|
|
18
|
+
ensure_current_store(resource)
|
|
19
|
+
|
|
20
|
+
if resource.save
|
|
21
|
+
render_serialized_payload(201) { serialize_resource(resource) }
|
|
22
|
+
else
|
|
23
|
+
render_error_payload(resource.errors)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def update
|
|
28
|
+
resource.assign_attributes(permitted_resource_params)
|
|
29
|
+
ensure_current_store(resource)
|
|
30
|
+
|
|
31
|
+
if resource.save
|
|
32
|
+
render_serialized_payload { serialize_resource(resource) }
|
|
33
|
+
else
|
|
34
|
+
render_error_payload(resource.errors)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def destroy
|
|
39
|
+
if resource.destroy
|
|
40
|
+
head 204
|
|
41
|
+
else
|
|
42
|
+
render_error_payload(resource.errors)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
protected
|
|
47
|
+
|
|
48
|
+
def resource_serializer
|
|
49
|
+
serializer_base_name = model_class.to_s.sub('Spree::', '')
|
|
50
|
+
"Spree::Api::V2::Platform::#{serializer_base_name}Serializer".constantize
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def collection_serializer
|
|
54
|
+
resource_serializer
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# overwriting to utilize ransack gem for filtering
|
|
58
|
+
# https://github.com/activerecord-hackery/ransack#search-matchers
|
|
59
|
+
def collection
|
|
60
|
+
@collection ||= scope.ransack(params[:filter]).result
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# overwriting to skip cancancan check if API is consumed by an application
|
|
64
|
+
def scope
|
|
65
|
+
return super if spree_current_user.present?
|
|
66
|
+
|
|
67
|
+
super(skip_cancancan: true)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# We're overwriting this method because the original one calls `dookreeper_authorize`
|
|
71
|
+
# which breaks our application authorizations defined on top of this controller
|
|
72
|
+
def spree_current_user
|
|
73
|
+
return nil unless doorkeeper_token
|
|
74
|
+
return nil if doorkeeper_token.resource_owner_id.nil?
|
|
75
|
+
return @spree_current_user if @spree_current_user
|
|
76
|
+
|
|
77
|
+
@spree_current_user ||= doorkeeper_token.resource_owner
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def access_denied(exception)
|
|
81
|
+
access_denied_401(exception)
|
|
82
|
+
end
|
|
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
|
+
|
|
90
|
+
# if using a user oAuth token we need to check CanCanCan abilities
|
|
91
|
+
# defined in https://github.com/spree/spree/blob/main/core/app/models/spree/ability.rb
|
|
92
|
+
def authorize_spree_user
|
|
93
|
+
return if spree_current_user.nil?
|
|
94
|
+
|
|
95
|
+
case action_name
|
|
96
|
+
when 'create'
|
|
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
|
|
104
|
+
else
|
|
105
|
+
spree_authorize! :update, resource
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def model_param_name
|
|
110
|
+
model_class.to_s.demodulize.underscore
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def spree_permitted_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 + metafields_params
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def metadata_params
|
|
124
|
+
if model_class.include?(Metadata)
|
|
125
|
+
[{ public_metadata: {}, private_metadata: {} }]
|
|
126
|
+
else
|
|
127
|
+
[]
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def metafields_params
|
|
132
|
+
if Spree::MetafieldDefinition.available_resources.map(&:name).include?(model_class.name) && model_class.method_defined?(:metafields)
|
|
133
|
+
[{ metafields_attributes: Spree::PermittedAttributes.metafield_attributes }]
|
|
134
|
+
else
|
|
135
|
+
[]
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def permitted_resource_params
|
|
140
|
+
params.require(model_param_name).permit(spree_permitted_attributes)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def allowed_sort_attributes
|
|
144
|
+
(super << spree_permitted_attributes).uniq.compact
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def write_request?
|
|
148
|
+
%w[put patch post delete].include?(request.request_method.downcase)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
|
|
12
|
+
def resource_serializer
|
|
13
|
+
Spree.api.platform_role_serializer
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
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.platform_shipment_create_service
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def update_service
|
|
125
|
+
Spree.api.platform_shipment_update_service
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def change_state_service
|
|
129
|
+
Spree.api.platform_shipment_change_state_service
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def add_item_service
|
|
133
|
+
Spree.api.platform_shipment_add_item_service
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def remove_item_service
|
|
137
|
+
Spree.api.platform_shipment_remove_item_service
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def resource_serializer
|
|
141
|
+
Spree.api.platform_shipment_serializer
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
|
|
12
|
+
def resource_serializer
|
|
13
|
+
Spree.api.platform_shipping_category_serializer
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
|
|
21
|
+
def resource_serializer
|
|
22
|
+
Spree.api.platform_shipping_method_serializer
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
16
|
+
def resource_serializer
|
|
17
|
+
Spree.api.platform_state_serializer
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
16
|
+
def resource_serializer
|
|
17
|
+
Spree.api.platform_stock_item_serializer
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
16
|
+
def resource_serializer
|
|
17
|
+
Spree.api.platform_stock_location_serializer
|
|
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 StoreCreditCategoriesController < ResourceController
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def model_class
|
|
9
|
+
Spree::StoreCreditCategory
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def resource_serializer
|
|
13
|
+
Spree.api.platform_store_credit_category_serializer
|
|
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 StoreCreditTypesController < ResourceController
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def model_class
|
|
9
|
+
Spree::StoreCreditType
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def resource_serializer
|
|
13
|
+
Spree.api.platform_store_credit_type_serializer
|
|
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 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
|
+
|
|
16
|
+
def resource_serializer
|
|
17
|
+
Spree.api.platform_store_credit_serializer
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
16
|
+
def resource_serializer
|
|
17
|
+
Spree.api.platform_tax_category_serializer
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
|
|
20
|
+
def resource_serializer
|
|
21
|
+
Spree.api.platform_tax_rate_serializer
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
16
|
+
def resource_serializer
|
|
17
|
+
Spree.api.platform_taxonomy_serializer
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Api
|
|
3
|
+
module V2
|
|
4
|
+
module Platform
|
|
5
|
+
class TaxonsController < ResourceController
|
|
6
|
+
include ::Spree::Api::V2::Platform::NestedSetRepositionConcern
|
|
7
|
+
|
|
8
|
+
private
|
|
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
|
+
|
|
29
|
+
def model_class
|
|
30
|
+
Spree::Taxon
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def scope_includes
|
|
34
|
+
node_includes = %i[icon parent taxonomy]
|
|
35
|
+
|
|
36
|
+
{
|
|
37
|
+
parent: node_includes,
|
|
38
|
+
children: node_includes,
|
|
39
|
+
taxonomy: [root: node_includes],
|
|
40
|
+
icon: [attachment_attachment: :blob]
|
|
41
|
+
}
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def serializer_params
|
|
45
|
+
super.merge(include_products: action_name == 'show')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def spree_permitted_attributes
|
|
49
|
+
super + [:new_parent_id, :new_position_idx]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def resource_serializer
|
|
53
|
+
Spree.api.platform_taxon_serializer
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module Api
|
|
3
|
+
module V2
|
|
4
|
+
module Platform
|
|
5
|
+
class UsersController < ResourceController
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def model_class
|
|
9
|
+
Spree.user_class
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def resource_serializer
|
|
13
|
+
Spree.api.platform_user_serializer
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def scope_includes
|
|
17
|
+
[:ship_address, :bill_address]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# we need to define this here as developers can configure their own `user_class`
|
|
21
|
+
def model_param_name
|
|
22
|
+
'user'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def spree_permitted_attributes
|
|
26
|
+
super + [:password, :password_confirmation]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# we need to override this method to avoid trying to create user role when creating a user
|
|
30
|
+
def ensure_current_store(_resource)
|
|
31
|
+
# do nothing
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|