Files
hose-use/src/domains/contact/tools.ts
T

69 lines
2.3 KiB
TypeScript

import { getEasoClient } from "../../core/http/unity-client.js";
import type { CorpConfig } from "../../core/config/types.js";
import { fetchStaffs } from "./api.js";
import type { ContactListResult, ContactSearchOptions, Staff } from "./types.js";
export const DEFAULT_STAFF_LIST_LIMIT = 20;
export async function listStaffsProtected(
corp: CorpConfig,
options: ContactSearchOptions = {}
): Promise<ContactListResult> {
const { client } = await getEasoClient(corp);
const allStaffs = await fetchStaffs(client, options.external ?? false);
const total = allStaffs.length;
const isFull = options.allowFull === true;
const limit = isFull ? total : options.limit ?? DEFAULT_STAFF_LIST_LIMIT;
const staffs = allStaffs.slice(0, limit);
const hasMore = total > staffs.length;
return {
total,
limit,
hasMore,
hint: hasMore
? `企业员工共 ${total} 人,当前仅展示前 ${staffs.length} 条以防止上下文溢出。可使用 'hose-use contact search <姓名|工号>' 精准检索,或附加 '--limit <数量>' / '-o <文件路径>' / '--full'`
: undefined,
staffs,
};
}
export async function searchStaffs(
corp: CorpConfig,
options: { keyword: string; limit?: number; external?: boolean }
): Promise<Staff[]> {
const { client } = await getEasoClient(corp);
const allStaffs = await fetchStaffs(client, options.external ?? false);
const kw = options.keyword.trim().toLowerCase();
if (!kw) {
return allStaffs.slice(0, options.limit ?? DEFAULT_STAFF_LIST_LIMIT);
}
const matched = allStaffs.filter((s) => {
const name = s.name?.toLowerCase() || "";
const email = s.email?.toLowerCase() || "";
const phone = s.cellphone || "";
const id = s.id?.toLowerCase() || "";
return name.includes(kw) || email.includes(kw) || phone.includes(kw) || id.includes(kw);
});
return matched.slice(0, options.limit ?? 50);
}
export async function getStaffById(
corp: CorpConfig,
staffId: string
): Promise<Staff | null> {
const { client } = await getEasoClient(corp);
const allStaffs = await fetchStaffs(client, false);
const cleanId = staffId.trim();
const found = allStaffs.find(
(s) => s.id === cleanId || s.id.endsWith(`:${cleanId}`) || s.id.split(":")[1] === cleanId
);
return found || null;
}