29 lines
1020 B
TypeScript
29 lines
1020 B
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
import { BookingReference } from "@calcom/prisma/client";
|
|
|
|
import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference";
|
|
|
|
type ResponseData = {
|
|
data?: BookingReference;
|
|
message?: string;
|
|
error?: string;
|
|
};
|
|
|
|
async function createBookingReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
|
const { body, method } = req;
|
|
const safe = schemaBookingReference.safeParse(body);
|
|
if (method === "POST" && safe.success) {
|
|
await prisma.bookingReference
|
|
.create({ data: safe.data })
|
|
.then((data) => res.status(201).json({ data }))
|
|
.catch((error) =>
|
|
res.status(400).json({ message: "Could not create bookingReference type", error: error })
|
|
);
|
|
// Reject any other HTTP method than POST
|
|
} else res.status(405).json({ error: "Only POST Method allowed" });
|
|
}
|
|
|
|
export default withValidBookingReference(createBookingReference);
|