-
Notifications
You must be signed in to change notification settings - Fork 12k
perf: Replace isTeamMember util with an index DB call
#24066
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import { enrichUserWithDelegationCredentialsIncludeServiceAccountKey } from "@calcom/lib/delegationCredential/server"; | ||
| import { getUserAvailabilityService } from "@calcom/lib/di/containers/GetUserAvailability"; | ||
| import { isTeamMember } from "@calcom/features/ee/teams/lib/queries"; | ||
| import { MembershipRepository } from "@calcom/lib/server/repository/membership"; | ||
| import type { TrpcSessionUser } from "@calcom/trpc/server/types"; | ||
|
|
||
|
|
@@ -17,8 +16,16 @@ type GetMemberAvailabilityOptions = { | |
|
|
||
| export const getMemberAvailabilityHandler = async ({ ctx, input }: GetMemberAvailabilityOptions) => { | ||
| const userAvailabilityService = getUserAvailabilityService(); | ||
| const team = await isTeamMember(ctx.user?.id, input.teamId); | ||
| if (!team) throw new TRPCError({ code: "UNAUTHORIZED" }); | ||
|
|
||
| const membership = await MembershipRepository.findUniqueByUserIdAndTeamId({ | ||
| userId: ctx.user.id, | ||
| teamId: input.teamId, | ||
| accepted: true, | ||
| }); | ||
|
|
||
| if (!membership) { | ||
| throw new TRPCError({ code: "UNAUTHORIZED", message: "User is not a member of this team" }); | ||
| } | ||
|
Comment on lines
+20
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix membership lookup:
Please switch the repository helper to use - return await prisma.membership.findUnique({
- where: {
- userId_teamId: {
- userId,
- teamId,
- },
- accepted,
- },
- });
+ return await prisma.membership.findFirst({
+ where: {
+ userId,
+ teamId,
+ ...(accepted !== undefined ? { accepted } : {}),
+ },
+ });Until that happens this handler will remain unusable.
|
||
|
|
||
| // verify member is in team | ||
| const members = await MembershipRepository.findByTeamIdForAvailability({ teamId: input.teamId }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the Prisma
findUniquemisuse with theacceptedfilter.prisma.membership.findUniqueonly accepts a unique selector; addingacceptedalongside theuserId_teamIdcomposite key makes the query invalid (MembershipWhereUniqueInputrejects non-unique fields). With the current change, any call passingaccepted: truewill throw anUnknown arg 'accepted'error at runtime. Please switch to afindFirst(or split the check) so the optionalacceptedpredicate is respected.📝 Committable suggestion
🤖 Prompt for AI Agents