spree_api 6.0.6 → 6.0.7

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.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +3 -0
  3. data/app/controllers/concerns/spree/api/v3/admin/product_membership.rb +2 -2
  4. data/app/controllers/concerns/spree/api/v3/admin/validation_details.rb +34 -13
  5. data/app/controllers/concerns/spree/api/v3/authentication_strategies.rb +14 -8
  6. data/app/controllers/concerns/spree/api/v3/error_handler.rb +31 -4
  7. data/app/controllers/concerns/spree/api/v3/invitation_resend.rb +28 -0
  8. data/app/controllers/concerns/spree/api/v3/locale_and_currency.rb +1 -1
  9. data/app/controllers/concerns/spree/api/v3/resource_serializer.rb +5 -0
  10. data/app/controllers/concerns/spree/api/v3/scoped_authorization.rb +20 -3
  11. data/app/controllers/spree/api/v3/admin/admin_users_controller.rb +8 -3
  12. data/app/controllers/spree/api/v3/admin/api_keys_controller.rb +6 -1
  13. data/app/controllers/spree/api/v3/admin/auth_controller.rb +1 -1
  14. data/app/controllers/spree/api/v3/admin/catalogs_controller.rb +2 -1
  15. data/app/controllers/spree/api/v3/admin/channels_controller.rb +2 -2
  16. data/app/controllers/spree/api/v3/admin/collection_rules_controller.rb +1 -1
  17. data/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb +1 -1
  18. data/app/controllers/spree/api/v3/admin/email_templates_controller.rb +2 -2
  19. data/app/controllers/spree/api/v3/admin/exports_controller.rb +49 -0
  20. data/app/controllers/spree/api/v3/admin/gift_cards_controller.rb +9 -0
  21. data/app/controllers/spree/api/v3/admin/integrations_controller.rb +4 -2
  22. data/app/controllers/spree/api/v3/admin/invitation_acceptances_controller.rb +10 -3
  23. data/app/controllers/spree/api/v3/admin/invitations_controller.rb +4 -5
  24. data/app/controllers/spree/api/v3/admin/order_routing_rules_controller.rb +1 -1
  25. data/app/controllers/spree/api/v3/admin/orders/payments_controller.rb +11 -3
  26. data/app/controllers/spree/api/v3/admin/password_resets_controller.rb +14 -2
  27. data/app/controllers/spree/api/v3/admin/payment_methods_controller.rb +23 -2
  28. data/app/controllers/spree/api/v3/admin/permissions_controller.rb +9 -3
  29. data/app/controllers/spree/api/v3/admin/products_controller.rb +18 -6
  30. data/app/controllers/spree/api/v3/admin/promotion_actions_controller.rb +1 -1
  31. data/app/controllers/spree/api/v3/admin/promotion_rules_controller.rb +1 -1
  32. data/app/controllers/spree/api/v3/admin/reporting_controller.rb +1 -5
  33. data/app/controllers/spree/api/v3/admin/sellers/invitations_controller.rb +12 -12
  34. data/app/controllers/spree/api/v3/admin/setup_controller.rb +2 -18
  35. data/app/controllers/spree/api/v3/admin/stock_levels_controller.rb +1 -1
  36. data/app/controllers/spree/api/v3/admin/tags_controller.rb +1 -1
  37. data/app/controllers/spree/api/v3/admin/tax_providers_controller.rb +20 -0
  38. data/app/controllers/spree/api/v3/admin/translatable_resources_controller.rb +3 -1
  39. data/app/controllers/spree/api/v3/admin/translations_coverage_controller.rb +11 -9
  40. data/app/controllers/spree/api/v3/admin/webhook_events_controller.rb +9 -3
  41. data/app/controllers/spree/api/v3/base_controller.rb +39 -0
  42. data/app/controllers/spree/api/v3/resource_controller.rb +0 -18
  43. data/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb +2 -2
  44. data/app/controllers/spree/api/v3/seller/invitations_controller.rb +11 -11
  45. data/app/controllers/spree/api/v3/seller/onboarding_controller.rb +1 -1
  46. data/app/controllers/spree/api/v3/seller/password_resets_controller.rb +12 -2
  47. data/app/controllers/spree/api/v3/seller/products_controller.rb +1 -1
  48. data/app/controllers/spree/api/v3/store/account/companies_controller.rb +7 -7
  49. data/app/controllers/spree/api/v3/store/auth_controller.rb +2 -2
  50. data/app/controllers/spree/api/v3/store/company_invitations_controller.rb +7 -4
  51. data/app/controllers/spree/api/v3/store/customer/password_resets_controller.rb +11 -1
  52. data/app/controllers/spree/api/v3/store/newsletter_subscribers_controller.rb +4 -3
  53. data/app/serializers/spree/api/v3/admin/api_key_serializer.rb +2 -1
  54. data/app/serializers/spree/api/v3/admin/bulk_product_result_serializer.rb +33 -0
  55. data/app/serializers/spree/api/v3/admin/bulk_stock_level_result_serializer.rb +17 -0
  56. data/app/serializers/spree/api/v3/admin/calculator_type_serializer.rb +19 -0
  57. data/app/serializers/spree/api/v3/admin/company_invitation_serializer.rb +5 -0
  58. data/app/serializers/spree/api/v3/admin/company_membership_serializer.rb +1 -0
  59. data/app/serializers/spree/api/v3/admin/country_serializer.rb +8 -0
  60. data/app/serializers/spree/api/v3/admin/delivery_method_serializer.rb +2 -1
  61. data/app/serializers/spree/api/v3/admin/fulfillment_serializer.rb +3 -0
  62. data/app/serializers/spree/api/v3/admin/integration_connection_result_serializer.rb +18 -0
  63. data/app/serializers/spree/api/v3/admin/integration_type_definition_serializer.rb +25 -0
  64. data/app/serializers/spree/api/v3/admin/password_reset_acknowledgement_serializer.rb +18 -0
  65. data/app/serializers/spree/api/v3/admin/payment_method_type_serializer.rb +17 -0
  66. data/app/serializers/spree/api/v3/admin/product_membership_result_serializer.rb +19 -0
  67. data/app/serializers/spree/api/v3/admin/reporting_dimension_value_serializer.rb +22 -0
  68. data/app/serializers/spree/api/v3/admin/reporting_metric_value_serializer.rb +49 -0
  69. data/app/serializers/spree/api/v3/admin/reporting_result_meta_serializer.rb +44 -0
  70. data/app/serializers/spree/api/v3/admin/reporting_result_serializer.rb +18 -55
  71. data/app/serializers/spree/api/v3/admin/reporting_row_serializer.rb +35 -0
  72. data/app/serializers/spree/api/v3/admin/resource_type_definition_serializer.rb +29 -0
  73. data/app/serializers/spree/api/v3/admin/setup_status_serializer.rb +17 -0
  74. data/app/serializers/spree/api/v3/admin/tag_serializer.rb +17 -0
  75. data/app/serializers/spree/api/v3/admin/translatable_resource_serializer.rb +28 -0
  76. data/app/serializers/spree/api/v3/admin/translation_coverage_serializer.rb +37 -0
  77. data/app/serializers/spree/api/v3/company_invitation_serializer.rb +10 -1
  78. data/app/serializers/spree/api/v3/company_membership_serializer.rb +2 -0
  79. data/app/serializers/spree/api/v3/fulfillment_serializer.rb +7 -6
  80. data/app/serializers/spree/api/v3/product_category_serializer.rb +2 -10
  81. data/app/serializers/spree/api/v3/product_collection_serializer.rb +2 -10
  82. data/app/serializers/spree/api/v3/seller/bulk_product_result_serializer.rb +22 -0
  83. data/app/serializers/spree/api/v3/seller/fulfillment_serializer.rb +1 -3
  84. data/app/serializers/spree/api/v3/seller/password_reset_acknowledgement_serializer.rb +18 -0
  85. data/app/serializers/spree/api/v3/seller/payout_account_link_serializer.rb +18 -0
  86. data/app/serializers/spree/api/v3/seller/stock_location_serializer.rb +8 -3
  87. data/app/services/spree/api/v3/filters_aggregator.rb +1 -5
  88. data/config/routes.rb +3 -1
  89. data/lib/spree/api/dependencies.rb +7 -2
  90. data/lib/spree/api/openapi/schema_helper.rb +86 -2
  91. data/lib/spree/api/preference_types.rb +8 -0
  92. data/lib/spree/api/testing_support/deterministic_openapi.rb +87 -0
  93. data/lib/spree/api/testing_support/openapi.rb +2 -1
  94. data/lib/spree/api/testing_support/openapi_tasks.rb +3 -0
  95. data/lib/spree/api/testing_support/response_schema.rb +64 -0
  96. data/lib/spree/api.rb +1 -0
  97. data/lib/typelizer/nullable_references.rb +31 -0
  98. data/openapi/admin.yaml +3571 -524
  99. data/openapi/store.yaml +353 -153
  100. metadata +29 -6
