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
@@ -30,10 +30,7 @@ module Spree
30
30
  def show
31
31
  invitation = find_by_token!
32
32
 
33
- render json: serialize_resource(invitation).merge(
34
- 'company_name' => invitation.company.name,
35
- 'store_name' => current_store.name
36
- )
33
+ render json: serialize_resource(invitation)
37
34
  end
38
35
 
39
36
  # POST /api/v3/store/company_invitations/:token/accept
@@ -77,6 +74,12 @@ module Spree
77
74
  Spree.api.company_invitation_serializer
78
75
  end
79
76
 
77
+ # The lookup still names the company and the store, as it did
78
+ # before `company` could be expanded.
79
+ def serializer_params
80
+ super.merge(invitee: true)
81
+ end
82
+
80
83
 
81
84
  private
82
85
 
@@ -62,7 +62,17 @@ module Spree
62
62
  )
63
63
  end
64
64
 
65
- if user.update(password: params[:password], password_confirmation: params[:password_confirmation])
65
+ password = string_param(:password)
66
+ # Without this a reset link could clear the password, or sign in
67
+ # without setting one: the model only validates a password it is given.
68
+ if password.blank?
69
+ user.errors.add(:password, :blank)
70
+ return render_errors(user.errors)
71
+ end
72
+
73
+ # The confirmation goes in as sent: a malformed one has to fail to
74
+ # match, where a nil one would skip the check.
75
+ if user.update(password: password, password_confirmation: params[:password_confirmation])
66
76
  jwt = generate_jwt(user)
67
77
  # Kill every existing session before minting the new one —
68
78
  # a stolen refresh token must not survive a password reset.
@@ -52,7 +52,7 @@ module Spree
52
52
 
53
53
  # POST /api/v3/store/newsletter_subscribers/verify
54
54
  def verify
55
- token = params[:token]
55
+ token = string_param(:token)
56
56
 
57
57
  if token.blank?
