cal.pub0.org/pages/api/selected-calendars/new.ts

29 lines
1020 B
TypeScript
Raw Normal View History

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