diff --git a/src/domains/hose/flow.test.ts b/src/domains/hose/flow.test.ts index 9c11976..08a3295 100644 --- a/src/domains/hose/flow.test.ts +++ b/src/domains/hose/flow.test.ts @@ -855,6 +855,61 @@ describe("Hose Flow Domain", () => { expect(r1.auditDrift).toBe(true); expect(r1.auditDriftReason).toContain("AI通过后单据发生撤回/修改重提"); }); + + test("handles multi-round retraction and resubmission: retracted run vs active approving run", () => { + const runs = [ + { + id: "run-retracted", + sourceDocumentSN: "B26002390", + taskMode: "assisted", + createdAt: 10000, + status: "failed", + manualApprovalStatus: "CANCELLED", + result: { decision: "ERROR" } + }, + { + id: "run-active", + sourceDocumentSN: "B26002390", + taskMode: "assisted", + createdAt: 20000, + status: "completed", + result: { decision: "REJECT" } + } + ] as any[]; + + const flow = { + state: "审批中", + rawState: "APPROVING", + currentNode: "上级主管", + history: [ + { time: 8000, action: "提交送审", rawAction: "freeflow.submit", operator: { name: "刘佳琼" } }, + { time: 9000, action: "审批同意", rawAction: "freeflow.agree", isEbot: true, operator: { name: "Ebot" } }, + { time: 15000, action: "单据撤回", rawAction: "freeflow.retract", operator: { name: "刘佳琼" } }, + { time: 18000, action: "提交送审", rawAction: "freeflow.submit", operator: { name: "刘佳琼" } }, + { time: 22000, action: "审批同意", rawAction: "freeflow.agree", isEbot: true, operator: { name: "Ebot" } } + ], + pending: [ + { + id: "node-mgr", + name: "上级主管", + isCurrent: true, + approver: { name: "张舒" } + } + ] + } as any; + + const aligned = alignApprovalRunsWithHoseFlow(runs, flow); + + const r1 = aligned.get("run-retracted")!; + expect(r1.eHumanAction).toBe("已撤回"); + expect(r1.eHumanComment).toBe("刘佳琼: 单据撤回"); + expect(r1.auditDrift).toBe(false); + + const r2 = aligned.get("run-active")!; + expect(r2.eHumanAction).toBe("审核中"); + expect(r2.eHumanComment).toBe("上级主管(张舒):当前审核节点"); + expect(r2.auditDrift).toBe(false); + }); }); }); diff --git a/src/domains/hose/flow.ts b/src/domains/hose/flow.ts index e962802..d14251d 100644 --- a/src/domains/hose/flow.ts +++ b/src/domains/hose/flow.ts @@ -712,6 +712,19 @@ export function alignApprovalRunsWithHoseFlow( else if (isReject) aiAction = isAutomatic ? "驳回" : "添加评论"; else if (normDecision === "REVIEW" || normDecision === "转交复核") aiAction = "转交复核"; + const doc = (run.document as Record | undefined) || {}; + const rawManual = + (doc.manualApprovalStatus as string | undefined) || + (res.manualApprovalStatus as string | undefined) || + ((run as Record).manualApprovalStatus as string | undefined); + const normManual = typeof rawManual === "string" ? rawManual.toUpperCase() : ""; + const isRunCancelledInSeal = + normManual === "CANCELLED" || + normManual === "CANCELED" || + normManual === "已取消" || + normManual === "已撤回" || + normManual === "REVOKED"; + // 5-second clock skew tolerance window [runTime - 5000, nextRunTime) const windowEvents = history.filter((e) => { const t = e.time ?? 0; @@ -725,6 +738,7 @@ export function alignApprovalRunsWithHoseFlow( let operator: FlowPerson | undefined; // Check for Post-AI Mutation / Audit Drift + const isAiApproved = isApprove || normDecision === "EXEMPT_APPROVE" || normDecision === "免审通过"; const postAiEvents = history.filter((e) => (e.time ?? 0) > runTime + 5000); const hasPostAiRetract = postAiEvents.some( (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" @@ -733,7 +747,7 @@ export function alignApprovalRunsWithHoseFlow( (e) => e.rawAction === "freeflow.submit" || e.action === "提交送审" ); - if (isLatestRun && (hasPostAiRetract || hasPostAiResubmit)) { + if (isAiApproved && isLatestRun && (hasPostAiRetract || hasPostAiResubmit)) { auditDrift = true; auditDriftReason = "AI通过后单据发生撤回/修改重提,后续未经AI复审"; } @@ -744,7 +758,7 @@ export function alignApprovalRunsWithHoseFlow( eHumanAction = "无操作"; eHumanComment = "-"; } else { - // Subsequent human events after current AI run + // Subsequent human events after current AI run in this run's window const subsequentHumanEvents = windowEvents.filter( (e) => (e.time ?? 0) >= runTime - 1000 && @@ -756,61 +770,132 @@ export function alignApprovalRunsWithHoseFlow( e.rawAction !== "freeflow.comment" ); + const retractEvent = subsequentHumanEvents.find( + (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" + ); const rejectEvent = subsequentHumanEvents.find( (e) => e.rawAction === "freeflow.reject" || e.action === "审批驳回" ); - const pendingNodes = flow.pending || []; - const currentPending = pendingNodes.find((p) => p.isCurrent) || pendingNodes[0]; + const isFlowRetractedOrCancelled = + flow.rawState === "DRAFT" || + flow.rawState === "REVOKED" || + flow.rawState === "CANCELED" || + flow.rawState === "CANCELLED" || + flow.state === "草稿" || + flow.state === "已撤回" || + flow.state === "已取消"; - if (!isFlowCompleted) { - // Flow is still approving / in progress - eHumanAction = auditDrift ? "审核中 (⚠️ AI通过后被修改重提)" : "审核中"; + const isFlowRejected = + flow.rawState === "REJECTED" || + flow.rawState === "TERMINATED" || + flow.state === "审批驳回" || + flow.state === "已驳回"; - const opinionLines: string[] = []; - for (const ev of subsequentHumanEvents) { - const opName = ev.operator?.name || ev.operator?.id || "未知"; - const nodePrefix = ev.nodeName?.trim() ? `${ev.nodeName.trim()}(${opName})` : opName; - const comment = - ev.comment || - (ev.rawAction === "freeflow.agree" || ev.action === "审批同意" - ? "同意" - : ev.action || "-"); - opinionLines.push(`${nodePrefix}: ${comment}`); - if (!operator) operator = ev.operator; - } - - if (currentPending) { - const pApprover = - currentPending.approver?.name || currentPending.approver?.id || "待定"; - const pNodeName = (currentPending.name || flow.currentNode || "").trim(); - const pPrefix = pNodeName ? `${pNodeName}(${pApprover})` : pApprover; - opinionLines.push(`${pPrefix}:当前审核节点`); - } else if (flow.currentNode?.trim()) { - opinionLines.push(`${flow.currentNode.trim()}:当前审核节点`); - } - - eHumanComment = opinionLines.length > 0 ? opinionLines.join("\n") : "当前审核节点"; - } else { - // Flow is completed - const hasRetract = subsequentHumanEvents.some( - (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" - ); - - if (auditDrift) { - eHumanAction = "通过 (⚠️ AI通过后被中途修改)"; + if (!isLatestRun) { + // Historical run that has been superseded by a newer run + if (retractEvent || isRunCancelledInSeal) { + eHumanAction = "已撤回"; + operator = retractEvent?.operator; + eHumanComment = + retractEvent?.comment || + (retractEvent ? `${retractEvent.operator?.name || "提交人"}: 单据撤回` : "单据已撤回/取消"); } else if (rejectEvent) { eHumanAction = "驳回"; - } else if (hasRetract) { - eHumanAction = "已撤回"; - } else { - eHumanAction = "通过"; - } - - if (rejectEvent) { - eHumanComment = rejectEvent.comment || "驳回"; operator = rejectEvent.operator; + eHumanComment = rejectEvent.comment || "驳回"; } else { + eHumanAction = "已修改重提"; + const opinionLines: string[] = []; + for (const ev of subsequentHumanEvents) { + const opName = ev.operator?.name || ev.operator?.id || "未知"; + const nodePrefix = ev.nodeName?.trim() ? `${ev.nodeName.trim()}(${opName})` : opName; + const comment = + ev.comment || + (ev.rawAction === "freeflow.agree" || ev.action === "审批同意" + ? "同意" + : ev.action || "-"); + opinionLines.push(`${nodePrefix}: ${comment}`); + if (!operator) operator = ev.operator; + } + eHumanComment = opinionLines.length > 0 ? opinionLines.join("\n") : "-"; + } + } else { + // Latest run for this document + if (isFlowCompleted) { + const hasRetract = subsequentHumanEvents.some( + (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" + ); + + if (auditDrift) { + eHumanAction = "通过 (⚠️ AI通过后被中途修改)"; + } else if (rejectEvent) { + eHumanAction = "驳回"; + } else if (hasRetract) { + eHumanAction = "已撤回"; + } else { + eHumanAction = "通过"; + } + + if (rejectEvent) { + eHumanComment = rejectEvent.comment || "驳回"; + operator = rejectEvent.operator; + } else { + const opinionLines: string[] = []; + for (const ev of subsequentHumanEvents) { + const opName = ev.operator?.name || ev.operator?.id || "未知"; + const nodePrefix = ev.nodeName?.trim() ? `${ev.nodeName.trim()}(${opName})` : opName; + const comment = + ev.comment || + (ev.rawAction === "freeflow.agree" || ev.action === "审批同意" + ? "同意" + : ev.action || "-"); + opinionLines.push(`${nodePrefix}: ${comment}`); + if (!operator) operator = ev.operator; + } + + if (opinionLines.length > 0) { + eHumanComment = opinionLines.join("\n"); + } else { + const lastHumanApproval = [...windowEvents] + .reverse() + .find( + (e) => + !e.isEbot && + (e.rawAction === "freeflow.agree" || e.action === "审批同意") + ); + eHumanComment = lastHumanApproval?.comment || "-"; + if (!operator) operator = lastHumanApproval?.operator; + } + } + } else if (isFlowRetractedOrCancelled || (retractEvent && !hasPostAiResubmit) || isRunCancelledInSeal) { + eHumanAction = "已撤回"; + operator = retractEvent?.operator; + const opinionLines: string[] = []; + for (const ev of subsequentHumanEvents) { + const opName = ev.operator?.name || ev.operator?.id || "未知"; + const nodePrefix = ev.nodeName?.trim() ? `${ev.nodeName.trim()}(${opName})` : opName; + const comment = + ev.comment || + (ev.rawAction === "freeflow.agree" || ev.action === "审批同意" + ? "同意" + : ev.action || "-"); + opinionLines.push(`${nodePrefix}: ${comment}`); + if (!operator) operator = ev.operator; + } + if (retractEvent && opinionLines.length === 0) { + opinionLines.push(`${retractEvent.operator?.name || "提交人"}: 单据撤回`); + } + eHumanComment = opinionLines.length > 0 ? opinionLines.join("\n") : "单据已撤回"; + } else if (isFlowRejected || rejectEvent) { + eHumanAction = "驳回"; + operator = rejectEvent?.operator; + const rej = flow.rejections?.[0]; + eHumanComment = rejectEvent?.comment || rej?.reason || rej?.nodeName || "审批驳回"; + } else { + // Flow is still actively approving + eHumanAction = auditDrift ? "审核中 (⚠️ AI通过后被修改重提)" : "审核中"; + const opinionLines: string[] = []; for (const ev of subsequentHumanEvents) { const opName = ev.operator?.name || ev.operator?.id || "未知"; @@ -824,19 +909,19 @@ export function alignApprovalRunsWithHoseFlow( if (!operator) operator = ev.operator; } - if (opinionLines.length > 0) { - eHumanComment = opinionLines.join("\n"); - } else { - const lastHumanApproval = [...windowEvents] - .reverse() - .find( - (e) => - !e.isEbot && - (e.rawAction === "freeflow.agree" || e.action === "审批同意") - ); - eHumanComment = lastHumanApproval?.comment || "-"; - if (!operator) operator = lastHumanApproval?.operator; + const pendingNodes = flow.pending || []; + const currentPending = pendingNodes.find((p) => p.isCurrent) || pendingNodes[0]; + if (currentPending) { + const pApprover = + currentPending.approver?.name || currentPending.approver?.id || "待定"; + const pNodeName = (currentPending.name || flow.currentNode || "").trim(); + const pPrefix = pNodeName ? `${pNodeName}(${pApprover})` : pApprover; + opinionLines.push(`${pPrefix}:当前审核节点`); + } else if (flow.currentNode?.trim()) { + opinionLines.push(`${flow.currentNode.trim()}:当前审核节点`); } + + eHumanComment = opinionLines.length > 0 ? opinionLines.join("\n") : "当前审核节点"; } } } diff --git a/src/domains/seal/approval-runs-export.ts b/src/domains/seal/approval-runs-export.ts index 05c804b..b595afe 100644 --- a/src/domains/seal/approval-runs-export.ts +++ b/src/domains/seal/approval-runs-export.ts @@ -818,6 +818,9 @@ export async function exportApprovalRunsToExcel( } else if (val.includes("处理中") || val.includes("审批中")) { cell.font = { name: "Microsoft YaHei", size: 10, color: { argb: "FF2563EB" } }; cell.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FFEFF6FF" } }; + } else if (val.includes("取消") || val.includes("撤回")) { + cell.font = { name: "Microsoft YaHei", size: 10, color: { argb: "FF64748B" } }; + cell.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FFF1F5F9" } }; } } @@ -840,6 +843,9 @@ export async function exportApprovalRunsToExcel( } else if (val.includes("待处理") || val.includes("处理中") || val.includes("审核中")) { cell.font = { name: "Microsoft YaHei", size: 10, color: { argb: "FF2563EB" } }; cell.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FFEFF6FF" } }; + } else if (val.includes("撤回") || val.includes("取消")) { + cell.font = { name: "Microsoft YaHei", size: 10, color: { argb: "FF64748B" } }; + cell.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FFF1F5F9" } }; } else if (val === "无操作") { cell.font = { name: "Microsoft YaHei", size: 10, color: { argb: "FF94A3B8" } }; }