- Replace PrismaClientKnownRequestError and other error classes with Prisma namespace equivalents - Remove internal DefaultArgs and InternalArgs type imports from Prisma extensions - Ensure all error handling uses stable public API exports - Maintain compatibility with future Prisma versions by avoiding runtime dependencies Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com>
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { Prisma } from "@prisma/client";
|
|
|
|
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);
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
function safeJSONStringify(x: any) {
|
|
try {
|
|
return JSON.stringify(x);
|
|
} catch {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
async function excludeLockedUsers(
|
|
args:
|
|
| Prisma.UserFindUniqueArgs
|
|
| Prisma.UserFindFirstArgs
|
|
| Prisma.UserFindManyArgs
|
|
| Prisma.UserFindUniqueOrThrowArgs
|
|
| Prisma.UserFindFirstOrThrowArgs,
|
|
query: <T>(args: T) => Promise<unknown>
|
|
) {
|
|
args.where = args.where || {};
|
|
const whereString = safeJSONStringify(args.where);
|
|
const shouldIncludeLocked = whereString.includes('"locked":');
|
|
// Unless explicitly specified, we exclude locked users
|
|
if (!shouldIncludeLocked) {
|
|
args.where.locked = false;
|
|
}
|
|
return query(args);
|
|
}
|