From 541b458040f7ea796e5d66bc47febc3503aa2193 Mon Sep 17 00:00:00 2001 From: yml2213 Date: Wed, 5 Aug 2026 20:20:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20affiliate-dash=20=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=88=90=E5=8A=9F(delivered)=E5=90=8E=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E9=A1=B5=E6=95=B0=E7=A7=92=E5=90=8E=E6=89=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - syncAffiliateDashTaskStatus 新增 snapshotOnly:submit 拿到 delivered 后直接用本地快照收敛任务为 REDEEMED,不等平台回调(此前要等 delivered notify 落地,约 9 秒) - 新增终态保护:已 REDEEMED/MANUAL_REVIEW/CLOSED/FAILED 的任务,非 delivered 状态不再降级(轮询时平台详情短暂返回 paid/delivering 会把成功任务打回 REDEEMING) - submitAffiliateDashClaim:result.status=delivered 时重新读任务并 snapshotOnly 收敛 --- .../claim/kuaishou-cloud-claim-service.ts | 11 +++++++++- .../fulfillment/affiliate-dash/index.ts | 21 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/apps/backend/src/services/claim/kuaishou-cloud-claim-service.ts b/apps/backend/src/services/claim/kuaishou-cloud-claim-service.ts index 2e0334d7..295c2231 100644 --- a/apps/backend/src/services/claim/kuaishou-cloud-claim-service.ts +++ b/apps/backend/src/services/claim/kuaishou-cloud-claim-service.ts @@ -1,5 +1,5 @@ import { createTaskEvent } from '../../repositories/task-event-repo.js' -import { updateTask } from '../../repositories/task-repo.js' +import { getTaskById, updateTask } from '../../repositories/task-repo.js' import { createHttpError } from '../../utils/http.js' import { parseTaskContext as parseTaskContextValue } from '../../utils/task-json.js' import { nowIso } from '../../utils/time.js' @@ -456,6 +456,15 @@ export async function submitAffiliateDashClaim( now, ) + // 平台已返回 delivered:立即用本地快照收敛任务为成功,不等平台回调(避免结果页等数秒)。 + // delivered 回调到达时 sync 幂等短路(已 REDEEMED 且 delivered 仍会刷新,无副作用)。 + if (result.status === 'delivered') { + const refreshedTask = await getTaskById(task.id) + if (refreshedTask) { + await syncAffiliateDashTaskStatus(refreshedTask, { snapshotOnly: true }) + } + } + return getKuaishouCloudClaimDetail(token) } diff --git a/apps/backend/src/services/fulfillment/affiliate-dash/index.ts b/apps/backend/src/services/fulfillment/affiliate-dash/index.ts index d0a4b90b..501980ce 100644 --- a/apps/backend/src/services/fulfillment/affiliate-dash/index.ts +++ b/apps/backend/src/services/fulfillment/affiliate-dash/index.ts @@ -154,7 +154,10 @@ export async function prepareAffiliateDashTask(task: TaskRow) { * delivered → 核销行业电子凭证 → redeemed / manual_review; * ship_failed → retry_pending;cancelled → closed。 */ -export async function syncAffiliateDashTaskStatus(task: TaskRow) { +export async function syncAffiliateDashTaskStatus( + task: TaskRow, + options: { snapshotOnly?: boolean } = {}, +) { if (!isAffiliateDashTask(task)) { return task } @@ -166,8 +169,10 @@ export async function syncAffiliateDashTaskStatus(task: TaskRow) { } const now = nowIso() - // dev-mock 任务:不请求 affiliate-dash 平台,直接用本地快照(flow 自身)收敛状态 - const order = flow.mock?.enabled + // dev-mock 任务或 snapshotOnly(如 submit 已拿到 delivered)不请求 affiliate-dash 平台, + // 直接用本地快照(flow 自身)收敛状态。 + const useSnapshot = Boolean(flow.mock?.enabled) || options.snapshotOnly + const order = useSnapshot ? ({ orderNo: flow.orderNo, orderStatus: flow.orderStatus, @@ -249,6 +254,16 @@ export async function syncAffiliateDashTaskStatus(task: TaskRow) { break } + const isAlreadyTerminal = ( + [TASK_STATUS.REDEEMED, TASK_STATUS.MANUAL_REVIEW, TASK_STATUS.CLOSED, TASK_STATUS.FAILED] as string[] + ).includes(task.task_status) + // 终态保护:已收敛成功的任务,非 delivered 状态不允许降级(如轮询时平台详情短暂返回 + // delivering/paid 会把 REDEEMED 打回 REDEEMING,导致结果页闪烁/倒退)。 + const shouldApply = !isAlreadyTerminal || order.orderStatus === 'delivered' + if (!shouldApply) { + return task + } + const updatedTask = await updateTask(task.id, { task_status: nextTaskStatus, delivery_status: deliveryStatus,