cal.pub0.org/pages/api/hooks/index.ts

96 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-05-10 07:56:38 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
import { v4 as uuidv4 } from "uuid";
2022-05-10 07:56:38 +00:00
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { WebhookResponse, WebhooksResponse } from "@lib/types";
2022-05-20 20:10:13 +00:00
import { schemaWebhookCreateBodyParams } from "@lib/validations/webhook";
2022-05-10 07:56:38 +00:00
async function createOrlistAllWebhooks(
{ method, body, userId }: NextApiRequest,
res: NextApiResponse<WebhooksResponse | WebhookResponse>
) {
if (method === "GET") {
/**
* @swagger
* /hooks:
* get:
* summary: Find all webhooks
2022-05-18 15:46:22 +00:00
* operationId: listWebhooks
2022-05-10 07:56:38 +00:00
* tags:
* - hooks
* externalDocs:
* url: https://docs.cal.com/webhooks
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No webhooks were found
*/
2022-05-20 20:10:13 +00:00
const webhooks = await prisma.webhook
2022-05-20 19:52:41 +00:00
.findMany({ where: { userId } })
2022-05-20 20:10:13 +00:00
.catch((error) => console.log(error));
if (!webhooks) {
console.log();
res.status(404).json({ message: "No webhooks were found" });
} else res.status(200).json({ webhooks });
2022-05-10 07:56:38 +00:00
} else if (method === "POST") {
/**
* @swagger
* /hooks:
* post:
* summary: Creates a new webhook
2022-05-18 15:46:22 +00:00
* operationId: addWebhook
2022-05-10 07:56:38 +00:00
* tags:
* - webhooks
* externalDocs:
* url: https://docs.cal.com/webhooks
* responses:
* 201:
* description: OK, webhook created
* 400:
* description: Bad request. webhook body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
2022-05-25 08:43:52 +00:00
const safe = schemaWebhookCreateBodyParams.safeParse(body);
if (!safe.success) {
res.status(400).json({ message: "Invalid request body" });
return;
}
2022-06-08 07:42:05 +00:00
if (safe.data.eventTypeId) {
2022-06-08 07:50:09 +00:00
const team = await prisma.team.findFirst({
2022-06-08 07:42:05 +00:00
where: {
eventTypes: {
some: {
id: safe.data.eventTypeId,
},
},
},
include: {
members: true,
},
});
// Team should be available and the user should be a member of the team
if (!team?.members.some((membership) => membership.userId === userId)) {
2022-06-08 07:57:48 +00:00
res.status(401).json({ message: "Unauthorized" });
return;
2022-06-08 07:42:05 +00:00
}
}
const data = await prisma.webhook.create({ data: { id: uuidv4(), ...safe.data, userId } });
2022-05-25 08:43:52 +00:00
if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new webhook",
error,
});
2022-05-10 07:56:38 +00:00
} else res.status(405).json({ message: `Method ${method} not allowed` });
}
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllWebhooks);