- 新增 `src/domains/bill/form-helpers.ts`:统一单据类型路由(`resolveBillFormType`:模板类型 > 流程类型 > 字段特征)、东八区日期区间解析、历史流转字段剥离、以及 `dataLinkEdits` 强制重置 `dataLinkId: null` - 更新 `draft-validation.ts`:按类型路由明细校验(非报销单允许无 `details`)、跳过 `hide`/`hidden` 必填字段、强制拦截缺失 `dataLinkId` 键的行程条目(`DATALINK_ID_KEY_MISSING`)与非法 `feeDatePeriod` - 更新 `copy.ts` 与 `cli/commands/bill/copy.ts`:支持申请单复制(不注入 `expenseMoney`/`payMoney`,自动升级最新启用模板版本并重置行程 `dataLinkId: null`),新增 `--reason`/`--description`、`--amount`、`--start-date`、`--end-date` - 新增 `bill edit`(别名 `update`)草稿编辑命令与 `editBillDraft` / `updateBillFlow`,支持修改 `draft` 草稿的标题、事由、申请金额与差旅起止日期(同步联动行程日期) - 更新 `submit.ts`:正确传递 `requisition` 等 `formType`,放行 `ebot`、自动跳过节点(`skippedType`)及条件/延迟计算节点(`conditionalType` / `laterCalcReason`) - 补齐域层与 CLI 单元测试并同步更新 `README.bill.md` 与 `skills/hose-use/references/bill.md`
114 lines
4.4 KiB
TypeScript
114 lines
4.4 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, 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/$<flowId>?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<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 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,
|
||
};
|
||
}
|