@@ -49,7 +49,9 @@ module Spree
49
49
  end
50
50
 
51
51
  # Always return 202 to prevent email enumeration
52
- render json: { message: I18n.t('spree.api.password_reset_requested') }, status: :accepted
52
+ render json: Spree.api.admin_password_reset_acknowledgement_serializer.new(
53
+ { message: I18n.t('spree.api.password_reset_requested') }
54
+ ).to_h, status: :accepted
53
55
  end
54
56
 
55
57
  # PATCH /api/v3/admin/password_resets/:id
@@ -68,7 +70,17 @@ module Spree
68
70
  # its password or sign it in.
69
71
  return render_account_deactivated if account_deactivated?(user)
70
72
 
71
- if user.update(password: params[:password], password_confirmation: params[:password_confirmation])
73
+ password = string_param(:password)
74
+ # Without this a reset link could clear the password, or sign in
75
+ # without setting one: the model only validates a password it is given.
76
+ if password.blank?
77
+ user.errors.add(:password, :blank)
78
+ return render_errors(user.errors)
79
+ end
80
+
81
+ # The confirmation goes in as sent: a malformed one has to fail to
82
+ # match, where a nil one would skip the check.
83
+ if user.update(password: password, password_confirmation: params[:password_confirmation])
72
84
  user.publish_event('admin_user.password_reset')
