2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-11 13:10:16 +00:00
|
|
|
import { DailyEventReferenceResponse, DailyEventReferencesResponse } from "@lib/types";
|
|
|
|
import {
|
2022-04-27 17:25:36 +00:00
|
|
|
schemaDailyEventReferenceCreateBodyParams,
|
|
|
|
schemaDailyEventReferenceReadPublic,
|
2022-05-11 13:57:00 +00:00
|
|
|
} from "@lib/validations/event-reference";
|
2022-04-01 23:55:41 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
async function createOrlistAllDailyEventReferences(
|
2022-04-29 15:29:57 +00:00
|
|
|
{ method, body, userId }: NextApiRequest,
|
2022-04-11 13:10:16 +00:00
|
|
|
res: NextApiResponse<DailyEventReferencesResponse | DailyEventReferenceResponse>
|
2022-04-01 23:55:41 +00:00
|
|
|
) {
|
2022-04-20 23:30:47 +00:00
|
|
|
const userBookings = await prisma.booking.findMany({ where: { userId } });
|
|
|
|
const userBookingIds = userBookings.map((booking) => booking.id);
|
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
if (method === "GET") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /event-references:
|
|
|
|
* get:
|
2022-05-11 13:57:00 +00:00
|
|
|
* summary: Find all event reference
|
|
|
|
* operationId: listEventReferences
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - event-references
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
2022-05-11 13:57:00 +00:00
|
|
|
* description: No event references were found
|
2022-04-29 15:29:57 +00:00
|
|
|
*/
|
2022-04-20 23:30:47 +00:00
|
|
|
const data = await prisma.dailyEventReference.findMany({
|
|
|
|
where: { bookingId: { in: userBookingIds } },
|
|
|
|
});
|
2022-04-11 13:10:16 +00:00
|
|
|
const daily_event_references = data.map((dailyEventReference) =>
|
2022-04-27 17:25:36 +00:00
|
|
|
schemaDailyEventReferenceReadPublic.parse(dailyEventReference)
|
2022-04-11 13:10:16 +00:00
|
|
|
);
|
|
|
|
if (daily_event_references) res.status(200).json({ daily_event_references });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No DailyEventReferences were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /event-references:
|
|
|
|
* post:
|
2022-05-11 13:57:00 +00:00
|
|
|
* summary: Creates a new event reference
|
|
|
|
* operationId: addEventReference
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - event-references
|
|
|
|
* responses:
|
|
|
|
* 201:
|
2022-05-11 13:57:00 +00:00
|
|
|
* description: OK, event reference created
|
2022-04-29 15:29:57 +00:00
|
|
|
* 400:
|
|
|
|
* description: Bad request. DailyEventReference body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
const safe = schemaDailyEventReferenceCreateBodyParams.safeParse(body);
|
2022-05-17 17:33:18 +00:00
|
|
|
if (!safe.success) {
|
|
|
|
res.status(400).json({ message: "Invalid request body" });
|
|
|
|
return;
|
|
|
|
}
|
2022-04-11 13:10:16 +00:00
|
|
|
|
|
|
|
const data = await prisma.dailyEventReference.create({ data: safe.data });
|
2022-04-27 17:25:36 +00:00
|
|
|
const daily_event_reference = schemaDailyEventReferenceReadPublic.parse(data);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
if (daily_event_reference)
|
|
|
|
res.status(201).json({ daily_event_reference, message: "DailyEventReference created successfully" });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
2022-05-11 13:57:00 +00:00
|
|
|
message: "Could not create new event reference",
|
2022-04-11 13:10:16 +00:00
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
2022-04-01 23:55:41 +00:00
|
|
|
|
2022-04-11 13:10:16 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllDailyEventReferences);
|