fix(fulfillment): 修复载体重试并完善核销任务列表
This commit is contained in:
@@ -54,7 +54,11 @@ async function hasAffiliateTask(orderItemId: number): Promise<boolean> {
|
||||
return result.rows[0]?.exists === true
|
||||
}
|
||||
|
||||
const BLOCKED_INACTIVE_CARRIER_STATUSES = new Set(['closed', 'cancelled', 'manual_review'])
|
||||
const BLOCKED_WORK_ORDER_STATUSES = new Set(['closed', 'cancelled', 'manual_review'])
|
||||
const HARD_BLOCKED_INACTIVE_REASONS = new Set([
|
||||
'carrier_task_user_refund',
|
||||
'carrier_task_cancelled',
|
||||
])
|
||||
|
||||
async function findBlockingInactiveCarrier(input: {
|
||||
orderItemId: number
|
||||
@@ -64,16 +68,16 @@ async function findBlockingInactiveCarrier(input: {
|
||||
|
||||
const result =
|
||||
input.channel === 'affiliate_dash'
|
||||
? await query<{ status: string }>(
|
||||
`SELECT task_status AS status
|
||||
? await query<{ status: string; result_code: string }>(
|
||||
`SELECT task_status AS status, result_code
|
||||
FROM fulfillment_tasks
|
||||
WHERE order_item_id = $1 AND executor_key = 'affiliate_dash'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1`,
|
||||
[input.orderItemId],
|
||||
)
|
||||
: await query<{ status: string }>(
|
||||
`SELECT status
|
||||
: await query<{ status: string; result_code: string }>(
|
||||
`SELECT status, '' AS result_code
|
||||
FROM work_orders
|
||||
WHERE order_item_id = $1
|
||||
ORDER BY id DESC
|
||||
@@ -82,7 +86,14 @@ async function findBlockingInactiveCarrier(input: {
|
||||
)
|
||||
|
||||
const status = String(result.rows[0]?.status || '')
|
||||
if (!BLOCKED_INACTIVE_CARRIER_STATUSES.has(status)) return null
|
||||
const resultCode = String(result.rows[0]?.result_code || '')
|
||||
const shouldBlock =
|
||||
input.channel === 'affiliate_dash'
|
||||
? INACTIVE_CARRIER_TASK_STATUSES.includes(
|
||||
status as (typeof INACTIVE_CARRIER_TASK_STATUSES)[number],
|
||||
)
|
||||
: BLOCKED_WORK_ORDER_STATUSES.has(status)
|
||||
if (!shouldBlock) return null
|
||||
|
||||
if (status === 'manual_review') {
|
||||
return {
|
||||
@@ -91,6 +102,34 @@ async function findBlockingInactiveCarrier(input: {
|
||||
message: '已有履约载体进入人工复核,需先处理后再重试',
|
||||
}
|
||||
}
|
||||
if (status === 'retry_pending' || status === 'failed') {
|
||||
return {
|
||||
status,
|
||||
reason: 'carrier_task_retry_pending',
|
||||
message: '已有履约任务失败待重试,需由运营确认后重新创建',
|
||||
}
|
||||
}
|
||||
if (status === 'expired') {
|
||||
return {
|
||||
status,
|
||||
reason: 'carrier_task_expired',
|
||||
message: '已有履约任务已过期,需由运营确认后重新创建',
|
||||
}
|
||||
}
|
||||
if (resultCode === 'USER_APPLY_REFUND') {
|
||||
return {
|
||||
status,
|
||||
reason: 'carrier_task_user_refund',
|
||||
message: '用户已申请退款,不能自动重新发货',
|
||||
}
|
||||
}
|
||||
if (resultCode === 'affiliate_dash_cancelled') {
|
||||
return {
|
||||
status,
|
||||
reason: 'carrier_task_cancelled',
|
||||
message: '上游履约订单已取消,不能自动重新发货',
|
||||
}
|
||||
}
|
||||
return {
|
||||
status,
|
||||
reason: 'carrier_task_closed',
|
||||
@@ -101,6 +140,14 @@ async function findBlockingInactiveCarrier(input: {
|
||||
}
|
||||
}
|
||||
|
||||
function shouldBlockInactiveCarrier(
|
||||
inactiveCarrier: { reason: string } | null,
|
||||
retryInactiveCarrier: boolean,
|
||||
): inactiveCarrier is { status: string; reason: string; message: string } {
|
||||
if (!inactiveCarrier) return false
|
||||
return !retryInactiveCarrier || HARD_BLOCKED_INACTIVE_REASONS.has(inactiveCarrier.reason)
|
||||
}
|
||||
|
||||
function toResolvedItem(item: OrderItemRow): ResolvedFulfillmentItem {
|
||||
return {
|
||||
...item,
|
||||
@@ -127,15 +174,8 @@ export async function ensureCarrierForOrderItem(input: {
|
||||
item: OrderItemRow
|
||||
channel: FulfillmentChannel
|
||||
retryInactiveCarrier?: boolean
|
||||
allowInactiveTaskRetry?: boolean
|
||||
}): Promise<EnsureCarrierResult> {
|
||||
const {
|
||||
order,
|
||||
item,
|
||||
channel,
|
||||
retryInactiveCarrier = false,
|
||||
allowInactiveTaskRetry = false,
|
||||
} = input
|
||||
const { order, item, channel, retryInactiveCarrier = false } = input
|
||||
const existingCarrier = findConflictingCarrier(
|
||||
await loadActiveCarrierEvidenceForItem(Number(item.id)),
|
||||
channel,
|
||||
@@ -194,7 +234,7 @@ export async function ensureCarrierForOrderItem(input: {
|
||||
orderItemId: Number(item.id),
|
||||
channel,
|
||||
})
|
||||
if (inactiveCarrier && !retryInactiveCarrier) {
|
||||
if (shouldBlockInactiveCarrier(inactiveCarrier, retryInactiveCarrier)) {
|
||||
return {
|
||||
ready: false,
|
||||
created: false,
|
||||
@@ -274,7 +314,7 @@ export async function ensureCarrierForOrderItem(input: {
|
||||
orderItemId: Number(item.id),
|
||||
channel,
|
||||
})
|
||||
if (inactiveCarrier && !retryInactiveCarrier) {
|
||||
if (shouldBlockInactiveCarrier(inactiveCarrier, retryInactiveCarrier)) {
|
||||
return {
|
||||
ready: false,
|
||||
created: false,
|
||||
@@ -326,7 +366,7 @@ export async function ensureCarrierForOrderItem(input: {
|
||||
item_snapshot_json: snapshot,
|
||||
} as OrderItemRow,
|
||||
],
|
||||
{ allowInactiveTaskRetry: retryInactiveCarrier || allowInactiveTaskRetry },
|
||||
{ allowInactiveTaskRetry: retryInactiveCarrier },
|
||||
)
|
||||
}
|
||||
const ready = await hasAffiliateTask(Number(item.id))
|
||||
|
||||
Reference in New Issue
Block a user