73
85
  # A password reset must kill every existing session — a stolen
74
86
  # refresh token must not survive the victim resetting their
@@ -51,7 +51,7 @@ module Spree
51
51
  entry.merge(installed: installed_shorthands.include?(entry[:type]))
52
52
  end
53
53
 
54
- render json: { data: entries }
54
+ render json: { data: serialize_types(entries, Spree.api.admin_payment_method_type_serializer) }
55
55
  end
56
56
 
57
57
  protected
@@ -74,8 +74,29 @@ module Spree
74
74
  params.permit(*permitted_attributes)
75
75
  end
76
76
 
77
+ # The add-payment dialog on an order picks from this list, so staff
78
+ # who manage payments read it without the settings key. Writing, and
79
+ # the provider types, stay a settings matter.
80
+ def scoped_resource_name
81
+ reads_through_payments? ? :payments : super
82
+ end
83
+
77
84
  def scope
78
- super.ordered
85
+ reads_through_payments? ? current_store.payment_methods.ordered : super.ordered
86
+ end
87
+
88
+ def authorize_resource!(resource = @resource, action = action_name.to_sym)
89
+ return authorize!(:show, Spree::Payment) if reads_through_payments?
90
+
91
+ super
92
+ end
93
+
94
+ def reads_through_payments?
95
+ return @reads_through_payments if defined?(@reads_through_payments)
96
+
97
+ @reads_through_payments = %w[index show].include?(action_name) &&
98
+ !holds_permission?('read_settings') &&
99
+ holds_permission?('read_payments')
79
100
  end
80
101
 
81
102
  # `types` is read-only discovery — maps to the read scope + :show ability.
@@ -16,13 +16,19 @@ module Spree
16
16
 
17
17
  # GET /api/v3/admin/permissions
18
18
  def index
19
- entries = Spree.permissions.entries
19
+ entries = paginate_whole(Spree.permissions.entries)
20
20
 
21
21
  render json: {
22
- data: entries.map { |entry| Spree.api.admin_permission_serializer.new(entry).to_h },
23
- meta: { count: entries.size }
22
+ data: entries.map { |entry| serializer_class.new(entry).to_h },
23
+ meta: collection_meta(entries)
24
24
  }
25
25
  end
26
+
27
+ private
28
+
29
+ def serializer_class
30
+ Spree.api.admin_permission_serializer
31
+ end
26
32
  end
27
33
  end
28
34
  end
@@ -207,7 +207,7 @@ module Spree
207
207
  # "nothing was skipped" to clients that never skip anything.
208
208
  payload[:skipped_in_review_count] = skipped if skipped.positive?
209
209
 
210
- render json: payload
210
+ render json: Spree.api.admin_bulk_product_result_serializer.new(payload).to_h
211
211
  end
212
212
 
213
213
  # POST /api/v3/admin/products/bulk_add_to_categories
@@ -243,7 +243,9 @@ module Spree
243
243
  product_ids = bulk_collection.distinct.ids
244
244
  channels.add_products(product_ids)
