176 lines
6.4 KiB
TypeScript
176 lines
6.4 KiB
TypeScript
import type { CorpConfig } from "../../core/config/types.js";
|
|
import type { createSealClient } from "../../core/http/factory.js";
|
|
import { existsSync } from "node:fs";
|
|
import { runTool } from "../helpers.js";
|
|
import * as api from "../../domains/seal/api.js";
|
|
import { findWebhookIntegration } from "../../domains/seal/api.js";
|
|
import { previewMaterialPack, pushMaterialPack, readMaterialPackFile } from "../../domains/seal/material-pack/index.js";
|
|
import {
|
|
booleanOption,
|
|
parseWebhookMode,
|
|
stringOption,
|
|
webhookRateOption,
|
|
webhookShowSecretOption
|
|
} from "../parser.js";
|
|
import { outputOptions } from "../printer.js";
|
|
import type { ParsedArgs } from "../types.js";
|
|
|
|
export async function handleWebhookMaterialPackPreview(filePath: string) {
|
|
if (!existsSync(filePath)) throw new Error(`File not found: ${filePath}`);
|
|
const input = readMaterialPackFile(filePath);
|
|
console.log(JSON.stringify(await previewMaterialPack(input.fileBuffer, input.fileName), null, 2));
|
|
}
|
|
|
|
export async function handleWebhookCommand(
|
|
action: string | undefined,
|
|
maybeName: string | undefined,
|
|
client: Awaited<ReturnType<typeof createSealClient>>,
|
|
corp: CorpConfig,
|
|
args: ParsedArgs
|
|
) {
|
|
if (action === "material-pack" || action === "material-pack-push") {
|
|
const operation = action === "material-pack-push" ? "push" : maybeName;
|
|
const filePath = action === "material-pack-push"
|
|
? stringOption(args.options.file) ?? stringOption(args.options.input)
|
|
: args.command[3] ?? stringOption(args.options.file) ?? stringOption(args.options.input);
|
|
if (!filePath) throw new Error("Usage: seal-use webhook material-pack <preview|push> <材料包.zip>");
|
|
if (!existsSync(filePath)) throw new Error(`File not found: ${filePath}`);
|
|
const input = readMaterialPackFile(filePath);
|
|
if (operation === "preview") {
|
|
console.log(JSON.stringify(await previewMaterialPack(input.fileBuffer, input.fileName), null, 2));
|
|
return;
|
|
}
|
|
if (operation !== "push") throw new Error("Usage: seal-use webhook material-pack <preview|push> <材料包.zip>");
|
|
const selector = stringOption(args.options.webhook) ?? stringOption(args.options.selector);
|
|
if (!selector) throw new Error("Push requires --webhook <idOrName>");
|
|
const integration = findWebhookIntegration(await api.listWebhookIntegrations(client), selector);
|
|
const result = await pushMaterialPack(input.fileBuffer, input.fileName, {
|
|
webhookUrl: integration.incomingDocumentWebhook.url,
|
|
secret: integration.secret
|
|
});
|
|
console.log(JSON.stringify(result, null, 2));
|
|
return;
|
|
}
|
|
if (action === "list") {
|
|
await runTool(
|
|
"seal_webhook_list",
|
|
client,
|
|
corp,
|
|
{
|
|
query: stringOption(args.options.query) ?? stringOption(args.options.keyword),
|
|
includeSecret: webhookShowSecretOption(args.options)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "get" || action === "find") {
|
|
const selector = maybeName ?? stringOption(args.options.selector) ?? stringOption(args.options.id);
|
|
if (!selector) throw new Error("Usage: seal-use webhook get <idOrName> [--show-secret]");
|
|
await runTool(
|
|
"seal_webhook_get",
|
|
client,
|
|
corp,
|
|
{
|
|
selector,
|
|
includeSecret: webhookShowSecretOption(args.options)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "add" || action === "create") {
|
|
const name = stringOption(args.options.name) ?? maybeName;
|
|
const outgoingResultWebhook =
|
|
stringOption(args.options.resultUrl) ??
|
|
stringOption(args.options["result-url"]) ??
|
|
stringOption(args.options.outgoingResultWebhook) ??
|
|
stringOption(args.options["outgoing-result-webhook"]);
|
|
if (!name || !outgoingResultWebhook) {
|
|
throw new Error("Usage: seal-use webhook add --name <name> --result-url <url> [--mode assisted|semi_automatic|fully_automatic]");
|
|
}
|
|
const autoApprovalRate = webhookRateOption(args.options);
|
|
await runTool(
|
|
"seal_webhook_add",
|
|
client,
|
|
corp,
|
|
{
|
|
name,
|
|
mode: parseWebhookMode(stringOption(args.options.mode)),
|
|
outgoingResultWebhook,
|
|
autoApprovalRate,
|
|
secret: stringOption(args.options.secret)
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "update") {
|
|
const selector = maybeName ?? stringOption(args.options.selector) ?? stringOption(args.options.id);
|
|
if (!selector) throw new Error("Usage: seal-use webhook update <idOrName> [options]");
|
|
const secret = stringOption(args.options.secret);
|
|
const rotateSecret = booleanOption(args.options.rotateSecret) ?? booleanOption(args.options["rotate-secret"]);
|
|
if (secret !== undefined && rotateSecret) {
|
|
throw new Error("Use either --secret or --rotate-secret, not both");
|
|
}
|
|
const name = stringOption(args.options.name);
|
|
const mode = parseWebhookMode(stringOption(args.options.mode));
|
|
const outgoingResultWebhook =
|
|
stringOption(args.options.resultUrl) ??
|
|
stringOption(args.options["result-url"]) ??
|
|
stringOption(args.options.outgoingResultWebhook) ??
|
|
stringOption(args.options["outgoing-result-webhook"]);
|
|
const autoApprovalRate = webhookRateOption(args.options);
|
|
if (
|
|
name === undefined &&
|
|
mode === undefined &&
|
|
outgoingResultWebhook === undefined &&
|
|
autoApprovalRate === undefined &&
|
|
secret === undefined &&
|
|
!rotateSecret
|
|
) {
|
|
throw new Error("Webhook update requires at least one field option");
|
|
}
|
|
await runTool(
|
|
"seal_webhook_update",
|
|
client,
|
|
corp,
|
|
{
|
|
selector,
|
|
name,
|
|
mode,
|
|
outgoingResultWebhook,
|
|
autoApprovalRate,
|
|
secret,
|
|
rotateSecret
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (action === "delete") {
|
|
const selector = maybeName ?? stringOption(args.options.selector) ?? stringOption(args.options.id);
|
|
if (!selector) throw new Error("Usage: seal-use webhook delete <idOrName> --yes");
|
|
if (booleanOption(args.options.yes) !== true) {
|
|
throw new Error("Refusing to delete Webhook integration without --yes");
|
|
}
|
|
await runTool(
|
|
"seal_webhook_delete",
|
|
client,
|
|
corp,
|
|
{
|
|
selector,
|
|
confirm: true
|
|
},
|
|
outputOptions(args.options)
|
|
);
|
|
return;
|
|
}
|
|
|
|
throw new Error(`Unknown webhook command: ${action ?? ""}. Run 'seal-use help' for available commands.`);
|
|
}
|