perf: optimize organization admin check (#17683)

This commit is contained in:
Omar López authored and GitHub committed 2024-11-16 18:26:23 +00:00
1 parent af9bf14d9e
commit 2c57ad0288
2 files changed
+12 -5

No files matched your search

@@ -375,6 +375,15 @@ export class ProfileRepository {
lockEventTypeCreationForUsers: true,
},
},
members: {
distinct: ["role"],
select: membershipSelect,
where: {
accepted: true,
// Filter out memberships that are not owned by the user
user: { profiles: { some: { id } } },
},
},
},
},
},
@@ -102,10 +102,8 @@ export async function getUserFromSession(ctx: TRPCContextInner, session: Maybe<S
// This helps to prevent reaching the 4MB payload limit by avoiding base64 and instead passing the avatar url
const locale = user?.locale ?? ctx.locale;
const isOrgAdmin = await UserRepository.isAdminOrOwnerOfTeam({
userId: user.id,
teamId: user.profile.organization?.id ?? -1,
});
const { members = [], ..._organization } = user.profile?.organization || {};
const isOrgAdmin = members.some((member) => ["OWNER", "ADMIN"].includes(member.role));
if (isOrgAdmin) {
logger.debug("User is an org admin", safeStringify({ userId: user.id }));
@@ -113,7 +111,7 @@ export async function getUserFromSession(ctx: TRPCContextInner, session: Maybe<S
logger.debug("User is not an org admin", safeStringify({ userId: user.id }));
}
const organization = {
...user.profile?.organization,
..._organization,
id: user.profile?.organization?.id ?? null,
isOrgAdmin,
metadata: orgMetadata,