245
245
 
246
- render json: { product_count: product_ids.size, channel_count: channels.size }
246
+ render json: Spree.api.admin_bulk_product_result_serializer.new(
247
+ { product_count: product_ids.size, channel_count: channels.size }
248
+ ).to_h
247
249
  end
248
250
 
249
251
  # POST /api/v3/admin/products/bulk_remove_from_channels
@@ -255,7 +257,9 @@ module Spree
255
257
  product_ids = bulk_collection.distinct.ids
256
258
  removed = channels.remove_products(product_ids)
257
259
 
258
- render json: { product_count: product_ids.size, channel_count: channels.size, removed: removed }
260
+ render json: Spree.api.admin_bulk_product_result_serializer.new(
261
+ { product_count: product_ids.size, channel_count: channels.size, removed: removed }
262
+ ).to_h
259
263
  end
260
264
 
261
265
  # DELETE /api/v3/admin/products/bulk_destroy
@@ -271,7 +275,7 @@ module Spree
271
275
  .where(id: decode_ids(params[:ids]))
272
276
  destroyed = destroy_scope.count(&:destroy)
273
277
 
274
- render json: { product_count: destroyed }
278
+ render json: Spree.api.admin_bulk_product_result_serializer.new({ product_count: destroyed }).to_h
275
279
  end
276
280
 
277
281
  # DELETE /api/v3/admin/products/:id
@@ -450,6 +454,10 @@ module Spree
450
454
  :product_count
451
455
  end
452
456
 
457
+ def bulk_tags_response
458
+ Spree.api.admin_bulk_product_result_serializer.new(super).to_h
459
+ end
460
+
453
461
  def apply_categories(service)
454
462
  authorize! :update, model_class
455
463
 
@@ -459,7 +467,9 @@ module Spree
459
467
 
460
468
  service.call(categories: categories, products: bulk_collection)
461
469
 
462
- render json: { product_count: bulk_collection.size, category_count: categories.size }
470
+ render json: Spree.api.admin_bulk_product_result_serializer.new(
471
+ { product_count: bulk_collection.size, category_count: categories.size }
472
+ ).to_h
463
473
  end
464
474
 
465
475
  def apply_collections(service)
@@ -474,7 +484,9 @@ module Spree
474
484
 
475
485
  service.call(collections: collections, products: bulk_collection)
476
486
 
477
- render json: { product_count: bulk_collection.size, collection_count: collections.size }
487
+ render json: Spree.api.admin_bulk_product_result_serializer.new(
488
+ { product_count: bulk_collection.size, collection_count: collections.size }
489
+ ).to_h
478
490
  end
479
491
 
480
492
  def scoped_channels
@@ -25,7 +25,7 @@ module Spree
25
25
  def types
26
26
  authorize! :read, model_class
27
27
 
28
- render json: { data: model_class.subclasses_with_preference_schema }
28
+ render json: { data: serialize_types(model_class.subclasses_with_preference_schema) }
29
29
  end
30
30
 
31
31
  # Returns the calculator subclasses registered for the given action
@@ -25,7 +25,7 @@ module Spree
25
25
  def types
26
26
  authorize! :read, model_class
27
27
 
28
- render json: { data: model_class.subclasses_with_preference_schema }
28
+ render json: { data: serialize_types(model_class.subclasses_with_preference_schema) }
29
29
  end
30
30
 
31
31
  protected
@@ -56,11 +56,7 @@ module Spree
56
56
 
57
57
  result = reporting_query.execute
58
58
 
59
- render json: ReportingResultSerializer.new(
60
- result,
61
- store: current_store,
62
- params: serializer_params
63
- ).to_h
59
+ render json: ReportingResultSerializer.new(result, params: serializer_params).to_h
64
60
  end
65
61
 
66
62
  # GET /api/v3/admin/reporting/schema
@@ -15,13 +15,16 @@ module Spree
15
15
  # `invite` action on the seller is the lifecycle step that opens a
16
16
  # pending seller for its first owner.
17
17
  class InvitationsController < Admin::BaseController
18
+ include Spree::Api::V3::InvitationResend
19
+
18
20
  scoped_resource :sellers
19
21
 
20
22
  before_action :set_seller
21
23
  # Every write hands out or withdraws a way onto the team, so it
22
24
  # needs the same right as changing the seller.
23
25
  before_action :authorize_seller_update, only: [:create, :destroy, :resend]
