feat(assist-platform): add ticket application command and enforce local config trust rules
This commit is contained in:
1 parent
2e6fe1a94a
commit
1e45c441c6
10 files changed
+661
-387
No files matched your search
@@ -780,6 +780,7 @@ export const COMMAND_METADATA: Record<string, CliCommandMetadata> = {
|
||||
"detail": "查看企业详细业务配置与开通商品能力",
|
||||
"tickets": "查询指定企业的协助工单与授权码",
|
||||
"staff": "查询企业内部员工工号与角色",
|
||||
"apply": "向目标企业发起新的协助授权工单申请",
|
||||
"enter": "一键换取免密直通链接并落盘为本地工作企业",
|
||||
"help": "查看帮助信息"
|
||||
},
|
||||
@@ -800,12 +801,16 @@ export const COMMAND_METADATA: Record<string, CliCommandMetadata> = {
|
||||
"orders": "tickets",
|
||||
"user": "staff",
|
||||
"users": "staff",
|
||||
"apply": "apply",
|
||||
"create": "apply",
|
||||
"request": "apply",
|
||||
"new": "apply",
|
||||
"import": "enter",
|
||||
"token": "enter",
|
||||
"get-token": "enter"
|
||||
},
|
||||
flags: {
|
||||
...flagList("token t user name u hose-token jwt ticket ticket-id auth-code staff alias corp-name count limit n start offset state status corpid id keyword query q", "value"),
|
||||
...flagList("token t user name u hose-token jwt ticket ticket-id auth-code staff alias corp-name count limit n start offset state status corpid id keyword query q message msg reason matter hours hour duration", "value"),
|
||||
...flagList("logout admin save import persist switch current overwrite", "boolean")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,4 +127,26 @@ describe("CLI Commands - assist-platform (合思协助平台)", () => {
|
||||
expect(res.stdout).toContain("[DRY-RUN]");
|
||||
expect(res.stdout).toContain("Cxa3oOtQi2eS$g");
|
||||
});
|
||||
|
||||
it("assist-platform apply --dry-run 能够输出申请工单变更预览", async () => {
|
||||
saveAssistPlatformSession({
|
||||
token: "mock-token",
|
||||
userName: "测试人员",
|
||||
baseUrl: "https://unity.ekuaibao.com/api",
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
|
||||
const runtime = createMockRuntime(tempDir);
|
||||
const parsed: CliParsedArgs = {
|
||||
action: "apply",
|
||||
args: ["ID01KrLMQJWdMH"],
|
||||
options: { dryRun: true, hours: 12, message: "测试联调" },
|
||||
raw: ["assist-platform", "apply", "ID01KrLMQJWdMH", "--dry-run", "--hours", "12"],
|
||||
};
|
||||
|
||||
const res = await handleAssistPlatformCommand(parsed, runtime);
|
||||
expect(res.stdout).toContain("[DRY-RUN]");
|
||||
expect(res.stdout).toContain("ID01KrLMQJWdMH");
|
||||
expect(res.stdout).toContain("12 小时");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,99 @@
|
||||
import { applyAssistTicket } from "../../../domains/assist-platform/services.js";
|
||||
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
|
||||
import { numberOption, 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 handleApply(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const client = resolveAssistClient(parsed, runtime);
|
||||
const corpId =
|
||||
parsed.args[0] ||
|
||||
stringOption(parsed.options, ["corp", "corpid", "id"]) ||
|
||||
runtime.selectedCorp?.id;
|
||||
|
||||
if (!corpId) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "用法: hose-use assist-platform apply <企业ID> [--message <事由>] [--hours <小时数(1-24)>] [--dry-run/--yes]\n示例: hose-use assist-platform apply ID01KrLMQJWdMH --message \"测试环境接口联调\" --hours 24 --yes\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const hours = numberOption(parsed.options, ["hours", "hour", "duration"]) ?? 24;
|
||||
if (hours < 1 || hours > 24) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] 申请协助时长必须介于 1 到 24 小时之间,当前传入: ${hours}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const message =
|
||||
stringOption(parsed.options, ["message", "msg", "reason", "matter"]) ||
|
||||
"测试环境接口联调与问题排查";
|
||||
|
||||
const targetCorp =
|
||||
runtime.corpContext?.getAllCorps?.()?.find((c) => c.id === corpId || c.corpId === corpId) ||
|
||||
(runtime.selectedCorp?.id === corpId || runtime.selectedCorp?.corpId === corpId
|
||||
? runtime.selectedCorp
|
||||
: undefined);
|
||||
const corpName = targetCorp?.name || corpId;
|
||||
|
||||
const nextConfirmCmd = `hose-use assist-platform apply ${corpId} --message "${message}" --hours ${hours} --yes`;
|
||||
|
||||
// 写操作两阶段确认守卫
|
||||
if (isDryRun(parsed, runtime)) {
|
||||
return mutationPreview(
|
||||
parsed,
|
||||
runtime,
|
||||
"发起合思协助工单申请",
|
||||
`${corpName} (${corpId})`,
|
||||
{
|
||||
申请事项: message,
|
||||
申请时长: `${hours} 小时 (${hours * 3600 * 1000} ms)`,
|
||||
初始状态: "待提供 (需企业管理员在端内开启授权)",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
requireMutationConfirmation(
|
||||
parsed,
|
||||
runtime,
|
||||
`向企业 [${corpName}] 发起协助授权申请 (时长: ${hours}h, 事由: ${message})`,
|
||||
nextConfirmCmd
|
||||
);
|
||||
|
||||
const res = await applyAssistTicket(client, {
|
||||
corporationId: corpId,
|
||||
applyMessage: message,
|
||||
assistTime: hours * 3600 * 1000,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(res, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const pairs: Record<string, string> = {
|
||||
企业名称: corpName,
|
||||
企业ID: res.corporationId,
|
||||
新建工单ID: res.id,
|
||||
工单状态: "🟡 待提供 (等待客户管理员在企业端授权)",
|
||||
申请时长: `${hours} 小时`,
|
||||
申请事项: res.applyMessage,
|
||||
申请人: res.userName || "运营实施人员",
|
||||
};
|
||||
|
||||
return {
|
||||
stdout: `====== 合思协助工单申请提交成功 ======\n${printKeyValue(pairs)}\n\n后续步骤:\n 1. 提示企业管理员登录合思 Web/桌面端,在【远程协助/授权中心】确认开启授权;\n 2. 授权通过后工单变为【🟢 生效中】,即可运行: hose-use assist-platform enter ${corpId} --staff <员工姓名> --save --yes\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { handleDetail } from "./detail.js";
|
||||
import { handleTickets } from "./tickets.js";
|
||||
import { handleStaff } from "./staff.js";
|
||||
import { handleEnter } from "./enter.js";
|
||||
import { handleApply } from "./apply.js";
|
||||
|
||||
export async function handleAssistPlatformCommand(
|
||||
parsed: CliParsedArgs,
|
||||
@@ -46,6 +47,12 @@ export async function handleAssistPlatformCommand(
|
||||
case "get-token":
|
||||
return handleEnter(parsed, runtime);
|
||||
|
||||
case "apply":
|
||||
case "create":
|
||||
case "request":
|
||||
case "new":
|
||||
return handleApply(parsed, runtime);
|
||||
|
||||
case "help":
|
||||
case "--help":
|
||||
case "-h":
|
||||
@@ -68,6 +75,7 @@ function printHelp(): CliResult {
|
||||
detail <企业ID> 查看企业全量业务配置(套餐规格、基础参数、已开通商品扩展能力)
|
||||
tickets <企业ID> 查询指定企业的协助工单(申请人、授权码、剩余时长、工单状态)
|
||||
staff <企业ID> <姓名> 查询企业内部指定员工的工号、账号、邮箱及 admin 属性
|
||||
apply <企业ID> 向目标企业发起新的协助授权工单申请(待客户管理员审批)
|
||||
enter <企业ID> 一键换取协助免密直通链接并进入(可选 --save 落盘为本地工作企业)
|
||||
|
||||
常用选项:
|
||||
|
||||
@@ -121,6 +121,9 @@ export async function searchAssistTicketsApi(
|
||||
? `${new Date(startTime).toLocaleString("zh-CN", { hour12: false })} 至 ${new Date(endTime).toLocaleString("zh-CN", { hour12: false })}`
|
||||
: undefined;
|
||||
|
||||
const rawStatus = item.state ?? item.status;
|
||||
const stateStr = rawStatus !== undefined && rawStatus !== null ? String(rawStatus) : "未知状态";
|
||||
|
||||
return {
|
||||
id: String(item.id || item.ticketId || ""),
|
||||
ticketId: String(item.ticketId || item.id || item.authCode || ""),
|
||||
@@ -128,7 +131,7 @@ export async function searchAssistTicketsApi(
|
||||
applicantId: item.applicantId ? String(item.applicantId) : undefined,
|
||||
principal: item.principal ? String(item.principal) : (item.operationUserName ? String(item.operationUserName) : undefined),
|
||||
principalId: item.principalId ? String(item.principalId) : undefined,
|
||||
state: String(item.state || item.status || "未知状态"),
|
||||
state: stateStr,
|
||||
duration: typeof item.duration === "number" ? item.duration : (typeof item.hours === "number" ? item.hours : undefined),
|
||||
startTime,
|
||||
endTime,
|
||||
@@ -245,3 +248,64 @@ export async function getAssistTokenApi(
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreateAssistTicketApiParams {
|
||||
corporationId: string;
|
||||
applyMessage?: string;
|
||||
assistTime?: number; // 毫秒,默认 24 小时
|
||||
}
|
||||
|
||||
export interface CreateAssistTicketApiResponse {
|
||||
id: string;
|
||||
corporationId: string;
|
||||
applyMessage: string;
|
||||
assistTime: number;
|
||||
userName?: string;
|
||||
responsibleUserName?: string;
|
||||
status: number;
|
||||
}
|
||||
|
||||
export async function createAssistTicketApi(
|
||||
client: KyInstance,
|
||||
params: CreateAssistTicketApiParams
|
||||
): Promise<CreateAssistTicketApiResponse> {
|
||||
const { corporationId, applyMessage, assistTime } = params;
|
||||
if (!corporationId || !corporationId.trim()) {
|
||||
throw new BusinessError("必须指定目标企业 ID");
|
||||
}
|
||||
|
||||
const durationMs = assistTime ?? 24 * 3600 * 1000;
|
||||
const message = applyMessage?.trim() || "自动化联调与测试";
|
||||
|
||||
try {
|
||||
const res = await client
|
||||
.post("assistPlatform/create", {
|
||||
json: {
|
||||
corporationId: corporationId.trim(),
|
||||
applyMessage: message,
|
||||
assistTime: durationMs,
|
||||
},
|
||||
})
|
||||
.json<{
|
||||
value?: Record<string, unknown>;
|
||||
}>();
|
||||
|
||||
const val = res.value || {};
|
||||
return {
|
||||
id: String(val.id || ""),
|
||||
corporationId: String(val.corporationId || corporationId),
|
||||
applyMessage: String(val.applyMessage || message),
|
||||
assistTime: typeof val.assistTime === "number" ? val.assistTime : durationMs,
|
||||
userName: val.userName ? String(val.userName) : undefined,
|
||||
responsibleUserName: val.responsibleUserName ? String(val.responsibleUserName) : undefined,
|
||||
status: typeof val.status === "number" ? val.status : 0,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof Error && (error.name === "AuthenticationError" || error.name === "BusinessError")) {
|
||||
throw error;
|
||||
}
|
||||
throw new EkbApiError(
|
||||
`发起协助工单申请失败: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import ky from "ky";
|
||||
import {
|
||||
applyAssistTicket,
|
||||
findStaff,
|
||||
generateAssistSession,
|
||||
getCorporationDetail,
|
||||
@@ -191,4 +192,30 @@ describe("domains/assist-platform - 协助平台业务编排", () => {
|
||||
expect(result.isAdmin).toBe(false);
|
||||
expect(result.staffId).toBe("Cxa3oOtQi2eS$g:2HQbkGx2DgnM00");
|
||||
});
|
||||
|
||||
it("applyAssistTicket 发起新的协助工单申请", async () => {
|
||||
const client = createMockClient(async (req) => {
|
||||
expect(req.url).toContain("assistPlatform/create");
|
||||
const body = (await req.json()) as any;
|
||||
expect(body.corporationId).toBe("ID01KrLMQJWdMH");
|
||||
expect(body.assistTime).toBe(86400000);
|
||||
return Response.json({
|
||||
value: {
|
||||
id: "ID01XelIEeCbw3",
|
||||
corporationId: "ID01KrLMQJWdMH",
|
||||
applyMessage: "测试环境接口联调",
|
||||
assistTime: 86400000,
|
||||
status: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const res = await applyAssistTicket(client, {
|
||||
corporationId: "ID01KrLMQJWdMH",
|
||||
applyMessage: "测试环境接口联调",
|
||||
assistTime: 86400000,
|
||||
});
|
||||
expect(res.id).toBe("ID01XelIEeCbw3");
|
||||
expect(res.status).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -3,11 +3,14 @@ import { BusinessError, NotFoundError } from "../../core/errors.js";
|
||||
import { importFromLink, type ImportFromLinkOptions } from "../../core/auth/link-import.js";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import {
|
||||
createAssistTicketApi,
|
||||
getAssistTokenApi,
|
||||
getCorporationDetailApi,
|
||||
getStaffApi,
|
||||
searchAssistTicketsApi,
|
||||
searchCorporationsApi,
|
||||
type CreateAssistTicketApiParams,
|
||||
type CreateAssistTicketApiResponse,
|
||||
type SearchCorporationsApiParams,
|
||||
type SearchCorporationsApiResponse,
|
||||
} from "./api.js";
|
||||
@@ -48,6 +51,13 @@ export async function findStaff(
|
||||
return await getStaffApi(client, corpId, name);
|
||||
}
|
||||
|
||||
export async function applyAssistTicket(
|
||||
client: KyInstance,
|
||||
params: CreateAssistTicketApiParams
|
||||
): Promise<CreateAssistTicketApiResponse> {
|
||||
return await createAssistTicketApi(client, params);
|
||||
}
|
||||
|
||||
export interface GenerateAssistSessionParams {
|
||||
corpId: string;
|
||||
ticketId?: string;
|
||||
|
||||
Reference in new issue
Block a user