fix(fulfillment): 封堵重复载体并完善统一SKU映射
This commit is contained in:
@@ -120,6 +120,7 @@ test('syncDeliveryTasksForOrder 为 affiliate_dash 商品创建任务上下文',
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
@@ -167,6 +168,7 @@ test('syncDeliveryTasksForOrder 已有部分任务时按订单项补建缺失任
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [existingTask],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
@@ -216,6 +218,7 @@ test('syncDeliveryTasksForOrder 数量增加时只补建缺失单元', async ()
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [existingTask],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
@@ -263,6 +266,7 @@ test('syncDeliveryTasksForOrder 规划失败时记录可定位的路由诊断',
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTaskEvent: async () => null,
|
||||
@@ -318,6 +322,7 @@ test('syncDeliveryTasksForOrder 并发创建冲突时复用已落库任务', asy
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => (listCount++ === 0 ? [] : [concurrentTask]),
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async () => {
|
||||
@@ -341,6 +346,77 @@ test('syncDeliveryTasksForOrder 并发创建冲突时复用已落库任务', asy
|
||||
assert.equal(eventCount, 0)
|
||||
})
|
||||
|
||||
test('来源重推时已分配其它渠道的订单不会补建 affiliate 任务', async () => {
|
||||
let createdCount = 0
|
||||
const tasks = await syncDeliveryTasksForOrderWithDeps(
|
||||
stubOrder({ id: 7, platform_order_id: 'assigned-other-channel' }),
|
||||
[
|
||||
stubOrderItem({
|
||||
id: 60,
|
||||
order_id: 7,
|
||||
sku_code: 'SKU-assigned',
|
||||
fulfillment_status: 'assigned',
|
||||
fulfillment_channel: 'work_order',
|
||||
item_snapshot_json: buildAffiliateSnapshot('AFF-A', '商品 A'),
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () => new Map(),
|
||||
listTasksByOrderId: async () => [],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
createdCount += 1
|
||||
return stubCreatedTask(501, input as CreatedTaskInput)
|
||||
},
|
||||
logInfo: () => null,
|
||||
logWarn: () => null,
|
||||
},
|
||||
)
|
||||
|
||||
assert.equal(createdCount, 0)
|
||||
assert.deepEqual(tasks, [])
|
||||
})
|
||||
|
||||
test('来源重推时已有其它真实载体的订单不会补建 affiliate 任务', async () => {
|
||||
let createdCount = 0
|
||||
const tasks = await syncDeliveryTasksForOrderWithDeps(
|
||||
stubOrder({ id: 8, platform_order_id: 'existing-carrier' }),
|
||||
[
|
||||
stubOrderItem({
|
||||
id: 70,
|
||||
order_id: 8,
|
||||
sku_code: 'SKU-existing-carrier',
|
||||
item_snapshot_json: buildAffiliateSnapshot('AFF-A', '商品 A'),
|
||||
}),
|
||||
],
|
||||
{
|
||||
loadActiveCarrierEvidence: async () =>
|
||||
new Map([
|
||||
[
|
||||
70,
|
||||
{
|
||||
workOrder: true,
|
||||
affiliateTask: false,
|
||||
manualTask: false,
|
||||
channels: ['work_order'],
|
||||
},
|
||||
],
|
||||
]),
|
||||
listTasksByOrderId: async () => [],
|
||||
getFulfillmentProfileByKey: async () => buildAffiliateProfile(),
|
||||
createTask: async (input) => {
|
||||
createdCount += 1
|
||||
return stubCreatedTask(502, input as CreatedTaskInput)
|
||||
},
|
||||
logInfo: () => null,
|
||||
logWarn: () => null,
|
||||
},
|
||||
)
|
||||
|
||||
assert.equal(createdCount, 0)
|
||||
assert.deepEqual(tasks, [])
|
||||
})
|
||||
|
||||
function buildAffiliateSnapshot(sku: string, productName: string): JsonObject {
|
||||
return {
|
||||
isConfigured: true,
|
||||
|
||||
@@ -8,13 +8,21 @@ import {
|
||||
type FulfillmentBindingLike,
|
||||
} from '../fulfillment/planner.js'
|
||||
import { preparePaidFulfillmentTask } from '../fulfillment/executors/registry.js'
|
||||
import type { FulfillmentPrepareDeps } from '../fulfillment/executors/types.js'
|
||||
import {
|
||||
FULFILLMENT_EXECUTOR_KEYS,
|
||||
type FulfillmentPrepareDeps,
|
||||
} from '../fulfillment/executors/types.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
import { randomId } from '../../utils/random.js'
|
||||
import { logInfo, logWarn } from '../../utils/logger.js'
|
||||
import { TASK_STATUS, resolveInitialPaidTaskStatus } from '../../domain/task-status.js'
|
||||
import type { JsonObject } from '../../types/json.js'
|
||||
import type { OrderItemRow, OrderRow, TaskRow } from '../../types/repository/rows.js'
|
||||
import {
|
||||
findConflictingCarrier,
|
||||
loadActiveCarrierEvidence,
|
||||
} from '../fulfillment/carrier-evidence-service.js'
|
||||
import type { FulfillmentChannel } from '../fulfillment/channel-types.js'
|
||||
|
||||
type ClaimTokenLike = {
|
||||
token: string
|
||||
@@ -30,6 +38,7 @@ type DeliveryTaskRow = TaskRow & {
|
||||
type DeliveryTaskDeps = {
|
||||
createTask?: typeof createTask
|
||||
listTasksByOrderId?: typeof listTasksByOrderId
|
||||
loadActiveCarrierEvidence?: typeof loadActiveCarrierEvidence
|
||||
updateTask?: typeof updateTask
|
||||
getFulfillmentProfileByKey?: (profileKey: string) => Promise<FulfillmentBindingLike | null>
|
||||
createTaskClaimToken?: (taskId: number | string) => Promise<ClaimTokenLike>
|
||||
@@ -60,6 +69,7 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
const {
|
||||
createTask: createDeliveryTask = createTask,
|
||||
listTasksByOrderId: listTasks = listTasksByOrderId,
|
||||
loadActiveCarrierEvidence: loadCarrierEvidence = loadActiveCarrierEvidence,
|
||||
updateTask: updateDeliveryTask = updateTask,
|
||||
getFulfillmentProfileByKey: getProfileByKey = getFulfillmentProfileByKey,
|
||||
createTaskClaimToken: createClaimToken = createTaskClaimToken,
|
||||
@@ -81,6 +91,7 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
const existingTasks = await listTasks(order.id)
|
||||
const tasks: DeliveryTaskRow[] = [...existingTasks]
|
||||
const existingTaskKeys = new Set(existingTasks.map(buildTaskUnitKey))
|
||||
const carrierEvidence = await loadCarrierEvidence(orderItems.map((item) => Number(item.id)))
|
||||
|
||||
writeInfoLog('[delivery-task-service]', '开始核对订单履约任务完整性', {
|
||||
...buildOrderLogContext(order),
|
||||
@@ -126,6 +137,32 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
continue
|
||||
}
|
||||
|
||||
const plannedChannel = executorKeyToFulfillmentChannel(plan.executorKey)
|
||||
if (plannedChannel) {
|
||||
const assignedChannel = String(item.fulfillment_channel || '').trim()
|
||||
if (item.fulfillment_status === 'assigned' && assignedChannel !== plannedChannel) {
|
||||
writeInfoLog('[delivery-task-service]', '订单项已分配其它渠道,跳过载体补建', {
|
||||
...buildOrderItemLogContext(item),
|
||||
assignedChannel,
|
||||
plannedChannel,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
const conflictingCarrier = findConflictingCarrier(
|
||||
carrierEvidence.get(Number(item.id)),
|
||||
plannedChannel,
|
||||
)
|
||||
if (conflictingCarrier) {
|
||||
writeWarnLog('[delivery-task-service]', '订单项已有其它真实载体,跳过载体补建', {
|
||||
...buildOrderItemLogContext(item),
|
||||
existingCarrier: conflictingCarrier,
|
||||
plannedChannel,
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
for (const unitIndex of missingUnitIndexes) {
|
||||
const createdAt = getNowIso()
|
||||
const initialStatus =
|
||||
@@ -256,6 +293,12 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
return preparedTasks.filter(isTaskRow)
|
||||
}
|
||||
|
||||
function executorKeyToFulfillmentChannel(executorKey: string): FulfillmentChannel | null {
|
||||
if (executorKey === FULFILLMENT_EXECUTOR_KEYS.AFFILIATE_DASH) return 'affiliate_dash'
|
||||
if (executorKey === FULFILLMENT_EXECUTOR_KEYS.MANUAL_DISPATCH) return 'manual'
|
||||
return null
|
||||
}
|
||||
|
||||
async function preparePaidTaskWithDiagnostics(
|
||||
order: OrderRow,
|
||||
task: TaskRow,
|
||||
|
||||
@@ -10,7 +10,13 @@ import {
|
||||
import { withTransaction } from '../../db/client.js'
|
||||
import { syncDeliveryTasksForOrder } from './delivery-task-service.js'
|
||||
import { resolveOrderItemForFulfillment } from '../fulfillment/product-resolution-service.js'
|
||||
import { applyIntakeChannelDecision } from '../fulfillment/intake-channel-decision-service.js'
|
||||
import { applyIntakeChannelPlan } from '../fulfillment/intake-channel-decision-service.js'
|
||||
import {
|
||||
loadInternalSkuChannelPlans,
|
||||
planInternalSkuChannels,
|
||||
type InternalSkuChannelPlan,
|
||||
} from '../fulfillment/internal-sku-channel-service.js'
|
||||
import { canonicalizeInternalSku } from '../fulfillment/internal-sku-resolver.js'
|
||||
import {
|
||||
resolveOrderFulfillmentReadiness,
|
||||
syncOrderFulfillmentAttachments,
|
||||
@@ -114,21 +120,24 @@ export async function upsertOrderFromSource(
|
||||
})
|
||||
|
||||
const resolvedItems = await Promise.all(
|
||||
event.items.map(async (item) => {
|
||||
const resolved = await resolveOrderItemForFulfillment({
|
||||
event.items.map((item) =>
|
||||
resolveOrderItemForFulfillment({
|
||||
provider: event.provider,
|
||||
platform: event.platform,
|
||||
shopId: event.shopId,
|
||||
item,
|
||||
})
|
||||
return applyIntakeChannelDecision(resolved, {
|
||||
provider: event.provider,
|
||||
platform: event.platform,
|
||||
shopId: event.shopId,
|
||||
})
|
||||
}),
|
||||
}),
|
||||
),
|
||||
)
|
||||
const resolvedFulfillmentItems = resolvedItems as FulfillmentOrderItem[]
|
||||
const channelPlans = await loadInternalSkuChannelPlans(
|
||||
resolvedItems.map((item) => String(item.snapshot.internalSku || '')),
|
||||
)
|
||||
const emptyPlan = planInternalSkuChannels([])
|
||||
const resolvedFulfillmentItems = resolvedItems.map((item) => {
|
||||
const internalSku = canonicalizeInternalSku(item.snapshot.internalSku)
|
||||
const plan: InternalSkuChannelPlan = channelPlans.get(internalSku) || emptyPlan
|
||||
return applyIntakeChannelPlan(item, plan)
|
||||
}) as FulfillmentOrderItem[]
|
||||
const configuredItems = resolvedFulfillmentItems.filter((item) => item.isConfigured)
|
||||
|
||||
const persisted = await withTransaction(async (client) => {
|
||||
|
||||
Reference in New Issue
Block a user