Skip to content

Scheduling infrastructure for absolutely everyone.

fix: merge w main

Agusti Fernandez Pardo6e68671c2a40
diff --cc README.md
index 7b9f1c74722,ad6c3a78ab5..009ac0dd0dd
--- a/README.md
+++ b/README.md
@@@ -180,11 -180,18 +180,26 @@@ See <https://github.com/vercel/next.js/
To remove this limitation, we need to ensure that on local endpoints are requested by swagger at /api/v1 and not /v1
-## Deployment
+## Hosted api through cal.com
+
+Go to console.com
+Add a deployment or go to an existing one.
+Activate API or Admin addon
+Provide your DATABASE-URL
+Store it safely, you'll get a customApiID, save it.
- call api.cal.com?apiKey=your_cal_instance_apiKey&customApiId=cal_datasource_key
++call api.cal.com?apiKey=your_cal_instance_apiKey&customApiId=cal_datasource_key
++## How to deploy
+
+ We recommend deploying API in vercel.
+
+ There's some settings that you'll need to setup.
+
+ Under Vercel > Your API Deployment > Settings
+
+ In General > Build & Development Settings
+ BUILD COMMAND: `yarn turbo run build --scope=@calcom/api --include-dependencies --no-deps`
+ OUTPUT DIRECTORY: `apps/api/.next`
+
+ See `scripts/vercel-deploy.sh` for more info on how the deployment is done.
+ ## Environment variables
-Lastly API requires an env var for `DATABASE_URL`
++Lastly API requires an env var for `DATABASE_URL`
diff --cc lib/helpers/extendRequest.ts
index d0226a255ca,00000000000..52af6da5444
mode 100644,000000..100644
--- a/lib/helpers/extendRequest.ts
+++ b/lib/helpers/extendRequest.ts
@@@ -1,21 -1,0 +1,23 @@@
+import type { IncomingMessage } from "http";
+import { NextMiddleware } from "next-api-middleware";
+
- import type { PrismaClient } from ".prisma/client";
++import type { PrismaClient } from "@calcom/prisma/client";
+
+/** @todo figure how to use the one from `@calcom/types` */
+/** @todo: remove once `@calcom/types` is updated with it.*/
+declare module "next" {
+ export interface NextApiRequest extends IncomingMessage {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ body: any;
+ userId: number;
+ method: string;
+ prisma: PrismaClient;
+ session: { user: { id: number } };
+ query: { [key: string]: string | string[] };
++ isAdmin: boolean;
+ }
+}
+export const extendRequest: NextMiddleware = async (req, res, next) => {
++ // @note: just an empty middleware, we care about extending the next api request types only.
+ await next();
+};
diff --cc lib/helpers/verifyApiKey.ts
index 9068f2be73b,08c02167372..ec6aaf1179a
--- a/lib/helpers/verifyApiKey.ts
+++ b/lib/helpers/verifyApiKey.ts
@@@ -1,8 -1,24 +1,9 @@@
- // 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";
+ import { isAdminGuard } from "@lib/utils/isAdmin";
+
-/** @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;
- isAdmin: boolean;
- query: { [key: string]: string | string[] };
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- session: any;
- }
-}
-
// Used to check if the apiKey is not expired, could be extracted if reused. but not for now.
export const dateNotInPast = function (date: Date) {
const now = new Date();
@@@ -13,20 -29,13 +14,17 @@@
// 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" });
- console.log("req.query.apiKey", req.query.apiKey);
+
// 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_", "");
- console.log("strippedApiKey", strippedApiKey);
+
// Hash the key again before matching against the database records.
const hashedKey = hashAPIKey(strippedApiKey);
- console.log("hashedKey", hashedKey);
// Check if the hashed api key exists in database.
const apiKey = await prisma.apiKey.findUnique({ where: { hashedKey } });
+ // console.log("apiKey", apiKey);
// If we cannot find any api key. Throw a 401 Unauthorized.
if (!apiKey) return res.status(401).json({ error: "Your apiKey is not valid" });
if (apiKey.expiresAt && dateNotInPast(apiKey.expiresAt)) {
diff --cc pages/api/attendees/[id].ts
index c37e92b9ba9,a72dbf5812c..d7e927e36e8
--- a/pages/api/attendees/[id].ts
+++ b/pages/api/attendees/[id].ts
@@@ -1,9 -1,9 +1,7 @@@
import type { NextApiRequest, NextApiResponse } from "next";
- // // import prisma from "@calcom/prisma";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AttendeeResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
import { schemaAttendeeEditBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee";
import {
schemaQueryIdParseInt,
@@@ -11,10 -11,9 +9,9 @@@
} from "@lib/validations/shared/queryIdTransformParseInt";
export async function attendeeById(
- { method, query, body, userId, prisma }: NextApiRequest,
- { method, query, body, userId, isAdmin }: NextApiRequest,
++ { method, query, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<AttendeeResponse>
) {
- const isAdmin = await isAdminGuard(userId);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) {
res.status(400).json({ error: safeQuery.error });
diff --cc pages/api/attendees/index.ts
index 25f459823ee,e4f3e54f79e..880c8d4d4b9
--- a/pages/api/attendees/index.ts
+++ b/pages/api/attendees/index.ts
@@@ -1,16 -1,15 +1,13 @@@
import type { NextApiRequest, NextApiResponse } from "next";
- // import db from "@calcom/prisma";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
-import { AttendeeResponse, AttendeesResponse } from "@lib/types";
+import type { AttendeeResponse, AttendeesResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
import { schemaAttendeeCreateBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee";
async function createOrlistAllAttendees(
- { method, userId, body, prisma }: NextApiRequest,
- { method, userId, body, isAdmin }: NextApiRequest,
++ { method, userId, body, prisma, isAdmin }: NextApiRequest,
res: NextApiResponse<AttendeesResponse | AttendeeResponse>
) {
- const isAdmin = await isAdminGuard(userId);
let attendees;
if (!isAdmin) {
const userBookings = await prisma.booking.findMany({
diff --cc pages/api/availabilities/[id].ts
index 3dcbdbbca3a,0b6a58c3381..fe621829787
--- a/pages/api/availabilities/[id].ts
+++ b/pages/api/availabilities/[id].ts
@@@ -1,7 -1,8 +1,6 @@@
import type { NextApiRequest, NextApiResponse } from "next";
- // // import prisma from "@calcom/prisma";
-import prisma from "@calcom/prisma";
-
--import { withMiddleware } from "@lib/helpers/withMiddleware";
++// import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AvailabilityResponse } from "@lib/types";
import {
schemaAvailabilityEditBodyParams,
diff --cc pages/api/availabilities/index.ts
index 0e45564270d,bf17bd1ede4..ab4ae8f73bc
--- a/pages/api/availabilities/index.ts
+++ b/pages/api/availabilities/index.ts
@@@ -1,6 -1,7 +1,5 @@@
import type { NextApiRequest, NextApiResponse } from "next";
- // import prisma from "@calcom/prisma";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { AvailabilityResponse, AvailabilitiesResponse } from "@lib/types";
import {
diff --cc pages/api/bookings/[id].ts
index 68540fb69b3,8603af73c86..f4f11b143fc
--- a/pages/api/bookings/[id].ts
+++ b/pages/api/bookings/[id].ts
@@@ -1,9 -1,9 +1,7 @@@
import type { NextApiRequest, NextApiResponse } from "next";
- // import prisma from "@calcom/prisma";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
import { schemaBookingEditBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
import {
schemaQueryIdParseInt,
@@@ -11,7 -11,7 +9,7 @@@
} from "@lib/validations/shared/queryIdTransformParseInt";
export async function bookingById(
- { method, query, body, userId, prisma }: NextApiRequest,
- { method, query, body, userId, isAdmin }: NextApiRequest,
++ { method, query, body, userId, prisma, isAdmin }: NextApiRequest,
res: NextApiResponse<BookingResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
diff --cc pages/api/bookings/index.ts
index f1befaeb239,560bca7b8e5..fb1fd347aac
--- a/pages/api/bookings/index.ts
+++ b/pages/api/bookings/index.ts
@@@ -1,16 -1,21 +1,18 @@@
+ import { WebhookTriggerEvents } from "@prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
+ import { v4 as uuidv4 } from "uuid";
- // import prisma from "@calcom/prisma";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
- import type { BookingResponse, BookingsResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
+ import { BookingResponse, BookingsResponse } from "@lib/types";
+ import sendPayload from "@lib/utils/sendPayload";
+ import getWebhooks from "@lib/utils/webhookSubscriptions";
import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
+ import { schemaEventTypeReadPublic } from "@lib/validations/event-type";
async function createOrlistAllBookings(
- { method, body, userId, prisma }: NextApiRequest,
- { method, body, userId, isAdmin }: NextApiRequest,
++ { method, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<BookingsResponse | BookingResponse>
) {
- const isAdmin = await isAdminGuard(userId);
- console.log("userIduserId", userId);
if (method === "GET") {
/**
* @swagger
diff --cc pages/api/event-types/[id].ts
index 117ee7bf434,013f9909be1..dd9ac1a4d9d
--- a/pages/api/event-types/[id].ts
+++ b/pages/api/event-types/[id].ts
@@@ -1,8 -1,9 +1,7 @@@
import type { NextApiRequest, NextApiResponse } from "next";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
import { schemaEventTypeEditBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";
import {
schemaQueryIdParseInt,
@@@ -10,10 -11,9 +9,9 @@@
} from "@lib/validations/shared/queryIdTransformParseInt";
export async function eventTypeById(
- { method, query, body, userId, prisma }: NextApiRequest,
- { method, query, body, userId, isAdmin }: NextApiRequest,
++ { method, query, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<EventTypeResponse>
) {
- const isAdmin = await isAdminGuard(userId);
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) {
res.status(400).json({ message: "Your query was invalid" });
diff --cc pages/api/event-types/index.ts
index b7939bd0c7c,dc6844e039c..b3a9d741387
--- a/pages/api/event-types/index.ts
+++ b/pages/api/event-types/index.ts
@@@ -1,15 -1,15 +1,13 @@@
import type { NextApiRequest, NextApiResponse } from "next";
-import prisma from "@calcom/prisma";
-
import { withMiddleware } from "@lib/helpers/withMiddleware";
- import type { EventTypeResponse, EventTypesResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
+ import { EventTypeResponse, EventTypesResponse } from "@lib/types";
import { schemaEventTypeCreateBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";
async function createOrlistAllEventTypes(
- { method, body, userId, prisma }: NextApiRequest,
- { method, body, userId, isAdmin }: NextApiRequest,
++ { method, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<EventTypesResponse | EventTypeResponse>
) {
- const isAdmin = await isAdminGuard(userId);
if (method === "GET") {
/**
* @swagger
diff --cc pages/api/users/index.ts
index cbb0eab69ad,c07846423fa..28231b487ab
--- a/pages/api/users/index.ts
+++ b/pages/api/users/index.ts
@@@ -1,58 -1,10 +1,68 @@@
++<<<<<<< HEAD
+import type { NextApiRequest, NextApiResponse } from "next";
++=======
+ import { defaultHandler } from "@calcom/lib/server";
++>>>>>>> main
import { withMiddleware } from "@lib/helpers/withMiddleware";
- import { UserResponse, UsersResponse } from "@lib/types";
- import { isAdminGuard } from "@lib/utils/isAdmin";
- import { schemaUserReadPublic, schemaUserCreateBodyParams } from "@lib/validations/user";
++<<<<<<< HEAD
+/**
+ * @swagger
+ * /users:
+ * get:
+ * operationId: listUsers
+ * summary: Find all users.
+ * tags:
+ * - users
+ * responses:
+ * 200:
+ * description: OK
+ * 401:
+ * description: Authorization information is missing or invalid.
+ * 404:
+ * description: No users were found
+ */
+async function getAllorCreateUser(
+ { userId, method, body, prisma }: NextApiRequest,
+ res: NextApiResponse<UsersResponse | UserResponse>
+) {
+ const isAdmin = await isAdminGuard(userId);
+ if (method === "GET") {
+ if (!isAdmin) {
+ // If user is not ADMIN, return only his data.
+ const data = await prisma.user.findMany({ where: { id: userId } });
+ const users = data.map((user) => schemaUserReadPublic.parse(user));
+ if (users) res.status(200).json({ users });
+ } else {
+ // If user is admin, return all users.
+ const data = await prisma.user.findMany({});
+ const users = data.map((user) => schemaUserReadPublic.parse(user));
+ if (users) res.status(200).json({ users });
+ }
+ } else if (method === "POST") {
+ // If user is not ADMIN, return unauthorized.
+ if (!isAdmin) res.status(401).json({ message: "You are not authorized" });
+ else {
+ const safeBody = schemaUserCreateBodyParams.safeParse(body);
+ if (!safeBody.success) {
+ res.status(400).json({ message: "Your body was invalid" });
+ return;
+ }
+ const user = await prisma.user.create({
+ data: safeBody.data,
+ });
+ res.status(201).json({ user });
+ }
+ }
+}
+
+export default withMiddleware("HTTP_GET_OR_POST")(getAllorCreateUser);
++=======
+ export default withMiddleware("HTTP_GET_OR_POST")(
+ defaultHandler({
+ GET: import("./_get"),
+ POST: import("./_post"),
+ })
+ );
++>>>>>>> main