fix(fulfillment): 封堵重复载体并完善统一SKU映射
This commit is contained in:
@@ -545,9 +545,72 @@ async function assignOrderItemsAfterCarrierReady(input: {
|
||||
SELECT count(*)::int AS count FROM updated`,
|
||||
[ids, input.channel, input.source, input.lock],
|
||||
)
|
||||
await syncAssignedChannelSnapshots(ids, input.channel, input.source)
|
||||
return { updated: Number(final.rows[0]?.count || assignedIds.length), carrierFailed }
|
||||
}
|
||||
|
||||
async function syncAssignedChannelSnapshots(
|
||||
ids: number[],
|
||||
channel: FulfillmentChannel,
|
||||
source: FulfillmentSource,
|
||||
) {
|
||||
if (ids.length === 0) return
|
||||
const configured = channel === 'affiliate_dash' || channel === 'manual'
|
||||
const route =
|
||||
channel === 'manual'
|
||||
? {
|
||||
selectedExecutorKey: 'manual_dispatch',
|
||||
selectedRuleId: '',
|
||||
reason: '运营指定人工客服',
|
||||
matchedRule: null,
|
||||
candidates: [],
|
||||
skipped: [],
|
||||
}
|
||||
: channel === 'affiliate_dash'
|
||||
? {
|
||||
selectedExecutorKey: 'affiliate_dash',
|
||||
selectedRuleId: '',
|
||||
reason: '运营指定自动发货',
|
||||
matchedRule: null,
|
||||
candidates: [],
|
||||
skipped: [],
|
||||
}
|
||||
: {
|
||||
selectedExecutorKey: '',
|
||||
selectedRuleId: '',
|
||||
reason:
|
||||
channel === 'work_order'
|
||||
? '内部 SKU 渠道配置指定接单平台'
|
||||
: '内部 SKU 渠道配置指定第三方卡券',
|
||||
matchedRule: null,
|
||||
candidates: [],
|
||||
skipped: [],
|
||||
}
|
||||
const decision = {
|
||||
internalSkuChannel: channel,
|
||||
internalSkuChannelSource: source,
|
||||
internalSkuChannelReason: '',
|
||||
isConfigured: configured,
|
||||
fulfillmentRoute: route,
|
||||
...(channel !== 'affiliate_dash' ? { affiliateDash: null } : {}),
|
||||
}
|
||||
await query(
|
||||
`UPDATE order_items
|
||||
SET item_snapshot_json = (
|
||||
COALESCE(item_snapshot_json, '{}'::jsonb) || $2::jsonb
|
||||
) || jsonb_build_object(
|
||||
'classification',
|
||||
COALESCE(item_snapshot_json->'classification', '{}'::jsonb)
|
||||
|| jsonb_build_object('channel', $3)
|
||||
),
|
||||
updated_at = NOW()
|
||||
WHERE id = ANY($1::bigint[])
|
||||
AND fulfillment_status = 'assigned'
|
||||
AND fulfillment_channel = $3`,
|
||||
[ids, JSON.stringify(decision), channel],
|
||||
)
|
||||
}
|
||||
|
||||
const WORK_ORDER_SWITCH_BLOCKED = new Set([
|
||||
'open',
|
||||
'in_progress',
|
||||
@@ -1131,10 +1194,15 @@ async function resolveInternalSkuCarrierConfig(
|
||||
`SELECT id, canonical_product_id
|
||||
FROM manual_fulfillment_configs
|
||||
WHERE canonicalize_internal_sku(sku_key) = $1 AND enabled = TRUE
|
||||
ORDER BY id
|
||||
LIMIT 1`,
|
||||
ORDER BY id`,
|
||||
[key],
|
||||
)
|
||||
if (manual.rows.length > 1) {
|
||||
throw createHttpError(`内部 SKU「${key}」存在多份人工履约配置`, {
|
||||
statusCode: 409,
|
||||
errorCode: 'carrier_config_ambiguous',
|
||||
})
|
||||
}
|
||||
return {
|
||||
canonicalProductId: Number(manual.rows[0]?.canonical_product_id || 0) || null,
|
||||
configJson: manual.rows[0] ? { manualConfigId: Number(manual.rows[0].id) } : {},
|
||||
@@ -1149,12 +1217,27 @@ async function resolveInternalSkuCarrierConfig(
|
||||
WHERE status = 'active'
|
||||
AND (canonical_sku LIKE $2 OR canonical_sku LIKE $3)
|
||||
AND canonicalize_internal_sku(canonical_name) = $1
|
||||
ORDER BY id
|
||||
LIMIT 1`,
|
||||
ORDER BY id`,
|
||||
[key, `${prefixes[0]}%`, `${prefixes[1] || prefixes[0]}%`],
|
||||
)
|
||||
const row = product.rows[0]
|
||||
if (!row) return { canonicalProductId: null, configJson: {} }
|
||||
const distinctCarrierKeys = new Set(
|
||||
product.rows.map((candidate) => {
|
||||
const rawSku = String(candidate.canonical_sku)
|
||||
const prefix = prefixes.find((item) => rawSku.startsWith(item)) || prefixes[0]!
|
||||
return rawSku.slice(prefix.length)
|
||||
}),
|
||||
)
|
||||
if (distinctCarrierKeys.size > 1) {
|
||||
throw createHttpError(
|
||||
`内部 SKU「${key}」在 ${channel} 渠道存在多个载体配置:${[...distinctCarrierKeys].join(' / ')}`,
|
||||
{
|
||||
statusCode: 409,
|
||||
errorCode: 'carrier_config_ambiguous',
|
||||
},
|
||||
)
|
||||
}
|
||||
const rawSku = String(row.canonical_sku)
|
||||
const matchedPrefix = prefixes.find((prefix) => rawSku.startsWith(prefix)) || prefixes[0]!
|
||||
const carrierKey = rawSku.slice(matchedPrefix.length)
|
||||
|
||||
Reference in New Issue
Block a user