24
- before_action :set_invitation, only: [:destroy, :resend]
26
+ before_action :set_invitation, only: [:destroy]
27
+ before_action :set_resend_invitation, only: [:resend]
25
28
 
26
29
  request_params 'SellerInvitationCreateParams', action: :create do |body|
27
30
  body.attribute :email, :string, required: true, example: 'teammate@example.com'
@@ -65,18 +68,9 @@ module Spree
65
68
  end
66
69
 
67
70
  # Sends the email again. The model refuses once the invitation has
68
- # expired, and says so rather than silently doing nothing.
71
+ # expired, been accepted or been revoked, and says which.
69
72
  def resend
70
- if @invitation.expired?
71
- return render_error(
72
- code: ErrorHandler::ERROR_CODES[:processing_error],
73
- message: I18n.t('spree.invitation_expired'),
74
- status: :unprocessable_content
75
- )
76
- end
77
-
78
- @invitation.resend!
79
- render json: serialize(@invitation)
73
+ render_invitation_resend(@invitation) { |invitation| serialize(invitation) }
80
74
  end
81
75
 
82
76
  protected
@@ -100,6 +94,12 @@ module Spree
100
94
  @invitation = @seller.invitations.pending.find_by_prefix_id!(params[:id])
101
95
  end
102
96
 
97
+ # Any status, so an accepted invitation is refused with the reason
98
+ # rather than reported as missing.
99
+ def set_resend_invitation
100
+ @invitation = @seller.invitations.find_by_prefix_id!(params[:id])
101
+ end
102
+
103
103
  def serialize(invitation)
104
104
  Spree.api.admin_invitation_serializer.new(
105
105
  invitation, params: { store: current_store }
@@ -57,7 +57,7 @@ module Spree
57
57
  # Leaks a single boolean that is self-evident on a fresh install —
58
58
  # the login page uses it to offer the setup screen.
59
59
  def show
60
- render json: { setup_required: setup_required? }
60
+ render json: Spree.api.admin_setup_status_serializer.new({ setup_required: setup_required? }).to_h
61
61
  end
62
62
 
63
63
  # GET /api/v3/admin/auth/setup/countries
@@ -68,7 +68,7 @@ module Spree
68
68
  def countries
69
69
  return render_setup_unavailable unless setup_required?
70
70
 
71
- render json: { countries: Spree::Country.all.map { |country| country_payload(country) } }
71
+ render json: { countries: Spree::Country.all.map { |country| Spree.api.admin_country_serializer.new(country).to_h } }
72
72
  end
73
73
 
74
74
  # POST /api/v3/admin/auth/setup
@@ -180,22 +180,6 @@ module Spree
180
180
  )
181
181
  end
182
182
 
183
- def country_payload(country)
184
- {
185
- code: country.iso,
186
- currency: country.default_currency,
187
- locales: offerable_locales(country)
188
- }
189
- end
190
-
191
- # A country's official languages, as the locales Spree lists for them
192
- # (Switzerland gives de, fr, it and rm) — content locales, whether
193
- # or not Spree ships translations, since the storefront and dashboard
194
- # carry their own. English stands in for a country with none listed.
195
- def offerable_locales(country)
196
- country.official_locales.filter_map { |language| Spree::Locales.for_language(language) }.uniq.presence || ['en']
197
- end
198
-
199
183
  # Token mismatch, spent token, and already-set-up all render the
200
184
  # same 404 so the endpoint leaks nothing about which it was.
201
185
  def render_setup_unavailable
@@ -86,7 +86,7 @@ module Spree
86
86
  )
87
87
  end
88
88
 
89
- render json: result.value
89
+ render json: Spree.api.admin_bulk_stock_level_result_serializer.new(result.value).to_h
90
90
  end
91
91
 
92
92
  # PATCH /api/v3/admin/stock_levels/:id
@@ -40,7 +40,7 @@ module Spree
40
40
  scope = scope.where('LOWER(name) LIKE ? ESCAPE ?', "%#{escaped}%", '\\')
41
41
  end
42
42
 
43
- render json: { data: scope.pluck(:name).map { |name| { name: name } } }
43
+ render json: { data: scope.pluck(:name).map { |name| Spree.api.admin_tag_serializer.new({ name: name }).to_h } }
44
44
  end
45
45
 
46
46
  private
@@ -14,10 +14,30 @@ module Spree
14
14
  class TaxProvidersController < BaseController
15
15
  scoped_resource :settings
16
16
 
