diff --git a/src/domains/hose/flow.test.ts b/src/domains/hose/flow.test.ts index 3143037..3d7ca83 100644 --- a/src/domains/hose/flow.test.ts +++ b/src/domains/hose/flow.test.ts @@ -964,6 +964,45 @@ describe("Hose Flow Domain", () => { // 5. No pending node appended expect(r.eHumanComment).not.toContain("当前审核节点"); }); + + test("handles AI approved run rejected by human operator before resubmission and payment: resolves to 驳回", () => { + const runs = [ + { + id: "run-rejected-then-paid", + sourceDocumentSN: "B26002305", + taskMode: "assisted", + createdAt: 10000, + status: "completed", + manualApprovalRecord: { + status: { type: "TERMINATED", label: "被驳回" }, + rejectComment: "修改附件" + }, + result: { decision: "EXEMPT_APPROVE" } + } + ] as any[]; + + const flow = { + state: "已支付", + rawState: "paid", + history: [ + { time: 8000, action: "提交送审", rawAction: "freeflow.submit", operator: { name: "张晶丰" } }, + { time: 11000, action: "审批同意", rawAction: "freeflow.agree", nodeName: "上级主管", operator: { name: "刘苏宁" } }, + { time: 15000, action: "审批驳回", rawAction: "freeflow.reject", nodeName: "提交人", comment: "修改附件", operator: { name: "熊雪倩" } }, + { time: 20000, action: "提交送审", rawAction: "freeflow.submit", operator: { name: "张晶丰" } }, + { time: 25000, action: "审批同意", rawAction: "freeflow.agree", operator: { name: "熊雪倩" } }, + { time: 30000, action: "支付完成", rawAction: "freeflow.pay", operator: { name: "高思琦" } } + ], + pending: [] + } as any; + + const aligned = alignApprovalRunsWithHoseFlow(runs, flow); + const r = aligned.get("run-rejected-then-paid")!; + expect(r.aiDecision).toBe("备注通过"); + expect(r.eHumanAction).toBe("驳回"); + expect(r.eHumanComment).toBe("修改附件"); + expect(r.auditDrift).toBe(false); + expect(r.operator?.name).toBe("熊雪倩"); + }); }); describe("Pending and end-state handling", () => { diff --git a/src/domains/hose/flow.ts b/src/domains/hose/flow.ts index 86630b2..cef2b38 100644 --- a/src/domains/hose/flow.ts +++ b/src/domains/hose/flow.ts @@ -806,6 +806,15 @@ export function alignApprovalRunsWithHoseFlow( else if (normDecision === "REVIEW" || normDecision === "转交复核") aiAction = "转交复核"; const doc = (run.document as Record | undefined) || {}; + const mar = + ((run as Record).manualApprovalRecord as Record | undefined) || + (res.manualApprovalRecord as Record | undefined) || + ((doc as Record).manualApprovalRecord as Record | undefined) || + {}; + const marStatus = + typeof mar.status === "object" && mar.status !== null + ? String((mar.status as Record).type || (mar.status as Record).label || "").toUpperCase() + : ""; const rawManual = (doc.manualApprovalStatus as string | undefined) || (res.manualApprovalStatus as string | undefined) || @@ -822,7 +831,18 @@ export function alignApprovalRunsWithHoseFlow( normManual === "已支付/审批完成" || normManual === "已通过" || normManual === "已完成" || - normManual === "已支付"; + normManual === "已支付" || + marStatus === "APPROVED"; + const isRunRejectedInSeal = + normManual === "TERMINATED" || + normManual === "REJECTED" || + normManual === "REJECT" || + normManual === "已驳回" || + normManual === "被驳回" || + marStatus === "TERMINATED" || + marStatus === "REJECTED" || + marStatus === "已驳回" || + marStatus === "被驳回"; // 5-second clock skew tolerance window [runTime - 5000, nextRunTime) const windowEvents = history.filter((e) => { @@ -893,16 +913,18 @@ export function alignApprovalRunsWithHoseFlow( if (!isLatestRun) { // Historical run that has been superseded by a newer run - if (retractEvent || isRunCancelledInSeal) { + if (rejectEvent || isRunRejectedInSeal) { + eHumanAction = "驳回"; + auditDrift = false; + auditDriftReason = undefined; + eHumanComment = rejectEvent?.comment || (mar as any)?.rejectComment || "驳回"; + operator = rejectEvent?.operator; + } else if (retractEvent || isRunCancelledInSeal) { eHumanAction = "已撤回"; operator = retractEvent?.operator; eHumanComment = retractEvent?.comment || (retractEvent ? `${retractEvent.operator?.name || "提交人"}: 单据撤回` : "单据已撤回/取消"); - } else if (rejectEvent) { - eHumanAction = "驳回"; - operator = rejectEvent.operator; - eHumanComment = rejectEvent.comment || "驳回"; } else { eHumanAction = "已修改重提"; const opinionLines = formatHumanOpinionLines(subsequentHumanEvents); @@ -913,43 +935,36 @@ export function alignApprovalRunsWithHoseFlow( } } else { // Latest run for this document - if (isFlowCompleted || isRunApprovedInSeal) { - const hasRetract = subsequentHumanEvents.some( - (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" - ); - + if (rejectEvent || isRunRejectedInSeal) { + eHumanAction = "驳回"; + auditDrift = false; + auditDriftReason = undefined; + eHumanComment = rejectEvent?.comment || (mar as any)?.rejectComment || "驳回"; + operator = rejectEvent?.operator; + } else if (isFlowCompleted || isRunApprovedInSeal) { 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 = formatHumanOpinionLines(subsequentHumanEvents); - for (const ev of subsequentHumanEvents) { - if (!operator && ev.operator) operator = ev.operator; - } + const opinionLines = formatHumanOpinionLines(subsequentHumanEvents); + for (const ev of subsequentHumanEvents) { + if (!operator && ev.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 === "审批同意" || e.rawAction === "freeflow.pay") - ); - eHumanComment = lastHumanApproval?.comment || "-"; - if (!operator) operator = lastHumanApproval?.operator; - } + if (opinionLines.length > 0) { + eHumanComment = opinionLines.join("\n"); + } else { + const lastHumanApproval = [...windowEvents] + .reverse() + .find( + (e) => + !e.isEbot && + (e.rawAction === "freeflow.agree" || e.action === "审批同意" || e.rawAction === "freeflow.pay") + ); + eHumanComment = lastHumanApproval?.comment || "-"; + if (!operator) operator = lastHumanApproval?.operator; } } else if (isFlowRetractedOrCancelled || (retractEvent && !hasPostAiResubmit) || isRunCancelledInSeal) { eHumanAction = "已撤回"; @@ -962,11 +977,11 @@ export function alignApprovalRunsWithHoseFlow( opinionLines.push(`${retractEvent.operator?.name || "提交人"}: 单据撤回`); } eHumanComment = opinionLines.length > 0 ? opinionLines.join("\n") : "单据已撤回"; - } else if (isFlowRejected || rejectEvent) { + } else if (isFlowRejected) { eHumanAction = "驳回"; - operator = rejectEvent?.operator; const rej = flow.rejections?.[0]; - eHumanComment = rejectEvent?.comment || rej?.reason || rej?.nodeName || "审批驳回"; + operator = rej?.operator; + eHumanComment = rej?.reason || rej?.nodeName || "审批驳回"; } else { // Flow is still actively approving eHumanAction = auditDrift ? "审核中 (⚠️ AI通过后被修改重提)" : "审核中";