feat(fulfillment): 新增渠道判定管道,规则优先、载体证据兜底

- 只写 channel/status/source,不再由「有没有任务」反推渠道
- 同优先级多渠道判为冲突并保持 pending,进配置冲突队列
- 运营指定渠道即锁定,自动判定不再覆盖
- 待分类队列按 SKU 聚合,支持按 SKU 批量指定
This commit is contained in:
yml2213
2026-10-31 18:12:06 +08:00
parent 4cf700e910
commit 8cdc2423d5
3 changed files with 702 additions and 0 deletions
@@ -0,0 +1,118 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
resolveChannelDecision,
type ChannelCandidateItem,
type ChannelRule,
} from './channel-assign-service.js'
function buildItem(patch: Partial<ChannelCandidateItem> = {}): ChannelCandidateItem {
return {
id: 1,
provider: '91kaquan',
shop_id: 'shop-a',
sku_code: 'suit_xxx',
sku_name: '套装-测试',
manual_sku_key: 'suit_xxx',
normalized_sku_name: '套装-测试',
has_work_order: false,
has_affiliate_task: false,
...patch,
}
}
function buildRule(patch: Partial<ChannelRule> = {}): ChannelRule {
return {
id: 1,
provider: '',
shop_id: '',
match_key_type: 'sku_code',
match_key: 'suit_xxx',
channel: 'affiliate_dash',
priority: 60,
enabled: true,
...patch,
}
}
test('渠道判定:精确 SKU 规则优先于载体证据', () => {
const decision = resolveChannelDecision(buildItem({ has_work_order: true }), [
buildRule({ channel: 'third_party_voucher', priority: 10 }),
])
assert.equal(decision.channel, 'third_party_voucher')
assert.equal(decision.source, 'auto_rule')
assert.equal(decision.conflict, false)
})
test('渠道判定:无规则时按载体证据判定', () => {
const workOrder = resolveChannelDecision(buildItem({ has_work_order: true }), [])
assert.equal(workOrder.channel, 'work_order')
assert.equal(workOrder.source, 'carrier_evidence')
const affiliate = resolveChannelDecision(buildItem({ has_affiliate_task: true }), [])
assert.equal(affiliate.channel, 'affiliate_dash')
assert.equal(affiliate.source, 'carrier_evidence')
})
test('渠道判定:既无规则也无载体时保持 pending,不兜底人工', () => {
const decision = resolveChannelDecision(buildItem(), [])
assert.equal(decision.channel, null)
assert.equal(decision.conflict, false)
})
test('渠道判定:同优先级多渠道判为冲突,不静默选择', () => {
const decision = resolveChannelDecision(buildItem(), [
buildRule({ id: 1, channel: 'affiliate_dash', priority: 10 }),
buildRule({ id: 2, channel: 'manual', priority: 10 }),
])
assert.equal(decision.channel, null)
assert.equal(decision.conflict, true)
assert.deepEqual(decision.conflictChannels.sort(), ['affiliate_dash', 'manual'])
})
test('渠道判定:不同优先级时取优先级更小的', () => {
const decision = resolveChannelDecision(buildItem(), [
buildRule({ id: 1, channel: 'affiliate_dash', priority: 60 }),
buildRule({ id: 2, channel: 'manual', priority: 70 }),
])
assert.equal(decision.channel, 'affiliate_dash')
})
test('渠道判定:provider / shop 限定生效,空串表示不限', () => {
const scoped = buildRule({ provider: 'pdd', shop_id: 'shop-b' })
assert.equal(resolveChannelDecision(buildItem(), [scoped]).channel, null)
const wide = buildRule({ provider: '', shop_id: '' })
assert.equal(resolveChannelDecision(buildItem(), [wide]).channel, 'affiliate_dash')
})
test('渠道判定:内部 SKU 与 SKU 名称两种键都能命中', () => {
const byInternal = buildRule({
match_key_type: 'internal_sku',
match_key: 'nick-001',
channel: 'manual',
})
assert.equal(
resolveChannelDecision(buildItem({ manual_sku_key: 'nick-001' }), [byInternal]).channel,
'manual',
)
const byName = buildRule({
match_key_type: 'sku_name',
match_key: '套装-测试',
channel: 'third_party_voucher',
})
assert.equal(
resolveChannelDecision(buildItem({ normalized_sku_name: '套装-测试' }), [byName]).channel,
'third_party_voucher',
)
})
test('渠道判定:停用的规则不参与', () => {
const decision = resolveChannelDecision(buildItem(), [buildRule({ enabled: false })])
assert.equal(decision.channel, null)
})