优化快手 Cloud 主链路状态机
This commit is contained in:
@@ -4,6 +4,12 @@ import { createTaskClaimToken } from '../claim/claim-service.js'
|
||||
import { notifyTaskAutoManualReview } from '../notification/domain-notifications.js'
|
||||
import { nowIso } from '../../utils/time.js'
|
||||
import { randomId } from '../../utils/random.js'
|
||||
import {
|
||||
TASK_STATUS,
|
||||
isPaidPreparationStableStatus,
|
||||
resolveInitialPaidTaskStatus,
|
||||
shouldEnsureKuaishouCloudClaimLink,
|
||||
} from '../../domain/task-status.js'
|
||||
import type { OrderItemRow, OrderRow, TaskRow } from '../../types/repository/rows.js'
|
||||
|
||||
type JsonObject = Record<string, unknown>
|
||||
@@ -130,7 +136,7 @@ export async function syncDeliveryTasksForOrderWithDeps(
|
||||
const createdAt = getNowIso()
|
||||
const initialStatus = order.pay_status === 'paid'
|
||||
? resolvePaidTaskStatus(profile)
|
||||
: 'pending_payment'
|
||||
: TASK_STATUS.PENDING_PAYMENT
|
||||
|
||||
const task = await createDeliveryTask({
|
||||
orderId: order.id,
|
||||
@@ -271,19 +277,12 @@ async function preparePaidTask(
|
||||
|
||||
const now = getNowIso()
|
||||
|
||||
if (['link_generated', 'claimed', 'role_confirmed', 'redeeming', 'redeemed', 'closed'].includes(task.task_status)) {
|
||||
if (isPaidPreparationStableStatus(task.task_status)) {
|
||||
return task
|
||||
}
|
||||
|
||||
if (isKuaishouCloudExecutor(task.executor_key)) {
|
||||
if ([
|
||||
'pending_binding_prepare',
|
||||
'waiting_binding',
|
||||
'dispatched_pending_return',
|
||||
'completed',
|
||||
'manual_review',
|
||||
'failed',
|
||||
].includes(String(task.task_status || '').trim())) {
|
||||
if (shouldEnsureKuaishouCloudClaimLink(task.task_status)) {
|
||||
if (!task.primary_claim_token_id && !String(task.claim_token || '').trim()) {
|
||||
const claimToken = await createClaimToken(task.id)
|
||||
return updateDeliveryTask(task.id, {
|
||||
@@ -300,7 +299,7 @@ async function preparePaidTask(
|
||||
const claimToken = await createClaimToken(task.id)
|
||||
|
||||
return updateDeliveryTask(task.id, {
|
||||
task_status: 'pending_binding_prepare',
|
||||
task_status: TASK_STATUS.PENDING_BINDING_PREPARE,
|
||||
claim_token: claimToken.token,
|
||||
claim_expires_at: claimToken.expired_at,
|
||||
user_action_status: 'pending_claim',
|
||||
@@ -312,7 +311,7 @@ async function preparePaidTask(
|
||||
if (!task.requires_claim || String(task.executor_key || '').trim() === 'manual_dispatch') {
|
||||
const lastError = task.last_error || '当前任务需要人工履约处理'
|
||||
const updatedTask = await updateDeliveryTask(task.id, {
|
||||
task_status: 'manual_review',
|
||||
task_status: TASK_STATUS.MANUAL_REVIEW,
|
||||
user_action_status: 'not_required',
|
||||
last_error: lastError,
|
||||
updated_at: now,
|
||||
@@ -329,19 +328,7 @@ async function preparePaidTask(
|
||||
}
|
||||
|
||||
function resolvePaidTaskStatus(profile: FulfillmentBindingLike | null | undefined): string {
|
||||
if (Boolean(profile?.requires_claim)) {
|
||||
return 'paid'
|
||||
}
|
||||
|
||||
if (isKuaishouCloudExecutor(profile?.executor_key)) {
|
||||
return 'pending_binding_prepare'
|
||||
}
|
||||
|
||||
if (String(profile?.executor_key || '').trim() === 'manual_dispatch') {
|
||||
return 'manual_review'
|
||||
}
|
||||
|
||||
return 'paid'
|
||||
return resolveInitialPaidTaskStatus(profile)
|
||||
}
|
||||
|
||||
function parseTaskContext(task: { context_json?: unknown } | null | undefined): TaskContext {
|
||||
|
||||
Reference in New Issue
Block a user