import { readFileSync } from "node:fs"; import type { CorpConfig } from "../../core/config/types.js"; import type { createSealClient } from "../../core/http/factory.js"; import { runCapability } from "../helpers.js"; import { booleanOption, jsonObject, numberOption, stringOption } from "../parser.js"; import { outputOptions } from "../printer.js"; import type { ParsedArgs } from "../types.js"; import * as api from "../../domains/seal/api.js"; import { listHoseIntegrations } from "../../domains/seal/outbound/push.js"; import { CliError } from "../errors.js"; export async function handleDeployCommand( systemOrAction: string | undefined, actionOrTarget: string | undefined, maybeTarget: string | undefined, client: Awaited>, corp: CorpConfig, args: ParsedArgs ) { if (!systemOrAction || systemOrAction === "help") { throw new Error( "Usage: seal-use deploy [options]\n\n" + "支持的审批系统与发布目标:\n" + " • hosecloud (合思费控): seal-use deploy hosecloud --corp \n" + " • webhook (通用审批流回调): seal-use webhook --corp \n" + " • feishu (飞书审批): 建议使用 Webhook 模式对接\n" + " • weaver (泛微 OA): 建议使用 Webhook 模式对接\n" + " • helios (汇联易): 建议使用 Webhook 模式对接\n" + " • maycur (每刻报销): 建议使用 Webhook 模式对接" ); } if (systemOrAction === "webhook" || systemOrAction === "webhooks") { throw new Error("Webhook 审批流发布请直接使用专有命令:seal-use webhook --corp "); } if (systemOrAction === "feishu" || systemOrAction === "lark") { throw new Error("飞书审批发布:建议使用 Webhook 通用发布模式对接。命令:seal-use webhook add --name '飞书审批' --result-url --corp "); } if (systemOrAction === "weaver") { throw new Error("泛微 OA 发布:建议使用 Webhook 通用发布模式对接。命令:seal-use webhook add --name '泛微OA' --result-url --corp "); } if (systemOrAction === "maycur" || systemOrAction === "helios") { const name = systemOrAction === "maycur" ? "每刻报销" : "汇联易"; throw new Error(`${name}发布:目前可通过 Webhook 模式接入。命令:seal-use webhook add --name '${name}' --result-url --corp `); } const isHosecloud = systemOrAction === "hosecloud"; const action = systemOrAction === "hosecloud" ? actionOrTarget : systemOrAction; const target = systemOrAction === "hosecloud" ? maybeTarget : actionOrTarget; if (!isHosecloud) { throw new Error(`未知的发布目标系统: ${systemOrAction}。当前支持: hosecloud (合思费控) 与 webhook (通用回调)。`); } if (action === "requirements") { await runCapability("sealDeployHosecloudRequirements", client, corp, {}, outputOptions(args.options)); return; } if (action === "list") { await runCapability("sealDeployHosecloudList", client, corp, {}, outputOptions(args.options)); return; } if (action === "get") { const signKey = target ?? stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]); if (!signKey) { const config = await api.getApprovalStylePreferences(client); const hoseConfigs = listHoseIntegrations(config); if (hoseConfigs.length === 0) { throw new Error( `当前企业 (${corp.name}) 尚未添加合思发布配置。\n` + `👉 请先运行配置命令:seal-use deploy hosecloud configure --json '{...}' --corp ${corp.id}` ); } console.error(`Usage: seal-use deploy hosecloud get [--corp ${corp.id}]\n`); console.error(`ℹ️ 当前企业发现以下可用的合思配置卡片:`); for (const item of hoseConfigs) { const name = item.outboundName ?? "SealAI 审批"; const key = item.signKey; const origin = item.apiOrigin ?? "production"; console.error(` • "${name}" (signKey: ${key}, 环境: ${origin})`); console.error(` 👉 查看命令: seal-use deploy hosecloud get ${key} --corp ${corp.id}\n`); } throw new CliError( "缺少必需参数 。请从上述候选列表中选择并指定 signKey。", 2, hoseConfigs.length === 1 ? `seal-use deploy hosecloud get ${hoseConfigs[0]!.signKey} --corp ${corp.id}` : undefined ); } await runCapability("sealDeployHosecloudGet", client, corp, { signKey }, outputOptions(args.options)); return; } if (action === "status") { await runCapability("sealDeployHosecloudStatus", client, corp, {}, outputOptions(args.options)); return; } if (action === "configure") { const rawInput = stringOption(args.options.input); const payload = rawInput ? JSON.parse(readFileSync(rawInput, "utf8")) : jsonObject(args.json); if (!payload || Object.keys(payload).length === 0) { throw new Error("Usage: seal-use deploy hosecloud configure --json '{...}' or --input "); } await runCapability("sealDeployHosecloudConfigure", client, corp, { ...payload, dryRun: booleanOption(args.options.dryRun) ?? booleanOption(args.options["dry-run"]), confirm: booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true }, outputOptions(args.options)); return; } if (action === "sync") { const signKey = target ?? stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]); const dryRun = booleanOption(args.options.dryRun) ?? booleanOption(args.options["dry-run"]); const confirm = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; const autoOpt = booleanOption(args.options.auto) ?? confirm; if (!signKey) { const config = await api.getApprovalStylePreferences(client, corp.seal.endpoints.approvalStylePreferences); const hoseConfigs = listHoseIntegrations(config); if (hoseConfigs.length === 0) { throw new Error( `当前企业 (${corp.name}) 尚未添加合思发布配置。\n` + `👉 请先运行配置命令:seal-use deploy hosecloud configure --json '{...}' --corp ${corp.id}` ); } const firstHose = hoseConfigs[0]; if (firstHose && hoseConfigs.length === 1 && autoOpt) { const singleKey = firstHose.signKey; console.error(`ℹ️ [auto] 检测到企业仅有 1 个合思配置,已自动选择 (signKey: ${singleKey})`); await runCapability("sealDeployHosecloudSync", client, corp, { signKey: singleKey, dryRun, confirm }, outputOptions(args.options)); return; } console.error(`Usage: seal-use deploy hosecloud sync [--dry-run] [--yes|--confirm] [--corp ${corp.id}]\n`); console.error(`ℹ️ 当前企业发现以下可用的合思配置卡片:`); for (const item of hoseConfigs) { const name = item.outboundName ?? "SealAI 审批"; const key = item.signKey; const origin = item.apiOrigin ?? "production"; console.error(` • "${name}" (signKey: ${key}, 环境: ${origin})`); console.error(` 👉 同步预览: seal-use deploy hosecloud sync ${key} --dry-run --corp ${corp.id}\n`); } if (hoseConfigs.length === 1) { console.error(`💡 提示:若希望自动选择单配置,可使用:seal-use deploy hosecloud sync --auto --dry-run --corp ${corp.id}\n`); } throw new CliError( "缺少必需参数 。请从上述候选列表中选择并指定 signKey 执行同步。", 2, hoseConfigs.length === 1 ? `seal-use deploy hosecloud sync ${hoseConfigs[0]!.signKey} --dry-run --corp ${corp.id}` : undefined ); } await runCapability("sealDeployHosecloudSync", client, corp, { signKey, dryRun, confirm }, outputOptions(args.options)); return; } if (action === "test-outbound" || action === "outbound-test") { const signKey = target ?? stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]); const flowId = stringOption(args.options.flowId) ?? stringOption(args.options["flow-id"]); const sn = stringOption(args.options.sn) ?? stringOption(args.options.sourceDocumentSN); const messageId = stringOption(args.options.messageId) ?? stringOption(args.options["message-id"]); const nodeId = stringOption(args.options.nodeId) ?? stringOption(args.options["node-id"]); const rejectionNum = numberOption(args.options.rejectionNum) ?? numberOption(args.options["rejection-num"]); const corporationId = stringOption(args.options.corporationId) ?? stringOption(args.options["corporation-id"]); const dryRun = booleanOption(args.options.dryRun) ?? booleanOption(args.options["dry-run"]); await runCapability( "sealDeployHosecloudTestOutbound", client, corp, { signKey, flowId, sn, messageId, nodeId, rejectionNum, corporationId, dryRun, confirm: booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true }, outputOptions(args.options) ); return; } throw new Error(`Unknown deploy hosecloud action: ${action ?? ""}. Available: requirements, list, get, status, configure, sync, test-outbound.`); }