feat: dynamic prisma
This commit is contained in:
1 parent
0eed3e51e4
commit
e407a16766
36 files changed
+201
-166
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
// This replaces the prisma client for the cusotm one if the customApiId is valid
|
||||
export const customApiEndpoints: NextMiddleware = async (req, res, next) => {
|
||||
const {
|
||||
query: { customApiId },
|
||||
} = req;
|
||||
// If no custom api Id is provided, return the regular cal.com prisma client.
|
||||
if (!customApiId) {
|
||||
req.prisma = prisma();
|
||||
await next();
|
||||
}
|
||||
// Hardcoded new database, this should be a dynamic call to console prisma/api?
|
||||
const newDatabseUrl = process.env.DATABASE_PROD_URL;
|
||||
req.prisma = prisma(newDatabseUrl);
|
||||
|
||||
await next();
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { IncomingMessage } from "http";
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
import type { PrismaClient } from ".prisma/client";
|
||||
|
||||
/** @todo figure how to use the one from `@calcom/types`fi */
|
||||
/** @todo: remove once `@calcom/types` is updated with it.*/
|
||||
declare module "next" {
|
||||
export interface NextApiRequest extends IncomingMessage {
|
||||
userId: number;
|
||||
method: string;
|
||||
prisma: PrismaClient;
|
||||
body: any;
|
||||
query: { [key: string]: string | string[] };
|
||||
}
|
||||
}
|
||||
export const extendRequest: NextMiddleware = async (req, res, next) => {
|
||||
await next();
|
||||
};
|
||||
+13
-11
@@ -1,18 +1,19 @@
|
||||
import type { IncomingMessage } from "http";
|
||||
// import type { IncomingMessage } from "http";
|
||||
import { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
import { hashAPIKey } from "@calcom/ee/lib/api/apiKeys";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
/** @todo figure how to use the one from `@calcom/types`fi */
|
||||
/** @todo: remove once `@calcom/types` is updated with it.*/
|
||||
declare module "next" {
|
||||
export interface NextApiRequest extends IncomingMessage {
|
||||
userId: number;
|
||||
method: string;
|
||||
query: { [key: string]: string | string[] };
|
||||
}
|
||||
}
|
||||
// // import prisma from "@calcom/prisma";
|
||||
|
||||
// /** @todo figure how to use the one from `@calcom/types`fi */
|
||||
// /** @todo: remove once `@calcom/types` is updated with it.*/
|
||||
// declare module "next" {
|
||||
// export interface NextApiRequest extends IncomingMessage {
|
||||
// userId: number;
|
||||
// method: string;
|
||||
// query: { [key: string]: string | string[] };
|
||||
// }
|
||||
// }
|
||||
|
||||
// Used to check if the apiKey is not expired, could be extracted if reused. but not for now.
|
||||
export const dateNotInPast = function (date: Date) {
|
||||
@@ -24,6 +25,7 @@ export const dateNotInPast = function (date: Date) {
|
||||
|
||||
// This verifies the apiKey and sets the user if it is valid.
|
||||
export const verifyApiKey: NextMiddleware = async (req, res, next) => {
|
||||
const { prisma } = req;
|
||||
if (!req.query.apiKey) return res.status(401).json({ message: "No apiKey provided" });
|
||||
// We remove the prefix from the user provided api_key. If no env set default to "cal_"
|
||||
const strippedApiKey = `${req.query.apiKey}`.replace(process.env.API_KEY_PREFIX || "cal_", "");
|
||||
|
||||
@@ -2,6 +2,8 @@ import { label } from "next-api-middleware";
|
||||
|
||||
import { addRequestId } from "./addRequestid";
|
||||
import { captureErrors } from "./captureErrors";
|
||||
import { customApiEndpoints } from "./customApiEndpoints";
|
||||
import { extendRequest } from "./extendRequest";
|
||||
import {
|
||||
HTTP_POST,
|
||||
HTTP_DELETE,
|
||||
@@ -22,9 +24,11 @@ const withMiddleware = label(
|
||||
HTTP_DELETE,
|
||||
addRequestId,
|
||||
verifyApiKey,
|
||||
customApiEndpoints,
|
||||
extendRequest,
|
||||
sentry: captureErrors,
|
||||
},
|
||||
["sentry", "verifyApiKey", "addRequestId"] // <-- Provide a list of middleware to call automatically
|
||||
["sentry", "customApiEndpoints", "verifyApiKey", "addRequestId", "extendRequest"] // <-- Provide a list of middleware to call automatically
|
||||
);
|
||||
|
||||
export { withMiddleware };
|
||||
@@ -3,6 +3,6 @@ import { UserPermissionRole } from "@prisma/client";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
export const isAdminGuard = async (userId: number) => {
|
||||
const user = await prisma.user.findUnique({ where: { id: userId } });
|
||||
const user = await prisma().user.findUnique({ where: { id: userId } });
|
||||
return user?.role === UserPermissionRole.ADMIN;
|
||||
};
|
||||
Reference in new issue
Block a user