The five closures that captured corp/client/confirmation state move into a per-invocation createBillActionContext factory; case bodies become ctx-taking handlers grouped by concern (approval write actions, list/get, flow/risk/budget, refs/relations/attachments/download, schema/link/spec). index.ts keeps the original switch semantics. No behavior change: tsc clean, 316 tests pass.
68 lines
4.6 KiB
TypeScript
68 lines
4.6 KiB
TypeScript
import { addNodeBill, approveBill, commentBill, rejectBill, transferBill } from "../../../domains/bill/tools.js";
|
|
import { printJson } from "../../printer.js";
|
|
import { isDryRun, mutationPreview } from "../../mutation.js";
|
|
import type { CliResult } from "../../types.js";
|
|
import type { BillActionContext } from "./context.js";
|
|
|
|
export async function handleApprove(ctx: BillActionContext): Promise<CliResult> {
|
|
const { parsed, runtime, corpId } = ctx;
|
|
await ctx.requireWriteConfirmation();
|
|
const target = ctx.actionTarget();
|
|
if (!target) throw new Error("请提供审批任务、单号或单据 ID: hose-use bill approve <单号|ID> --yes");
|
|
if (isDryRun(parsed, runtime)) return mutationPreview(parsed, runtime, "同意单据审批", target, ctx.actionOptions());
|
|
const result = await approveBill(await ctx.getClient(), corpId, target, ctx.actionOptions());
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `审批同意成功\n单号: ${result.billCode || target}\n任务 ID: ${result.backlogId}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
export async function handleReject(ctx: BillActionContext): Promise<CliResult> {
|
|
const { parsed, runtime, corpId } = ctx;
|
|
await ctx.requireWriteConfirmation();
|
|
const target = ctx.actionTarget();
|
|
if (!target) throw new Error("请提供审批任务、单号或单据 ID: hose-use bill reject <单号|ID> --comment <原因> --yes");
|
|
const options = ctx.actionOptions();
|
|
if (!options.comment?.trim()) throw new Error("驳回原因不能为空,请通过 --comment 提供原因");
|
|
if (isDryRun(parsed, runtime)) return mutationPreview(parsed, runtime, "驳回单据审批", target, options);
|
|
const result = await rejectBill(await ctx.getClient(), corpId, target, options);
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `驳回成功\n单号: ${result.billCode || target}\n任务 ID: ${result.backlogId}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
export async function handleTransfer(ctx: BillActionContext): Promise<CliResult> {
|
|
const { parsed, runtime, corpId } = ctx;
|
|
await ctx.requireWriteConfirmation();
|
|
const target = ctx.actionTarget();
|
|
if (!target) throw new Error("请提供审批任务、单号或单据 ID: hose-use bill transfer <单号|ID> --approver-id <员工ID> --yes");
|
|
const rawOptions = ctx.actionOptions();
|
|
if (isDryRun(parsed, runtime)) return mutationPreview(parsed, runtime, "转交单据审批", target, rawOptions);
|
|
const options = { ...rawOptions, approverIds: await ctx.resolveApproverIds(rawOptions.approverIds) };
|
|
const result = await transferBill(await ctx.getClient(), corpId, target, options);
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `转交审批成功\n单号: ${result.billCode || target}\n任务 ID: ${result.backlogId}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
export async function handleAddNode(ctx: BillActionContext): Promise<CliResult> {
|
|
const { parsed, runtime, corpId } = ctx;
|
|
await ctx.requireWriteConfirmation();
|
|
const target = ctx.actionTarget();
|
|
if (!target) throw new Error("请提供审批任务、单号或单据 ID: hose-use bill add-node <单号|ID> --approver-id <员工ID> --yes");
|
|
const rawOptions = ctx.actionOptions();
|
|
if (isDryRun(parsed, runtime)) return mutationPreview(parsed, runtime, "单据加签", target, rawOptions);
|
|
const options = { ...rawOptions, approverIds: await ctx.resolveApproverIds(rawOptions.approverIds) };
|
|
const result = await addNodeBill(await ctx.getClient(), corpId, target, options);
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `加签审批成功\n单号: ${result.billCode || target}\n任务 ID: ${result.backlogId}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
export async function handleComment(ctx: BillActionContext): Promise<CliResult> {
|
|
const { parsed, runtime, corpId } = ctx;
|
|
await ctx.requireWriteConfirmation();
|
|
const target = ctx.actionTarget();
|
|
if (!target) throw new Error("请提供单号或单据 ID: hose-use bill comment <单号|ID> --comment <内容> --yes");
|
|
const options = ctx.actionOptions();
|
|
if (isDryRun(parsed, runtime)) return mutationPreview(parsed, runtime, "添加单据评论", target, options);
|
|
const result = await commentBill(await ctx.getClient(), corpId, target, options);
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `单据评论成功\n单号: ${result.billCode || target}\n流程 ID: ${result.flowId}\n`, stderr: "", exitCode: 0 };
|
|
}
|