feat: 完善履约路由、订单服务与自动核销
This commit is contained in:
@@ -197,6 +197,88 @@ test('syncDeliveryTasksForOrder 已有部分任务时按订单项补建缺失任
|
||||
assert.deepEqual(taskEvents, [{ taskId: 202, eventType: 'fulfillment_task_planned' }])
|
||||
})
|
||||
|
||||
test('syncDeliveryTasksForOrder 关闭任务不阻塞新的履约尝试', async () => {
|
||||
const closedTask = stubCreatedTask(205, {
|
||||
orderId: 5,
|
||||
orderItemId: 25,
|
||||
unitIndex: 1,
|
||||
executorKey: 'affiliate_dash',
|
||||
taskStatus: 'closed',
|
||||
})
|
||||
const createdInputs: CreatedTaskInput[] = []
|
||||
|
||||
await syncDeliveryTasksForOrderWithDeps(
|
||||
stubOrder({ id: 5, platform_order_id: 'retry-closed-order' }),
|
||||
[
|
||||
stubOrderItem({
|
||||
id: 25,
|
||||
order_id: 5,
|
||||
sku_code: 'SKU-retry',
|
||||
item_snapshot_json: buildAffiliateSnapshot('AFF-RETRY', '商品 RETRY'),
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [closedTask],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
createdInputs.push(input as CreatedTaskInput)
|
||||
return stubCreatedTask(206, input as CreatedTaskInput)
|
||||
},
|
||||
createTaskEvent: async () => null,
|
||||
logInfo: () => null,
|
||||
logWarn: () => null,
|
||||
allowInactiveTaskRetry: true,
|
||||
nowIso: () => '2026-05-29T09:05:46.658Z',
|
||||
randomId: () => 'DT-retry-closed',
|
||||
},
|
||||
)
|
||||
|
||||
assert.deepEqual(
|
||||
createdInputs.map((input) => [input.orderItemId, input.unitIndex]),
|
||||
[[25, 1]],
|
||||
)
|
||||
})
|
||||
|
||||
test('syncDeliveryTasksForOrder 默认不会因历史关闭任务自动补建', async () => {
|
||||
const closedTask = stubCreatedTask(208, {
|
||||
orderId: 6,
|
||||
orderItemId: 26,
|
||||
unitIndex: 1,
|
||||
executorKey: 'affiliate_dash',
|
||||
taskStatus: 'closed',
|
||||
})
|
||||
const createdInputs: CreatedTaskInput[] = []
|
||||
|
||||
await syncDeliveryTasksForOrderWithDeps(
|
||||
stubOrder({ id: 6, platform_order_id: 'closed-order-no-retry' }),
|
||||
[
|
||||
stubOrderItem({
|
||||
id: 26,
|
||||
order_id: 6,
|
||||
sku_code: 'SKU-closed-no-retry',
|
||||
item_snapshot_json: buildAffiliateSnapshot('AFF-CLOSED', '商品 CLOSED'),
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [closedTask],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
createdInputs.push(input as CreatedTaskInput)
|
||||
return stubCreatedTask(209, input as CreatedTaskInput)
|
||||
},
|
||||
createTaskEvent: async () => null,
|
||||
logInfo: () => null,
|
||||
logWarn: () => null,
|
||||
nowIso: () => '2026-05-29T09:05:46.658Z',
|
||||
randomId: () => 'DT-closed-no-retry',
|
||||
},
|
||||
)
|
||||
|
||||
assert.equal(createdInputs.length, 0)
|
||||
})
|
||||
|
||||
test('syncDeliveryTasksForOrder 数量增加时只补建缺失单元', async () => {
|
||||
const existingTask = stubCreatedTask(301, {
|
||||
orderId: 4,
|
||||
|
||||
@@ -22,7 +22,10 @@ import {
|
||||
findConflictingCarrier,
|
||||
loadActiveCarrierEvidence,
|
||||
} from '../fulfillment/carrier-evidence-service.js'
|
||||
import type { FulfillmentChannel } from '../fulfillment/channel-types.js'
|
||||
import {
|
||||
isInactiveCarrierTaskStatus,
|
||||
type FulfillmentChannel,
|
||||
} from '../fulfillment/channel-types.js'
|
||||
|
||||
type ClaimTokenLike = {
|
||||
token: string
|
||||
@@ -52,13 +55,15 @@ type DeliveryTaskDeps = {
|
||||
createTaskEvent?: typeof createTaskEvent
|
||||
logInfo?: typeof logInfo
|
||||
logWarn?: typeof logWarn
|
||||
allowInactiveTaskRetry?: boolean
|
||||
}
|
||||
|
||||
export async function syncDeliveryTasksForOrder(
|
||||
order: OrderRow,
|
||||
orderItems: OrderItemRow[],
|
||||
options: { allowInactiveTaskRetry?: boolean } = {},
|
||||
): Promise<TaskRow[]> {
|
||||
return syncDeliveryTasksForOrderWithDeps(order, orderItems)
|
||||
return syncDeliveryTasksForOrderWithDeps(order, orderItems, options)
|
||||
}
|
||||
|
||||
export async function syncDeliveryTasksForOrderWithDeps(
|
||||
@@ -79,6 +84,7 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
createTaskEvent: appendTaskEvent = createTaskEvent,
|
||||
logInfo: writeInfoLog = logInfo,
|
||||
logWarn: writeWarnLog = logWarn,
|
||||
allowInactiveTaskRetry = false,
|
||||
} = deps
|
||||
|
||||
const runtimeDeps: FulfillmentPrepareDeps = {
|
||||
@@ -90,7 +96,12 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
|
||||
const existingTasks = await listTasks(order.id)
|
||||
const tasks: DeliveryTaskRow[] = [...existingTasks]
|
||||
const existingTaskKeys = new Set(existingTasks.map(buildTaskUnitKey))
|
||||
const existingTaskKeys = new Set(
|
||||
(allowInactiveTaskRetry
|
||||
? existingTasks.filter((task) => !isInactiveCarrierTaskStatus(task.task_status))
|
||||
: existingTasks
|
||||
).map(buildTaskUnitKey),
|
||||
)
|
||||
const carrierEvidence = await loadCarrierEvidence(orderItems.map((item) => Number(item.id)))
|
||||
|
||||
writeInfoLog('[delivery-task-service]', '开始核对订单履约任务完整性', {
|
||||
@@ -205,6 +216,9 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
task =
|
||||
concurrentTasks.find(
|
||||
(candidate) =>
|
||||
(allowInactiveTaskRetry
|
||||
? !isInactiveCarrierTaskStatus(candidate.task_status)
|
||||
: true) &&
|
||||
buildTaskUnitKey(candidate) === buildOrderItemUnitKey(item.id, unitIndex),
|
||||
) || null
|
||||
if (!task) {
|
||||
|
||||
Reference in New Issue
Block a user