calcom/pages/api/webhook.ts
Syed Ali Shahbaz 4c07faefe7
Feature/cal 274 add webhooks (#628)
* added prisma models and migration, minor webhook init --WIP

* --WIP

* --WIP

* added radix-checkbox and other webhook additions --WIP

* added API connections and other modifications --WIP

* --WIP

* replaced checkbox with toggle --WIP

* updated to use Dialog instead of modal --WIP

* fixed API and other small fixes -WIP

* created a dummy hook for test --WIP

* replaced static hook with dynamic hooks

* yarn lock conflict quickfix

* added cancel event hook and other minor additions --WIP

* minor improvements --WIP

* added more add-webhook flow items--WIP

* updated migration to have alter table for eventType

* many ui/ux fixes, logic fixes and action fixes --WIP

* bugfix for incorrect webhook filtering

* some more fixes, edit webhook --WIP

* removed redundant checkbox

* more bugfixes and edit-webhook flow --WIP

* more build and lint fixes

* --WIP

* more fixes and added toast notif --WIP

* --updated iconButton

* clean-up

* fixed enabled check in edit webhook

* another fix

* fixed edit webhook bug

* added await to payload lambda

* wrapped payload call in promise

* fixed cancel/uid CTA alignment

* --requested changes --removed eventType relationship

* Adds missing migration

* Fixes missing daysjs plugin and type fixes

* Adds failsafe for webhooks

* Adds missing dayjs utc plugins

* Fixed schema and migrations

* Updates webhooks query

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Co-authored-by: Omar López <zomars@me.com>
2021-10-04 17:40:52 -06:00

49 lines
1.3 KiB
TypeScript

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import type { NextApiRequest, NextApiResponse } from "next";
import short from "short-uuid";
import { v5 as uuidv5 } from "uuid";
import { getSession } from "@lib/auth";
import prisma from "@lib/prisma";
dayjs.extend(utc);
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession({ req });
if (!session?.user?.id) {
res.status(401).json({ message: "Not authenticated" });
return;
}
// List webhooks
if (req.method === "GET") {
const webhooks = await prisma.webhook.findMany({
where: {
userId: session.user.id,
},
});
return res.status(200).json({ webhooks: webhooks });
}
if (req.method === "POST") {
const translator = short();
const seed = `${req.body.subscriberUrl}:${dayjs(new Date()).utc().format()}`;
const uid = translator.fromUUID(uuidv5(seed, uuidv5.URL));
await prisma.webhook.create({
data: {
id: uid,
userId: session.user.id,
subscriberUrl: req.body.subscriberUrl,
eventTriggers: req.body.eventTriggers,
},
});
return res.status(201).json({ message: "Webhook created" });
}
res.status(404).json({ message: "Webhook not found" });
}