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 { schemaQueryIdAsString } from "~/lib/validations/shared/queryIdString";
|
|
|
|
import { schemaWebhookEditBodyParams, schemaWebhookReadPublic } from "~/lib/validations/webhook";
|
2022-10-19 18:26:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /webhooks/{id}:
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing webhook
|
|
|
|
* operationId: editWebhookById
|
|
|
|
* 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 successfully
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Webhook body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function patchHandler(req: NextApiRequest) {
|
|
|
|
const { prisma, query, userId, isAdmin } = req;
|
|
|
|
const { id } = schemaQueryIdAsString.parse(query);
|
|
|
|
const { eventTypeId, userId: bodyUserId, ...data } = schemaWebhookEditBodyParams.parse(req.body);
|
|
|
|
const args: Prisma.WebhookUpdateArgs = { where: { id }, data };
|
|
|
|
|
|
|
|
if (eventTypeId) {
|
|
|
|
const where: Prisma.EventTypeWhereInput = { id: eventTypeId };
|
|
|
|
if (!isAdmin) where.userId = userId;
|
|
|
|
await prisma.eventType.findFirstOrThrow({ where });
|
|
|
|
args.data.eventTypeId = eventTypeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAdmin && bodyUserId) throw new HttpError({ statusCode: 403, message: `ADMIN required for userId` });
|
|
|
|
|
|
|
|
if (isAdmin && bodyUserId) {
|
2022-10-21 18:55:35 +00:00
|
|
|
const where: Prisma.UserWhereInput = { id: bodyUserId };
|
2022-10-19 18:26:12 +00:00
|
|
|
await prisma.user.findFirstOrThrow({ where });
|
2022-10-21 18:55:35 +00:00
|
|
|
args.data.userId = bodyUserId;
|
2022-10-19 18:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = await prisma.webhook.update(args);
|
|
|
|
return { webhook: schemaWebhookReadPublic.parse(result) };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(patchHandler);
|