refactor(fulfillment): 收敛配置与商品关联写入
This commit is contained in:
@@ -3,6 +3,7 @@ import { query } from '../../db/client.js'
|
||||
import { assignPendingOrderItemChannels } from '../fulfillment/channel-assign-service.js'
|
||||
import { runAffiliateFallback } from '../fulfillment/affiliate-fallback-service.js'
|
||||
import { reconcilePendingCarriers } from '../fulfillment/carrier-reconciliation-service.js'
|
||||
import { attachFulfillmentConfigurations } from '../fulfillment/fulfillment-state-service.js'
|
||||
import {
|
||||
fulfillmentCreatedAfterSql,
|
||||
getFulfillmentAutomationCutoff,
|
||||
@@ -149,16 +150,16 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
|
||||
)
|
||||
|
||||
let attached = 0
|
||||
for (const row of manual.rows) {
|
||||
const updated = await query(
|
||||
`
|
||||
UPDATE order_items
|
||||
SET fulfillment_config_id = $2, updated_at = NOW()
|
||||
WHERE id = $1 AND fulfillment_config_id IS NULL
|
||||
`,
|
||||
[row.id, row.config_id],
|
||||
)
|
||||
attached += updated.rowCount || 0
|
||||
if (manual.rows.length > 0) {
|
||||
const configured = await attachFulfillmentConfigurations({
|
||||
mappings: manual.rows.map((row) => ({
|
||||
orderItemId: Number(row.id),
|
||||
configId: Number(row.config_id),
|
||||
})),
|
||||
expected: { status: 'assigned', channel: 'manual' },
|
||||
onlyWhenConfigNull: true,
|
||||
})
|
||||
attached = configured.length
|
||||
}
|
||||
|
||||
// 人工渠道但没有配置:进「人工 SKU 未配置」队列。
|
||||
@@ -401,15 +402,20 @@ async function resolveCarrierDispatchExceptions(cutoffCreatedAt: string): Promis
|
||||
// 游标会跳过已扫过的订单项;配置是后来补的,这里兜底把配置挂上。
|
||||
const attachParams: unknown[] = []
|
||||
const attachCutoff = fulfillmentCreatedAfterSql(attachParams, cutoffCreatedAt)
|
||||
await query(
|
||||
const attachCandidates = await query<{ order_item_id: number; config_id: number }>(
|
||||
`
|
||||
UPDATE order_items oi
|
||||
SET fulfillment_config_id = cfg.id, updated_at = NOW()
|
||||
FROM manual_fulfillment_configs cfg
|
||||
SELECT DISTINCT ON (oi.id)
|
||||
oi.id AS order_item_id,
|
||||
cfg.id AS config_id
|
||||
FROM order_items oi
|
||||
JOIN manual_fulfillment_configs cfg
|
||||
ON canonicalize_internal_sku(cfg.sku_key) = resolve_internal_sku(
|
||||
oi.manual_sku_key, oi.sku_name, oi.sku_code
|
||||
)
|
||||
AND cfg.enabled
|
||||
WHERE canonicalize_internal_sku(cfg.sku_key) = resolve_internal_sku(
|
||||
oi.manual_sku_key, oi.sku_name, oi.sku_code
|
||||
)
|
||||
AND cfg.enabled
|
||||
AND oi.fulfillment_status = 'assigned'
|
||||
AND oi.fulfillment_channel = 'manual'
|
||||
AND oi.fulfillment_config_id IS NULL
|
||||
@@ -422,9 +428,21 @@ async function resolveCarrierDispatchExceptions(cutoffCreatedAt: string): Promis
|
||||
AND ce.status <> 'resolved'
|
||||
)
|
||||
${attachCutoff}
|
||||
ORDER BY oi.id, cfg.id
|
||||
LIMIT 500
|
||||
`,
|
||||
attachParams,
|
||||
)
|
||||
if (attachCandidates.rows.length > 0) {
|
||||
await attachFulfillmentConfigurations({
|
||||
mappings: attachCandidates.rows.map((row) => ({
|
||||
orderItemId: Number(row.order_item_id),
|
||||
configId: Number(row.config_id),
|
||||
})),
|
||||
expected: { status: 'assigned', channel: 'manual' },
|
||||
onlyWhenConfigNull: true,
|
||||
})
|
||||
}
|
||||
|
||||
const carrierParams: unknown[] = []
|
||||
const carrierCutoff = fulfillmentCreatedAfterSql(carrierParams, cutoffCreatedAt)
|
||||
|
||||
Reference in New Issue
Block a user