154 lines
4.9 KiB
TypeScript
154 lines
4.9 KiB
TypeScript
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||
|
|
||
|
import prisma from "@calcom/prisma";
|
||
|
|
||
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||
|
import type { WebhookResponse } from "@lib/types";
|
||
|
import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook";
|
||
|
import {
|
||
|
schemaQueryIdParseInt,
|
||
|
withValidQueryIdTransformParseInt,
|
||
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
||
|
|
||
|
export async function WebhookById(
|
||
|
{ method, query, body, userId }: NextApiRequest,
|
||
|
res: NextApiResponse<WebhookResponse>
|
||
|
) {
|
||
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
||
|
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
||
|
const data = await prisma.webhook.findMany({ where: { userId } });
|
||
|
const userWebhooks = data.map((webhook) => webhook.id);
|
||
|
if (!userWebhooks.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" });
|
||
|
else {
|
||
|
switch (method) {
|
||
|
/**
|
||
|
* @swagger
|
||
|
* /hooks/{id}:
|
||
|
* get:
|
||
|
* summary: Find a webhook
|
||
|
* parameters:
|
||
|
* - in: path
|
||
|
* name: id
|
||
|
* schema:
|
||
|
* type: integer
|
||
|
* required: true
|
||
|
* description: Numeric ID of the webhook to get
|
||
|
* security:
|
||
|
* - ApiKeyAuth: []
|
||
|
* tags:
|
||
|
* - hooks
|
||
|
* externalDocs:
|
||
|
* url: https://docs.cal.com/hooks
|
||
|
* responses:
|
||
|
* 200:
|
||
|
* description: OK
|
||
|
* 401:
|
||
|
* description: Authorization information is missing or invalid.
|
||
|
* 404:
|
||
|
* description: Webhook was not found
|
||
|
*/
|
||
|
case "GET":
|
||
|
await prisma.webhook
|
||
|
.findUnique({ where: { id: safeQuery.data.id } })
|
||
|
.then((data) => schemaWebhookPublic.parse(data))
|
||
|
.then((hook) => res.status(200).json({ hook }))
|
||
|
.catch((error: Error) =>
|
||
|
res.status(404).json({
|
||
|
message: `Webhook with id: ${safeQuery.data.id} not found`,
|
||
|
error,
|
||
|
})
|
||
|
);
|
||
|
break;
|
||
|
/**
|
||
|
* @swagger
|
||
|
* /hooks/{id}:
|
||
|
* patch:
|
||
|
* summary: Edit an existing webhook
|
||
|
* parameters:
|
||
|
* - in: path
|
||
|
* name: id
|
||
|
* schema:
|
||
|
* type: integer
|
||
|
* required: true
|
||
|
* description: Numeric ID of the webhook to edit
|
||
|
* security:
|
||
|
* - ApiKeyAuth: []
|
||
|
* tags:
|
||
|
* - hooks
|
||
|
* externalDocs:
|
||
|
* url: https://docs.cal.com/hooks
|
||
|
* responses:
|
||
|
* 201:
|
||
|
* description: OK, webhook edited successfuly
|
||
|
* 400:
|
||
|
* description: Bad request. Webhook body is invalid.
|
||
|
* 401:
|
||
|
* description: Authorization information is missing or invalid.
|
||
|
*/
|
||
|
case "PATCH":
|
||
|
const safeBody = schemaWebhookBodyParams.safeParse(body);
|
||
|
if (!safeBody.success) {
|
||
|
throw new Error("Invalid request body");
|
||
|
}
|
||
|
await prisma.webhook
|
||
|
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
||
|
.then((data) => schemaWebhookPublic.parse(data))
|
||
|
.then((hook) => res.status(200).json({ hook }))
|
||
|
.catch((error: Error) =>
|
||
|
res.status(404).json({
|
||
|
message: `Webhook with id: ${safeQuery.data.id} not found`,
|
||
|
error,
|
||
|
})
|
||
|
);
|
||
|
break;
|
||
|
/**
|
||
|
* @swagger
|
||
|
* /hooks/{id}:
|
||
|
* delete:
|
||
|
* summary: Remove an existing hook
|
||
|
* parameters:
|
||
|
* - in: path
|
||
|
* name: id
|
||
|
* schema:
|
||
|
* type: integer
|
||
|
* required: true
|
||
|
* description: Numeric ID of the hooks to delete
|
||
|
* security:
|
||
|
* - ApiKeyAuth: []
|
||
|
* tags:
|
||
|
* - hooks
|
||
|
* externalDocs:
|
||
|
* url: https://docs.cal.com/hooks
|
||
|
* responses:
|
||
|
* 201:
|
||
|
* description: OK, hook removed successfuly
|
||
|
* 400:
|
||
|
* description: Bad request. hook id is invalid.
|
||
|
* 401:
|
||
|
* description: Authorization information is missing or invalid.
|
||
|
*/
|
||
|
case "DELETE":
|
||
|
await prisma.webhook
|
||
|
.delete({ where: { id: safeQuery.data.id } })
|
||
|
.then(() =>
|
||
|
res.status(200).json({
|
||
|
message: `Webhook with id: ${safeQuery.data.id} deleted`,
|
||
|
})
|
||
|
)
|
||
|
.catch((error: Error) =>
|
||
|
res.status(404).json({
|
||
|
message: `Webhook with id: ${safeQuery.data.id} not found`,
|
||
|
error,
|
||
|
})
|
||
|
);
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
res.status(405).json({ message: "Method not allowed" });
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(WebhookById));
|