forked from bai/curriculum-project-hub
36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
/**
|
|
* 默认 GroupResolver 实现:读 in-hub MemberGroup 闭包(ADR-0028)。
|
|
*
|
|
* resolveMemberGroupIds(user) = 用户**活跃直接组 ∪ 这些组的活跃祖先**,去重
|
|
* (闭包 depth0 自身行令每个直接组也是自己的祖先)。等价于:授权放在组 G 上,
|
|
* G 及其全部子孙的成员都命中(需求 3.2 权限沿树向下 → 解析沿树向上收集)。
|
|
*
|
|
* 实时、不缓存(契约 D4/G4):成员变更在下一次受保护请求即可见。
|
|
* MemberGroup 全局(无 organizationId),解析不做 org scope。
|
|
* 两条 Prisma 查询,不用裸 SQL(与 treeService 风格一致)。
|
|
*/
|
|
|
|
import type { PrismaClient } from "@prisma/client";
|
|
import type { GroupResolver } from "./groupResolver.js";
|
|
|
|
export function createMemberGroupResolver(prisma: PrismaClient): GroupResolver {
|
|
return {
|
|
async resolveMemberGroupIds(userId) {
|
|
// 1) 活跃直接组:成员未撤销 + 组未归档。
|
|
const direct = await prisma.memberGroupMembership.findMany({
|
|
where: { userId, revokedAt: null, group: { archivedAt: null } },
|
|
select: { groupId: true },
|
|
});
|
|
if (direct.length === 0) return [];
|
|
const directIds = direct.map((m) => m.groupId);
|
|
|
|
// 2) 经闭包取活跃祖先(含 depth0 自身);祖先组须未归档。
|
|
const ancestors = await prisma.memberGroupClosure.findMany({
|
|
where: { descendantId: { in: directIds }, ancestor: { archivedAt: null } },
|
|
select: { ancestorId: true },
|
|
});
|
|
return [...new Set(ancestors.map((a) => a.ancestorId))];
|
|
},
|
|
};
|
|
}
|