17
+ # A shorter query matches too much of a catalogue to be worth asking for.
18
+ MIN_TAX_CODE_QUERY_LENGTH = 2
19
+
17
20
  # GET /api/v3/admin/tax_providers
18
21
  def index
19
22
  render json: { data: Spree.tax_providers.map { |provider| provider.to_api_hash(current_store) } }
20
23
  end
24
+
25
+ # GET /api/v3/admin/tax_providers/:id/tax_codes?q=
26
+ #
27
+ # Searches an engine's own tax codes for the tax category form. An
28
+ # engine with no catalogue answers an empty list rather than an error,
29
+ # so the form can ask any engine in use.
30
+ def tax_codes
31
+ authorize! :read, Spree::TaxCategory
32
+
33
+ provider = Spree::TaxProvider::Base.registered(params[:id])
34
+ raise ActiveRecord::RecordNotFound unless provider
35
+
36
+ query = params[:q].to_s.strip
37
+ codes = query.length >= MIN_TAX_CODE_QUERY_LENGTH ? Array(provider.tax_codes(current_store, query: query)) : []
38
+
39
+ render json: { data: codes.map { |code| { code: code[:code].to_s, description: code[:description].to_s } } }
40
+ end
21
41
  end
22
42
  end
23
43
  end
@@ -14,7 +14,9 @@ module Spree
14
14
  readable = readable_resource_types
15
15
 
16
16
  data = Spree::Translations.registry.map do |entry|
17
- entry.merge('readable' => readable.include?(entry['resource_type']))
17
+ Spree.api.admin_translatable_resource_serializer.new(
18
+ entry.symbolize_keys.merge(readable: readable.include?(entry['resource_type']))
19
+ ).to_h
18
20
  end
19
21
 
20
22
  render json: { data: data }
@@ -16,15 +16,17 @@ module Spree
16
16
  locales = translatable_locales
17
17
 
18
18
  render json: {
19
- data: {
20
- 'resource_type' => Spree::Translations.public_resource_type(model_class),
21
- 'default_locale' => current_store.default_locale,
22
- 'locales' => locales,
23
- 'field_count' => model_class.public_translatable_fields.size,
24
- 'search_field' => search_field,
25
- 'coverage' => Spree::Translations.coverage_for(scope, model_class, locales),
26
- 'records' => serialize_records(records, locales)
27
- },
19
+ data: Spree.api.admin_translation_coverage_serializer.new(
20
+ {
21
+ resource_type: Spree::Translations.public_resource_type(model_class),
22
+ default_locale: current_store.default_locale,
23
+ locales: locales,
24
+ field_count: model_class.public_translatable_fields.size,
25
+ search_field: search_field,
26
+ coverage: Spree::Translations.coverage_for(scope, model_class, locales),
27
+ records: serialize_records(records, locales)
28
+ }
29
+ ).to_h,
28
30
  meta: collection_meta(records)
29
31
  }
30
32
  end
@@ -11,13 +11,19 @@ module Spree
11
11
  # GET /api/v3/admin/webhook_events
12
12
  def index
13
13
  authorize!(:show, Spree::WebhookEndpoint)
14
- entries = Spree::Events.catalog.webhook_events
14
+ entries = paginate_whole(Spree::Events.catalog.webhook_events)
15
15
 
16
16
  render json: {
17
- data: entries.map { |entry| Spree.api.admin_webhook_event_serializer.new(entry).to_h },
18
- meta: { count: entries.size }
17
+ data: entries.map { |entry| serializer_class.new(entry).to_h },
18
+ meta: collection_meta(entries)
19
19
  }
20
20
  end
21
+
22
+ private
23
+
24
+ def serializer_class
25
+ Spree.api.admin_webhook_event_serializer
26
+ end
21
27
  end
22
28
  end
23
29
  end
@@ -50,6 +50,45 @@ module Spree
50
50
 
51
51
  protected
52
52
 
