fix(fulfillment): 统一渠道规则与标准商品绑定

This commit is contained in:
yml2213
2026-11-05 18:17:12 +08:00
parent 8b9b4fc4ad
commit 89a68b5734
3 changed files with 318 additions and 59 deletions
@@ -54,6 +54,8 @@ test(
const retryWorkOrderSku = `集成测试重试接单-${suffix}`
const manualRemoveSku = `集成测试移除人工-${suffix}`
const protectedRuleSku = `集成测试强制删除-${suffix}`
const missingVoucherSku = `集成测试无商品卡券-${suffix}`
const reactivatedVoucherSku = `集成测试归档卡券-${suffix}`
const retryWorkOrderRuleKey = `it-retry-${suffix}`
const orderIds: number[] = []
const orderItemIds: number[] = []
@@ -69,8 +71,10 @@ test(
retryWorkOrderSku,
manualRemoveSku,
protectedRuleSku,
missingVoucherSku,
reactivatedVoucherSku,
]
let generatedVoucherProductId: number | null = null
const createdVoucherProductIds: number[] = []
let autoManualProductId: number | null = null
let retryWorkOrderRuleId: number | null = null
let fallbackProfileId: number | null = null
@@ -251,8 +255,6 @@ test(
'third_party_voucher',
)
assert.equal(switchedItem.rows[0]?.item_snapshot_json.isConfigured, false)
generatedVoucherProductId = Number(switchedItem.rows[0]?.canonical_product_id || 0) || null
const missingAffiliateItemId = await createPendingOrderItem(missingAffiliateSku)
const affiliateFailed = await assignChannelByInternalSku({
internalSku: missingAffiliateSku,
@@ -644,13 +646,47 @@ test(
['closed', 'paid'],
)
await ensureVoucherRuleForSku(voucherSku)
const voucherChannel = await query<{ channel: string; source: string }>(
`SELECT channel, source FROM internal_sku_channels
assert.equal(await ensureVoucherRuleForSku(missingVoucherSku), false)
const missingVoucherRules = await query<{ count: number }>(
`SELECT count(*)::int AS count FROM internal_sku_channels
WHERE internal_sku = $1 AND channel = 'third_party_voucher'`,
[missingVoucherSku],
)
assert.equal(missingVoucherRules.rows[0]?.count, 0)
const voucherProduct = await query<{ id: number }>(
`INSERT INTO product_catalog (
canonical_sku, canonical_name, category, unit_name, status,
attributes_json, created_at, updated_at
) VALUES ($1, $2, '卡券', '张', 'active', '{}'::jsonb, NOW(), NOW())
RETURNING id`,
[`tpv:${voucherSku}`, `卡券展示名-${suffix}`],
)
createdVoucherProductIds.push(Number(voucherProduct.rows[0]!.id))
assert.equal(await ensureVoucherRuleForSku(voucherSku), true)
const voucherChannel = await query<{
channel: string
source: string
canonical_product_id: string | null
priority: number
is_fallback: boolean
config_json: Record<string, unknown>
}>(
`SELECT channel, source, canonical_product_id, priority, is_fallback, config_json
FROM internal_sku_channels
WHERE internal_sku = $1 ORDER BY channel`,
[voucherSku],
)
assert.deepEqual(voucherChannel.rows, [{ channel: 'third_party_voucher', source: 'voucher' }])
assert.equal(voucherChannel.rows.length, 1)
assert.equal(voucherChannel.rows[0]?.channel, 'third_party_voucher')
assert.equal(voucherChannel.rows[0]?.source, 'voucher')
assert.equal(
Number(voucherChannel.rows[0]?.canonical_product_id),
createdVoucherProductIds[0],
)
assert.equal(voucherChannel.rows[0]?.priority, 10)
assert.equal(voucherChannel.rows[0]?.is_fallback, false)
assert.equal(voucherChannel.rows[0]?.config_json.voucherSku, voucherSku)
const legacyVoucherRules = await query<{ count: number }>(
`SELECT count(*)::int AS count FROM sku_channel_rules
WHERE match_key = $1`,
@@ -692,6 +728,50 @@ test(
const deletedRules = await deleteChannelRules([savedRule.id])
assert.equal(deletedRules.deletedCount, 1)
assert.deepEqual(deletedRules.deletedIds, [savedRule.id])
const archivedVoucherProduct = await query<{ id: number }>(
`INSERT INTO product_catalog (
canonical_sku, canonical_name, category, unit_name, status,
attributes_json, created_at, updated_at
) VALUES (
$1, $2, '卡券', '张', 'archived',
'{"deletedAt":"2026-01-01T00:00:00.000Z","deletedBy":"integration_test"}'::jsonb,
NOW(), NOW()
)
RETURNING id`,
[`tpv:${reactivatedVoucherSku}`, reactivatedVoucherSku],
)
const archivedVoucherProductId = Number(archivedVoucherProduct.rows[0]!.id)
createdVoucherProductIds.push(archivedVoucherProductId)
const reactivatedRule = await upsertChannelRule({
matchKeyType: 'internal_sku',
matchKey: reactivatedVoucherSku,
channel: 'third_party_voucher',
priority: 80,
enabled: true,
note: '集成测试重新激活归档商品',
adminUserId: null,
})
const reactivated = await query<{
status: string
attributes_json: Record<string, unknown>
canonical_product_id: string | null
priority: number
is_fallback: boolean
}>(
`SELECT product.status, product.attributes_json,
channel.canonical_product_id, channel.priority, channel.is_fallback
FROM internal_sku_channels channel
JOIN product_catalog product ON product.id = channel.canonical_product_id
WHERE channel.id = $1`,
[reactivatedRule.id],
)
assert.equal(reactivated.rows[0]?.status, 'active')
assert.equal(Number(reactivated.rows[0]?.canonical_product_id), archivedVoucherProductId)
assert.equal(reactivated.rows[0]?.priority, 10)
assert.equal(reactivated.rows[0]?.is_fallback, false)
assert.equal(reactivated.rows[0]?.attributes_json.deletedAt, undefined)
assert.equal(typeof reactivated.rows[0]?.attributes_json.reactivatedAt, 'string')
} finally {
if (orderItemIds.length > 0) {
await query(`DELETE FROM cost_exception_items WHERE order_item_id = ANY($1::bigint[])`, [
@@ -731,32 +811,40 @@ test(
WHERE provider = '91kaquan' AND shop_id = $1 AND match_key = ANY($2::text[])`,
[shopId, internalSkus],
)
if (generatedVoucherProductId) {
await query(`DELETE FROM sales_daily_facts WHERE canonical_product_id = $1`, [
generatedVoucherProductId,
const generatedProducts = await query<{ id: string }>(
`SELECT id
FROM product_catalog
WHERE canonicalize_internal_sku(substring(canonical_sku from 5)) = ANY($1::text[])`,
[internalSkus],
)
const generatedProductIds = generatedProducts.rows.map((row) => Number(row.id))
if (generatedProductIds.length > 0) {
await query(
`DELETE FROM cost_snapshots
WHERE canonical_product_id = ANY($1::bigint[])
OR cost_version_id IN (
SELECT id FROM cost_versions WHERE canonical_product_id = ANY($1::bigint[])
)`,
[generatedProductIds],
)
await query(
`DELETE FROM sales_daily_facts WHERE canonical_product_id = ANY($1::bigint[])`,
[generatedProductIds],
)
await query(
`DELETE FROM channel_product_mappings WHERE canonical_product_id = ANY($1::bigint[])`,
[generatedProductIds],
)
await query(`DELETE FROM cost_versions WHERE canonical_product_id = ANY($1::bigint[])`, [
generatedProductIds,
])
await query(`DELETE FROM cost_snapshots WHERE canonical_product_id = $1`, [
generatedVoucherProductId,
await query(
`DELETE FROM product_channel_capabilities WHERE product_id = ANY($1::bigint[])`,
[generatedProductIds],
)
await query(`DELETE FROM product_catalog WHERE id = ANY($1::bigint[])`, [
generatedProductIds,
])
await query(`DELETE FROM channel_product_mappings WHERE canonical_product_id = $1`, [
generatedVoucherProductId,
])
await query(`DELETE FROM cost_versions WHERE canonical_product_id = $1`, [
generatedVoucherProductId,
])
await query(`DELETE FROM product_catalog WHERE id = $1 AND canonical_sku = $2`, [
generatedVoucherProductId,
`tpv:${manualSku}`,
])
}
if (autoManualProductId) {
await query(`DELETE FROM sales_daily_facts WHERE canonical_product_id = $1`, [
autoManualProductId,
])
await query(`DELETE FROM cost_versions WHERE canonical_product_id = $1`, [
autoManualProductId,
])
await query(`DELETE FROM product_catalog WHERE id = $1`, [autoManualProductId])
}
if (retryWorkOrderRuleId) {
await query(`DELETE FROM work_product_match_logs WHERE rule_id = $1`, [