2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-04-01 20:05:10 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-07 01:29:53 +00:00
|
|
|
import { AttendeeResponse, AttendeesResponse } from "@lib/types";
|
2022-04-08 16:08:26 +00:00
|
|
|
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
2022-04-07 01:29:53 +00:00
|
|
|
import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-04-01 20:05:10 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /api/attendees:
|
|
|
|
* get:
|
|
|
|
* summary: Get all attendees
|
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No attendees were found
|
2022-04-07 01:29:53 +00:00
|
|
|
* post:
|
|
|
|
* summary: Creates a new attendee
|
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, attendee created
|
|
|
|
* model: Attendee
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Attendee body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
2022-04-01 20:05:10 +00:00
|
|
|
*/
|
2022-04-07 01:29:53 +00:00
|
|
|
async function createOrlistAllAttendees(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<AttendeesResponse | AttendeeResponse>
|
|
|
|
) {
|
|
|
|
const { method } = req;
|
2022-04-08 01:16:53 +00:00
|
|
|
const userId = getCalcomUserId(res);
|
|
|
|
// Here we make sure to only return attendee's of the user's own bookings.
|
|
|
|
const userBookings = await prisma.booking.findMany({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
attendees: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userBookingsAttendees = userBookings.map((booking) => booking.attendees).flat();
|
2022-04-07 01:29:53 +00:00
|
|
|
if (method === "GET") {
|
2022-04-08 01:16:53 +00:00
|
|
|
if (userBookingsAttendees) res.status(200).json({ data: userBookingsAttendees });
|
2022-04-07 01:29:53 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No Attendees were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
|
|
|
const safe = schemaAttendeeBodyParams.safeParse(req.body);
|
2022-04-08 01:16:53 +00:00
|
|
|
if (!safe.success) {
|
|
|
|
console.log(safe.error);
|
|
|
|
throw new Error("Invalid request body", safe.error);
|
|
|
|
}
|
|
|
|
const bookingId = safe.data.bookingId;
|
|
|
|
delete safe.data.bookingId;
|
|
|
|
const noBookingId = safe.data;
|
|
|
|
const attendee = await prisma.attendee.create({
|
|
|
|
data: { ...noBookingId, booking: { connect: { id: parseInt(bookingId as string) } } },
|
|
|
|
});
|
2022-04-07 01:29:53 +00:00
|
|
|
const data = schemaAttendeePublic.parse(attendee);
|
2022-04-01 20:05:10 +00:00
|
|
|
|
2022-04-07 01:29:53 +00:00
|
|
|
if (data) res.status(201).json({ data, message: "Attendee created successfully" });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new attendee",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-26 23:58:22 +00:00
|
|
|
}
|
2022-04-01 20:05:10 +00:00
|
|
|
|
2022-04-08 01:16:53 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllAttendees);
|