calcom/pages/api/webhooks/[hook]/index.ts
Omar López 30f97117e8
Revert "Revert "Feature/cal 274 add webhooks (#628)" (#854)" (#876)
This reverts commit 6868474c92.

Co-authored-by: Bailey Pumfleet <pumfleet@hey.com>
2021-10-07 15:14:47 +00:00

57 lines
1.4 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import { getSession } from "next-auth/client";
import prisma from "@lib/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req: req });
if (!session) {
return res.status(401).json({ message: "Not authenticated" });
}
// GET /api/webhook/{hook}
const webhooks = await prisma.webhook.findFirst({
where: {
id: String(req.query.hook),
userId: session.user.id,
},
});
if (req.method === "GET") {
return res.status(200).json({ webhooks: webhooks });
}
// DELETE /api/webhook/{hook}
if (req.method === "DELETE") {
await prisma.webhook.delete({
where: {
id: String(req.query.hook),
},
});
return res.status(200).json({});
}
if (req.method === "PATCH") {
const webhook = await prisma.webhook.findUnique({
where: {
id: req.query.hook as string,
},
});
if (!webhook) {
return res.status(404).json({ message: "Invalid Webhook" });
}
await prisma.webhook.update({
where: {
id: req.query.hook as string,
},
data: {
subscriberUrl: req.body.subscriberUrl,
eventTriggers: req.body.eventTriggers,
active: req.body.enabled,
},
});
return res.status(200).json({ message: "Webhook updated successfully" });
}
}