fix: preserve explicit assist ticket and reject ambiguous recipients

This commit is contained in:
gitea committed 2026-10-21 16:04:57 +08:00
1 parent 3597ec6694
commit 6ce83dd8a7
7 files changed
+339 -56

No files matched your search

@@ -35,3 +35,9 @@
- 已知管理员、角色或高权限证据仍显示 HIGH/MEDIUM;导入时补用 JWT 已确认的身份。同一员工的历史权限及高风险状态不会因本次探测不完整而被降级,其他员工的画像不会被沿用。
SDK 消费者应处理画像布尔值的 null 和新增的 UNKNOWN 风险枚举;会话结果顶层 `isAdmin` 仍表示请求的授权模式,其 boolean 类型不变。
## 审批与转交目标解析
- `approve <工单ID> --corp <企业ID>`:显式 `--corp` 下,唯一位置参数始终为工单 ID;也支持 `approve <企业ID> <工单ID>`、`approve <企业ID> --ticket <工单ID>`,以及使用当前企业的 `approve <工单ID>` / `approve --ticket <工单ID>`。位置工单与 `--ticket` 不一致或位置参数多余时拒绝执行;只有完全未指定工单时才自动查询待授权工单。
- 转交收件人依次按账号 ID、工号、姓名精确匹配,再尝试模糊匹配。同一账号的多条角色记录按用户 ID 去重;同一匹配级别命中多个用户时拒绝选择,须改用唯一工号或账号。
- 转交的 dry-run 与执行使用相同的收件人解析规则。歧义、未找到人员或查询失败都会终止,不会将未解析的输入直接提交给转交接口。
+26 -25
View File
@@ -431,31 +431,39 @@ export async function resolveTransferRecipient(
}
const candidates = await listTransferCandidates(client);
const exactMatches = candidates.filter(
(c) => c.name === cleanTarget || c.code === cleanTarget || c.id === cleanTarget
);
if (exactMatches.length === 1) {
return exactMatches[0];
}
if (exactMatches.length > 1) {
return exactMatches[0];
// 一个用户可能有多条角色记录;先匹配,再按稳定账号 ID 去重。
const uniqueUsers = (matches: TransferCandidate[]) =>
[...new Map(matches.map((candidate) => [candidate.id, candidate])).values()];
const rejectAmbiguous = (matches: TransferCandidate[]): never => {
const names = matches
.slice(0, 5)
.map((m) => `${m.name}(${m.id}${m.code ? `, 工号: ${m.code}` : ""})`)
.join("、");
throw new BusinessError(`匹配到多个可转交人员 [${names}],请指定唯一工号或账号`);
};
// 账号 ID 优先于工号、姓名,避免一个人的姓名覆盖另一个人的明确账号。
for (const matches of [
candidates.filter((c) => c.id === cleanTarget),
candidates.filter((c) => c.code === cleanTarget),
candidates.filter((c) => c.name === cleanTarget),
]) {
const users = uniqueUsers(matches);
if (users.length === 1) return users[0];
if (users.length > 1) rejectAmbiguous(users);
}
const fuzzyMatches = candidates.filter(
const fuzzyMatches = uniqueUsers(candidates.filter(
(c) =>
c.name.includes(cleanTarget) ||
(c.code && c.code.includes(cleanTarget)) ||
c.id.includes(cleanTarget)
);
));
if (fuzzyMatches.length === 1) {
return fuzzyMatches[0];
}
if (fuzzyMatches.length > 1) {
const names = fuzzyMatches
.slice(0, 5)
.map((m) => `${m.name}(${m.code || m.id})`)
.join("、");
throw new BusinessError(`匹配到多个可转交人员 [${names}],请指定具体工号或全名`);
rejectAmbiguous(fuzzyMatches);
}
throw new NotFoundError(`未在具备协助权限的人员库中找到匹配的人员: "${cleanTarget}"`);
@@ -469,16 +477,9 @@ export async function transferAssistTicket(
remarks?: string;
}
): Promise<TransferTicketResult> {
let resolvedRecipientId = params.recipient;
let recipientName: string | undefined;
try {
const candidate = await resolveTransferRecipient(client, params.recipient);
resolvedRecipientId = candidate.id;
recipientName = candidate.name;
} catch {
// 若解析不到,仍尝试直接以原值请求
}
const candidate = await resolveTransferRecipient(client, params.recipient);
const resolvedRecipientId = candidate.id;
const recipientName = candidate.name;
await transferAssistTicketApi(client, {
ticketId: params.ticketId,
@@ -0,0 +1,98 @@
import { describe, expect, it } from "bun:test";
import ky from "ky";
import { BusinessError, EkbApiError, NotFoundError } from "../../core/errors.js";
import { resolveTransferRecipient, transferAssistTicket } from "./services.js";
type User = { id: string; name: string; code?: string };
const first: User = { id: "zhang3016", name: "张三", code: "3016" };
const second: User = { id: "zhang3020", name: "张三", code: "3020" };
function mockClient(users: User[], failSearch = false) {
const writes: unknown[] = [];
const client = ky.create({
prefix: "https://assist.invalid/api",
retry: 0,
fetch: async (input, init) => {
const request = input instanceof Request ? input : new Request(input.toString(), init);
if (request.url.endsWith("userRole/search")) {
if (failSearch) return new Response("unavailable", { status: 503 });
return Response.json({ items: users.map((userId, i) => ({
id: `role-row-${i}`,
userId,
roleIds: [{ id: `role-${i}`, name: `角色${i}` }],
})) });
}
if (request.url.endsWith("assistPlatform/transfer")) {
writes.push(await request.json());
return Response.json({ value: true });
}
throw new Error(`Unexpected request: ${request.url}`);
},
});
return { client, writes };
}
describe("assist-platform 转交目标解析", () => {
it("同名不同账号拒绝精确匹配,错误提示唯一工号或账号", async () => {
const { client } = mockClient([first, second]);
await expect(resolveTransferRecipient(client, "张三")).rejects.toBeInstanceOf(BusinessError);
await expect(resolveTransferRecipient(client, "张三")).rejects.toThrow("唯一工号或账号");
});
it("相同工号的不同用户也拒绝选择第一人", async () => {
const { client } = mockClient([first, { ...second, code: first.code }]);
await expect(resolveTransferRecipient(client, "3016")).rejects.toThrow("多个可转交人员");
});
it("账号精确匹配优先于其他人的工号和姓名", async () => {
const { client } = mockClient([
{ ...second, code: first.id },
{ id: "other", name: first.id },
first,
]);
expect((await resolveTransferRecipient(client, first.id)).id).toBe(first.id);
});
it("工号精确匹配优先于其他人的姓名", async () => {
const { client } = mockClient([{ ...second, name: "3016" }, first]);
expect((await resolveTransferRecipient(client, "3016")).id).toBe(first.id);
});
it.each(["张三", "3016", "zhang3016", "zhang", " 3016 "])(
"同一稳定账号的多条角色记录不会令 %s 产生歧义",
async (target) => {
const { client } = mockClient([first, first]);
expect((await resolveTransferRecipient(client, target)).id).toBe(first.id);
}
);
it("模糊匹配存在多个用户时拒绝", async () => {
const { client } = mockClient([first, second]);
await expect(resolveTransferRecipient(client, "zhang")).rejects.toThrow("多个可转交人员");
});
it.each(["张三", "不存在的账号"])("执行转交时 %s 解析失败不提交原始值", async (recipient) => {
const { client, writes } = mockClient([first, second]);
const ErrorClass = recipient === "张三" ? BusinessError : NotFoundError;
await expect(transferAssistTicket(client, { ticketId: "TICKET", recipient }))
.rejects.toBeInstanceOf(ErrorClass);
expect(writes).toEqual([]);
});
it("查询候选人失败时不以原始账号继续写入", async () => {
const { client, writes } = mockClient([], true);
await expect(transferAssistTicket(client, { ticketId: "TICKET", recipient: first.id }))
.rejects.toBeInstanceOf(EkbApiError);
expect(writes).toEqual([]);
});
it("使用唯一工号时写入稳定账号 ID", async () => {
const { client, writes } = mockClient([first, first, second]);
const result = await transferAssistTicket(client, {
ticketId: "TICKET", recipient: "3016", remarks: "交接",
});
expect(result.recipient).toBe(first.id);
expect(result.recipientName).toBe(first.name);
expect(writes).toEqual([{ assistId: "TICKET", recipient: first.id, remarks: "交接" }]);
});
});