58
58
  return render_error(
@@ -136,9 +136,10 @@ module Spree
136
136
  end
137
137
 
138
138
  def find_subscriber_by_unsubscribe_token
139
- return if params[:token].blank?
139
+ token = string_param(:token)
140
+ return if token.blank?
140
141
 
141
- subscriber = Spree::NewsletterSubscriber.for_store(current_store).find_by_token_for(:unsubscribe, params[:token])
142
+ subscriber = Spree::NewsletterSubscriber.for_store(current_store).find_by_token_for(:unsubscribe, token)
142
143
  subscriber if subscriber&.prefixed_id == params[:id]
143
144
  end
144
145
  end
@@ -21,9 +21,10 @@ module Spree
21
21
  created_by_email: [:string, nullable: true],
22
22
  created_by_type: [:string, nullable: true, enum: Spree::Actor::BUILT_IN_KINDS, enum_type_name: 'ActorKind'],
23
23
  created_by_label: [:string, nullable: true],
24
+ created_by_ip_address: [:string, nullable: true],
24
25
  channel_id: [:string, nullable: true]
25
26
 
26
- attributes :name, :key_type, :token_prefix, :scopes,
27
+ attributes :name, :key_type, :token_prefix, :scopes, :created_by_ip_address,
27
28
  created_at: :iso8601, updated_at: :iso8601,
28
29
  revoked_at: :iso8601, last_used_at: :iso8601
29
30
 
@@ -0,0 +1,33 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # What a bulk product action did. Products from other stores, and ones the caller
6
+ # may not change, are left out of the count. Each action adds the
7
+ # figures that describe it, so one type serves them all.
8
+ class BulkProductResultSerializer
9
+ include Alba::Resource
10
+ include Typelizer::DSL
11
+
12
+ OPTIONAL = {
13
+ status: [:string, 'The status the products were moved to. Sent by a status change.'],
14
+ skipped_in_review_count: [:number, 'Products waiting for review that a status change left alone. Sent only when there were any.'],
15
+ category_count: [:number, 'Categories the action applied to'],
16
+ collection_count: [:number, 'Collections the action applied to'],
17
+ channel_count: [:number, 'Channels the action applied to'],
18
+ removed: [:number, 'Publications removed. Sent when unpublishing.'],
19
+ tag_count: [:number, 'Tags sent in the request']
20
+ }.freeze
21
+
22
+ typelize product_count: [:number, comment: 'Products the action applied to']
23
+ attributes :product_count
24
+
25
+ OPTIONAL.each do |name, (type, comment)|
26
+ typelize name => [type, optional: true, comment: comment]
27
+ attribute(name, if: proc { |result| result.key?(name) }) { |result| result[name] }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # What a bulk stock level upsert did.
6
+ class BulkStockLevelResultSerializer
7
+ include Alba::Resource
8
+ include Typelizer::DSL
9
+
10
+ typelize stock_level_count: [:number, comment: 'Stock levels written']
11
+
12
+ attributes :stock_level_count
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # A registered calculator, with the JSON Schema of its preferences.
6
+ class CalculatorTypeSerializer
7
+ include Alba::Resource
8
+ include Typelizer::DSL
9
+
10
+ typelize type: [:string, comment: 'The value to send as the calculator `type`, e.g. flat_rate'],
11
+ name: :string,
12
+ schema: [:PreferenceSchema, comment: "JSON Schema of the calculator's `preferences`"]
13
+
14
+ attributes :type, :name, :schema
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -8,6 +8,11 @@ module Spree
8
8
 
9
9
  attributes accepted_at: :iso8601, revoked_at: :iso8601,
10
10
  created_at: :iso8601, updated_at: :iso8601
11
+
12
+ one :company, resource: proc { Spree.api.admin_company_serializer }, if: proc { expand?('company') }
13
+
14
+ # Only the Store API's token lookup sends these.
15
+ _attributes.delete_if { |key, _| %i[company_name store_name].include?(key.to_sym) }
11
16
  end
12
17
  end
13
18
  end
@@ -8,6 +8,7 @@ module Spree
8
8
  # The member as a customer, so a list can show their name, not only
9
9
  # the email.
10
10
  one :customer, resource: proc { Spree.api.admin_customer_serializer }, if: proc { expand?('customer') }
11
+ one :company, resource: proc { Spree.api.admin_company_serializer }, if: proc { expand?('company') }
11
12
  end
12
13
  end
13
14
  end
@@ -9,6 +9,14 @@ module Spree
9
9
  class CountrySerializer < V3::CountrySerializer
10
10
  _attributes.delete(:name)
11
11
 
12
+ typelize currency: [:string, nullable: true, enum: Spree::Money::Rounding::CURRENCY_CODES, enum_type_name: 'CurrencyCode',
13
+ comment: "The country's own currency, which a store set up there starts with"],
14
+ locales: [:string, multi: true, enum: Spree::Locales::ALL, enum_type_name: 'LocaleCode',
15
+ comment: "The country's official languages, such as `de`, `fr`, `it` and `rm` for Switzerland"]
16
+
17
+ attribute :currency, &:default_currency
18
+ attribute :locales, &:offerable_locales
19
+
12
20
  many :states,
13
21
  resource: proc { Spree.api.admin_state_serializer },
14
22
  if: proc { params[:expand]&.include?('states') }
@@ -18,7 +18,8 @@ module Spree
18
18
  markup_percent: [:string, nullable: true],
19
19
  available_to_sellers: :boolean,
20
20
  seller_id: [:string, nullable: true],
21
- seller_name: [:string, nullable: true]
21
+ seller_name: [:string, nullable: true],
22
+ deleted_at: [:string, nullable: true]
22
23
 
23
24
  attributes :admin_name, :storefront_visible, :tracking_url, :available_to_sellers,
24
25
  created_at: :iso8601, updated_at: :iso8601, deleted_at: :iso8601
@@ -7,6 +7,9 @@ module Spree
7
7
 
8
8
  without_formatted_money
9
9
 
10
+ # The admin reads the counter from the order's preferred_stock_location.
11
+ _attributes.delete(:pickup_location)
12
+
10
13
  # The Admin API has no guest gating — money fields inherited from the
11
14
  # store serializer are always present, so override their nullability.
12
15
  typelize cost: [:string, nullable: false],
@@ -0,0 +1,18 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # The outcome of a live connection check on an integration.
6
+ class IntegrationConnectionResultSerializer
7
+ include Alba::Resource
8
+ include Typelizer::DSL
9
+
10
+ typelize connected: :boolean,
11
+ error_message: [:string, nullable: true, comment: 'What the provider answered when the connection failed']
12
+
13
+ attributes :connected, :error_message
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,25 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # A registered integration, with its gallery listing and the JSON
6
+ # Schema of its preferences. A plain hash from
7
+ # {Spree::Integration.discovery_entries}, so no id.
8
+ class IntegrationTypeDefinitionSerializer
9
+ include Alba::Resource
10
+ include Typelizer::DSL
11
+
12
+ typelize type: [:string, comment: 'The value to send as `type`, e.g. klaviyo'],
13
+ name: :string,
14
+ group: [:string, nullable: true, comment: 'Display grouping, e.g. shipping or tax'],
15
+ description: [:string, nullable: true],
16
+ logo_url: [:string, nullable: true, comment: 'Gallery logo: an absolute URL or a `data:` URI'],
17
+ docs_url: [:string, nullable: true, comment: "Absolute URL of the integration's setup guide"],
18
+ schema: [:PreferenceSchema, comment: "JSON Schema of the type's `preferences`"]
19
+
20
+ attributes :type, :name, :group, :description, :logo_url, :docs_url, :schema
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # The answer to a password reset request. It reads the same whether
6
+ # or not an account matched, so it reveals nothing about who exists.
7
+ class PasswordResetAcknowledgementSerializer
8
+ include Alba::Resource
9
+ include Typelizer::DSL
10
+
11
+ typelize message: :string
12
+
13
+ attributes :message
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # A payment method type, with its gallery listing.
6
+ class PaymentMethodTypeSerializer < ResourceTypeDefinitionSerializer
7
+ typelize logo_url: [:string, nullable: true, comment: 'Gallery logo: an absolute URL or a `data:` URI'],
8
+ docs_url: [:string, nullable: true, comment: "Absolute URL of the provider's setup guide"],
9
+ third_party: [:boolean, comment: 'Backed by an outside payment provider rather than handled by the store itself'],
10
+ installed: [:boolean, comment: 'The store has a payment method of this type already']
11
+
12
+ attributes :logo_url, :docs_url, :third_party, :installed
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # How many products a category, collection, price list or catalog
6
+ # gained or lost. Products it already held, or never held, are not counted.
7
+ class ProductMembershipResultSerializer
8
+ include Alba::Resource
9
+ include Typelizer::DSL
10
+
11
+ %i[added_count removed_count].each do |name|
12
+ typelize name => [:number, optional: true, comment: "Sent when products were #{name == :added_count ? 'added' : 'removed'}"]
13
+ attribute(name, if: proc { |result| result.key?(name) }) { |result| result[name] }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # The display payload of a lookup-backed or enumerated dimension, as
6
+ # Spree::Reporting::Hydration builds it.
7
+ class ReportingDimensionValueSerializer
8
+ include Alba::Resource
9
+ include Typelizer::DSL
10
+
11
+ typelize id: [:string, nullable: true,
12
+ comment: 'Prefixed id of the record (`cust_…`), or the raw value of an enumerated dimension. Null when ' \
13
+ 'there is no record behind the group, such as guest customers.'],
14
+ label: :string,
15
+ meta: 'Record<string, unknown>'
16
+
17
+ attributes :id, :label, :meta
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # One figure of a reporting result, as the adapter reports it
6
+ # (`{ value:, previous:, growth: }`), written for the wire by the
7
+ # metric and currency passed in `params`.
8
+ class ReportingMetricValueSerializer
9
+ include Alba::Resource
10
+ include Typelizer::DSL
11
+
12
+ typelize value: [%i[string number], nullable: true,
13
+ comment: 'A decimal string for money, decimal and percent metrics (`"110.00"`); a number for integer ' \
14
+ 'metrics. Null in `totals` for a metric that only means something per group.'],
15
+ previous: [%i[string number], comment: 'Present when the query requested a comparison; the same shape as `value`.'],
16
+ growth: [:string, nullable: true,
17
+ comment: 'Percentage change against the previous period, as a decimal string (`"12.5"`); null when ' \
18
+ 'there is no baseline. Present when the query requested a comparison.'],
19
+ display: [:string, comment: '`value` formatted with its currency symbol. Present on money metrics only.']
20
+
21
+ # @param payloads [Hash{Symbol => Hash}] adapter payloads by metric name
22
+ # @param currency [::Money::Currency] the result's currency
23
+ # @return [Hash{Symbol => Hash}]
24
+ def self.keyed(payloads, currency:)
25
+ payloads.to_h do |name, payload|
26
+ [name, new(payload, params: { metric: Spree.reporting.metrics.fetch(name), currency: currency }).to_h]
27
+ end
28
+ end
29
+
30
+ attribute :value do |payload|
31
+ params[:metric].format_value(payload[:value], params[:currency])
32
+ end
33
+
34
+ attribute :previous, if: proc { |payload| payload.key?(:previous) } do |payload|
35
+ params[:metric].format_value(payload[:previous], params[:currency])
36
+ end
37
+
38
+ attribute :growth, if: proc { |payload| payload.key?(:growth) } do |payload|
39
+ Spree::Money::Rounding.format_decimal(payload[:growth])
40
+ end
41
+
42
+ attribute :display, if: proc { params[:metric].money? } do |payload|
43
+ Spree::Money.new(payload[:value], currency: params[:currency]).to_s
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,44 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # What a reporting result answers: the resolved currency, periods,
6
+ # metrics and dimensions of the query that produced it.
7
+ class ReportingResultMetaSerializer
8
+ include Alba::Resource
9
+ include Typelizer::DSL
10
+
11
+ typelize currency: :string, metrics: [:string, multi: true]
12
+
13
+ attributes :currency
14
+
15
+ typelize({ since: :string, until: :string }, comment: 'The period queried, as ISO 8601 times.')
16
+ attribute :time_range do |meta|
17
+ iso_range(meta[:time_range])
18
+ end
19
+
20
+ typelize({ since: :string, until: :string }, nullable: true,
21
+ comment: 'The period compared against; null without `compare`.')
22
+ attribute :previous_time_range do |meta|
23
+ iso_range(meta[:previous_time_range])
24
+ end
25
+
26
+ attributes :metrics
27
+
28
+ typelize({ name: :string, grain: [:string, optional: true] }, multi: true)
29
+ attribute :dimensions do |meta|
30
+ meta[:dimensions]
31
+ end
32
+
33
+ private
34
+
35
+ def iso_range(range)
36
+ return unless range
37
+
38
+ { since: range.first.iso8601, until: range.last.iso8601 }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,75 +2,38 @@ module Spree
2
2
  module Api
3
3
  module V3
4
4
  module Admin
5
- # Decorates a Spree::Reporting::Result for the wire: money as decimal
5
+ # Writes a Spree::Reporting::Result for the wire: money as decimal
6
6
  # strings in the result's currency, ratios and growth as plain decimal
7
- # strings, counts as integers, money display strings, ISO ranges, and dimension hydration (raw group keys become
8
- # { id, label, meta } display payloads — see Spree::Reporting::Hydration).
7
+ # strings, counts as integers, money display strings, ISO ranges, and
8
+ # dimension hydration (raw group keys become { id, label, meta }
9
+ # display payloads — see Spree::Reporting::Hydration).
9
10
  class ReportingResultSerializer
10
- attr_reader :result, :store, :params
11
+ include Alba::Resource
12
+ include Typelizer::DSL
11
13
 
12
- def initialize(result, store:, params: {})
13
- @result = result
14
- @store = store
15
- @params = params
16
- end
17
-
18
- def to_h
19
- {
20
- meta: result.meta.merge(
21
- time_range: iso_range(result.meta[:time_range]),
22
- previous_time_range: iso_range(result.meta[:previous_time_range])
23
- ),
24
- totals: result.totals.to_h { |name, payload| [name, metric_payload(name, payload)] },
25
- rows: result.rows.map do |row|
26
- {
27
- dimensions: row[:dimensions].to_h { |name, raw| [name, dimension_value(name, raw)] },
28
- metrics: row[:metrics].to_h { |name, payload| [name, metric_payload(name, payload)] }
29
- }
30
- end
31
- }
32
- end
14
+ typelize totals: ['Record<string, ReportingMetricValue>',
15
+ comment: 'Each metric over the whole period, by metric name. Filters on a metric narrow the rows ' \
16
+ 'only, never these.'],
17
+ rows: [:ReportingRow, multi: true]
33
18
 
34
- private
35
-
36
- def iso_range(range)
37
- return unless range
19
+ one :meta, resource: ReportingResultMetaSerializer
38
20
 
39
- { since: range.first.iso8601, until: range.last.iso8601 }
21
+ attribute :totals do |result|
22
+ ReportingMetricValueSerializer.keyed(result.totals, currency: currency)
40
23
  end
41
24
 
42
- def metric_payload(name, payload)
43
- metric = Spree.reporting.metrics[name]
44
- output = payload.dup
45
- if metric
46
- output[:value] = metric.format_value(payload[:value], result.meta[:currency])
47
- output[:previous] = metric.format_value(payload[:previous], result.meta[:currency]) if payload.key?(:previous)
48
- end
49
- output[:growth] = Spree::Money::Rounding.format_decimal(payload[:growth]) if payload.key?(:growth)
50
- output[:display] = money(payload[:value]) if money_metrics.include?(name)
51
- output
25
+ attribute :rows do |result|
26
+ ReportingRowSerializer.new(result.rows, params: { hydration: hydration, currency: currency }).to_h
52
27
  end
53
28
 
54
- # Resolved once per response rather than per cell — a 1000-row report
55
- # with five metrics would otherwise build 5000 Money objects.
56
- def money_metrics
57
- @money_metrics ||= result.meta[:metrics].select { |name| Spree.reporting.metrics[name]&.money? }.to_set
58
- end
29
+ private
59
30
 
60
31
  def currency
61
- @currency ||= ::Money::Currency.find(result.meta[:currency])
62
- end
63
-
64
- def money(amount)
65
- Spree::Money.new(amount, currency: currency).to_s
66
- end
67
-
68
- def dimension_value(name, raw)
69
- hydration.value(name, raw)
32
+ @currency ||= ::Money::Currency.find(object.meta[:currency])
70
33
  end
71
34
 
72
35
  def hydration
73
- @hydration ||= Spree::Reporting::Hydration.new(result, store: store, params: params)
36
+ @hydration ||= Spree::Reporting::Hydration.new(object, store: params[:store], params: params)
74
37
  end
75
38
  end
76
39
  end
@@ -0,0 +1,35 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # One group of a reporting result. `params` carry the result's
6
+ # hydration and currency, resolved once per response by
7
+ # ReportingResultSerializer.
8
+ class ReportingRowSerializer
9
+ include Alba::Resource
10
+ include Typelizer::DSL
11
+
12
+ typelize dimensions: ['Record<string, string | ReportingDimensionValue | null>',
13
+ comment: 'By dimension name: the raw value for plain dimensions (time buckets, country codes), a ' \
14
+ 'display payload for lookup-backed and enumerated ones.'],
15
+ metrics: ['Record<string, ReportingMetricValue>', comment: 'By metric name.']
16
+
17
+ attribute :dimensions do |row|
18
+ row[:dimensions].to_h { |name, raw| [name, dimension_value(name, raw)] }
19
+ end
20
+
21
+ attribute :metrics do |row|
22
+ ReportingMetricValueSerializer.keyed(row[:metrics], currency: params[:currency])
23
+ end
24
+
25
+ private
26
+
27
+ def dimension_value(name, raw)
28
+ value = params[:hydration].value(name, raw)
29
+ value.is_a?(Hash) ? ReportingDimensionValueSerializer.new(value).to_h : value
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # One entry of a `types` list: a type a configurable resource can
6
+ # take, with the JSON Schema of its preferences. A plain hash from
7
+ # `subclasses_with_preference_schema`, so no id.
8
+ class ResourceTypeDefinitionSerializer
9
+ include Alba::Resource
10
+ include Typelizer::DSL
11
+
12
+ typelize type: [:string, comment: 'The value to send as `type`, e.g. flat_rate'],
13
+ label: :string,
14
+ description: [:string, nullable: true],
15
+ schema: [:PreferenceSchema, comment: "JSON Schema of the type's `preferences`"],
16
+ superseded: [:boolean, optional: true,
17
+ comment: 'Present, and true, only on a type something better has replaced. ' \
18
+ 'Existing records keep working; stop offering it for new ones.']
19
+
20
+ attributes :type, :label, :description, :schema
21
+
22
+ attribute :superseded, if: proc { |entry| entry.key?(:superseded) } do |entry|
23
+ entry[:superseded]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # Whether the installation still waits for its first admin.
6
+ class SetupStatusSerializer
7
+ include Alba::Resource
8
+ include Typelizer::DSL
9
+
10
+ typelize setup_required: [:boolean, comment: 'True only while the installation has no admin user']
11
+
12
+ attributes :setup_required
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Api
3
+ module V3
4
+ module Admin
5
+ # A tag by name: tags have no identity of their own in the API.
6
+ class TagSerializer
7
+ include Alba::Resource
8
+ include Typelizer::DSL
9
+
10
+ typelize name: :string
11
+
12
+ attributes :name
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end