fix(finance): 重写履约路由迁移与统一渠道配置

重写 120/121 迁移,统一内部 SKU、渠道 id、config_json 和旧规则转换。

后台渠道配置改读统一渠道表,卡券与人工配置不再写旧规则。

增加安全渠道切换,并排除已关闭、失败或过期的历史任务载体。

按生产 119 基线重跑迁移、完整履约财务链路和严格预检。
This commit is contained in:
yml2213
2026-11-02 15:29:13 +08:00
parent 2f87445ed9
commit 6bf28489e0
31 changed files with 958 additions and 464 deletions
@@ -2,7 +2,11 @@ import { query } from '../../db/client.js'
import { getOrderById } from '../../repositories/order-repo.js'
import type { OrderItemRow } from '../../types/repository/rows.js'
import { logWarn } from '../../utils/logger.js'
import { FULFILLMENT_CHANNELS, type FulfillmentChannel } from './channel-types.js'
import {
FULFILLMENT_CHANNELS,
INACTIVE_CARRIER_TASK_STATUSES,
type FulfillmentChannel,
} from './channel-types.js'
import { ensureCarrierForOrderItem } from './carrier-creation-service.js'
import {
planInternalSkuChannels,
@@ -21,6 +25,7 @@ function normalizeChannels(value: unknown): InternalSkuChannelRow[] {
return value
.filter((row): row is Record<string, unknown> => Boolean(row) && typeof row === 'object')
.map((row) => ({
id: Number(row.id || 0),
internal_sku: String(row.internal_sku || '').trim(),
channel: String(row.channel || '') as FulfillmentChannel,
priority: Number(row.priority || 100),
@@ -33,6 +38,9 @@ function normalizeChannels(value: unknown): InternalSkuChannelRow[] {
? (row.config_json as Record<string, unknown>)
: {},
source: String(row.source || ''),
note: String(row.note || ''),
created_by_admin_user_id:
row.created_by_admin_user_id == null ? null : Number(row.created_by_admin_user_id),
}))
.filter((row) => FULFILLMENT_CHANNELS.includes(row.channel))
}
@@ -49,10 +57,14 @@ async function loadCandidateItems(input: {
const statusFilter = input.assignedMissingCarrier
? `oi.fulfillment_status = 'assigned'
AND oi.fulfillment_channel IN ('affiliate_dash', 'work_order')
AND NOT EXISTS (SELECT 1 FROM work_orders wo WHERE wo.order_item_id = oi.id)
AND NOT EXISTS (
SELECT 1 FROM work_orders wo
WHERE wo.order_item_id = oi.id AND wo.status <> 'cancelled'
)
AND NOT EXISTS (
SELECT 1 FROM fulfillment_tasks ft
WHERE ft.order_item_id = oi.id AND ft.executor_key = 'affiliate_dash'
AND ft.task_status NOT IN (${INACTIVE_CARRIER_TASK_STATUSES.map((status) => `'${status}'`).join(', ')})
)`
: `oi.fulfillment_status = 'pending' AND oi.fulfillment_locked = FALSE`
@@ -70,6 +82,7 @@ async function loadCandidateItems(input: {
SELECT COALESCE(
jsonb_agg(
jsonb_build_object(
'id', channel.id,
'internal_sku', channel.internal_sku,
'channel', channel.channel,
'priority', channel.priority,
@@ -78,6 +91,8 @@ async function loadCandidateItems(input: {
'canonical_product_id', channel.canonical_product_id,
'config_json', channel.config_json,
'source', channel.source
,'note', channel.note
,'created_by_admin_user_id', channel.created_by_admin_user_id
)
ORDER BY channel.priority, channel.channel
),
@@ -138,6 +153,8 @@ async function selectSafeFallback(input: {
fulfillment_reason = $3,
channel_switch_reason = $3,
channel_switched_at = NOW(),
canonical_product_id = NULL,
fulfillment_config_id = NULL,
fulfillment_pending_reason = NULL,
fulfillment_pending_detail = NULL,
updated_at = NOW()
@@ -328,10 +345,14 @@ export async function reconcilePendingCarriers(
FROM order_items oi
WHERE oi.fulfillment_status IN ('pending', 'assigned')
AND oi.fulfillment_channel IN ('affiliate_dash', 'work_order')
AND NOT EXISTS (SELECT 1 FROM work_orders wo WHERE wo.order_item_id = oi.id)
AND NOT EXISTS (
SELECT 1 FROM work_orders wo
WHERE wo.order_item_id = oi.id AND wo.status <> 'cancelled'
)
AND NOT EXISTS (
SELECT 1 FROM fulfillment_tasks ft
WHERE ft.order_item_id = oi.id AND ft.executor_key = 'affiliate_dash'
AND ft.task_status NOT IN (${INACTIVE_CARRIER_TASK_STATUSES.map((status) => `'${status}'`).join(', ')})
)
`,
)