fix: move team availability search to serverside (#17549)
* move search to severside * transform zod stirng to lowercase
This commit is contained in:
1 parent
5ae6e1e983
commit
4976e24e12
3 files changed
+57
-4
No files matched your search
+48
-2
@@ -25,18 +25,29 @@ async function getTeamMembers({
|
||||
teamIds,
|
||||
cursor,
|
||||
limit,
|
||||
searchString,
|
||||
}: {
|
||||
teamId?: number;
|
||||
organizationId: number | null;
|
||||
teamIds?: number[];
|
||||
cursor: number | null | undefined;
|
||||
limit: number;
|
||||
searchString?: string | null;
|
||||
}) {
|
||||
const memberships = await prisma.membership.findMany({
|
||||
where: {
|
||||
teamId: {
|
||||
in: teamId ? [teamId] : teamIds,
|
||||
},
|
||||
...(searchString
|
||||
? {
|
||||
OR: [
|
||||
{ user: { username: { contains: searchString } } },
|
||||
{ user: { name: { contains: searchString } } },
|
||||
{ user: { email: { contains: searchString } } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
@@ -128,13 +139,22 @@ async function buildMember(member: Member, dateFrom: Dayjs, dateTo: Dayjs) {
|
||||
}
|
||||
|
||||
async function getInfoForAllTeams({ ctx, input }: GetOptions) {
|
||||
const { cursor, limit } = input;
|
||||
const { cursor, limit, searchString } = input;
|
||||
|
||||
// Get all teamIds for the user
|
||||
const teamIds = await prisma.membership
|
||||
.findMany({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
...(searchString
|
||||
? {
|
||||
OR: [
|
||||
{ user: { username: { contains: searchString } } },
|
||||
{ user: { name: { contains: searchString } } },
|
||||
{ user: { email: { contains: searchString } } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
@@ -152,6 +172,7 @@ async function getInfoForAllTeams({ ctx, input }: GetOptions) {
|
||||
organizationId: ctx.user.organizationId,
|
||||
cursor,
|
||||
limit,
|
||||
searchString,
|
||||
});
|
||||
|
||||
// Get total team count across all teams the user is in (for pagination)
|
||||
@@ -169,7 +190,7 @@ async function getInfoForAllTeams({ ctx, input }: GetOptions) {
|
||||
}
|
||||
|
||||
export const listTeamAvailabilityHandler = async ({ ctx, input }: GetOptions) => {
|
||||
const { cursor, limit } = input;
|
||||
const { cursor, limit, searchString } = input;
|
||||
const teamId = input.teamId || ctx.user.organizationId;
|
||||
|
||||
let teamMembers: Member[] = [];
|
||||
@@ -198,6 +219,15 @@ export const listTeamAvailabilityHandler = async ({ ctx, input }: GetOptions) =>
|
||||
totalTeamMembers = await prisma.membership.count({
|
||||
where: {
|
||||
teamId: teamId,
|
||||
...(searchString
|
||||
? {
|
||||
OR: [
|
||||
{ user: { username: { contains: searchString } } },
|
||||
{ user: { name: { contains: searchString } } },
|
||||
{ user: { email: { contains: searchString } } },
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -207,6 +237,7 @@ export const listTeamAvailabilityHandler = async ({ ctx, input }: GetOptions) =>
|
||||
cursor,
|
||||
limit,
|
||||
organizationId: ctx.user.organizationId,
|
||||
searchString,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -224,11 +255,26 @@ export const listTeamAvailabilityHandler = async ({ ctx, input }: GetOptions) =>
|
||||
|
||||
const members = await Promise.all(buildMembers);
|
||||
|
||||
let belongsToTeam = true;
|
||||
|
||||
if (totalTeamMembers === 0) {
|
||||
const membership = await prisma.membership.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
belongsToTeam = !!membership;
|
||||
}
|
||||
|
||||
return {
|
||||
rows: members || [],
|
||||
nextCursor,
|
||||
meta: {
|
||||
totalRowCount: totalTeamMembers,
|
||||
isApartOfAnyTeam: belongsToTeam,
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -7,6 +7,7 @@ export const ZListTeamAvailaiblityScheme = z.object({
|
||||
endDate: z.string(),
|
||||
loggedInUsersTz: z.string(),
|
||||
teamId: z.number().optional(),
|
||||
searchString: z.string().toLowerCase().optional(),
|
||||
});
|
||||
|
||||
export type TListTeamAvailaiblityScheme = z.infer<typeof ZListTeamAvailaiblityScheme>;
|
||||
Reference in new issue
Block a user