2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-26 23:58:22 +00:00
|
|
|
import { Attendee } from "@calcom/prisma/client";
|
|
|
|
|
|
|
|
import { schemaAttendee, withValidAttendee } from "@lib/validations/attendee";
|
|
|
|
|
|
|
|
type ResponseData = {
|
|
|
|
data?: Attendee;
|
|
|
|
message?: string;
|
|
|
|
error?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function createAttendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
|
|
|
const { body, method } = req;
|
|
|
|
const safe = schemaAttendee.safeParse(body);
|
2022-03-30 12:17:55 +00:00
|
|
|
|
2022-03-26 23:58:22 +00:00
|
|
|
if (method === "POST" && safe.success) {
|
2022-03-30 12:17:55 +00:00
|
|
|
await prisma.attendee
|
|
|
|
.create({ data: safe.data })
|
|
|
|
.then((attendee) => res.status(201).json({ data: attendee }))
|
|
|
|
.catch((error) => res.status(400).json({ message: "Could not create attendee type", error: error }));
|
|
|
|
// Reject any other HTTP method than POST
|
2022-03-26 23:58:22 +00:00
|
|
|
} else res.status(405).json({ error: "Only POST Method allowed" });
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withValidAttendee(createAttendee);
|