import type { KyInstance } from "ky"; import { NotFoundError, ValidationError } from "../../core/errors.js"; import { computeBillApprovalPlan, getFormSpecification, getRawBillFlow, submitBillFlow } from "./api.js"; import { assertBillDraftValid, loadBillValidationOptions, type BillDraftForm } from "./draft-validation.js"; import { resolveBillFormType } from "./form-helpers.js"; import type { BillPlanNode } from "./types.js"; /** * 纯接口提交单据草稿。 * * 流程(实测): * 1. `POST /api/v1/flow/plans/computed` 计算审批计划(nodes) * 2. `POST /api/flow/v1/flows/$?pushMessageCode=debug` * body: { name: "freeflow.submit", formType, form, nodes } * * 未指派审批人的普通必填节点必须由调用方通过 `--approver` 指定,绝不自动编造; * ebot、自动跳过(skippedType != NO_SKIPPED)与条件/延迟计算节点原样回传。 */ export interface SubmitBillOptions { /** 用于补齐未指派节点的审批人(员工 ID) */ approverId?: string; } export interface SubmitBillResult { flowId: string; code: string; nodeCount: number; response: unknown; } function nodeRequiresApprover(node: BillPlanNode): boolean { if (node.type === "ebot") return false; if (typeof node.skippedType === "string" && node.skippedType.trim() !== "" && node.skippedType !== "NO_SKIPPED") { return false; } if (typeof node.conditionalType === "string" && node.conditionalType.trim() !== "") { return false; } if (typeof node.laterCalcReason === "string" && node.laterCalcReason.trim() !== "") { return false; } return true; } export async function submitBillDraft( client: KyInstance, corpId: string, target: string, options: SubmitBillOptions = {} ): Promise { const raw = await getRawBillFlow(client, corpId, target); const flow = raw && typeof raw.flowId === "object" ? (raw.flowId as Record) : undefined; const form = flow && typeof flow.form === "object" ? (flow.form as Record) : 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 as BillDraftForm, await loadBillValidationOptions(client, corpId, form as BillDraftForm, 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 = resolveBillFormType(form as BillDraftForm, { specType: spec?.type, flowFormType: typeof flow.formType === "string" ? flow.formType : undefined, }); 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 (!nodeRequiresApprover(node)) 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, }; }