328 lines
12 KiB
TypeScript
328 lines
12 KiB
TypeScript
import type { CorpConfig } from "../../core/config/types.js";
|
|
import type { createSealClient } from "../../core/http/factory.js";
|
|
import * as api from "../../domains/seal/api.js";
|
|
import { exportApprovalRunsToExcel } from "../../domains/seal/approval-runs-export.js";
|
|
import {
|
|
downloadApprovalRunAttachments,
|
|
extractApprovalRunAttachments
|
|
} from "../../domains/seal/attachments.js";
|
|
import { runTool, runToolResult } from "../helpers.js";
|
|
import { booleanOption, numberOption, stringOption } from "../parser.js";
|
|
import { outputOptions, printJson } from "../printer.js";
|
|
import type { ParsedArgs } from "../types.js";
|
|
import { handleWebhookCommand } from "./webhook.js";
|
|
|
|
export async function handleApprovalRunsCommand(
|
|
action: string | undefined,
|
|
maybeName: string | undefined,
|
|
client: Awaited<ReturnType<typeof createSealClient>>,
|
|
corp: CorpConfig,
|
|
args: ParsedArgs
|
|
) {
|
|
if (action === "replay") {
|
|
await handleWebhookCommand("replay", maybeName, client, corp, args);
|
|
return;
|
|
}
|
|
|
|
if (action === "summary") {
|
|
const result = await runToolResult(
|
|
"seal_approval_runs_summary",
|
|
client,
|
|
corp,
|
|
{
|
|
date: stringOption(args.options.date),
|
|
timezone: stringOption(args.options.timezone),
|
|
limit: numberOption(args.options.limit),
|
|
startDate: stringOption(args.options.startDate),
|
|
endDate: stringOption(args.options.endDate),
|
|
status: stringOption(args.options.status),
|
|
taskMode: stringOption(args.options.taskMode),
|
|
manualApprovalStatus: stringOption(args.options.manualApprovalStatus),
|
|
sourceDocumentSN: stringOption(args.options.sourceDocumentSN),
|
|
sourceDocumentId: stringOption(args.options.sourceDocumentId),
|
|
humanResult: stringOption(args.options.humanResult),
|
|
query: stringOption(args.options.query)
|
|
}
|
|
);
|
|
if ((result as any)?.matched === 0 && (result as any)?.total > 0) {
|
|
console.error(
|
|
`提示:在 ${(result as any).date} 未匹配到记录(该企业共有 ${(result as any).total} 条历史记录)。可使用 --date YYYY-MM-DD 指定日期,或运行 seal-use approval-runs list --limit 10 查看最近记录。`
|
|
);
|
|
}
|
|
printJson(result, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
if (action === "stats") {
|
|
await runTool(
|
|
"seal_approval_runs_stats",
|
|
client,
|
|
corp,
|
|
{
|
|
since: stringOption(args.options.since),
|
|
dedupeBy: (stringOption(args.options.dedupeBy) ?? stringOption(args.options["dedupe-by"])) as any,
|
|
status: stringOption(args.options.status),
|
|
sourceSystem: stringOption(args.options.sourceSystem) ?? stringOption(args.options["source-system"]),
|
|
taskMode: stringOption(args.options.taskMode) ?? stringOption(args.options["task-mode"])
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "count") {
|
|
await runTool(
|
|
"seal_approval_runs_count",
|
|
client,
|
|
corp,
|
|
{
|
|
status: stringOption(args.options.status),
|
|
sourceSystem: stringOption(args.options.sourceSystem) ?? stringOption(args.options["source-system"]),
|
|
taskMode: stringOption(args.options.taskMode) ?? stringOption(args.options["task-mode"]),
|
|
search: stringOption(args.options.search) ?? stringOption(args.options.query)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "search" || action === "list") {
|
|
await runTool(
|
|
"seal_approval_runs_search",
|
|
client,
|
|
corp,
|
|
{
|
|
limit: numberOption(args.options.limit),
|
|
offset: numberOption(args.options.offset),
|
|
startDate: stringOption(args.options.startDate),
|
|
endDate: stringOption(args.options.endDate),
|
|
status: stringOption(args.options.status),
|
|
taskMode: stringOption(args.options.taskMode),
|
|
manualApprovalStatus: stringOption(args.options.manualApprovalStatus),
|
|
sourceDocumentSN: stringOption(args.options.sourceDocumentSN),
|
|
sourceDocumentId: stringOption(args.options.sourceDocumentId),
|
|
humanResult: stringOption(args.options.humanResult),
|
|
query: stringOption(args.options.query),
|
|
includeBridge: booleanOption(args.options.includeBridge)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "bridge") {
|
|
await runTool(
|
|
"seal_approval_run_langfuse_bridge_get",
|
|
client,
|
|
corp,
|
|
{
|
|
recordId: stringOption(args.options.recordId),
|
|
sourceDocumentSN: stringOption(args.options.sourceDocumentSN),
|
|
sourceDocumentId: stringOption(args.options.sourceDocumentId),
|
|
simulationBatchId: stringOption(args.options.simulationBatchId),
|
|
limit: numberOption(args.options.limit)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "pick") {
|
|
const query = maybeName ?? stringOption(args.options.query) ?? stringOption(args.options.sn);
|
|
if (!query) throw new Error("Usage: seal-use approval-runs pick --sn <documentSN>");
|
|
await runTool(
|
|
"seal_approval_run_pick",
|
|
client,
|
|
corp,
|
|
{
|
|
query,
|
|
sn: stringOption(args.options.sn),
|
|
batchId: stringOption(args.options.batch) ?? stringOption(args.options.batchId),
|
|
latest: booleanOption(args.options.latest),
|
|
fields: stringOption(args.options.fields),
|
|
limit: numberOption(args.options.limit),
|
|
includeBridge: booleanOption(args.options.includeBridge)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "get") {
|
|
const recordId = args.command[2];
|
|
if (!recordId) throw new Error("Missing approval run recordId");
|
|
await runTool(
|
|
"seal_approval_run_get",
|
|
client,
|
|
corp,
|
|
{
|
|
recordId,
|
|
fields: stringOption(args.options.fields),
|
|
summary: booleanOption(args.options.summary)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "cited-rules") {
|
|
const recordId = args.command[2];
|
|
if (!recordId) throw new Error("Usage: seal-use approval-runs cited-rules <recordId>");
|
|
await runTool("seal_approval_run_cited_rules_get", client, corp, { recordId }, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
if (action === "document-summary") {
|
|
const recordId = args.command[2];
|
|
if (!recordId) throw new Error("Usage: seal-use approval-runs document-summary <recordId>");
|
|
await runTool("seal_approval_run_document_summary_get", client, corp, { recordId }, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
if (action === "attachments") {
|
|
const subAction = args.command[2];
|
|
if (subAction === "download") {
|
|
const recordId = args.command[3];
|
|
if (!recordId) {
|
|
throw new Error(
|
|
"Usage: seal-use approval-runs attachments download <recordId> [--all] [--name <name>] [--out-dir <dir>]"
|
|
);
|
|
}
|
|
const record = await api.getApprovalRun(client, recordId);
|
|
const downloadResult = await downloadApprovalRunAttachments({
|
|
record,
|
|
all: booleanOption(args.options.all),
|
|
name: stringOption(args.options.name),
|
|
outDir:
|
|
stringOption(args.options["out-dir"]) ??
|
|
stringOption(args.options.outDir) ??
|
|
stringOption(args.options.dir)
|
|
});
|
|
|
|
printJson(downloadResult, outputOptions(args.options));
|
|
if (downloadResult.total > 0 && downloadResult.downloaded === 0) {
|
|
throw new Error(`所有 ${downloadResult.total} 个附件下载均失败。`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const recordId = args.command[2];
|
|
if (!recordId) throw new Error("Usage: seal-use approval-runs attachments <recordId> | seal-use approval-runs attachments download <recordId>");
|
|
|
|
if (booleanOption(args.options.download)) {
|
|
const record = await api.getApprovalRun(client, recordId);
|
|
const downloadResult = await downloadApprovalRunAttachments({
|
|
record,
|
|
all: booleanOption(args.options.all) ?? true,
|
|
name: stringOption(args.options.name),
|
|
outDir:
|
|
stringOption(args.options["out-dir"]) ??
|
|
stringOption(args.options.outDir) ??
|
|
stringOption(args.options.dir)
|
|
});
|
|
|
|
printJson(downloadResult, outputOptions(args.options));
|
|
if (downloadResult.total > 0 && downloadResult.downloaded === 0) {
|
|
throw new Error(`所有 ${downloadResult.total} 个附件下载均失败。`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const record = await api.getApprovalRun(client, recordId);
|
|
const result = extractApprovalRunAttachments(record, {
|
|
summary: booleanOption(args.options.summary)
|
|
});
|
|
printJson(result, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
if (action === "download") {
|
|
const recordId = args.command[2];
|
|
if (!recordId) {
|
|
throw new Error(
|
|
"Usage: seal-use approval-runs download <recordId> [--all] [--name <name>] [--out-dir <dir>]"
|
|
);
|
|
}
|
|
const record = await api.getApprovalRun(client, recordId);
|
|
const downloadResult = await downloadApprovalRunAttachments({
|
|
record,
|
|
all: booleanOption(args.options.all),
|
|
name: stringOption(args.options.name),
|
|
outDir:
|
|
stringOption(args.options["out-dir"]) ??
|
|
stringOption(args.options.outDir) ??
|
|
stringOption(args.options.dir)
|
|
});
|
|
|
|
printJson(downloadResult, outputOptions(args.options));
|
|
if (downloadResult.total > 0 && downloadResult.downloaded === 0) {
|
|
throw new Error(`所有 ${downloadResult.total} 个附件下载均失败。`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (action === "result") {
|
|
const recordId = args.command[2];
|
|
if (!recordId) throw new Error("Usage: seal-use approval-runs result <recordId> [--summary]");
|
|
await runTool(
|
|
"seal_approval_run_result_get",
|
|
client,
|
|
corp,
|
|
{
|
|
recordId,
|
|
summary: booleanOption(args.options.summary)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "url") {
|
|
const sso = booleanOption(args.options.sso) ?? false;
|
|
const expiresIn = numberOption(args.options.expire) ?? numberOption(args.options.expiresIn) ?? 7200;
|
|
const sn = stringOption(args.options.sn) ?? stringOption(args.options.sourceDocumentSN);
|
|
|
|
await runTool(
|
|
"seal_approval_run_url_get",
|
|
client,
|
|
corp,
|
|
{
|
|
recordId: maybeName ?? stringOption(args.options.id) ?? stringOption(args.options.recordId),
|
|
sourceDocumentSN: sn,
|
|
sourceDocumentId: stringOption(args.options.sourceDocumentId),
|
|
sso,
|
|
expiresIn
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "export") {
|
|
const result = await exportApprovalRunsToExcel(client, corp, {
|
|
limit: numberOption(args.options.limit),
|
|
startDate:
|
|
stringOption(args.options.start) ??
|
|
stringOption(args.options.startDate) ??
|
|
stringOption(args.options.since),
|
|
endDate: stringOption(args.options.end) ?? stringOption(args.options.endDate),
|
|
status: stringOption(args.options.status),
|
|
manualApprovalStatus:
|
|
stringOption(args.options["manual-status"]) ??
|
|
stringOption(args.options.manualStatus) ??
|
|
stringOption(args.options.manualApprovalStatus),
|
|
includeCompletedOnly:
|
|
booleanOption(args.options["completed-only"]) ?? booleanOption(args.options.completedOnly),
|
|
outputFile:
|
|
stringOption(args.options.out) ??
|
|
stringOption(args.options.output) ??
|
|
stringOption(args.options.file),
|
|
toDownloads: booleanOption(args.options.downloads) ?? booleanOption(args.options.download)
|
|
});
|
|
printJson(result, outputOptions(args.options));
|
|
return;
|
|
}
|
|
|
|
throw new Error(`Unknown approval-runs command: ${action ?? ""}. Run 'seal-use help' for available commands.`);
|
|
}
|