cal.pub0.org/pages/api/bookings/[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-26 21:29:30 +00:00
2022-03-30 12:17:55 +00:00
import prisma from "@calcom/prisma";
2022-03-26 21:29:30 +00:00
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingResponse } from "@lib/types";
import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking";
2022-03-30 12:17:55 +00:00
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
2022-03-26 21:29:30 +00:00
/**
* @swagger
* /api/bookings/{id}/edit:
* patch:
* summary: Edit an existing booking
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking to edit
* tags:
* - bookings
* responses:
* 201:
* description: OK, booking edited successfuly
* model: Booking
* 400:
* description: Bad request. Booking body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editBooking(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaBookingBodyParams.safeParse(req.body);
2022-03-26 21:29:30 +00:00
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const booking = await prisma.booking.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaBookingPublic.parse(booking);
2022-03-26 21:29:30 +00:00
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,
});
2022-03-26 21:29:30 +00:00
}
export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidBooking(editBooking)));