feat(bill): add pure-API create and submit commands
- bill create: 按模板创建草稿(最小表单 + 一条费用明细,版本化 specificationId) - bill submit: plans/computed 计算审批计划 → freeflow.submit(未指派节点需显式 --approver) - 文档与 Skill 路由同步
This commit is contained in:
1 parent
1de9e3f552
commit
3c00a0c0a6
9 files changed
+920
-442
No files matched your search
@@ -0,0 +1,107 @@
|
||||
import type { KyInstance } from "ky";
|
||||
import { NotFoundError, ValidationError } from "../../core/errors.js";
|
||||
import { getFormSpecification, getRawBillFlow } from "./api.js";
|
||||
|
||||
/**
|
||||
* 纯接口提交单据草稿。
|
||||
*
|
||||
* 流程(实测):
|
||||
* 1. `POST /api/v1/flow/plans/computed` 计算审批计划(nodes)
|
||||
* 2. `POST /api/flow/v1/flows/$<flowId>?pushMessageCode=debug`
|
||||
* body: { name: "freeflow.submit", formType, form, nodes }
|
||||
*
|
||||
* 未指派审批人的节点必须由调用方通过 `--approver` 指定,绝不自动编造。
|
||||
*/
|
||||
|
||||
export interface SubmitBillOptions {
|
||||
/** 用于补齐未指派节点的审批人(员工 ID) */
|
||||
approverId?: string;
|
||||
}
|
||||
|
||||
export interface SubmitBillResult {
|
||||
flowId: string;
|
||||
code: string;
|
||||
nodeCount: number;
|
||||
response: unknown;
|
||||
}
|
||||
|
||||
interface PlanNode {
|
||||
id?: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
approverId?: unknown;
|
||||
approverIds?: unknown[];
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export async function submitBillDraft(
|
||||
client: KyInstance,
|
||||
corpId: string,
|
||||
target: string,
|
||||
options: SubmitBillOptions = {}
|
||||
): Promise<SubmitBillResult> {
|
||||
const raw = await getRawBillFlow(client, corpId, target);
|
||||
const flow = raw && typeof raw.flowId === "object" ? (raw.flowId as Record<string, unknown>) : undefined;
|
||||
const form = flow && typeof flow.form === "object" ? (flow.form as Record<string, unknown>) : undefined;
|
||||
if (!flow || !form) {
|
||||
throw new NotFoundError(`未找到单号或 ID 为 [${target}] 的单据`);
|
||||
}
|
||||
|
||||
const flowId = String(flow.id ?? "");
|
||||
if (!flowId) throw new NotFoundError("单据缺少流程 ID");
|
||||
if (flow.state !== "draft") {
|
||||
throw new ValidationError(`单据当前状态为 ${String(flow.state)},仅 draft 可提交`);
|
||||
}
|
||||
|
||||
const specificationId = form.specificationId;
|
||||
const spec = specificationId
|
||||
? await getFormSpecification(client, corpId, String(specificationId))
|
||||
: undefined;
|
||||
const configs = (spec as { configs?: Array<{ ability?: string; flowPlanConfigId?: string }> } | undefined)?.configs;
|
||||
const flowPlanConfigId = configs?.find((config) => config.ability === "flow")?.flowPlanConfigId;
|
||||
if (!flowPlanConfigId) {
|
||||
throw new ValidationError("模板缺少审批流配置(flowPlanConfigId),无法计算审批计划");
|
||||
}
|
||||
|
||||
const formType = String(form.type ?? flow.formType ?? "expense");
|
||||
const planResponse = await client.post("api/v1/flow/plans/computed", {
|
||||
searchParams: { corpId },
|
||||
json: { flowId, flowPlanConfigId, form, formType, params: {} },
|
||||
});
|
||||
const planJson = (await planResponse.json()) as { value?: { nodes?: PlanNode[] } };
|
||||
const nodes = planJson.value?.nodes ?? [];
|
||||
if (nodes.length === 0) {
|
||||
throw new ValidationError("审批计划为空,无法提交");
|
||||
}
|
||||
|
||||
const unassigned: string[] = [];
|
||||
for (const node of nodes) {
|
||||
if (node.type === "ebot") continue;
|
||||
const hasApprover =
|
||||
Boolean(node.approverId) || (Array.isArray(node.approverIds) && node.approverIds.length > 0);
|
||||
if (hasApprover) continue;
|
||||
if (options.approverId) {
|
||||
if (node.type === "countersign") node.approverIds = [options.approverId];
|
||||
else node.approverId = options.approverId;
|
||||
} else {
|
||||
unassigned.push(String(node.name ?? node.id ?? "unknown"));
|
||||
}
|
||||
}
|
||||
if (unassigned.length > 0) {
|
||||
throw new ValidationError(
|
||||
`以下审批节点未指派审批人:${unassigned.join("、")};请通过 --approver 指定员工`
|
||||
);
|
||||
}
|
||||
|
||||
const response = await client.post(`api/flow/v1/flows/$${flowId}`, {
|
||||
searchParams: { corpId, pushMessageCode: "debug" },
|
||||
json: { id: flowId, name: "freeflow.submit", formType, form, nodes },
|
||||
});
|
||||
|
||||
return {
|
||||
flowId,
|
||||
code: String(form.code ?? target),
|
||||
nodeCount: nodes.length,
|
||||
response: await response.json(),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user