fix(fulfillment): 封堵重复载体并完善统一SKU映射
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { resolveOrderItemSyncPlan } from './order-item-repo.js'
|
||||
import { preserveAssignedOrderItemState, resolveOrderItemSyncPlan } from './order-item-repo.js'
|
||||
|
||||
test('resolveOrderItemSyncPlan preserves matching order item ids across repeated source syncs', () => {
|
||||
const existingItems = [
|
||||
@@ -98,3 +98,52 @@ test('resolveOrderItemSyncPlan keeps surplus existing ids for conditional cleanu
|
||||
assert.deepEqual(plan.creates, [])
|
||||
assert.deepEqual(plan.deletes, [32])
|
||||
})
|
||||
|
||||
test('已分配订单重推时冻结原渠道决策与中文内部 SKU', () => {
|
||||
const existing = {
|
||||
id: 41,
|
||||
fulfillment_status: 'assigned',
|
||||
fulfillment_channel: 'affiliate_dash',
|
||||
sku_code: 'lucky_coin_x2',
|
||||
sku_name: '幸运币2个',
|
||||
quantity: 1,
|
||||
spec_json: { skuNick: '幸运币2个', sourceSpec: 'old' },
|
||||
item_snapshot_json: {
|
||||
internalSku: '幸运币2个',
|
||||
internalSkuChannel: 'affiliate_dash',
|
||||
internalSkuChannelSource: 'operator',
|
||||
isConfigured: true,
|
||||
fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' },
|
||||
affiliateDash: { sku: 'lucky_coin_x2' },
|
||||
externalSkuName: 'old title',
|
||||
},
|
||||
}
|
||||
const incoming = {
|
||||
skuCode: 'platform-new',
|
||||
skuName: '幸运币2个',
|
||||
quantity: 1,
|
||||
specJson: { skuNick: '新映射', sourceSpec: 'new' },
|
||||
itemSnapshotJson: {
|
||||
internalSku: '新映射',
|
||||
internalSkuChannel: 'work_order',
|
||||
isConfigured: false,
|
||||
fulfillmentRoute: null,
|
||||
externalSkuName: 'new title',
|
||||
},
|
||||
createdAt: '2026-11-03T00:00:00.000Z',
|
||||
updatedAt: '2026-11-03T00:00:00.000Z',
|
||||
}
|
||||
|
||||
const stabilized = preserveAssignedOrderItemState(existing as never, incoming)
|
||||
|
||||
assert.deepEqual(stabilized.specJson, { skuNick: '幸运币2个', sourceSpec: 'new' })
|
||||
assert.deepEqual(stabilized.itemSnapshotJson, {
|
||||
externalSkuName: 'new title',
|
||||
internalSku: '幸运币2个',
|
||||
internalSkuChannel: 'affiliate_dash',
|
||||
internalSkuChannelSource: 'operator',
|
||||
isConfigured: true,
|
||||
fulfillmentRoute: { selectedExecutorKey: 'affiliate_dash' },
|
||||
affiliateDash: { sku: 'lucky_coin_x2' },
|
||||
})
|
||||
})
|
||||
|
||||
@@ -87,8 +87,13 @@ export async function replaceOrderItemsWithExecutor(
|
||||
): Promise<OrderItemRow[]> {
|
||||
const existingItems = await listOrderItemsByOrderIdWithExecutor(executor, orderId)
|
||||
const plan = resolveOrderItemSyncPlan(existingItems, items)
|
||||
const existingById = new Map(existingItems.map((item) => [Number(item.id), item]))
|
||||
|
||||
for (const update of plan.updates) {
|
||||
const effectiveItem = preserveAssignedOrderItemState(
|
||||
existingById.get(update.orderItemId),
|
||||
update.item,
|
||||
)
|
||||
await executor(
|
||||
`
|
||||
UPDATE order_items
|
||||
@@ -105,9 +110,9 @@ export async function replaceOrderItemsWithExecutor(
|
||||
update.item.skuCode,
|
||||
update.item.skuName,
|
||||
update.item.quantity,
|
||||
safeJsonStringify(update.item.specJson || '{}'),
|
||||
safeJsonStringify(update.item.itemSnapshotJson || update.item.specJson || '{}'),
|
||||
update.item.updatedAt,
|
||||
safeJsonStringify(effectiveItem.specJson || '{}'),
|
||||
safeJsonStringify(effectiveItem.itemSnapshotJson || effectiveItem.specJson || '{}'),
|
||||
effectiveItem.updatedAt,
|
||||
update.orderItemId,
|
||||
],
|
||||
)
|
||||
@@ -151,6 +156,72 @@ export async function replaceOrderItemsWithExecutor(
|
||||
return listOrderItemsByOrderIdWithExecutor(executor, orderId)
|
||||
}
|
||||
|
||||
const FROZEN_FULFILLMENT_SNAPSHOT_KEYS = [
|
||||
'fulfillmentRoute',
|
||||
'affiliateDash',
|
||||
'classification',
|
||||
'internalSku',
|
||||
'internalSkuSource',
|
||||
'internalSkuMappingId',
|
||||
'internalSkuChannel',
|
||||
'internalSkuChannelSource',
|
||||
'internalSkuChannelReason',
|
||||
'isConfigured',
|
||||
] as const
|
||||
|
||||
export function preserveAssignedOrderItemState(
|
||||
existing: OrderItemRow | undefined,
|
||||
incoming: OrderItemReplaceInput,
|
||||
): OrderItemReplaceInput {
|
||||
if (
|
||||
!existing ||
|
||||
(existing.fulfillment_status !== 'assigned' && existing.fulfillment_status !== 'archived')
|
||||
) {
|
||||
return incoming
|
||||
}
|
||||
|
||||
const existingSnapshot = parseJsonObject(existing.item_snapshot_json)
|
||||
const incomingSnapshot = parseJsonObject(incoming.itemSnapshotJson)
|
||||
const frozenSnapshot: Record<string, unknown> = {}
|
||||
for (const key of FROZEN_FULFILLMENT_SNAPSHOT_KEYS) {
|
||||
if (Object.prototype.hasOwnProperty.call(existingSnapshot, key)) {
|
||||
frozenSnapshot[key] = existingSnapshot[key]
|
||||
}
|
||||
}
|
||||
|
||||
const existingSpec = parseJsonObject(existing.spec_json)
|
||||
const incomingSpec = parseJsonObject(incoming.specJson)
|
||||
const frozenInternalSku = String(
|
||||
existingSpec.skuNick || existingSnapshot.internalSku || incomingSpec.skuNick || '',
|
||||
).trim()
|
||||
|
||||
return {
|
||||
...incoming,
|
||||
specJson: {
|
||||
...incomingSpec,
|
||||
...(frozenInternalSku ? { skuNick: frozenInternalSku } : {}),
|
||||
},
|
||||
itemSnapshotJson: {
|
||||
...incomingSnapshot,
|
||||
...frozenSnapshot,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function parseJsonObject(value: unknown): Record<string, unknown> {
|
||||
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
||||
return value as Record<string, unknown>
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(String(value || '{}'))
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||
? (parsed as Record<string, unknown>)
|
||||
: {}
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveOrderItemSyncPlan(
|
||||
existingItems: OrderItemRow[],
|
||||
nextItems: OrderItemReplaceInput[],
|
||||
|
||||
Reference in New Issue
Block a user