cal.pub0.org/pages/api/selected-calendars/[id]/edit.ts

57 lines
1.8 KiB
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 { withMiddleware } from "@lib/helpers/withMiddleware";
import type { SelectedCalendarResponse } from "@lib/types";
import {
schemaSelectedCalendarBodyParams,
schemaSelectedCalendarPublic,
withValidSelectedCalendar,
} from "@lib/validations/selected-calendar";
2022-03-30 12:17:55 +00:00
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<SelectedCalendarResponse>
) {
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))
);