53
+ # Paginates a list held in memory. The whole list is one page unless the
54
+ # caller asks for less: clients read these lists in full.
55
+ #
56
+ # @param entries [Array]
57
+ # @return [Array] the requested page
58
+ def paginate_whole(entries)
59
+ @pagy, page = pagy(:offset, entries, limit: [entries.size, 1].max)
60
+ page
61
+ end
62
+
63
+ # Metadata for collection responses
64
+ # @return [Hash] pagination metadata
65
+ def collection_meta(_collection)
66
+ return {} unless @pagy
67
+
68
+ {
69
+ page: @pagy.page,
70
+ limit: @pagy.limit,
71
+ count: @pagy.count,
72
+ pages: @pagy.pages,
73
+ from: @pagy.from,
74
+ to: @pagy.to,
75
+ in: @pagy.in,
76
+ previous: @pagy.previous,
77
+ next: @pagy.next
78
+ }
79
+ end
80
+
81
+ # A request parameter, only when it arrived as a string. `token[]=x`
82
+ # and `token[k]=x` parse to an Array and a Hash, which token lookups
83
+ # and verifiers built for a string cannot read.
84
+ #
85
+ # @param key [Symbol]
86
+ # @return [String, nil]
87
+ def string_param(key)
88
+ value = params[key]
89
+ value if value.is_a?(String)
90
+ end
91
+
53
92
  # Refuses a throttled request. Must render (not return a value) —
54
93
  # Rails runs `with:` responders via instance_exec inside a
55
94
  # before_action, where only render/redirect halts the chain. Headers
@@ -458,24 +458,6 @@ module Spree
458
458
  [limit_param, 100].min # Max 100 per page
459
459
  end
460
460
 
461
- # Metadata for collection responses
462
- # @return [Hash] pagination metadata
463
- def collection_meta(_collection)
464
- return {} unless @pagy
465
-
466
- {
467
- page: @pagy.page,
468
- limit: @pagy.limit,
469
- count: @pagy.count,
470
- pages: @pagy.pages,
471
- from: @pagy.from,
472
- to: @pagy.to,
473
- in: @pagy.in,
474
- previous: @pagy.previous,
475
- next: @pagy.next
476
- }
477
- end
478
-
479
461
  # Base scope, store-scoped (or parent-scoped for nested resources).
480
462
  # Deliberately free of CanCanCan: the Store API authorizes by ownership
481
463
  # — every account controller scopes through `current_user` — while the
@@ -79,7 +79,7 @@ module Spree
79
79
  decoded_id = Spree::Invitation.decode_prefixed_id(params[:id])
80
80
  @invitation = Spree::Invitation.pending.not_expired.
81
81
  where(resource_type: Spree::Seller.to_s).
82
- find_by(id: decoded_id, token: params[:token])
82
+ find_by(id: decoded_id, token: string_param(:token).to_s)
83
83
 
84
84
  unless @invitation
