优化订单履约分流与后台筛选

This commit is contained in:
yml2213
2026-09-02 11:59:38 +08:00
parent 50c526a855
commit de63daf1ac
19 changed files with 1269 additions and 162 deletions
@@ -6,7 +6,9 @@ import type { OrderItemRow, OrderRow, TaskRow } from '../../types/repository/row
import type { JsonObject } from '../../types/json.js'
type CreatedTaskInput = {
orderId?: number
orderItemId?: number
unitIndex?: number
contextJson?: string
taskStatus?: string
executorKey?: string
@@ -59,9 +61,9 @@ function stubOrderItem(
function stubCreatedTask(id: number, input: CreatedTaskInput): TaskRow {
return {
id,
order_id: 0,
order_id: Number(input.orderId || 0),
order_item_id: Number(input.orderItemId || 0),
unit_index: 1,
unit_index: Number(input.unitIndex || 1),
platform_order_id: '',
profile_id: 0,
task_no: 'DT-test',
@@ -145,6 +147,7 @@ test('syncDeliveryTasksForOrder 多账号候选时不提前固定 cloudtentacles
},
nowIso: () => '2026-05-29T09:05:46.658Z',
randomId: () => 'DT-test',
createTaskEvent: async () => null,
},
)
@@ -194,6 +197,7 @@ test('syncDeliveryTasksForOrder 单账号候选时保留固定 cloudtentacles
},
nowIso: () => '2026-05-29T09:05:46.658Z',
randomId: () => 'DT-test',
createTaskEvent: async () => null,
},
)
@@ -203,3 +207,229 @@ test('syncDeliveryTasksForOrder 单账号候选时保留固定 cloudtentacles
assert.deepEqual(binding.cloudSourceKeys, ['account-a'])
assert.equal(binding.resolvedSourceKey, 'account-a')
})
test('syncDeliveryTasksForOrder 已有部分任务时按订单项补建缺失任务', async () => {
const existingTask = stubCreatedTask(201, {
orderId: 3,
orderItemId: 20,
unitIndex: 1,
executorKey: 'kuaishou_ct_assisted',
})
const createdInputs: CreatedTaskInput[] = []
const taskEvents: Array<{ taskId: number; eventType: string }> = []
const tasks = await syncDeliveryTasksForOrderWithDeps(
stubOrder({ id: 3, platform_order_id: 'partial-order' }),
[
stubOrderItem({
id: 20,
order_id: 3,
sku_code: 'SKU-existing',
item_snapshot_json: buildCloudSnapshot(74, '商品 A'),
}),
stubOrderItem({
id: 21,
order_id: 3,
sku_code: 'SKU-missing',
item_snapshot_json: buildCloudSnapshot(75, '商品 B'),
}),
],
{
listTasksByOrderId: async () => [existingTask],
getFulfillmentProfileByKey: async () => buildCloudProfile(),
createTask: async (input) => {
createdInputs.push(input as CreatedTaskInput)
return stubCreatedTask(202, input as CreatedTaskInput)
},
createTaskEvent: async (taskId, eventType) => {
taskEvents.push({ taskId: Number(taskId), eventType })
return null
},
logInfo: () => null,
logWarn: () => null,
nowIso: () => '2026-05-29T09:05:46.658Z',
randomId: () => 'DT-partial',
},
)
assert.deepEqual(
createdInputs.map((input) => [input.orderItemId, input.unitIndex]),
[[21, 1]],
)
assert.deepEqual(
tasks.map((task) => task.id),
[201, 202],
)
assert.deepEqual(taskEvents, [{ taskId: 202, eventType: 'fulfillment_task_planned' }])
})
test('syncDeliveryTasksForOrder 数量增加时只补建缺失单元', async () => {
const existingTask = stubCreatedTask(301, {
orderId: 4,
orderItemId: 30,
unitIndex: 1,
executorKey: 'kuaishou_ct_assisted',
})
const createdInputs: CreatedTaskInput[] = []
const tasks = await syncDeliveryTasksForOrderWithDeps(
stubOrder({ id: 4, platform_order_id: 'quantity-increased' }),
[
stubOrderItem({
id: 30,
order_id: 4,
sku_code: 'SKU-quantity',
quantity: 2,
item_snapshot_json: buildCloudSnapshot(74, '商品 A'),
}),
],
{
listTasksByOrderId: async () => [existingTask],
getFulfillmentProfileByKey: async () => buildCloudProfile(),
createTask: async (input) => {
createdInputs.push(input as CreatedTaskInput)
return stubCreatedTask(302, input as CreatedTaskInput)
},
createTaskEvent: async () => null,
logInfo: () => null,
logWarn: () => null,
nowIso: () => '2026-05-29T09:05:46.658Z',
randomId: () => 'DT-quantity',
},
)
assert.deepEqual(
createdInputs.map((input) => input.unitIndex),
[2],
)
assert.deepEqual(
tasks.map((task) => task.unit_index),
[1, 2],
)
})
test('syncDeliveryTasksForOrder 规划失败时记录可定位的路由诊断', async () => {
const warnings: Array<{ message: string; detail: Record<string, unknown> }> = []
const tasks = await syncDeliveryTasksForOrderWithDeps(
stubOrder({ id: 5, platform_order_id: 'planning-failed' }),
[
stubOrderItem({
id: 40,
order_id: 5,
sku_code: 'SKU-broken',
item_snapshot_json: {
isConfigured: true,
fulfillmentRoute: {
selectedExecutorKey: 'kuaishou_ct_assisted',
reason: '规则指定云履约',
},
cloudtentacles: {
cloudSkuId: 74,
cloudSkuName: '商品 A',
cloudSourceKeys: [],
},
} as JsonObject,
}),
],
{
listTasksByOrderId: async () => [],
getFulfillmentProfileByKey: async () => buildCloudProfile(),
createTaskEvent: async () => null,
logInfo: () => null,
logWarn: (_scope, message, detail) => {
warnings.push({
message: String(message),
detail: detail as Record<string, unknown>,
})
return null
},
},
)
assert.deepEqual(tasks, [])
assert.equal(warnings.length, 1)
assert.equal(warnings[0]?.message, '订单项已配置履约路由但无法生成任务计划')
assert.equal(warnings[0]?.detail.orderItemId, 40)
assert.deepEqual(warnings[0]?.detail.missingUnitIndexes, [1])
assert.deepEqual(warnings[0]?.detail.planningDiagnostic, {
snapshotIsConfigured: true,
selectedExecutorKey: 'kuaishou_ct_assisted',
routeReason: '规则指定云履约',
candidateAvailability: [],
sourceItemTitle: '',
sourceSkuNick: '',
cloudSkuId: 74,
cloudSkuName: '商品 A',
cloudSourceKeys: [],
feifeiProductCode: '',
affiliateDashSku: '',
})
})
test('syncDeliveryTasksForOrder 并发创建冲突时复用已落库任务', async () => {
const concurrentTask = stubCreatedTask(401, {
orderId: 6,
orderItemId: 50,
unitIndex: 1,
executorKey: 'kuaishou_ct_assisted',
})
let listCount = 0
let eventCount = 0
const tasks = await syncDeliveryTasksForOrderWithDeps(
stubOrder({ id: 6, platform_order_id: 'concurrent-order' }),
[
stubOrderItem({
id: 50,
order_id: 6,
sku_code: 'SKU-concurrent',
item_snapshot_json: buildCloudSnapshot(74, '商品 A'),
}),
],
{
listTasksByOrderId: async () => (listCount++ === 0 ? [] : [concurrentTask]),
getFulfillmentProfileByKey: async () => buildCloudProfile(),
createTask: async () => {
throw new Error('duplicate key value violates unique constraint')
},
createTaskEvent: async () => {
eventCount += 1
return null
},
logInfo: () => null,
logWarn: () => null,
nowIso: () => '2026-05-29T09:05:46.658Z',
randomId: () => 'DT-concurrent',
},
)
assert.deepEqual(
tasks.map((task) => task.id),
[401],
)
assert.equal(eventCount, 0)
})
function buildCloudSnapshot(cloudSkuId: number, cloudSkuName: string): JsonObject {
return {
isConfigured: true,
fulfillmentRoute: {
selectedExecutorKey: 'kuaishou_ct_assisted',
},
cloudtentacles: {
cloudSourceKeys: ['account-a'],
cloudSkuId,
cloudSkuName,
},
}
}
function buildCloudProfile() {
return {
id: 9,
profile_key: 'kuaishou_ct_assisted',
profile_name: 'kuaishou-lewan 履约',
executor_key: 'kuaishou_ct_assisted',
}
}