Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: Show seller and category names on commission rate conditions (#14649)

Damian Legawiecc4ca50bd981c
diff --git a/packages/admin-sdk/src/types/generated/CommissionRule.ts b/packages/admin-sdk/src/types/generated/CommissionRule.ts
index 3011a91ac2..3a0053808c 100644
--- a/packages/admin-sdk/src/types/generated/CommissionRule.ts
+++ b/packages/admin-sdk/src/types/generated/CommissionRule.ts
@@ -1,6 +1,7 @@
-// Typelizer digest ee96578c34f51a02c419aa40bade3695
+// Typelizer digest 98f5e3659ab172d56f253aca2a9368a2
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
+import type {Seller, Category} from '@/types'
interface CommissionRule {
id: string;
@@ -11,7 +12,11 @@ interface CommissionRule {
commission_rate_id: string;
preferences: Record<string, unknown>;
preference_schema: Array<{ key: string; type: string; default: unknown }>;
- product_ids?: Array<string>;
+ product_ids: Array<string> | null;
+ seller_ids: Array<string> | null;
+ category_ids: Array<string> | null;
+ sellers?: Array<Seller>;
+ categories?: Array<Category>;
}
export default CommissionRule;
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/sellers/commission-rates.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/sellers/commission-rates.tsx
index 3899cfaa70..989b4209a4 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/sellers/commission-rates.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/sellers/commission-rates.tsx
@@ -55,7 +55,13 @@ import { PlusIcon, Trash2Icon, TriangleAlertIcon } from '@spree/dashboard-ui/ico
import { useQueryClient } from '@tanstack/react-query'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { useEffect } from 'react'
-import { Controller, type UseFormReturn, useFieldArray, useForm } from 'react-hook-form'
+import {
+ Controller,
+ type FieldPath,
+ type UseFormReturn,
+ useFieldArray,
+ useForm,
+} from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { z } from 'zod/v4'
import { commissionRuleSubjectPicker } from '../../../../components/spree/commission-rule-subjects'
@@ -738,7 +744,13 @@ function CommissionRulesField({ form }: { form: UseFormReturn<CommissionRateForm
<DropdownMenuItem
key={type.type}
onClick={() =>
- rulesArray.append({ type: type.type, preferences: {}, product_ids: [] })
+ rulesArray.append({
+ type: type.type,
+ preferences: {},
+ product_ids: [],
+ seller_ids: [],
+ category_ids: [],
+ })
}
>
{typeLabel('commission_rule', type.type, type.name)}
@@ -828,11 +840,11 @@ function CommissionRuleRow({
{picker && associationField ? (
<Controller
control={form.control}
- name={`rules.${index}.product_ids`}
+ name={`rules.${index}.${associationField}` as FieldPath<CommissionRateFormValues>}
render={({ field }) => (
<ResourceMultiAutocomplete
{...picker(`commission-rule-${ruleType?.type}`)}
- value={field.value ?? []}
+ value={(field.value ?? []) as string[]}
onChange={field.onChange}
/>
)}
diff --git a/packages/dashboard/src/schemas/commission-rate.test.ts b/packages/dashboard/src/schemas/commission-rate.test.ts
new file mode 100644
index 0000000000..45157fd47f
--- /dev/null
+++ b/packages/dashboard/src/schemas/commission-rate.test.ts
@@ -0,0 +1,100 @@
+import type { CommissionRate } from '@spree/admin-sdk'
+import { describe, expect, it } from 'vitest'
+import { commissionRateToFormValues, commissionRateValuesToParams } from './commission-rate'
+
+function commissionRateStub(rules: CommissionRate['rules']): CommissionRate {
+ return {
+ id: 'crate_1',
+ name: 'Audio',
+ code: null,
+ enabled: true,
+ position: 1,
+ kind: 'percentage',
+ value: '10',
+ tax_inclusive: false,
+ include_shipping: false,
+ commission_tax_rate: null,
+ metadata: null,
+ deleted_at: null,
+ created_at: '2026-01-01T00:00:00Z',
+ updated_at: '2026-01-01T00:00:00Z',
+ amounts: {},
+ bounds: {},
+ global: false,
+ rules,
+ }
+}
+
+describe('commissionRateToFormValues', () => {
+ it('seeds seller and category pickers from prefixed top-level ids', () => {
+ const rate = commissionRateStub([
+ {
+ id: 'crule_1',
+ type: 'seller_rule',
+ commission_rate_id: 'crate_1',
+ preferences: { seller_ids: [42] },
+ preference_schema: [],
+ created_at: '2026-01-01T00:00:00Z',
+ updated_at: '2026-01-01T00:00:00Z',
+ product_ids: null,
+ seller_ids: ['sel_abc'],
+ category_ids: null,
+ },
+ {
+ id: 'crule_2',
+ type: 'category_rule',
+ commission_rate_id: 'crate_1',
+ preferences: { category_ids: [7] },
+ preference_schema: [],
+ created_at: '2026-01-01T00:00:00Z',
+ updated_at: '2026-01-01T00:00:00Z',
+ product_ids: null,
+ seller_ids: null,
+ category_ids: ['ctg_xyz'],
+ },
+ ])
+
+ const values = commissionRateToFormValues(rate)
+
+ expect(values.rules[0]?.seller_ids).toEqual(['sel_abc'])
+ expect(values.rules[0]?.preferences).toEqual({})
+ expect(values.rules[1]?.category_ids).toEqual(['ctg_xyz'])
+ expect(values.rules[1]?.preferences).toEqual({})
+ })
+})
+
+describe('commissionRateValuesToParams', () => {
+ it('writes seller and category ids back into preferences as prefixed ids', () => {
+ const params = commissionRateValuesToParams({
+ name: 'Audio',
+ code: '',
+ enabled: true,
+ kind: 'percentage',
+ value: 10,
+ amounts: {},
+ bounds: {},
+ tax_inclusive: false,
+ include_shipping: false,
+ commission_tax_rate: '',
+ rules: [
+ {
+ type: 'seller_rule',
+ preferences: {},
+ product_ids: [],
+ seller_ids: ['sel_abc'],
+ category_ids: [],
+ },
+ {
+ type: 'category_rule',
+ preferences: {},
+ product_ids: [],
+ seller_ids: [],
+ category_ids: ['ctg_xyz'],
+ },
+ ],
+ })
+
+ expect(params.rules?.[0]?.preferences).toEqual({ seller_ids: ['sel_abc'] })
+ expect(params.rules?.[1]?.preferences).toEqual({ category_ids: ['ctg_xyz'] })
+ })
+})
diff --git a/packages/dashboard/src/schemas/commission-rate.ts b/packages/dashboard/src/schemas/commission-rate.ts
index 793a6c0ab0..e9cff9cce5 100644
--- a/packages/dashboard/src/schemas/commission-rate.ts
+++ b/packages/dashboard/src/schemas/commission-rate.ts
@@ -37,8 +37,11 @@ const commissionRateFieldsSchema = z.object({
id: z.string().optional(),
type: z.string(),
preferences: z.record(z.string(), z.unknown()).default({}),
- // Catalog-scale references ride beside preferences, not inside them.
+ // Reference lists ride beside preferences, not inside them — the API
+ // exposes prefixed ids at the top level while preferences hold raw ids.
product_ids: z.array(z.string()).default([]),
+ seller_ids: z.array(z.string()).default([]),
+ category_ids: z.array(z.string()).default([]),
}),
),
})
@@ -92,12 +95,22 @@ export function commissionRateToFormValues(rate: CommissionRate): CommissionRate
include_shipping: rate.include_shipping,
commission_tax_rate:
rate.commission_tax_rate == null ? '' : String(Number(rate.commission_tax_rate) * 100),
- rules: (rate.rules ?? []).map((rule) => ({
- id: rule.id,
- type: rule.type,
- preferences: (rule.preferences ?? {}) as Record<string, unknown>,
- product_ids: rule.product_ids ?? [],
- })),
+ rules: (rate.rules ?? []).map((rule) => {
+ const preferences = { ...(rule.preferences ?? {}) } as Record<string, unknown>
+ // Top-level id lists are the picker source of truth; raw ids in
+ // preferences would show as numbers after save.
+ delete preferences.seller_ids
+ delete preferences.category_ids
+
+ return {
+ id: rule.id,
+ type: rule.type,
+ preferences,
+ product_ids: rule.product_ids ?? [],
+ seller_ids: rule.seller_ids ?? [],
+ category_ids: rule.category_ids ?? [],
+ }
+ }),
}
}
@@ -154,13 +167,21 @@ export function commissionRateValuesToParams(
}),
),
commission_tax_rate: taxPercentage === null ? null : taxPercentage / 100,
- // `product_ids` is only meaningful to kinds that name products; sending an
- // empty array to the others is noise the server would ignore.
- rules: v.rules.map((rule) => ({
- id: rule.id,
- type: rule.type,
- preferences: rule.preferences,
- ...(rule.product_ids.length > 0 ? { product_ids: rule.product_ids } : {}),
- })),
+ rules: v.rules.map((rule) => {
+ const preferences = { ...rule.preferences }
+
+ if (rule.type === 'seller_rule') {
+ preferences.seller_ids = rule.seller_ids
+ } else if (rule.type === 'category_rule') {
+ preferences.category_ids = rule.category_ids
+ }
+
+ return {
+ id: rule.id,
+ type: rule.type,
+ preferences,
+ ...(rule.product_ids.length > 0 ? { product_ids: rule.product_ids } : {}),
+ }
+ }),
}
}
diff --git a/spree/api/app/controllers/spree/api/v3/admin/commission_rates_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/commission_rates_controller.rb
index 40f325a195..daf890f1e6 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/commission_rates_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/commission_rates_controller.rb
@@ -156,11 +156,16 @@ module Spree
end
def association_fields_for(klass)
- return [] unless klass.respond_to?(:additional_permitted_attributes)
-
- klass.additional_permitted_attributes.flat_map do |attribute|
- attribute.is_a?(Hash) ? attribute.keys : attribute
- end.map(&:to_s)
+ case klass.api_type
+ when 'seller_rule' then ['seller_ids']
+ when 'category_rule' then ['category_ids']
+ else
+ return [] unless klass.respond_to?(:additional_permitted_attributes)
+
+ klass.additional_permitted_attributes.flat_map do |attribute|
+ attribute.is_a?(Hash) ? attribute.keys : attribute
+ end.map(&:to_s)
+ end
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/commission_rule_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/commission_rule_serializer.rb
index 1fb3259b13..dbcd504461 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/commission_rule_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/commission_rule_serializer.rb
@@ -14,7 +14,9 @@ module Spree
commission_rate_id: :string,
preferences: 'Record<string, unknown>',
preference_schema: 'Array<{ key: string; type: string; default: unknown }>',
- product_ids: [:string, multi: true]
+ product_ids: 'Array<string> | null',
+ seller_ids: 'Array<string> | null',
+ category_ids: 'Array<string> | null'
attributes created_at: :iso8601, updated_at: :iso8601
@@ -29,12 +31,29 @@ module Spree
attribute :preferences, &:serialized_preferences
attribute :preference_schema, &:serialized_preference_schema
- # Catalog-scale references live in their own table rather than the
- # preferences blob, so they are read back separately — omitted
- # entirely for rule kinds that carry none.
- attribute :product_ids, if: proc { |rule| rule.respond_to?(:product_prefixed_ids) } do |rule|
- rule.product_prefixed_ids
+ # Reference lists are returned as prefixed IDs to match the rest of
+ # the API. Product ids are read through a join table; seller and
+ # category ids live in preferences server-side but are not exposed
+ # that way on the wire — the picker round-trips prefixed ids.
+ attribute :product_ids do |rule|
+ rule.product_prefixed_ids if rule.respond_to?(:product_prefixed_ids)
end
+
+ attribute :seller_ids do |rule|
+ rule.sellers.map(&:prefixed_id) if rule.respond_to?(:sellers)
+ end
+
+ attribute :category_ids do |rule|
+ rule.categories.map(&:prefixed_id) if rule.respond_to?(:categories)
+ end
+
+ many :sellers,
+ resource: proc { Spree.api.admin_seller_serializer },
+ if: proc { |rule| rule.respond_to?(:sellers) }
+
+ many :categories,
+ resource: proc { Spree.api.admin_category_serializer },
+ if: proc { |rule| rule.respond_to?(:categories) }
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/commission_rates_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/commission_rates_controller_spec.rb
index e0301fa261..ccf25997f2 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/commission_rates_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/commission_rates_controller_spec.rb
@@ -23,8 +23,11 @@ RSpec.describe Spree::Api::V3::Admin::CommissionRatesController, type: :controll
expect(row['kind']).to eq('percentage')
# The rule is identified by `type` alone — the dashboard names it from
# its own locale files rather than from a server-resolved label.
- expect(row['rules'].first).to include('type' => 'seller_rule')
- expect(row['rules'].first).not_to have_key('label')
+ rule_payload = row['rules'].first
+ expect(rule_payload).to include('type' => 'seller_rule')
+ expect(rule_payload).not_to have_key('label')
+ expect(rule_payload['seller_ids']).to eq([seller.prefixed_id])
+ expect(rule_payload['sellers'].first['id']).to eq(seller.prefixed_id)
end
it 'returns rates in precedence order' do
@@ -70,7 +73,9 @@ RSpec.describe Spree::Api::V3::Admin::CommissionRatesController, type: :controll
expect(response).to have_http_status(:created)
expect(json_response['value']).to eq('12.5')
- expect(json_response['rules'].first['type']).to eq('seller_rule')
+ rule_payload = json_response['rules'].first
+ expect(rule_payload['type']).to eq('seller_rule')
+ expect(rule_payload['seller_ids']).to eq([seller.prefixed_id])
end
it 'refuses a fixed rate that states no amount anywhere' do
@@ -127,6 +132,7 @@ RSpec.describe Spree::Api::V3::Admin::CommissionRatesController, type: :controll
expect(response).to have_http_status(:ok)
expect(rate.reload.commission_rules.map(&:class)).to eq([Spree::CommissionRules::CategoryRule])
+ expect(json_response['rules'].first['category_ids']).to eq([category.prefixed_id])
end
# A rule can only name records of its own marketplace. The preference
@@ -252,6 +258,12 @@ RSpec.describe Spree::Api::V3::Admin::CommissionRatesController, type: :controll
products = types.find { |row| row['type'] == 'product_rule' }
expect(products['association_fields']).to include('product_ids')
+
+ sellers = types.find { |row| row['type'] == 'seller_rule' }
+ expect(sellers['association_fields']).to include('seller_ids')
+
+ categories = types.find { |row| row['type'] == 'category_rule' }
+ expect(categories['association_fields']).to include('category_ids')
end
end
end