feat(fulfillment): 收敛履约状态写入口并追加状态事件

This commit is contained in:
yml2213
2026-11-05 20:52:22 +08:00
parent ca214854bb
commit fcd0c137d5
16 changed files with 1386 additions and 402 deletions
@@ -66,7 +66,7 @@ function stubCreatedTask(id: number, input: CreatedTaskInput): TaskRow {
unit_index: Number(input.unitIndex || 1),
platform_order_id: '',
profile_id: 0,
task_no: 'DT-test',
task_no: String(input.taskNo || 'DT-test'),
executor_key: String(input.executorKey || ''),
task_status: String(input.taskStatus || 'pending'),
delivery_status: 'pending',
@@ -428,6 +428,51 @@ test('syncDeliveryTasksForOrder 并发创建冲突时复用已落库任务', asy
assert.equal(eventCount, 0)
})
test('createTask 幂等返回已有活跃任务时不重复记计划事件', async () => {
const existingTask = {
...stubCreatedTask(402, {
orderId: 61,
orderItemId: 51,
unitIndex: 1,
executorKey: 'affiliate_dash',
}),
task_no: 'DT-existing',
}
let eventCount = 0
const tasks = await syncDeliveryTasksForOrderWithDeps(
stubOrder({ id: 61, platform_order_id: 'idempotent-create-order' }),
[
stubOrderItem({
id: 51,
order_id: 61,
sku_code: 'SKU-idempotent',
item_snapshot_json: buildAffiliateSnapshot('AFF-IDEMPOTENT', '商品 IDEMPOTENT'),
}),
],
{
loadActiveCarrierEvidence: async () => new Map(),
listTasksByOrderId: async () => [],
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
createTask: async () => existingTask,
createTaskEvent: async () => {
eventCount += 1
return null
},
logInfo: () => null,
logWarn: () => null,
nowIso: () => '2026-05-29T09:05:46.658Z',
randomId: () => 'DT-reused',
},
)
assert.deepEqual(
tasks.map((task) => task.id),
[402],
)
assert.equal(eventCount, 0)
})
test('来源重推时已分配其它渠道的订单不会补建 affiliate 任务', async () => {
let createdCount = 0
const tasks = await syncDeliveryTasksForOrderWithDeps(
@@ -181,8 +181,9 @@ export async function syncDeliveryTasksForOrderWithDeps(
? resolvePaidTaskStatus(plan.profile)
: TASK_STATUS.PENDING_PAYMENT
let createdFresh = true
let createdFresh = false
let task: TaskRow | null
const plannedTaskNo = createRandomId('DT')
try {
task = await createDeliveryTask({
orderId: order.id,
@@ -193,7 +194,7 @@ export async function syncDeliveryTasksForOrderWithDeps(
shopId: order.shop_id,
shopName: order.shop_name,
platformOrderId: order.platform_order_id,
taskNo: createRandomId('DT'),
taskNo: plannedTaskNo,
profileId: plan.profileId,
executorKey: plan.executorKey,
taskStatus: initialStatus,
@@ -211,6 +212,7 @@ export async function syncDeliveryTasksForOrderWithDeps(
createdAt,
updatedAt: createdAt,
})
createdFresh = task?.task_no === plannedTaskNo
} catch (error) {
const concurrentTasks = await listTasks(order.id)
task =
@@ -229,6 +231,14 @@ export async function syncDeliveryTasksForOrderWithDeps(
profileId: plan.profileId,
profileKey: plan.profileKey,
executorKey: plan.executorKey,
candidateTasks: concurrentTasks
.filter((candidate) => candidate.order_item_id === item.id)
.map((candidate) => ({
id: candidate.id,
unitIndex: candidate.unit_index,
status: candidate.task_status,
isCurrent: candidate.is_current,
})),
error: error instanceof Error ? error.message : String(error),
})
throw error