85
85
  render_error(
@@ -122,7 +122,7 @@ module Spree
122
122
  end
123
123
 
124
124
  def create_new_invitee(invitation)
125
- if params[:password].blank?
125
+ if string_param(:password).blank?
126
126
  render_error(
127
127
  code: ErrorHandler::ERROR_CODES[:parameter_missing],
128
128
  message: I18n.t('spree.password_required_to_create_account'),
@@ -13,9 +13,12 @@ module Spree
13
13
  # Rooted at `current_seller.invitations` throughout, so a seller can
14
14
  # neither see nor touch an invitation sent by anyone else.
15
15
  class InvitationsController < Seller::BaseController
16
+ include Spree::Api::V3::InvitationResend
17
+
16
18
  scoped_resource :seller_profile
17
19
 
18
- before_action :set_invitation, only: [:destroy, :resend]
20
+ before_action :set_invitation, only: [:destroy]
21
+ before_action :set_resend_invitation, only: [:resend]
19
22
 
20
23
  # Pending only: an accepted invitation is a team member, and the
21
24
  # panel already lists those.
@@ -35,16 +38,7 @@ module Spree
35
38
  # the first one. The model refuses once the invitation has expired
36
39
  # or been accepted, and says so rather than silently doing nothing.
37
40
  def resend
38
- if @invitation.expired?
39
- return render_error(
40
- code: ErrorHandler::ERROR_CODES[:processing_error],
41
- message: I18n.t('spree.invitation_expired'),
42
- status: :unprocessable_content
43
- )
44
- end
45
-
46
- @invitation.resend!
47
- render json: serialize(@invitation)
41
+ render_invitation_resend(@invitation) { |invitation| serialize(invitation) }
48
42
  end
49
43
 
50
44
  protected
@@ -59,6 +53,12 @@ module Spree
59
53
  @invitation = current_seller.invitations.pending.find_by_prefix_id!(params[:id])
60
54
  end
61
55
 
56
+ # Any status, so an accepted invitation is refused with the reason
57
+ # rather than reported as missing.
58
+ def set_resend_invitation
59
+ @invitation = current_seller.invitations.find_by_prefix_id!(params[:id])
60
+ end
61
+
62
62
  def serialize(invitation)
63
63
  Spree.api.seller_invitation_serializer.new(
64
64
  invitation, params: { store: current_store }
@@ -65,7 +65,7 @@ module Spree
65
65
  # A null link rather than an empty response: the panel has to tell
66
66
  # "this provider hosts nothing" from "the request failed", and a
67
67
  # 204 makes the client infer that from an absence.
68
- render json: { url: url.presence }
68
+ render json: Spree.api.seller_payout_account_link_serializer.new({ url: url.presence }).to_h
69
69
  rescue Spree::Core::GatewayError => e
70
70
  render_service_error(e.message)
71
71
  end
@@ -50,7 +50,9 @@ module Spree
50
50
 
51
51
  # Always 202, whether or not anything matched — this endpoint must
52
52
  # not become a way to discover which addresses exist.
53
- render json: { message: I18n.t('spree.api.password_reset_requested') }, status: :accepted
53
+ render json: Spree.api.seller_password_reset_acknowledgement_serializer.new(
54
+ { message: I18n.t('spree.api.password_reset_requested') }
55
+ ).to_h, status: :accepted
54
56
  end
55
57
 
56
58
  # PATCH /api/v3/seller/auth/password_resets/:id
@@ -65,10 +67,18 @@ module Spree
65
67
  # its password or sign it in.
66
68
  return render_account_deactivated if account_deactivated?(user)
67
69
 
68
- unless user.update(password: params[:password], password_confirmation: params[:password_confirmation])
70
+ password = string_param(:password)
71
+ # Without this a reset link could clear the password, or sign in
72
+ # without setting one: the model only validates a password it is given.
73
+ if password.blank?
74
+ user.errors.add(:password, :blank)
69
75
  return render_errors(user.errors)
70
76
  end
71
77
 
78
+ # The confirmation goes in as sent: a malformed one has to fail to
79
+ # match, where a nil one would skip the check.
80
+ return render_errors(user.errors) unless user.update(password: password, password_confirmation: params[:password_confirmation])
81
+
72
82
  user.publish_event('seller_user.password_reset')
73
83
 
74
84
  # A reset must kill every existing session — a stolen refresh token
@@ -413,7 +413,7 @@ module Spree
413
413
  # Only when it happened: a key on every response would tell clients
414
414
  # that never skip anything that nothing was skipped.
415
415
  payload[:skipped_count] = skipped if skipped.positive?
416
- payload
416
+ Spree.api.seller_bulk_product_result_serializer.new(payload).to_h
417
417
  end
418
418
 
419
419
  # A block may answer with the workflow's own Result, or with a plain
@@ -16,13 +16,7 @@ module Spree
16
16
  merge(Spree::Company.where(store_id: current_store.id)).
17
17
  includes(company: :parent)
18
18
 
19
- render json: {
20
- 'data' => memberships.map do |membership|
21
- Spree.api.company_membership_serializer.new(membership, params: serializer_params).to_h.merge(
22
- 'company' => Spree.api.company_serializer.new(membership.company, params: serializer_params).to_h
23
- )
24
- end
25
- }
19
+ render json: { 'data' => serialize_collection(memberships) }
26
20
  end
27
21
 
28
22
  protected
@@ -34,6 +28,12 @@ module Spree
34
28
  def serializer_class
35
29
  Spree.api.company_membership_serializer
36
30
  end
31
+
32
+ # A membership is listed here to choose a company to buy for, so
33
+ # each one always comes with its node.
34
+ def serializer_params
35
+ super.merge(expand: expand_list | ['company'])
36
+ end
37
37
  end
38
38
  end
39
39
  end
@@ -52,7 +52,7 @@ module Spree
52
52
  # Accepts: { "refresh_token": "rt_xxx" }
53
53
  # Returns new access JWT + rotated refresh token
54
54
  def refresh
55
- refresh_token_value = params[:refresh_token]
55
+ refresh_token_value = string_param(:refresh_token)
56
56
 
57
57
  if refresh_token_value.blank?
58
58
  return render_error(
@@ -89,7 +89,7 @@ module Spree
89
89
  # access token can still log out. Narrowed to this surface's own
90
90
  # tokens: ending a session belongs to the surface that started it.
91
91
  def logout
92
- refresh_token_value = params[:refresh_token]
92
+ refresh_token_value = string_param(:refresh_token)
93
93
 
94
94
  if refresh_token_value.present?
95
95
  Spree::RefreshToken.for_audience(JWT_AUDIENCE_STORE).find_by(token: refresh_token_value)&.destroy