feat: add CLI bill approval flow timeline
This commit is contained in:
1 parent
155f179fcd
commit
a8bdb67293
7 files changed
+234
-5
No files matched your search
@@ -5,6 +5,7 @@ import { createEkbClient } from "../../core/auth/ekb.js";
|
||||
import {
|
||||
searchAndFormatBills,
|
||||
getBillByCodeOrId,
|
||||
getBillFlow,
|
||||
getFieldDictionary,
|
||||
} from "../../domains/bill/tools.js";
|
||||
|
||||
@@ -195,6 +196,46 @@ export async function handleBillCommand(
|
||||
};
|
||||
}
|
||||
|
||||
case "flow":
|
||||
case "records":
|
||||
case "timeline": {
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id"]);
|
||||
if (!target) throw new Error("请提供要查询流程的单号或单据 ID: hose bill flow <单号|ID>");
|
||||
const flow = await getBillFlow(client, corpId, target);
|
||||
if (!flow) {
|
||||
if (runtime.jsonMode) return { stdout: printJson({ found: false, target }, runtime.outputOptions), stderr: "", exitCode: 1 };
|
||||
return { stdout: "", stderr: `[NOT FOUND] 未找到单据流程: ${target}\n`, exitCode: 1 };
|
||||
}
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson({ corpId, corpName: corp.name, ...flow }, runtime.outputOptions), stderr: "", exitCode: 0 };
|
||||
}
|
||||
const historyRows = flow.history.map((entry) => [
|
||||
entry.time ? new Date(entry.time).toLocaleString("zh-CN") : "-",
|
||||
entry.nodeName || "-",
|
||||
entry.action || "-",
|
||||
entry.operator?.name || (entry.automatic ? "系统" : "-"),
|
||||
entry.comment || "-",
|
||||
]);
|
||||
const pendingRows = flow.pending.map((node) => [
|
||||
node.state || "-",
|
||||
node.name || "-",
|
||||
node.type || "-",
|
||||
node.approver?.name || node.approvers?.map((person) => person.name).filter(Boolean).join(", ") || "-",
|
||||
]);
|
||||
const output = [
|
||||
`====== 审批流程 (单号: ${flow.billCode || target} | 状态: ${flow.state || "-"}) ======`,
|
||||
`流程 ID: ${flow.flowId || "-"} 当前节点: ${flow.currentNode || "-"}`,
|
||||
"",
|
||||
"--- 历史流转 ---",
|
||||
flow.history.length ? printTable(["时间", "节点", "动作", "处理人", "意见"], historyRows) : " (暂无历史记录)",
|
||||
"",
|
||||
"--- 后续节点 ---",
|
||||
flow.pending.length ? printTable(["状态", "节点", "类型", "审批人"], pendingRows) : " (暂无后续节点)",
|
||||
"",
|
||||
].join("\n");
|
||||
return { stdout: output, stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
case "link":
|
||||
case "url": {
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "c"]);
|
||||
@@ -257,6 +298,6 @@ export async function handleBillCommand(
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, schema, link <单号>`);
|
||||
throw new Error(`未知 bill 子命令: ${action}。支持: list, get <单号>, flow <单号>, schema, link <单号>`);
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -119,7 +119,7 @@ export function printHelp(): string {
|
||||
corps 多企业租户管理 (list, switch, current)
|
||||
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, set)
|
||||
workflow 工作流管理 (list, get, find, toggle)
|
||||
bill 单据与费用明细检索 (list, get <单号|ID>, schema, link <单号|ID>)
|
||||
bill 单据与费用明细检索 (list, get <单号|ID>, flow <单号|ID>, schema, link <单号|ID>)
|
||||
contact 企业通讯录与人员检索 (list, search <关键词>, get <staffId>)
|
||||
task 待办中心与审批任务 (todo, approved, records, submitted, cc)
|
||||
message 消息中心与通知管理 (list, count, read, read-all)
|
||||
@@ -160,6 +160,7 @@ export function printHelp(): string {
|
||||
hose-use workflow list # 汇总查看全部工作流
|
||||
hose-use workflow find --task=code-js2 # 搜索包含自定义脚本的工作流
|
||||
hose-use bill list # 查看当前企业单据与报销列表
|
||||
hose-use bill flow S26000001 # 查看单据审批历史与后续节点
|
||||
hose-use bill link CL20260901001 # 获取单据 Web 详情直达免密跳转链接
|
||||
hose-use bill list --json -o /tmp/bills.json # 导出单据报文到本地磁盘并防爆
|
||||
hose-use bill get n0GMIM0dkQmKbS # 根据单号精准查询单据结构化详情
|
||||
|
||||
@@ -8,6 +8,71 @@ import {
|
||||
type EntityMeta,
|
||||
} from "./types.js";
|
||||
|
||||
function quoted(value: string): string {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function flowLookupSelect(): string {
|
||||
return "flowId(form(code,title),`...`),`...`";
|
||||
}
|
||||
|
||||
function flowDetailSelect(): string {
|
||||
return "flowId(form(code,title,submitDate,submitterId),logs(operatorId,nextOperatorId,`...`),plan(`...`),`...`),`...`";
|
||||
}
|
||||
|
||||
export async function getRawBillFlow(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
target: string
|
||||
): Promise<Record<string, unknown> | undefined> {
|
||||
let match: Record<string, unknown> | undefined;
|
||||
const filters = [
|
||||
`(id==${quoted(target)})||((flowId.id==${quoted(target)})||(flowId.form.code==${quoted(target)}))`,
|
||||
`(state.in("APPROVING","PROCESSED","REJECTED","DRAFT"))`,
|
||||
];
|
||||
for (const filterBy of filters) {
|
||||
try {
|
||||
const lookup = await client.post("api/flow/v2/backlogs/search", {
|
||||
searchParams: { corpId },
|
||||
json: {
|
||||
filterBy,
|
||||
limit: { start: 0, count: 100 },
|
||||
select: flowLookupSelect(),
|
||||
},
|
||||
});
|
||||
const lookupJson = (await lookup.json()) as { items?: Record<string, unknown>[] };
|
||||
match = (lookupJson.items ?? []).find((item) => {
|
||||
const flow = item.flowId && typeof item.flowId === "object" ? item.flowId as Record<string, unknown> : {};
|
||||
const form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
|
||||
return item.id === target || flow.id === target || form.code === target;
|
||||
});
|
||||
if (match) break;
|
||||
} catch {
|
||||
// Fall through to the broad status lookup or direct detail request.
|
||||
}
|
||||
}
|
||||
const backlogId = typeof match?.id === "string" ? match.id : target;
|
||||
|
||||
const detail = await client.post("api/flow/v2/backlogs/id", {
|
||||
searchParams: (() => {
|
||||
const params = new URLSearchParams({ corpId });
|
||||
for (const join of [
|
||||
"flowId.logs.operatorId,operatorId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.logs.nextOperatorId,nextOperatorId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.plan.nodes.approverId,approverId,/v1/organization/staffs?select=id,name,code",
|
||||
"flowId.plan.nodes.counterSigners.signerId,signerId,/v1/organization/staffs?select=id,name,code",
|
||||
]) params.append("join", join);
|
||||
return params;
|
||||
})(),
|
||||
json: {
|
||||
filterBy: `(id==${quoted(backlogId)})`,
|
||||
select: flowDetailSelect(),
|
||||
},
|
||||
});
|
||||
const json = (await detail.json()) as { value?: Record<string, unknown>; items?: Record<string, unknown>[] };
|
||||
return json.value ?? json.items?.[0];
|
||||
}
|
||||
|
||||
// ── 原始单据检索 API ──────────────────────────────────────────────────────────
|
||||
|
||||
export async function searchRawBills(
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { z } from "zod";
|
||||
import { KyInstance } from "ky";
|
||||
import { searchRawBills, getRawBillsByIds, listEntitySchemas } from "./api.js";
|
||||
import { searchRawBills, getRawBillsByIds, listEntitySchemas, getRawBillFlow } from "./api.js";
|
||||
import {
|
||||
type RawBillItem,
|
||||
type FormattedBill,
|
||||
type BillPageResult,
|
||||
type EntityMeta,
|
||||
type BillQueryParams,
|
||||
type BillFlowEntry,
|
||||
type BillFlowNode,
|
||||
type BillFlowTimeline,
|
||||
} from "./types.js";
|
||||
import { CorpContext } from "../../core/config/manager.js";
|
||||
import { createEkbClient } from "../../core/auth/ekb.js";
|
||||
@@ -217,6 +220,81 @@ export async function getBillByCodeOrId(
|
||||
return matched.map((it) => formatSingleBill(it, dictionary));
|
||||
}
|
||||
|
||||
function flowPerson(value: unknown): { id?: string; name?: string; code?: string } | undefined {
|
||||
if (typeof value === "string") return { id: value };
|
||||
if (!value || typeof value !== "object") return undefined;
|
||||
const person = value as Record<string, unknown>;
|
||||
return {
|
||||
id: typeof person.id === "string" ? person.id : undefined,
|
||||
name: typeof person.name === "string" ? person.name : undefined,
|
||||
code: typeof person.code === "string" ? person.code : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getBillFlow(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
target: string
|
||||
): Promise<BillFlowTimeline | undefined> {
|
||||
const raw = await getRawBillFlow(client, corpId, target);
|
||||
if (!raw) return undefined;
|
||||
const flow = raw.flowId && typeof raw.flowId === "object" ? raw.flowId as Record<string, unknown> : {};
|
||||
const form = flow.form && typeof flow.form === "object" ? flow.form as Record<string, unknown> : {};
|
||||
const nodeState = raw.nodeState && typeof raw.nodeState === "object" ? raw.nodeState as Record<string, unknown> : {};
|
||||
const logs = Array.isArray(flow.logs) ? flow.logs : [];
|
||||
const history: BillFlowEntry[] = logs.map((value) => {
|
||||
const log = value && typeof value === "object" ? value as Record<string, unknown> : {};
|
||||
const attributes = log.attributes && typeof log.attributes === "object" ? log.attributes as Record<string, unknown> : {};
|
||||
const nextOperators = Array.isArray(log.nextOperatorIds)
|
||||
? log.nextOperatorIds.map(flowPerson).filter((person): person is NonNullable<typeof person> => Boolean(person))
|
||||
: [];
|
||||
return {
|
||||
id: typeof log.id === "string" ? log.id : undefined,
|
||||
time: typeof log.time === "number" ? log.time : undefined,
|
||||
state: typeof log.state === "string" ? log.state : undefined,
|
||||
action: typeof log.action === "string" ? log.action : undefined,
|
||||
nodeName: typeof attributes.nodeName === "string" ? attributes.nodeName : typeof attributes.nextName === "string" ? attributes.nextName : undefined,
|
||||
comment: typeof attributes.comment === "string" && attributes.comment ? attributes.comment : undefined,
|
||||
automatic: attributes.isEbotNode === true || attributes.isAuto === true || !log.operatorId,
|
||||
operator: flowPerson(log.operatorId),
|
||||
nextOperators,
|
||||
};
|
||||
});
|
||||
const plan = flow.plan && typeof flow.plan === "object" ? flow.plan as Record<string, unknown> : {};
|
||||
const nextNodeId = typeof plan.nextNodeId === "string" ? plan.nextNodeId : undefined;
|
||||
const currentNode = typeof raw.nodeName === "string" ? raw.nodeName : typeof nodeState.nodeName === "string" ? nodeState.nodeName : undefined;
|
||||
const nodes = Array.isArray(plan.nodes) ? plan.nodes : [];
|
||||
const pending: BillFlowNode[] = nodes.map((value) => {
|
||||
const node = value && typeof value === "object" ? value as Record<string, unknown> : {};
|
||||
const approvers = Array.isArray(node.counterSigners)
|
||||
? node.counterSigners.map(flowPerson).filter((person): person is NonNullable<typeof person> => Boolean(person))
|
||||
: [];
|
||||
const approver = flowPerson(node.approverId);
|
||||
return {
|
||||
id: typeof node.id === "string" ? node.id : undefined,
|
||||
name: typeof node.name === "string" ? node.name : undefined,
|
||||
type: typeof node.type === "string" ? node.type : undefined,
|
||||
state: typeof node.id === "string" && node.id === nextNodeId || typeof node.name === "string" && node.name === currentNode ? "current" : "planned",
|
||||
approver,
|
||||
approvers,
|
||||
};
|
||||
});
|
||||
const lastAttributes = logs.at(-1) && typeof logs.at(-1) === "object" && (logs.at(-1) as Record<string, unknown>).attributes && typeof (logs.at(-1) as Record<string, unknown>).attributes === "object"
|
||||
? (logs.at(-1) as Record<string, unknown>).attributes as Record<string, unknown>
|
||||
: {};
|
||||
return {
|
||||
backlogId: typeof raw.id === "string" ? raw.id : target,
|
||||
flowId: typeof flow.id === "string" ? flow.id : undefined,
|
||||
billCode: typeof form.code === "string" ? form.code : undefined,
|
||||
title: typeof form.title === "string" ? form.title : undefined,
|
||||
state: typeof raw.state === "string" ? raw.state : typeof flow.state === "string" ? flow.state : undefined,
|
||||
currentNode: currentNode ?? (typeof lastAttributes.nextName === "string" ? lastAttributes.nextName : undefined),
|
||||
history,
|
||||
pending,
|
||||
raw,
|
||||
};
|
||||
}
|
||||
|
||||
// ── 业务操作:多条件搜索与分页单据 ─────────────────────────────────────────────
|
||||
|
||||
export async function searchAndFormatBills(
|
||||
|
||||
@@ -103,3 +103,36 @@ export interface BillQueryParams {
|
||||
start?: number;
|
||||
count?: number;
|
||||
}
|
||||
|
||||
export interface BillFlowEntry {
|
||||
id?: string;
|
||||
time?: number;
|
||||
state?: string;
|
||||
action?: string;
|
||||
nodeName?: string;
|
||||
comment?: string;
|
||||
automatic?: boolean;
|
||||
operator?: { id?: string; name?: string; code?: string };
|
||||
nextOperators?: Array<{ id?: string; name?: string; code?: string }>;
|
||||
}
|
||||
|
||||
export interface BillFlowNode {
|
||||
id?: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
state?: string;
|
||||
approver?: { id?: string; name?: string; code?: string };
|
||||
approvers?: Array<{ id?: string; name?: string; code?: string }>;
|
||||
}
|
||||
|
||||
export interface BillFlowTimeline {
|
||||
backlogId: string;
|
||||
flowId?: string;
|
||||
billCode?: string;
|
||||
title?: string;
|
||||
state?: string;
|
||||
currentNode?: string;
|
||||
history: BillFlowEntry[];
|
||||
pending: BillFlowNode[];
|
||||
raw: Record<string, unknown>;
|
||||
}
|
||||
Reference in new issue
Block a user