cal.pub0.org/pages/api/bookings/[id]/edit.ts

46 lines
1.4 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 { Booking } from "@calcom/prisma/client";
import { schemaBooking, 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
type ResponseData = {
data?: Booking;
message?: string;
error?: unknown;
};
export async function editBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
2022-03-26 21:29:30 +00:00
const safeBody = await schemaBooking.safeParse(body);
if (method === "PATCH") {
if (safeQuery.success && safeBody.success) {
2022-03-30 12:17:55 +00:00
await prisma.booking
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((booking) => {
res.status(200).json({ data: booking });
})
.catch((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
}
} else {
// Reject any other HTTP method than POST
2022-03-30 12:17:55 +00:00
res.status(405).json({ message: "Only PATCH Method allowed for updating bookings" });
2022-03-26 21:29:30 +00:00
}
}
export default withValidQueryIdTransformParseInt(withValidBooking(editBooking));