Files
hose-use/src/cli/commands/task.ts
T

74 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
import { booleanOption, 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"
: action === "cc" || action === "carboncopy" || action === "carbon-copy"
? "cc"
: action === "submitted" || action === "mine" || action === "created"
? "submitted"
: undefined;
if (!view) {
throw new Error("未知 task 子命令。支持: todo, approved, records, submitted, cc");
}
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 read = booleanOption(parsed.options, ["read"]);
const unread = booleanOption(parsed.options, ["unread"]);
// Carbon copy defaults to unread, while the other views do not use this flag.
const readFilter = view === "cc" ? read && !unread : undefined;
const result = await listTasks(await createEkbClient(corp), corp.corpId || corp.id, view, { start, count, staffId, from, to, read: readFilter });
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: "审批记录",
submitted: "我发起的",
cc: readFilter ? "抄送(已读)" : "抄送(未读)",
};
return {
stdout: `====== ${labels[view]}任务 (企业: ${corp.name} | 总数: ${result.total} | 当前返回: ${result.items.length}) ======\n${printTable(headers, rows)}\n`,
stderr: "",
exitCode: 0,
};
}