documenso/packages/lib/server-only/team/get-user-team-groups.ts
Catalin Pit c4754553c9
feat: implement template search functionality (#2376)
- Added  function to handle template searches based on user input
- Introduced in the TRPC router to facilitate authenticated template
searches
- Updated to include template search results alongside document search
results
- Enhanced query handling by enabling searches only when the input is
valid
- Created corresponding Zod schemas for request and response validation
in
2026-03-09 10:44:51 +11:00

46 lines
1 KiB
TypeScript

import type { TeamGroup } from '@prisma/client';
import { prisma } from '@documenso/prisma';
export type GetUserTeamIdsOptions = {
userId: number;
};
/**
* Pre-resolve all team groups a user has access to via their organisation group memberships,
* keyed by team ID.
*
* This is significantly cheaper than joining team groups inline in a Prisma `findMany`
* because it avoids deep EXISTS subqueries and redundant LEFT JOINs per row.
*/
export const getUserTeamGroups = async ({
userId,
}: GetUserTeamIdsOptions): Promise<Map<number, TeamGroup[]>> => {
const teamGroups = await prisma.teamGroup.findMany({
where: {
organisationGroup: {
organisationGroupMembers: {
some: {
organisationMember: {
userId,
},
},
},
},
},
});
const map = new Map<number, TeamGroup[]>();
for (const tg of teamGroups) {
const existing = map.get(tg.teamId);
if (existing) {
existing.push(tg);
} else {
map.set(tg.teamId, [tg]);
}
}
return map;
};