core: group api (#1168)

This commit is contained in:
Baoshuo authored and GitHub committed 2026-06-14 02:15:37 +08:00
1 parent e654a5b7e6
commit 81cf24106b
2 files changed
+15 -13

No files matched your search

+5 -11
View File
@@ -438,21 +438,15 @@ export const DomainApi = {
),
groups: Query(
Schema.object({
search: Schema.string(),
names: Schema.array(Schema.string()),
domainId: Schema.string().required(),
uid: Schema.number().step(1),
names: Schema.array(Schema.string()),
search: Schema.string(),
limit: Schema.number().step(1).max(100),
}),
async (ctx, args) => {
if (!ctx.user.hasPerm(PERM.PERM_VIEW) && !ctx.user.hasPriv(PRIV.PRIV_VIEW_ALL_DOMAIN)) throw new PermissionError(PERM.PERM_VIEW);
const groups = await user.listGroup(args.domainId);
if (args.names?.length) {
return groups.filter((g) => args.names.includes(g.name));
}
if (args.search) {
const searchLower = args.search.toLowerCase();
return groups.filter((g) => g.name.toLowerCase().includes(searchLower));
}
return groups;
return user.listGroup(args.domainId, args.uid, args.names, args.search, args.limit ?? 20);
},
),
'domain.group': Mutation(
+10 -2
View File
@@ -479,8 +479,16 @@ class UserModel {
]);
}
static async listGroup(domainId: string, uid?: number) {
const groups = await collGroup.find(typeof uid === 'number' ? { domainId, uids: uid } : { domainId }).toArray();
static async listGroup(domainId: string, uid?: number, names?: string[], search?: string, limit?: number) {
const filter: Filter<GDoc> = { domainId };
if (typeof uid === 'number') filter.uids = uid;
const $and: Filter<GDoc>[] = [];
if (names?.length) $and.push({ name: { $in: names } });
if (search) $and.push({ name: { $regex: escapeRegExp(search), $options: 'i' } });
if ($and.length) filter.$and = $and;
let cursor = collGroup.find(filter);
if (limit) cursor = cursor.limit(limit);
const groups = await cursor.toArray();
if (uid) {
groups.push({
_id: new ObjectId(), domainId, uids: [uid], name: uid.toString(),