feat: add CLI task center queries

This commit is contained in:
gitea committed 2026-09-05 13:33:33 +08:00
1 parent 6c0f4dc567
commit d28b0c92d9
11 files changed
+338 -1

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
import { numberOption, stringOption } from "../parser.js";
import { printJson, printTable } from "../printer.js";
import { createEkbClient } from "../../core/auth/ekb.js";
import { listTasks } from "../../domains/task/tools.js";
import type { TaskView } from "../../domains/task/types.js";
function dateOption(value: string | undefined): number | undefined {
if (!value) return undefined;
const numeric = Number(value);
if (Number.isFinite(numeric)) return numeric;
const parsed = Date.parse(value);
return Number.isNaN(parsed) ? undefined : parsed;
}
export async function handleTaskCommand(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const corp = runtime.selectedCorp;
if (!corp) throw new Error("未指定或未找到有效企业。请运行 hose corps list 或指定 --corp=<id>");
const action = parsed.action || parsed.args[0] || "todo";
const view: TaskView | undefined = action === "todo" || action === "pending"
? "todo"
: action === "approved" || action === "done"
? "approved"
: action === "processed" || action === "records" || action === "history"
? "processed"
: undefined;
if (!view) {
throw new Error("未知 task 子命令。支持: todo, approved, records (processed)");
}
const count = numberOption(parsed.options, ["count", "limit", "n"], 20);
const start = numberOption(parsed.options, ["start", "offset"], 0);
const staffId = stringOption(parsed.options, ["staff", "staff-id", "staffId"]);
const from = dateOption(stringOption(parsed.options, ["from", "start-date"]));
const to = dateOption(stringOption(parsed.options, ["to", "end-date"]));
const result = await listTasks(await createEkbClient(corp), corp.corpId || corp.id, view, { start, count, staffId, from, to });
if (runtime.jsonMode) {
return { stdout: printJson({ corpId: corp.corpId || corp.id, corpName: corp.name, ...result }, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const headers = ["#", "任务 ID", "单号", "标题", "模板", "提交人", "金额", "状态", "当前节点", "动作"];
const rows = result.items.map((item, index) => [
index + 1,
item.taskId,
item.billCode || "-",
item.title || "-",
item.templateName || "-",
item.submitter ? `${item.submitter.name || "-"}${item.submitter.code ? `(${item.submitter.code})` : ""}` : "-",
item.amount ? `${item.amount.symbol || ""}${item.amount.value} ${item.amount.currency || ""}`.trim() : "-",
item.state || "-",
item.nodeName || "-",
item.action || "-",
]);
const labels: Record<TaskView, string> = { todo: "待审批", approved: "已审批", processed: "审批记录" };
return {
stdout: `====== ${labels[view]}任务 (企业: ${corp.name} | 总数: ${result.total} | 当前返回: ${result.items.length}) ======\n${printTable(headers, rows)}\n`,
stderr: "",
exitCode: 0,
};
}