fix(fulfillment): 封堵重复载体并完善统一SKU映射
This commit is contained in:
@@ -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