fix(fulfillment): 封堵重复载体并完善统一SKU映射
This commit is contained in:
@@ -28,17 +28,33 @@ test(
|
||||
switchChannelForOrderItems,
|
||||
upsertChannelRule,
|
||||
} = await import('./channel-assign-service.js')
|
||||
const { runAffiliateFallback } = await import('./affiliate-fallback-service.js')
|
||||
const {
|
||||
deleteInternalSkuMapping,
|
||||
listInternalSkuMappingsPage,
|
||||
resolveInternalSkuMapping,
|
||||
upsertInternalSkuMapping,
|
||||
} = await import('./internal-sku-mapping-service.js')
|
||||
const suffix = `${Date.now()}_${Math.floor(Math.random() * 1_000_000)}`
|
||||
const shopId = `it-routing-${suffix}`
|
||||
const manualSku = `集成测试人工-${suffix}`
|
||||
const missingAffiliateSku = `集成测试自动-${suffix}`
|
||||
const voucherSku = `集成测试卡券-${suffix}`
|
||||
const triggerSku = `集成测试规范-${suffix}`
|
||||
const fallbackSku = `集成测试兜底-${suffix}`
|
||||
const orderIds: number[] = []
|
||||
const orderItemIds: number[] = []
|
||||
const crossShopSku = `集成测试跨店-${suffix}`
|
||||
const internalSkus = [manualSku, missingAffiliateSku, voucherSku, crossShopSku, triggerSku]
|
||||
const internalSkus = [
|
||||
manualSku,
|
||||
missingAffiliateSku,
|
||||
voucherSku,
|
||||
crossShopSku,
|
||||
triggerSku,
|
||||
fallbackSku,
|
||||
]
|
||||
let generatedVoucherProductId: number | null = null
|
||||
let fallbackProfileId: number | null = null
|
||||
|
||||
async function createPendingOrderItem(
|
||||
sku: string,
|
||||
@@ -75,13 +91,16 @@ test(
|
||||
const canonicalResolution = await query<{
|
||||
internal_sku: string | null
|
||||
platform_only: string | null
|
||||
cjk_extension: string | null
|
||||
}>(
|
||||
`SELECT
|
||||
resolve_internal_sku('platform-code', ' 套装-浪漫 天命 ', 'platform-code') AS internal_sku,
|
||||
resolve_internal_sku('platform-code', 'Platform Title', 'platform-code') AS platform_only`,
|
||||
resolve_internal_sku('platform-code', 'Platform Title', 'platform-code') AS platform_only,
|
||||
resolve_internal_sku('', '𠀀', '') AS cjk_extension`,
|
||||
)
|
||||
assert.equal(canonicalResolution.rows[0]?.internal_sku, '套装-浪漫天命')
|
||||
assert.equal(canonicalResolution.rows[0]?.platform_only, null)
|
||||
assert.equal(canonicalResolution.rows[0]?.cjk_extension, '𠀀')
|
||||
|
||||
await query(
|
||||
`INSERT INTO internal_sku_channels (internal_sku, channel, priority, is_fallback, enabled, source)
|
||||
@@ -93,6 +112,32 @@ test(
|
||||
)
|
||||
assert.equal(normalizedInsert.rows[0]?.internal_sku, triggerSku)
|
||||
|
||||
const mappingValue = `MAP-${suffix}`
|
||||
const mappingSku = `集成映射-${suffix}`
|
||||
const mappingId = await upsertInternalSkuMapping({
|
||||
provider: '91kaquan',
|
||||
shopId,
|
||||
matchType: 'sku_code',
|
||||
matchValue: ` ${mappingValue} `,
|
||||
internalSku: mappingSku,
|
||||
priority: 10,
|
||||
source: 'integration_test',
|
||||
note: '集成测试映射',
|
||||
})
|
||||
const resolvedMapping = await resolveInternalSkuMapping({
|
||||
provider: '91kaquan',
|
||||
shopId,
|
||||
candidates: [{ matchType: 'sku_code', value: mappingValue }],
|
||||
})
|
||||
assert.equal(resolvedMapping?.internalSku, mappingSku)
|
||||
const mappingPage = await listInternalSkuMappingsPage({
|
||||
keyword: mappingSku,
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
})
|
||||
assert.equal(mappingPage.total, 1)
|
||||
assert.equal(await deleteInternalSkuMapping(mappingId), true)
|
||||
|
||||
const manualItemId = await createPendingOrderItem(manualSku)
|
||||
await query(
|
||||
`INSERT INTO manual_fulfillment_configs (
|
||||
@@ -136,9 +181,10 @@ test(
|
||||
original_channel: string | null
|
||||
canonical_product_id: number | null
|
||||
canonical_sku: string | null
|
||||
item_snapshot_json: Record<string, unknown>
|
||||
}>(
|
||||
`SELECT oi.fulfillment_status, oi.fulfillment_channel, oi.original_channel,
|
||||
oi.canonical_product_id, product.canonical_sku
|
||||
oi.canonical_product_id, product.canonical_sku, oi.item_snapshot_json
|
||||
FROM order_items oi
|
||||
LEFT JOIN product_catalog product ON product.id = oi.canonical_product_id
|
||||
WHERE oi.id = $1`,
|
||||
@@ -148,6 +194,11 @@ test(
|
||||
assert.equal(switchedItem.rows[0]?.fulfillment_channel, 'third_party_voucher')
|
||||
assert.equal(switchedItem.rows[0]?.original_channel, 'manual')
|
||||
assert.ok(switchedItem.rows[0]?.canonical_sku?.startsWith('tpv:'))
|
||||
assert.equal(
|
||||
switchedItem.rows[0]?.item_snapshot_json.internalSkuChannel,
|
||||
'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)
|
||||
@@ -191,6 +242,104 @@ test(
|
||||
assert.deepEqual(pendingGroups.items[0]?.providers.sort(), ['91kaquan', 'pdd'])
|
||||
assert.deepEqual(pendingGroups.items[0]?.shop_ids.sort(), [crossShopA, crossShopB])
|
||||
|
||||
const fallbackItemId = await createPendingOrderItem(fallbackSku)
|
||||
await query(
|
||||
`INSERT INTO internal_sku_channels (
|
||||
internal_sku, channel, priority, is_fallback, enabled, config_json, source
|
||||
) VALUES
|
||||
($1, 'affiliate_dash', 10, FALSE, TRUE, '{}'::jsonb, 'integration_test'),
|
||||
($1, 'manual', 90, TRUE, TRUE, '{}'::jsonb, 'integration_test')`,
|
||||
[fallbackSku],
|
||||
)
|
||||
const fallbackItem = await query<{ order_id: number }>(
|
||||
`UPDATE order_items
|
||||
SET fulfillment_status = 'assigned', fulfillment_channel = 'affiliate_dash',
|
||||
fulfillment_source = 'operator_assigned', fulfillment_locked = FALSE,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING order_id`,
|
||||
[fallbackItemId],
|
||||
)
|
||||
const fallbackOrderId = Number(fallbackItem.rows[0]!.order_id)
|
||||
const fallbackProfile = await query<{ id: number }>(
|
||||
`INSERT INTO fulfillment_profiles (
|
||||
profile_key, name, executor_key, requires_claim, auto_dispatch,
|
||||
inventory_strategy, config_json, created_at, updated_at
|
||||
) VALUES ($1, '集成测试自动发货', 'affiliate_dash', TRUE, FALSE,
|
||||
'static_pool', '{}'::jsonb, NOW(), NOW())
|
||||
RETURNING id`,
|
||||
[`it-affiliate-${suffix}`],
|
||||
)
|
||||
fallbackProfileId = Number(fallbackProfile.rows[0]!.id)
|
||||
const fallbackOrder = await query<{
|
||||
provider: string
|
||||
platform: string
|
||||
shop_id: string
|
||||
shop_name: string
|
||||
platform_order_id: string
|
||||
}>(
|
||||
`SELECT provider, platform, shop_id, shop_name, platform_order_id
|
||||
FROM orders WHERE id = $1`,
|
||||
[fallbackOrderId],
|
||||
)
|
||||
const orderRow = fallbackOrder.rows[0]!
|
||||
await query(
|
||||
`INSERT INTO fulfillment_tasks (
|
||||
order_id, order_item_id, unit_index, task_no, provider, platform,
|
||||
shop_id, shop_name, platform_order_id, profile_id, executor_key,
|
||||
task_status, created_at, updated_at
|
||||
) VALUES ($1, $2, 1, $3, $4, $5, $6, $7, $8, $9, 'affiliate_dash', 'failed', NOW(), NOW())`,
|
||||
[
|
||||
fallbackOrderId,
|
||||
fallbackItemId,
|
||||
`IT-AFF-FAIL-${suffix}`,
|
||||
orderRow.provider,
|
||||
orderRow.platform,
|
||||
orderRow.shop_id,
|
||||
orderRow.shop_name,
|
||||
orderRow.platform_order_id,
|
||||
fallbackProfileId,
|
||||
],
|
||||
)
|
||||
|
||||
const fallbackWithoutConfig = await runAffiliateFallback({ limit: 50 })
|
||||
assert.equal(fallbackWithoutConfig.switched, 0)
|
||||
assert.equal(
|
||||
(
|
||||
await query<{ fulfillment_channel: string }>(
|
||||
`SELECT fulfillment_channel FROM order_items WHERE id = $1`,
|
||||
[fallbackItemId],
|
||||
)
|
||||
).rows[0]?.fulfillment_channel,
|
||||
'affiliate_dash',
|
||||
)
|
||||
|
||||
const manualFallbackConfig = await query<{ id: number }>(
|
||||
`INSERT INTO manual_fulfillment_configs (
|
||||
sku_key, platform_key, credit_quantity, cost_mode, enabled, created_at, updated_at
|
||||
) VALUES ($1, 'nanhang', 0, 'variable', TRUE, NOW(), NOW())
|
||||
RETURNING id`,
|
||||
[fallbackSku],
|
||||
)
|
||||
const fallbackWithConfig = await runAffiliateFallback({ limit: 50 })
|
||||
assert.equal(fallbackWithConfig.switched, 1)
|
||||
const fallbackAssigned = await query<{
|
||||
fulfillment_channel: string
|
||||
fulfillment_config_id: number | null
|
||||
item_snapshot_json: Record<string, unknown>
|
||||
}>(
|
||||
`SELECT fulfillment_channel, fulfillment_config_id, item_snapshot_json
|
||||
FROM order_items WHERE id = $1`,
|
||||
[fallbackItemId],
|
||||
)
|
||||
assert.equal(fallbackAssigned.rows[0]?.fulfillment_channel, 'manual')
|
||||
assert.equal(fallbackAssigned.rows[0]?.item_snapshot_json.internalSkuChannel, 'manual')
|
||||
assert.equal(fallbackAssigned.rows[0]?.item_snapshot_json.isConfigured, true)
|
||||
assert.equal(
|
||||
Number(fallbackAssigned.rows[0]?.fulfillment_config_id),
|
||||
Number(manualFallbackConfig.rows[0]!.id),
|
||||
)
|
||||
|
||||
await ensureVoucherRuleForSku(voucherSku)
|
||||
const voucherChannel = await query<{ channel: string; source: string }>(
|
||||
`SELECT channel, source FROM internal_sku_channels
|
||||
@@ -244,7 +393,13 @@ test(
|
||||
await query(`DELETE FROM orders WHERE id = ANY($1::bigint[])`, [orderIds])
|
||||
}
|
||||
await query(`DELETE FROM cost_exceptions WHERE scope_key LIKE $1`, [`%${suffix}%`])
|
||||
await query(`DELETE FROM manual_fulfillment_configs WHERE sku_key = $1`, [manualSku])
|
||||
await query(`DELETE FROM manual_fulfillment_configs WHERE sku_key = ANY($1::text[])`, [
|
||||
internalSkus,
|
||||
])
|
||||
await query(
|
||||
`DELETE FROM internal_sku_mappings WHERE provider = '91kaquan' AND shop_id = $1`,
|
||||
[shopId],
|
||||
)
|
||||
await query(`DELETE FROM internal_sku_channels WHERE internal_sku = ANY($1::text[])`, [
|
||||
internalSkus,
|
||||
])
|
||||
@@ -271,6 +426,9 @@ test(
|
||||
`tpv:${manualSku}`,
|
||||
])
|
||||
}
|
||||
if (fallbackProfileId) {
|
||||
await query(`DELETE FROM fulfillment_profiles WHERE id = $1`, [fallbackProfileId])
|
||||
}
|
||||
await closeDb()
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user