pollo/src/pages/api/webhooks/index.ts

33 lines
859 B
TypeScript
Raw Normal View History

2023-08-14 21:16:11 -06:00
import type { NextApiRequest, NextApiResponse } from "next";
import {
onUserCreatedHandler,
onUserDeletedHandler,
} from "~/server/webhookHelpers";
2023-08-15 00:54:46 -06:00
import { WebhookEventBodySchema, WebhookEvents } from "~/utils/types";
2023-08-14 21:16:11 -06:00
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
2023-08-15 00:54:46 -06:00
try {
const requestBody = WebhookEventBodySchema.parse(req.body);
2023-08-14 21:16:11 -06:00
2023-08-15 00:54:46 -06:00
switch (requestBody.type) {
case WebhookEvents.USER_CREATED:
await onUserCreatedHandler(requestBody.data.id, res);
return;
2023-08-14 21:16:11 -06:00
2023-08-15 00:54:46 -06:00
case WebhookEvents.USER_DELETED:
await onUserDeletedHandler(requestBody.data.id, res);
return;
2023-08-14 21:16:11 -06:00
2023-08-15 00:54:46 -06:00
default:
res.status(400).json({ error: "INVALID WEBHOOK EVENT TYPE" });
return;
}
} catch {
res.status(400).json({ error: "INVALID WEBHOOK EVENT BODY" });
return;
2023-08-14 21:16:11 -06:00
}
}