将模板、当前员工、草稿创建/删除、审批计划和送审共七处 HTTP 调用收口到 bill/api.ts,业务层函数签名保持兼容。创建与预演复用实际规格校验,缺失明细规格、必填字段或超长文本均先于写入拒绝。 统一检查已知业务错误及有效流程 ID,保留 flow/value/裸对象成功包装与已有单号字段;补充业务拒绝、HTTP 失败、无效 ID、计划失败不送审及 CLI 非零退出回归,同步域文档。 验证:bun test 911 通过、0 失败;bun run check、git diff --check 通过。未调用真实企业接口。
94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
import type { KyInstance } from "ky";
|
||
import { NotFoundError, ValidationError } from "../../core/errors.js";
|
||
import { computeBillApprovalPlan, getFormSpecification, getRawBillFlow, submitBillFlow } from "./api.js";
|
||
import { assertBillDraftValid, loadBillValidationOptions } from "./draft-validation.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;
|
||
}
|
||
|
||
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 = typeof form.specificationId === "object" && form.specificationId
|
||
? (form.specificationId as { id?: string }).id : form.specificationId;
|
||
const spec = specificationId
|
||
? await getFormSpecification(client, corpId, String(specificationId))
|
||
: undefined;
|
||
assertBillDraftValid(form, await loadBillValidationOptions(client, corpId, form, spec));
|
||
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 nodes = await computeBillApprovalPlan(client, corpId, flowId, flowPlanConfigId, form, formType);
|
||
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 submitBillFlow(client, corpId, flowId, form, formType, nodes);
|
||
|
||
return {
|
||
flowId,
|
||
code: String(form.code ?? target),
|
||
nodeCount: nodes.length,
|
||
response,
|
||
};
|
||
}
|