cal.pub0.org/pages/api/daily-event-references/new.ts

56 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";
2022-04-01 23:55:41 +00:00
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DailyEventReferenceResponse } from "@lib/types";
2022-03-30 12:17:55 +00:00
import {
2022-04-01 23:55:41 +00:00
schemaDailyEventReferenceBodyParams,
schemaDailyEventReferencePublic,
2022-03-30 12:17:55 +00:00
withValidDailyEventReference,
} from "@lib/validations/daily-event-reference";
2022-04-01 23:55:41 +00:00
/**
* @swagger
* /api/daily-event-references/new:
* post:
* summary: Creates a new dailyEventReference
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
2022-04-01 23:55:41 +00:00
* schema:
* $ref: '#/components/schemas/DailyEventReference'
* tags:
* - daily-event-references
2022-04-01 23:55:41 +00:00
* responses:
* 201:
* description: OK, dailyEventReference created
* model: DailyEventReference
* 400:
* description: Bad request. DailyEventReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createDailyEventReference(
req: NextApiRequest,
res: NextApiResponse<DailyEventReferenceResponse>
) {
const safe = schemaDailyEventReferenceBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
2022-04-01 23:55:41 +00:00
const dailyEventReference = await prisma.dailyEventReference.create({ data: safe.data });
const data = schemaDailyEventReferencePublic.parse(dailyEventReference);
if (data) res.status(201).json({ data, message: "DailyEventReference created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new dailyEventReference",
error,
});
}
2022-04-01 23:55:41 +00:00
export default withMiddleware("HTTP_POST")(withValidDailyEventReference(createDailyEventReference));