import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { SelectedCalendarResponse } from "@lib/types"; import { schemaSelectedCalendarBodyParams, schemaSelectedCalendarPublic, withValidSelectedCalendar, } from "@lib/validations/selected-calendar"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; /** * @swagger * /api/selectedCalendars/:id/edit: * patch: * description: Edits an existing selectedCalendar * responses: * 201: * description: OK, selectedCalendar edited successfuly * model: SelectedCalendar * 400: * description: Bad request. SelectedCalendar body is invalid. * 401: * description: Authorization information is missing or invalid. */ export async function editSelectedCalendar( req: NextApiRequest, res: NextApiResponse ) { const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); const safeBody = await schemaSelectedCalendarBodyParams.safeParse(req.body); if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); const selectedCalendar = await prisma.selectedCalendar.update({ where: { id: safeQuery.data.id }, data: safeBody.data, }); const data = schemaSelectedCalendarPublic.parse(selectedCalendar); if (data) res.status(200).json({ data }); else (error: Error) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error, }); } export default withMiddleware("HTTP_PATCH")( withValidQueryIdTransformParseInt(withValidSelectedCalendar(editSelectedCalendar)) );