feat(bill,flow): add unified bill trace and flow get diagnostic engine (Phase 2)
This commit is contained in:
1 parent
7891959853
commit
ffec604ed9
10 files changed
+970
-32
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||||
import { createBillActionContext } from "./context.js";
|
||||
import { handleApprove, handleReject, handleTransfer, handleAddNode, handleComment } from "./approve.js";
|
||||
import { handleList, handleGet } from "./search.js";
|
||||
import { handleList, handleGet, handleTrace } from "./search.js";
|
||||
import { handleFlow, handleRisk, handleBudget } from "./flow.js";
|
||||
import { handleRefs, handleRelations, handleAttachments, handleDownload } from "./related.js";
|
||||
import { handleSchema, handleLink, handleSpec } from "./meta.js";
|
||||
@@ -53,6 +53,11 @@ export async function handleBillCommand(
|
||||
case "find":
|
||||
return handleGet(ctx);
|
||||
|
||||
case "trace":
|
||||
case "inspect":
|
||||
case "diagnose":
|
||||
return handleTrace(ctx);
|
||||
|
||||
case "schema":
|
||||
case "schemas":
|
||||
case "entities":
|
||||
@@ -100,6 +105,6 @@ export async function handleBillCommand(
|
||||
return handleSpec(ctx);
|
||||
|
||||
default:
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号> [--risk], spec <specificationId>, refs <单号>, relations <单号>, attachments <单号>, download <单号>, flow <单号>, risk <单号>, budget <单号>, schema, link <单号>`);
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号> [--risk], trace <单号|ID>, spec <specificationId>, refs <单号>, relations <单号>, attachments <单号>, download <单号>, flow <单号>, risk <单号>, budget <单号>, schema, link <单号>`);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { getBillByCodeOrId, getBillRisk, searchAndFormatBills } from "../../../domains/bill/tools.js";
|
||||
import { getBillByCodeOrId, getBillRisk, searchAndFormatBills, traceBillFlowDetail } from "../../../domains/bill/tools.js";
|
||||
import { booleanOption, numberOption, stringOption } from "../../parser.js";
|
||||
import { printJson, printTable } from "../../printer.js";
|
||||
import { printJson, printTable, printKeyValue } from "../../printer.js";
|
||||
import type { CliResult } from "../../types.js";
|
||||
import type { BillActionContext } from "./context.js";
|
||||
|
||||
@@ -198,3 +198,102 @@ export async function handleGet(ctx: BillActionContext): Promise<CliResult> {
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
export async function handleTrace(ctx: BillActionContext): Promise<CliResult> {
|
||||
const { parsed, runtime, corp, corpId } = ctx;
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "target", "flowId"]);
|
||||
if (!target) {
|
||||
throw new Error("请提供要穿透诊断的单号或单据/流程 ID: hose-use bill trace <单号|ID> 或 hose-use flow get <单号|ID>");
|
||||
}
|
||||
|
||||
const client = await ctx.getClient();
|
||||
const report = await traceBillFlowDetail(client, corpId, target);
|
||||
|
||||
if (!report.found) {
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(report, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 在企业 [${corp.name}] 下未找到匹配 [${target}] 的单据或审批流实例\n 诊断提示: ${report.diagnostic.advice}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(
|
||||
{
|
||||
corpId,
|
||||
corpName: corp.name,
|
||||
...report,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const approverNames = report.pendingApprovers
|
||||
.map((p) => (p.name ? `${p.name}${p.code ? ` (${p.code})` : ""}` : p.id))
|
||||
.filter(Boolean)
|
||||
.join("、");
|
||||
|
||||
const lines: string[] = [
|
||||
`====== 单据全域穿透诊断 [${report.billCode || report.target}] ======`,
|
||||
printKeyValue({
|
||||
"单据编号": report.billCode || "-",
|
||||
"单据 ID": report.billId || "-",
|
||||
"流程实例 (flowId)": report.flowId || "-",
|
||||
"单据类型": report.specificationName
|
||||
? `${report.specificationName}${report.specificationId ? ` (${report.specificationId})` : ""}`
|
||||
: "-",
|
||||
"单据标题": report.title || "-",
|
||||
"提报人员": report.submitter
|
||||
? `${report.submitter.name || "-"}${report.submitter.id ? ` (${report.submitter.id})` : ""}`
|
||||
: "-",
|
||||
"归属部门": report.department || "-",
|
||||
"提报时间": report.submitDate || "-",
|
||||
"单据金额": report.amount || "-",
|
||||
"全局流转状态": `${report.state || "-"} (${report.rawState || "-"})`,
|
||||
}),
|
||||
"",
|
||||
"── 🩺 待办与审批诊断 ──",
|
||||
` 阶段判定 : ${report.diagnostic.stageLabel}`,
|
||||
` 当前驻留节点: ${report.currentNode?.name || "-"}${report.currentNode?.id ? ` (${report.currentNode.id})` : ""}`,
|
||||
` 待办处理人: ${approverNames || "(当前无待办审批人)"}`,
|
||||
` 是否可审批: ${report.diagnostic.canApprove ? "✅ 是 (支持执行同意/驳回)" : "❌ 否 (当前不可审批)"}`,
|
||||
` 诊断结论 : ${report.diagnostic.summary}`,
|
||||
` 排障建议 : ${report.diagnostic.advice}`,
|
||||
];
|
||||
|
||||
if (report.timeline.length > 0) {
|
||||
lines.push("");
|
||||
lines.push("── ⏱️ 审批流转历史轨迹 (Timeline) ──");
|
||||
const timelineRows = report.timeline.map((item) => [
|
||||
`#${item.index}`,
|
||||
item.time || "-",
|
||||
item.nodeName || "-",
|
||||
item.operator?.name || item.operator?.id || (item.automatic ? "自动执行" : "-"),
|
||||
item.action || item.state || "-",
|
||||
item.comment ? (item.comment.length > 30 ? item.comment.slice(0, 30) + "..." : item.comment) : "-",
|
||||
]);
|
||||
lines.push(
|
||||
printTable(
|
||||
["#", "时间", "节点名称", "操作人", "动作", "审批意见"],
|
||||
timelineRows
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `${lines.join("\n")}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
import { describe, expect, it, spyOn, afterEach } from "bun:test";
|
||||
import { handleFlowCommand } from "./flow.js";
|
||||
import { handleBillCommand } from "./bill/index.js";
|
||||
import { findCommandByArea } from "../registry.js";
|
||||
import * as sessionModule from "../../core/http/session-client.js";
|
||||
import type { CliRuntime } from "../types.js";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
|
||||
const mockCorp: CorpConfig = {
|
||||
id: "corp-test-1",
|
||||
name: "测试企业",
|
||||
baseUrl: "https://app.ekuaibao.com",
|
||||
source: "openapi",
|
||||
credentials: {
|
||||
accessToken: "mock-token",
|
||||
corpId: "corp-test-1",
|
||||
staffId: "corp-test-1:STAFF_TEST",
|
||||
},
|
||||
};
|
||||
|
||||
const mockRuntime: CliRuntime = {
|
||||
corpContext: {
|
||||
listCorps: () => [mockCorp],
|
||||
getCurrent: () => mockCorp,
|
||||
switchCorp: () => true,
|
||||
resolve: () => mockCorp,
|
||||
} as any,
|
||||
selectedCorp: mockCorp,
|
||||
jsonMode: false,
|
||||
outputOptions: {},
|
||||
};
|
||||
|
||||
describe("CLI - flow & trace 全域透视与穿透诊断命令族", () => {
|
||||
let spies: ReturnType<typeof spyOn>[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const spy of spies) {
|
||||
spy.mockRestore();
|
||||
}
|
||||
spies = [];
|
||||
});
|
||||
|
||||
it("registry 正确注册 flow 命令与 trace 别名", () => {
|
||||
const flowDef = findCommandByArea("flow");
|
||||
expect(flowDef).toBeDefined();
|
||||
expect(flowDef?.area).toBe("flow");
|
||||
expect(flowDef?.aliases).toContain("trace");
|
||||
|
||||
const traceDef = findCommandByArea("trace");
|
||||
expect(traceDef).toBeDefined();
|
||||
expect(traceDef?.area).toBe("flow");
|
||||
});
|
||||
|
||||
it("未选中企业时返回错误并提示切换企业", async () => {
|
||||
const res = await handleFlowCommand(
|
||||
{
|
||||
area: "flow",
|
||||
action: "get",
|
||||
args: ["ZJ-001"],
|
||||
options: {},
|
||||
raw: ["flow", "get", "ZJ-001"],
|
||||
},
|
||||
{ ...mockRuntime, selectedCorp: null }
|
||||
);
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stderr).toContain("未选中企业");
|
||||
});
|
||||
|
||||
it("hose-use flow --help 输出帮助信息", async () => {
|
||||
const res = await handleFlowCommand(
|
||||
{
|
||||
area: "flow",
|
||||
action: undefined,
|
||||
args: [],
|
||||
options: { help: true },
|
||||
raw: ["flow", "--help"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("hose-use flow - 审批流全域透视与穿透诊断命令族");
|
||||
expect(res.stdout).toContain("hose-use flow get <单号|ID>");
|
||||
});
|
||||
|
||||
it("hose-use flow <单号> (位置参数) 自动识别并穿透诊断", async () => {
|
||||
const mockRawFlow = {
|
||||
id: "backlog-1",
|
||||
state: "APPROVING",
|
||||
flowId: {
|
||||
id: "flow-1",
|
||||
state: "APPROVING",
|
||||
form: {
|
||||
code: "ZJ-20261014-001",
|
||||
title: "转转合同付款",
|
||||
payMoney: "12500.00",
|
||||
submitterId: { id: "u1", name: "张敏" },
|
||||
expenseDepartment: { name: "财务部" },
|
||||
},
|
||||
plan: {
|
||||
nodes: [
|
||||
{ id: "n1", name: "出纳打款", state: "APPROVING", approverId: { id: "u2", name: "李出纳" } },
|
||||
],
|
||||
},
|
||||
logs: [],
|
||||
},
|
||||
};
|
||||
|
||||
const mockClient = {
|
||||
post: async () => new Response(JSON.stringify({ value: mockRawFlow })),
|
||||
} as any;
|
||||
|
||||
spies.push(spyOn(sessionModule, "createSessionClient").mockResolvedValue(mockClient));
|
||||
|
||||
const res = await handleFlowCommand(
|
||||
{
|
||||
area: "flow",
|
||||
action: "zj-20261014-001",
|
||||
args: [],
|
||||
positional: ["flow", "ZJ-20261014-001"],
|
||||
options: {},
|
||||
raw: ["flow", "ZJ-20261014-001"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("单据全域穿透诊断 [ZJ-20261014-001]");
|
||||
expect(res.stdout).toContain("提报人员");
|
||||
expect(res.stdout).toContain("张敏");
|
||||
expect(res.stdout).toContain("待办处理人: 李出纳");
|
||||
expect(res.stdout).toContain("出纳打款");
|
||||
});
|
||||
|
||||
it("hose-use flow get <单号> --json 结构化输出", async () => {
|
||||
const mockRawFlow = {
|
||||
id: "backlog-2",
|
||||
state: "PAYING",
|
||||
flowId: {
|
||||
id: "flow-2",
|
||||
state: "PAYING",
|
||||
form: {
|
||||
code: "ZJ-20261014-002",
|
||||
title: "资金调拨",
|
||||
},
|
||||
plan: {
|
||||
nodes: [
|
||||
{ id: "n1", name: "出纳打款", state: "APPROVING" },
|
||||
],
|
||||
},
|
||||
logs: [],
|
||||
},
|
||||
};
|
||||
|
||||
const mockClient = {
|
||||
post: async () => new Response(JSON.stringify({ value: mockRawFlow })),
|
||||
} as any;
|
||||
|
||||
spies.push(spyOn(sessionModule, "createSessionClient").mockResolvedValue(mockClient));
|
||||
|
||||
const res = await handleFlowCommand(
|
||||
{
|
||||
area: "flow",
|
||||
action: "get",
|
||||
args: ["ZJ-20261014-002"],
|
||||
positional: ["flow", "get", "ZJ-20261014-002"],
|
||||
options: { json: true },
|
||||
raw: ["flow", "get", "ZJ-20261014-002", "--json"],
|
||||
},
|
||||
{ ...mockRuntime, jsonMode: true }
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(0);
|
||||
const parsed = JSON.parse(res.stdout);
|
||||
expect(parsed.found).toBe(true);
|
||||
expect(parsed.billCode).toBe("ZJ-20261014-002");
|
||||
expect(parsed.diagnostic.stage).toBe("PAYING");
|
||||
expect(parsed.diagnostic.canApprove).toBe(false);
|
||||
expect(parsed.diagnostic.advice).toContain("400");
|
||||
});
|
||||
|
||||
it("hose-use bill trace <单号> 平滑集成并完成诊断", async () => {
|
||||
const mockRawFlow = {
|
||||
id: "backlog-3",
|
||||
state: "APPROVING",
|
||||
flowId: {
|
||||
id: "flow-3",
|
||||
state: "APPROVING",
|
||||
form: {
|
||||
code: "BX-999",
|
||||
title: "日常差旅报销",
|
||||
},
|
||||
plan: {
|
||||
nodes: [
|
||||
{ id: "n1", name: "总监审批", state: "APPROVING" },
|
||||
],
|
||||
},
|
||||
logs: [],
|
||||
},
|
||||
};
|
||||
|
||||
const mockClient = {
|
||||
post: async () => new Response(JSON.stringify({ value: mockRawFlow })),
|
||||
} as any;
|
||||
|
||||
spies.push(spyOn(sessionModule, "createSessionClient").mockResolvedValue(mockClient));
|
||||
|
||||
const res = await handleBillCommand(
|
||||
{
|
||||
area: "bill",
|
||||
action: "trace",
|
||||
args: ["BX-999"],
|
||||
positional: ["bill", "trace", "BX-999"],
|
||||
options: {},
|
||||
raw: ["bill", "trace", "BX-999"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("单据全域穿透诊断 [BX-999]");
|
||||
expect(res.stdout).toContain("总监审批");
|
||||
expect(res.stdout).toContain("是否可审批: ✅ 是");
|
||||
});
|
||||
|
||||
it("未找到单据时返回 exitCode 1 与排障建议", async () => {
|
||||
const mockClient = {
|
||||
post: async () => new Response(JSON.stringify({ items: [] })),
|
||||
} as any;
|
||||
|
||||
spies.push(spyOn(sessionModule, "createSessionClient").mockResolvedValue(mockClient));
|
||||
|
||||
const res = await handleFlowCommand(
|
||||
{
|
||||
area: "flow",
|
||||
action: "get",
|
||||
args: ["UNKNOWN-BILL"],
|
||||
positional: ["flow", "get", "UNKNOWN-BILL"],
|
||||
options: {},
|
||||
raw: ["flow", "get", "UNKNOWN-BILL"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stderr).toContain("[NOT FOUND]");
|
||||
expect(res.stderr).toContain("诊断提示");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,99 @@
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
import { createBillActionContext } from "./bill/context.js";
|
||||
import { handleTrace, handleList } from "./bill/search.js";
|
||||
import { handleFlow, handleRisk } from "./bill/flow.js";
|
||||
import { handleApprove, handleReject } from "./bill/approve.js";
|
||||
|
||||
function printFlowHelp(): string {
|
||||
return [
|
||||
"hose-use flow - 审批流全域透视与穿透诊断命令族",
|
||||
"",
|
||||
"用法:",
|
||||
" hose-use flow <单号|ID> 等价于 flow get/trace,快速透视单据流转与诊断待办",
|
||||
" hose-use flow get <单号|ID> 深度穿透单据全貌、当前驻留节点与 400 待办根因诊断",
|
||||
" hose-use flow trace <单号|ID> (别名) 同 flow get",
|
||||
" hose-use flow history <单号|ID> 查看审批历史流转时间线与后续节点",
|
||||
" hose-use flow risk <单号|ID> 查看费控与合规风控诊断",
|
||||
" hose-use flow list [options] 列出当前待审批/已审批单据流",
|
||||
" hose-use flow approve <单号|ID> 执行审批同意 (受 --dry-run 与 --yes 约束)",
|
||||
" hose-use flow reject <单号|ID> 执行审批驳回 (受 --dry-run 与 --yes 约束)",
|
||||
"",
|
||||
"选项:",
|
||||
" --corp=<id> 指定操作企业 (默认激活企业)",
|
||||
" --json 以 JSON 结构化格式输出诊断数据",
|
||||
" --verbose 显示完整调试信息",
|
||||
" -h, --help 显示帮助信息",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export async function handleFlowCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
if (parsed.options.help || parsed.action === "help") {
|
||||
return {
|
||||
stdout: printFlowHelp() + "\n",
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const corp = runtime.selectedCorp;
|
||||
if (!corp) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "未选中企业,请先通过 hose-use corps switch 或 --corp 指定企业\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const action = parsed.action?.toLowerCase();
|
||||
const args = [...parsed.args];
|
||||
|
||||
// 1. 无动作无参数且无 --code/--id 选项时,默认展示列表
|
||||
if (!action && args.length === 0 && !parsed.options.code && !parsed.options.id) {
|
||||
const ctx = createBillActionContext(parsed, runtime);
|
||||
return handleList(ctx);
|
||||
}
|
||||
|
||||
// 2. 识别透视与穿透类动词
|
||||
const flowTraceActions = new Set(["get", "trace", "inspect", "diagnose", "show", "find"]);
|
||||
if (action && flowTraceActions.has(action)) {
|
||||
const ctx = createBillActionContext(parsed, runtime);
|
||||
return handleTrace(ctx);
|
||||
}
|
||||
|
||||
// 3. 识别其他常用审批流动作
|
||||
const flowOtherActions = new Set(["history", "records", "timeline", "risk", "list", "search", "approve", "agree", "reject", "deny"]);
|
||||
if (action && flowOtherActions.has(action)) {
|
||||
const ctx = createBillActionContext(parsed, runtime);
|
||||
switch (action) {
|
||||
case "history":
|
||||
case "records":
|
||||
case "timeline":
|
||||
return handleFlow(ctx);
|
||||
case "risk":
|
||||
return handleRisk(ctx);
|
||||
case "list":
|
||||
case "search":
|
||||
return handleList(ctx);
|
||||
case "approve":
|
||||
case "agree":
|
||||
return handleApprove(ctx);
|
||||
case "reject":
|
||||
case "deny":
|
||||
return handleReject(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 用户直接输入单号:hose-use flow <单号|ID>
|
||||
// 从原始 positional[1] 获取未经 lowercase 转换的原始单号/ID
|
||||
const rawTarget = parsed.positional?.[1] || parsed.action;
|
||||
const rewrittenParsed: CliParsedArgs = {
|
||||
...parsed,
|
||||
action: "trace",
|
||||
args: [rawTarget!, ...args],
|
||||
};
|
||||
const ctx = createBillActionContext(rewrittenParsed, runtime);
|
||||
return handleTrace(ctx);
|
||||
}
|
||||
Reference in new issue
Block a user