cal.pub0.org/pages/api/booking-references/new.ts

52 lines
1.7 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 { BookingReferenceResponse } from "@lib/types";
import {
schemaBookingReferenceBodyParams,
schemaBookingReferencePublic,
withValidBookingReference,
} from "@lib/validations/booking-reference";
/**
* @swagger
* /api/booking-references/new:
* post:
* summary: Creates a new bookingReference
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BookingReference'
* tags:
* - booking-references
* responses:
* 201:
* description: OK, bookingReference created
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createBookingReference(req: NextApiRequest, res: NextApiResponse<BookingReferenceResponse>) {
const safe = schemaBookingReferenceBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
const bookingReference = await prisma.bookingReference.create({ data: safe.data });
const data = schemaBookingReferencePublic.parse(bookingReference);
if (data) res.status(201).json({ data, message: "BookingReference created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new bookingReference",
error,
});
}
export default withMiddleware("HTTP_POST")(withValidBookingReference(createBookingReference));