diff --git a/src/domains/hose/flow.test.ts b/src/domains/hose/flow.test.ts index 08a3295..3143037 100644 --- a/src/domains/hose/flow.test.ts +++ b/src/domains/hose/flow.test.ts @@ -18,7 +18,9 @@ import { getHoseFlow, getHoseOpenFlow, getHoseCloseFlow, - alignApprovalRunsWithHoseFlow + alignApprovalRunsWithHoseFlow, + formatAiDecision, + formatHumanOpinionLines } from "./flow.js"; import { searchCloseBacklogs, @@ -910,6 +912,93 @@ describe("Hose Flow Domain", () => { expect(r2.eHumanComment).toBe("上级主管(张舒):当前审核节点"); expect(r2.auditDrift).toBe(false); }); + + test("handles completed paid flow: resolves through, deduplicates opinions, omits cc, and clears pending", () => { + const runs = [ + { + id: "run-paid", + sourceDocumentSN: "B26002324", + taskMode: "assisted", + createdAt: 10000, + status: "completed", + manualApprovalStatus: "APPROVED", + 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: 12000, action: "审批同意", rawAction: "freeflow.agree", nodeName: "财务助理", operator: { name: "熊雪倩" } }, + { time: 13000, action: "自动同意", rawAction: "freeflow.autoAgree", nodeName: "部门主管", operator: { name: "张舒" } }, + { time: 14000, action: "发表评论", rawAction: "freeflow.comment", comment: "ETA2026年家庭日费用报销", operator: { name: "高思琦" } }, + { time: 14500, action: "发表评论", rawAction: "freeflow.comment", comment: "ETA2026年家庭日费用报销", operator: { name: "高思琦" } }, + { time: 15000, action: "支付完成", rawAction: "freeflow.pay", operator: { name: "高思琦" } }, + { time: 16000, action: "抄送通知", rawAction: "freeflow.carbonCopy", operator: { name: "高思琦" } } + ], + pending: [] + } as any; + + const aligned = alignApprovalRunsWithHoseFlow(runs, flow); + const r = aligned.get("run-paid")!; + expect(r.aiDecision).toBe("备注通过"); + expect(r.eHumanAction).toBe("通过"); + expect(r.auditDrift).toBe(false); + + // Verify opinions: + // 1. Agree nodes present + expect(r.eHumanComment).toContain("上级主管(张舒): 同意"); + expect(r.eHumanComment).toContain("财务助理(熊雪倩): 同意"); + expect(r.eHumanComment).toContain("部门主管(张舒): 自动同意"); + // 2. Duplicate comment deduplicated (appears only once) + const matches = r.eHumanComment.match(/高思琦: ETA2026年家庭日费用报销/g); + expect(matches?.length).toBe(1); + // 3. freeflow.pay mapped to 支付完成 + expect(r.eHumanComment).toContain("高思琦: 支付完成"); + // 4. freeflow.carbonCopy omitted + expect(r.eHumanComment).not.toContain("freeflow.carbonCopy"); + expect(r.eHumanComment).not.toContain("抄送通知"); + // 5. No pending node appended + expect(r.eHumanComment).not.toContain("当前审核节点"); + }); + }); + + describe("Pending and end-state handling", () => { + test("parseHoseOpenPending filters out CANCELED / PROCESSED nodes even with logId -1", () => { + const allLogs = [ + { id: "1", logId: -1, state: "CANCELED", nodeName: "已取消节点", ownerId: "user-1" }, + { id: "2", logId: -1, state: "APPROVING", nodeName: "当前待审节点", ownerId: "user-2" } + ] as any[]; + const pending = parseHoseOpenPending(allLogs); + expect(pending.length).toBe(1); + expect(pending[0]?.name).toBe("当前待审节点"); + }); + + test("parseHoseOpenComposite clears pending nodes when flow is paid or completed", () => { + const composite = { + flowDetails: { + id: "flow-1", + state: "paid", + logs: [] + }, + allLogs: [ + { id: "1", logId: -1, state: "APPROVING", nodeName: "残留待审节点", ownerId: "user-1" } + ] + } as any; + const detail = parseHoseOpenComposite(composite); + expect(detail?.state).toBe("已支付"); + expect(detail?.pending).toEqual([]); + }); + + test("formatAiDecision maps codes to Chinese labels", () => { + expect(formatAiDecision("EXEMPT_APPROVE")).toBe("备注通过"); + expect(formatAiDecision("ERROR")).toBe("错误"); + expect(formatAiDecision("APPROVE")).toBe("通过"); + expect(formatAiDecision("REJECT")).toBe("驳回"); + expect(formatAiDecision("REVIEW")).toBe("转交复核"); + }); }); }); - diff --git a/src/domains/hose/flow.ts b/src/domains/hose/flow.ts index 751dcf1..86630b2 100644 --- a/src/domains/hose/flow.ts +++ b/src/domains/hose/flow.ts @@ -250,13 +250,24 @@ export function parseHoseOpenPending( allLogs: HoseOpenAllLogsItem[], staffMap?: Map ): HoseFlowPendingNode[] { - const pendingItems = allLogs.filter( - (item) => + const pendingItems = allLogs.filter((item) => { + const stateUpper = String(item.state || "").toUpperCase(); + if ( + stateUpper === "CANCELED" || + stateUpper === "CANCELLED" || + stateUpper === "PROCESSED" || + stateUpper === "COMPLETED" || + stateUpper === "PAID" + ) { + return false; + } + return ( item.logId === -1 || - item.state === "APPROVING" || - item.state === "PROCESSING" || - item.state === "PAYING" - ); + stateUpper === "APPROVING" || + stateUpper === "PROCESSING" || + stateUpper === "PAYING" + ); + }); return pendingItems.map((item, index) => { let approver = extractFlowPerson(item.ownerId); @@ -450,7 +461,19 @@ export function parseHoseOpenComposite( const form = (flowDetails.form || {}) as Record; const history = parseHoseTimeline(flowDetails.logs || [], staffMap); - const pending = parseHoseOpenPending(allLogs, staffMap); + const rawState = flowDetails.state; + const state = translateFlowState(rawState); + const rawStateUpper = String(rawState || "").toUpperCase(); + const isFlowInEndState = [ + "COMPLETED", + "APPROVED", + "PAID", + "REVOKED", + "CANCELED", + "CANCELLED", + "REJECTED" + ].includes(rawStateUpper); + const pending = isFlowInEndState ? [] : parseHoseOpenPending(allLogs, staffMap); const currentNode = pending.find((p) => p.isCurrent)?.name; const metrics = calculateFlowMetrics( @@ -462,9 +485,6 @@ export function parseHoseOpenComposite( const staffOpinions = extractStaffOpinions(history); const rejections = extractRejections(history); - const rawState = flowDetails.state; - const state = translateFlowState(rawState); - let specification: { id?: string; name?: string; code?: string } | undefined; if (form.specificationId && typeof form.specificationId === "object") { const specObj = form.specificationId as Record; @@ -542,11 +562,6 @@ export function parseHoseFlow( ? nodeState.nodeId : undefined; - const pending = parseHosePending(plan, currentNode, currentNodeId); - const metrics = calculateFlowMetrics(flow, history, raw); - const staffOpinions = extractStaffOpinions(history); - const rejections = extractRejections(history); - const rawState = typeof raw.state === "string" ? raw.state @@ -554,6 +569,20 @@ export function parseHoseFlow( ? flow.state : undefined; const state = translateFlowState(rawState); + const rawStateUpper = String(rawState || "").toUpperCase(); + const isFlowInEndState = [ + "COMPLETED", + "APPROVED", + "PAID", + "REVOKED", + "CANCELED", + "CANCELLED", + "REJECTED" + ].includes(rawStateUpper); + const pending = isFlowInEndState ? [] : parseHosePending(plan, currentNode, currentNodeId); + const metrics = calculateFlowMetrics(flow, history, raw); + const staffOpinions = extractStaffOpinions(history); + const rejections = extractRejections(history); const lastAttributes = history.at(-1)?.nodeName ?? @@ -648,6 +677,60 @@ export async function getHoseFlow( return getHoseOpenFlow(client, flowId, staffCache); } +export function formatAiDecision(decision?: string): string { + if (!decision) return "-"; + const norm = decision.toUpperCase(); + if (norm === "APPROVE" || norm === "通过") return "通过"; + if (norm === "EXEMPT_APPROVE" || norm === "免审通过" || norm === "备注通过") return "备注通过"; + if (norm === "REVIEW" || norm === "转交复核") return "转交复核"; + if (norm === "REJECT" || norm === "驳回") return "驳回"; + if (norm === "ERROR" || norm === "错误") return "错误"; + return decision; +} + +export function formatHumanOpinionLines(events: HoseFlowTimelineEntry[]): string[] { + const lines: string[] = []; + for (const ev of events) { + if ( + (ev.rawAction === "freeflow.carbonCopy" || + ev.rawAction === "freeflow.cc" || + ev.rawAction === "freeflow.print" || + ev.action === "打印提醒" || + ev.action === "抄送通知") && + !ev.comment + ) { + continue; + } + + const opName = ev.operator?.name || ev.operator?.id || "未知"; + const nodePrefix = ev.nodeName?.trim() ? `${ev.nodeName.trim()}(${opName})` : opName; + let comment = ev.comment; + if (!comment) { + if (ev.rawAction === "freeflow.agree" || ev.action === "审批同意") { + comment = "同意"; + } else if (ev.rawAction === "freeflow.autoAgree") { + comment = "自动同意"; + } else if (ev.rawAction === "freeflow.pay" || ev.action === "支付完成") { + comment = "支付完成"; + } else if (ev.rawAction === "freeflow.paying" || ev.action === "支付中") { + comment = "支付中"; + } else if (ev.rawAction === "freeflow.reject" || ev.action === "审批驳回") { + comment = "驳回"; + } else if (ev.rawAction === "freeflow.retract" || ev.action === "单据撤回") { + comment = "单据撤回"; + } else { + comment = ev.action || "-"; + } + } + + const line = `${nodePrefix}: ${comment}`; + if (lines.length === 0 || lines[lines.length - 1] !== line) { + lines.push(line); + } + } + return lines; +} + export function alignApprovalRunsWithHoseFlow( runs: ApprovalRun[], flow?: HoseFlowDetail @@ -663,7 +746,7 @@ export function alignApprovalRunsWithHoseFlow( recordId: run.id, sourceDocumentSN: run.sourceDocumentSN, taskMode: run.taskMode, - aiDecision: typeof aiDecision === "string" ? aiDecision : "-", + aiDecision: formatAiDecision(typeof aiDecision === "string" ? aiDecision : "-"), aiAction: "-", eHumanAction: "-", eHumanComment: "-", @@ -678,11 +761,17 @@ export function alignApprovalRunsWithHoseFlow( ); const history = flow.history || []; + const rawStateUpper = String(flow.rawState || "").toUpperCase(); + const stateUpper = String(flow.state || "").toUpperCase(); const isFlowCompleted = - flow.rawState === "COMPLETED" || - flow.rawState === "APPROVED" || - flow.state === "已完成" || - flow.state === "审批通过"; + rawStateUpper === "COMPLETED" || + rawStateUpper === "APPROVED" || + rawStateUpper === "PAID" || + rawStateUpper === "PAYING" || + stateUpper === "已完成" || + stateUpper === "审批通过" || + stateUpper === "已支付" || + stateUpper === "支付中"; for (let i = 0; i < sortedRuns.length; i++) { const run = sortedRuns[i]; @@ -728,6 +817,12 @@ export function alignApprovalRunsWithHoseFlow( normManual === "已取消" || normManual === "已撤回" || normManual === "REVOKED"; + const isRunApprovedInSeal = + normManual === "APPROVED" || + normManual === "已支付/审批完成" || + normManual === "已通过" || + normManual === "已完成" || + normManual === "已支付"; // 5-second clock skew tolerance window [runTime - 5000, nextRunTime) const windowEvents = history.filter((e) => { @@ -769,9 +864,9 @@ export function alignApprovalRunsWithHoseFlow( !e.isEbot && e.rawAction !== "freeflow.submit" && e.action !== "提交送审" && - e.rawAction !== "freeflow.print" && - e.action !== "打印提醒" && - e.rawAction !== "freeflow.comment" + !((e.rawAction === "freeflow.comment" || e.action === "发表评论") && !e.comment) && + !((e.rawAction === "freeflow.print" || e.action === "打印提醒") && !e.comment) && + !(e.rawAction === "freeflow.carbonCopy" || e.rawAction === "freeflow.cc" || e.action === "抄送通知") ); const retractEvent = subsequentHumanEvents.find( @@ -782,19 +877,19 @@ export function alignApprovalRunsWithHoseFlow( ); const isFlowRetractedOrCancelled = - flow.rawState === "DRAFT" || - flow.rawState === "REVOKED" || - flow.rawState === "CANCELED" || - flow.rawState === "CANCELLED" || - flow.state === "草稿" || - flow.state === "已撤回" || - flow.state === "已取消"; + rawStateUpper === "DRAFT" || + rawStateUpper === "REVOKED" || + rawStateUpper === "CANCELED" || + rawStateUpper === "CANCELLED" || + stateUpper === "草稿" || + stateUpper === "已撤回" || + stateUpper === "已取消"; const isFlowRejected = - flow.rawState === "REJECTED" || - flow.rawState === "TERMINATED" || - flow.state === "审批驳回" || - flow.state === "已驳回"; + rawStateUpper === "REJECTED" || + rawStateUpper === "TERMINATED" || + stateUpper === "审批驳回" || + stateUpper === "已驳回"; if (!isLatestRun) { // Historical run that has been superseded by a newer run @@ -810,23 +905,15 @@ export function alignApprovalRunsWithHoseFlow( eHumanComment = rejectEvent.comment || "驳回"; } else { eHumanAction = "已修改重提"; - const opinionLines: string[] = []; + const opinionLines = formatHumanOpinionLines(subsequentHumanEvents); 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 (!operator && ev.operator) operator = ev.operator; } eHumanComment = opinionLines.length > 0 ? opinionLines.join("\n") : "-"; } } else { // Latest run for this document - if (isFlowCompleted) { + if (isFlowCompleted || isRunApprovedInSeal) { const hasRetract = subsequentHumanEvents.some( (e) => e.rawAction === "freeflow.retract" || e.action === "单据撤回" ); @@ -845,17 +932,9 @@ export function alignApprovalRunsWithHoseFlow( eHumanComment = rejectEvent.comment || "驳回"; operator = rejectEvent.operator; } else { - const opinionLines: string[] = []; + const opinionLines = formatHumanOpinionLines(subsequentHumanEvents); 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 (!operator && ev.operator) operator = ev.operator; } if (opinionLines.length > 0) { @@ -866,7 +945,7 @@ export function alignApprovalRunsWithHoseFlow( .find( (e) => !e.isEbot && - (e.rawAction === "freeflow.agree" || e.action === "审批同意") + (e.rawAction === "freeflow.agree" || e.action === "审批同意" || e.rawAction === "freeflow.pay") ); eHumanComment = lastHumanApproval?.comment || "-"; if (!operator) operator = lastHumanApproval?.operator; @@ -875,17 +954,9 @@ export function alignApprovalRunsWithHoseFlow( } else if (isFlowRetractedOrCancelled || (retractEvent && !hasPostAiResubmit) || isRunCancelledInSeal) { eHumanAction = "已撤回"; operator = retractEvent?.operator; - const opinionLines: string[] = []; + const opinionLines = formatHumanOpinionLines(subsequentHumanEvents); 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 (!operator && ev.operator) operator = ev.operator; } if (retractEvent && opinionLines.length === 0) { opinionLines.push(`${retractEvent.operator?.name || "提交人"}: 单据撤回`); @@ -900,17 +971,9 @@ export function alignApprovalRunsWithHoseFlow( // Flow is still actively approving eHumanAction = auditDrift ? "审核中 (⚠️ AI通过后被修改重提)" : "审核中"; - const opinionLines: string[] = []; + const opinionLines = formatHumanOpinionLines(subsequentHumanEvents); 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 (!operator && ev.operator) operator = ev.operator; } const pendingNodes = flow.pending || []; @@ -934,7 +997,7 @@ export function alignApprovalRunsWithHoseFlow( recordId: run.id, sourceDocumentSN: run.sourceDocumentSN, taskMode: run.taskMode, - aiDecision: aiDecisionStr, + aiDecision: formatAiDecision(aiDecisionStr), aiAction, eHumanAction, eHumanComment, diff --git a/src/domains/hose/types.ts b/src/domains/hose/types.ts index f0e5923..0d06f1c 100644 --- a/src/domains/hose/types.ts +++ b/src/domains/hose/types.ts @@ -8,7 +8,11 @@ export const HOSE_FLOW_ACTION_LABELS: Record = { "freeflow.print": "打印单据", "freeflow.retract": "单据撤回", "freeflow.addnode": "加签/转交", - "freeflow.comment": "发表评论" + "freeflow.comment": "发表评论", + "freeflow.pay": "支付完成", + "freeflow.paying": "支付中", + "freeflow.carbonCopy": "抄送通知", + "freeflow.cc": "抄送通知" }; export const HOSE_FLOW_STATE_LABELS: Record = { @@ -23,7 +27,9 @@ export const HOSE_FLOW_STATE_LABELS: Record = { PENDING: "待审批", WAITING: "待处理", COMPLETED: "已完成", - DRAFT: "草稿" + DRAFT: "草稿", + PAID: "已支付", + PAYING: "支付中" }; export const FlowPersonSchema = z.object({