feat(assist-platform): add approve command and support direct link generation for enter
This commit is contained in:
1 parent
e98948529e
commit
f0fc7148ec
11 files changed
+806
-455
No files matched your search
@@ -149,4 +149,26 @@ describe("CLI Commands - assist-platform (合思协助平台)", () => {
|
||||
expect(res.stdout).toContain("ID01KrLMQJWdMH");
|
||||
expect(res.stdout).toContain("12 小时");
|
||||
});
|
||||
|
||||
it("assist-platform approve --dry-run 能够输出工单审批变更预览", async () => {
|
||||
const runtime = createMockRuntime(tempDir);
|
||||
runtime.selectedCorp = {
|
||||
id: "ID01ofWoOC1wiX",
|
||||
name: "沈玥妍测试企业",
|
||||
corpId: "ID01ofWoOC1wiX",
|
||||
credentials: { accessToken: "mock-token" },
|
||||
} as any;
|
||||
|
||||
const parsed: CliParsedArgs = {
|
||||
action: "approve",
|
||||
args: ["ID01Xeno3GpoNV"],
|
||||
options: { dryRun: true },
|
||||
raw: ["assist-platform", "approve", "ID01Xeno3GpoNV", "--dry-run"],
|
||||
};
|
||||
|
||||
const res = await handleAssistPlatformCommand(parsed, runtime);
|
||||
expect(res.stdout).toContain("[DRY-RUN]");
|
||||
expect(res.stdout).toContain("ID01Xeno3GpoNV");
|
||||
expect(res.stdout).toContain("同意授权");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,146 @@
|
||||
import { createSessionClient } from "../../../core/http/session-client.js";
|
||||
import {
|
||||
approveAssistTicket,
|
||||
listAssistTickets,
|
||||
} from "../../../domains/assist-platform/services.js";
|
||||
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
|
||||
import { stringOption } from "../../parser.js";
|
||||
import { printJson, printKeyValue } from "../../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||||
import { resolveAssistClient } from "./common.js";
|
||||
|
||||
export async function handleApprove(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const explicitCorpId =
|
||||
stringOption(parsed.options, ["corp", "corpid", "id"]) ||
|
||||
parsed.args[0] ||
|
||||
runtime.selectedCorp?.id;
|
||||
|
||||
if (!explicitCorpId) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr:
|
||||
"用法: hose-use assist-platform approve [工单ID] [--corp <企业ID>] [--dry-run/--yes]\n" +
|
||||
"示例: hose-use assist-platform approve ID01Xeno3GpoNV --corp ID01ofWoOC1wiX --yes\n" +
|
||||
" hose-use assist-platform approve --corp ID01ofWoOC1wiX --yes (自动审批最新待授权工单)\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
// 1. 识别 corpId 与 ticketId
|
||||
const allCorps = runtime.corpContext?.getAllCorps?.() || [];
|
||||
let targetCorp =
|
||||
allCorps.find((c) => c.id === explicitCorpId || c.corpId === explicitCorpId) ||
|
||||
(runtime.selectedCorp?.id === explicitCorpId || runtime.selectedCorp?.corpId === explicitCorpId
|
||||
? runtime.selectedCorp
|
||||
: undefined);
|
||||
|
||||
let ticketId = stringOption(parsed.options, ["ticket", "ticket-id"]);
|
||||
|
||||
// 如果第 0 个参数不是企业,可能是工单编号
|
||||
if (!targetCorp && parsed.args[0]) {
|
||||
ticketId = parsed.args[0];
|
||||
targetCorp = runtime.selectedCorp ?? undefined;
|
||||
} else if (targetCorp && parsed.args[1]) {
|
||||
ticketId = parsed.args[1];
|
||||
}
|
||||
|
||||
if (!targetCorp) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] 未在本地企业配置中找到企业 [${explicitCorpId}]。审批工单需要企业管理员权限凭证,请先确保目标企业已配置在本地并具备管理员会话。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const corpName = targetCorp.name || targetCorp.id;
|
||||
const actualCorpId = targetCorp.corpId || targetCorp.id;
|
||||
|
||||
// 2. 若未显式传入工单编号,自动查询该企业当前处于【待提供】(status: 0) 的工单
|
||||
if (!ticketId) {
|
||||
try {
|
||||
const assistClient = resolveAssistClient(parsed, runtime);
|
||||
const tickets = await listAssistTickets(assistClient, actualCorpId);
|
||||
const pendingTicket = tickets.find(
|
||||
(t) => t.state === "0" || t.state.includes("待提供")
|
||||
);
|
||||
if (pendingTicket?.ticketId) {
|
||||
ticketId = pendingTicket.ticketId;
|
||||
}
|
||||
} catch {
|
||||
// 容错降级
|
||||
}
|
||||
}
|
||||
|
||||
if (!ticketId) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[WARN] 企业 [${corpName}] (${actualCorpId}) 协助中心未查询到待授权的工单。可指定 --ticket <工单ID>,或使用 hose-use assist-platform tickets ${actualCorpId} 查看工单列表。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const nextConfirmCmd = `hose-use assist-platform approve ${ticketId} --corp ${actualCorpId} --yes`;
|
||||
|
||||
// 3. 写操作两阶段确认守卫
|
||||
if (isDryRun(parsed, runtime)) {
|
||||
return mutationPreview(
|
||||
parsed,
|
||||
runtime,
|
||||
"审批通过合思协助授权申请",
|
||||
`${corpName} (${actualCorpId})`,
|
||||
{
|
||||
工单编号: ticketId,
|
||||
审批动作: "同意授权 (通过并生效)",
|
||||
授权范围: "允许运维与技术实施人员在有效期限内免密进入系统协助",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
requireMutationConfirmation(
|
||||
parsed,
|
||||
runtime,
|
||||
`审批通过企业 [${corpName}] 的协助授权工单 [${ticketId}]`,
|
||||
nextConfirmCmd
|
||||
);
|
||||
|
||||
// 4. 调用企业会话客户端执行审批
|
||||
const sessionClient = await createSessionClient(targetCorp);
|
||||
const res = await approveAssistTicket(sessionClient, {
|
||||
ticketId,
|
||||
corporationId: actualCorpId,
|
||||
status: 1,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(res, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const startStr = res.startTime ? new Date(res.startTime).toLocaleString("zh-CN", { hour12: false }) : "-";
|
||||
const endStr = res.endTime ? new Date(res.endTime).toLocaleString("zh-CN", { hour12: false }) : "-";
|
||||
|
||||
const pairs: Record<string, string> = {
|
||||
企业名称: corpName,
|
||||
企业ID: res.corporationId,
|
||||
授权工单: res.id,
|
||||
工单状态: "🟢 生效中 (授权已开启)",
|
||||
有效期限: `${startStr} 至 ${endStr}`,
|
||||
申请事项: res.applyMessage || "-",
|
||||
申请人: res.userName || "运营实施人员",
|
||||
};
|
||||
|
||||
return {
|
||||
stdout:
|
||||
`====== 合思协助工单授权通过成功 ======\n${printKeyValue(pairs)}\n\n后续步骤:\n` +
|
||||
` 1. 工单已处于生效状态,可直接换取免密链接调试: hose-use assist-platform enter ${actualCorpId} --save --yes\n` +
|
||||
` 2. 查看实时工单列表: hose-use assist-platform tickets ${actualCorpId}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { handleTickets } from "./tickets.js";
|
||||
import { handleStaff } from "./staff.js";
|
||||
import { handleEnter } from "./enter.js";
|
||||
import { handleApply } from "./apply.js";
|
||||
import { handleApprove } from "./approve.js";
|
||||
|
||||
export async function handleAssistPlatformCommand(
|
||||
parsed: CliParsedArgs,
|
||||
@@ -53,6 +54,11 @@ export async function handleAssistPlatformCommand(
|
||||
case "new":
|
||||
return handleApply(parsed, runtime);
|
||||
|
||||
case "approve":
|
||||
case "authorize":
|
||||
case "pass":
|
||||
return handleApprove(parsed, runtime);
|
||||
|
||||
case "help":
|
||||
case "--help":
|
||||
case "-h":
|
||||
@@ -76,6 +82,7 @@ function printHelp(): CliResult {
|
||||
tickets <企业ID> 查询指定企业的协助工单(申请人、授权码、剩余时长、工单状态)
|
||||
staff <企业ID> <姓名> 查询企业内部指定员工的工号、账号、邮箱及 admin 属性
|
||||
apply <企业ID> 向目标企业发起新的协助授权工单申请(待客户管理员审批)
|
||||
approve [工单ID] 审批通过协助授权工单申请(端内管理员通过授权)
|
||||
enter <企业ID> 一键换取协助免密直通链接并进入(可选 --save 落盘为本地工作企业)
|
||||
|
||||
常用选项:
|
||||
|
||||
Reference in new issue
Block a user