mirror of
https://github.com/documenso/documenso
synced 2026-04-21 13:27:18 +00:00
- 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
46 lines
1 KiB
TypeScript
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;
|
|
};
|