cal.pub0.org/pages/api/webhooks/_get.ts

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-10-19 18:26:12 +00:00
import type { Prisma } from "@prisma/client";
import type { NextApiRequest } from "next";
import { HttpError } from "@calcom/lib/http-error";
import { defaultResponder } from "@calcom/lib/server";
2022-11-25 13:56:58 +00:00
import { schemaQuerySingleOrMultipleUserIds } from "~/lib/validations/shared/queryUserId";
import { schemaWebhookReadPublic } from "~/lib/validations/webhook";
2022-10-19 18:26:12 +00:00
/**
* @swagger
* /webhooks:
* get:
* summary: Find all webhooks
* operationId: listWebhooks
* 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
*/
async function getHandler(req: NextApiRequest) {
const { userId, isAdmin, prisma } = req;
const args: Prisma.WebhookFindManyArgs = isAdmin
? {}
: { where: { OR: [{ eventType: { userId } }, { userId }] } };
/** Only admins can query other users */
if (!isAdmin && req.query.userId) throw new HttpError({ statusCode: 403, message: "ADMIN required" });
if (isAdmin && req.query.userId) {
const query = schemaQuerySingleOrMultipleUserIds.parse(req.query);
const userIds = Array.isArray(query.userId) ? query.userId : [query.userId || userId];
args.where = { OR: [{ eventType: { userId: { in: userIds } } }, { userId: { in: userIds } }] };
if (Array.isArray(query.userId)) args.orderBy = { userId: "asc", eventType: { userId: "asc" } };
}
const data = await prisma.webhook.findMany(args);
return { webhooks: data.map((v) => schemaWebhookReadPublic.parse(v)) };
}
export default defaultResponder(getHandler);