Skip to content

Commit

Permalink
fix: Exclude locked users by default (calcom#17889)
Browse files Browse the repository at this point in the history
  • Loading branch information
zomars authored Nov 28, 2024
1 parent 4b71710 commit 693356d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
43 changes: 43 additions & 0 deletions packages/prisma/extensions/exclude-locked-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Prisma } from "@prisma/client";
import type { DefaultArgs, InternalArgs } from "@prisma/client/runtime/library";

export function excludeLockedUsersExtension() {
return Prisma.defineExtension({
query: {
user: {
async findUnique({ args, query }) {
return excludeLockedUsers(args, query);
},
async findFirst({ args, query }) {
return excludeLockedUsers(args, query);
},
async findMany({ args, query }) {
return excludeLockedUsers(args, query);
},
async findUniqueOrThrow({ args, query }) {
return excludeLockedUsers(args, query);
},
async findFirstOrThrow({ args, query }) {
return excludeLockedUsers(args, query);
},
},
},
});
}

async function excludeLockedUsers(
args:
| Prisma.UserFindUniqueArgs<InternalArgs & DefaultArgs>
| Prisma.UserFindFirstArgs<InternalArgs & DefaultArgs>
| Prisma.UserFindManyArgs<InternalArgs & DefaultArgs>
| Prisma.UserFindUniqueOrThrowArgs<InternalArgs & DefaultArgs>
| Prisma.UserFindFirstOrThrowArgs<InternalArgs & DefaultArgs>,
query: <T>(args: T) => Promise<unknown>
) {
args.where = args.where || {};
// Unless explicitly specified, we exclude locked users
if (args.where.locked === undefined) {
args.where.locked = false;
}
return query(args);
}
3 changes: 3 additions & 0 deletions packages/prisma/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { withAccelerate } from "@prisma/extension-accelerate";

import { bookingIdempotencyKeyExtension } from "./extensions/booking-idempotency-key";
import { disallowUndefinedDeleteUpdateManyExtension } from "./extensions/disallow-undefined-delete-update-many";
import { excludeLockedUsersExtension } from "./extensions/exclude-locked-users";
import { excludePendingPaymentsExtension } from "./extensions/exclude-pending-payment-teams";
import { usageTrackingExtention } from "./extensions/usage-tracking";
import { bookingReferenceMiddleware } from "./middleware";
Expand Down Expand Up @@ -43,6 +44,7 @@ const prismaWithoutClientExtensions =
export const customPrisma = (options?: Prisma.PrismaClientOptions) =>
new PrismaClientWithoutExtension({ ...prismaOptions, ...options })
.$extends(usageTrackingExtention())
.$extends(excludeLockedUsersExtension())
.$extends(excludePendingPaymentsExtension())
.$extends(bookingIdempotencyKeyExtension())
.$extends(disallowUndefinedDeleteUpdateManyExtension())
Expand All @@ -56,6 +58,7 @@ bookingReferenceMiddleware(prismaWithoutClientExtensions);
// Specifically we get errors like `Type 'string | Date | null | undefined' is not assignable to type 'Exact<string | Date | null | undefined, string | Date | null | undefined>'`
const prismaWithClientExtensions = prismaWithoutClientExtensions
.$extends(usageTrackingExtention())
.$extends(excludeLockedUsersExtension())
.$extends(excludePendingPaymentsExtension())
.$extends(bookingIdempotencyKeyExtension())
.$extends(disallowUndefinedDeleteUpdateManyExtension())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ const listPaginatedHandler = async ({ input }: GetOptions) => {
const users = await prisma.user.findMany({
cursor: cursor ? { id: cursor } : undefined,
take: limit + 1, // We take +1 as itll be used for the next cursor
where: searchFilters,
where: {
...searchFilters,
// To bypass the excludeLockedUsersExtension
OR: [{ locked: false }, { locked: true }],
},
orderBy: {
id: "asc",
},
Expand Down

0 comments on commit 693356d

Please sign in to comment.