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-26 20:23:33 +00:00
|
|
|
import { schemaAttendeeCreateBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-04-07 01:29:53 +00:00
|
|
|
async function createOrlistAllAttendees(
|
2022-04-29 00:30:59 +00:00
|
|
|
{ method, userId, body }: NextApiRequest,
|
2022-04-07 01:29:53 +00:00
|
|
|
res: NextApiResponse<AttendeesResponse | AttendeeResponse>
|
|
|
|
) {
|
2022-04-08 01:16:53 +00:00
|
|
|
const userBookings = await prisma.booking.findMany({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
attendees: true,
|
|
|
|
},
|
|
|
|
});
|
2022-04-11 10:03:15 +00:00
|
|
|
const attendees = userBookings.map((booking) => booking.attendees).flat();
|
2022-04-07 01:29:53 +00:00
|
|
|
if (method === "GET") {
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /attendees:
|
|
|
|
* get:
|
|
|
|
* summary: Find all attendees
|
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No attendees were found
|
|
|
|
*/
|
2022-04-11 10:03:15 +00:00
|
|
|
if (attendees) res.status(200).json({ attendees });
|
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") {
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /attendees:
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new attendee
|
|
|
|
* requestBody:
|
|
|
|
* description: Create a new attendee related to one of your bookings
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* required:
|
|
|
|
* - bookingId
|
|
|
|
* - name
|
|
|
|
* - email
|
|
|
|
* - timeZone
|
|
|
|
* properties:
|
|
|
|
* bookingId:
|
|
|
|
* type: number
|
|
|
|
* example: 1
|
|
|
|
* email:
|
|
|
|
* type: string
|
|
|
|
* example: email@example.com
|
|
|
|
* name:
|
|
|
|
* type: string
|
|
|
|
* example: John Doe
|
|
|
|
* timeZone:
|
|
|
|
* type: string
|
|
|
|
* example: Europe/London
|
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, attendee created
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Attendee body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
const safePost = schemaAttendeeCreateBodyParams.safeParse(body);
|
2022-04-27 17:25:36 +00:00
|
|
|
if (!safePost.success) {
|
|
|
|
res.status(400).json({ error: safePost.error });
|
|
|
|
throw new Error("Invalid request body", safePost.error);
|
2022-04-08 01:16:53 +00:00
|
|
|
}
|
2022-04-11 14:47:01 +00:00
|
|
|
const userWithBookings = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
include: { bookings: true },
|
2022-04-08 01:16:53 +00:00
|
|
|
});
|
2022-04-11 14:47:01 +00:00
|
|
|
if (!userWithBookings) {
|
|
|
|
throw new Error("User not found");
|
|
|
|
}
|
|
|
|
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
2022-04-23 03:51:26 +00:00
|
|
|
// Here we make sure to only return attendee's of the user's own bookings.
|
2022-04-27 17:25:36 +00:00
|
|
|
if (!userBookingIds.includes(safePost.data.bookingId)) res.status(401).json({ message: "Unauthorized" });
|
2022-04-23 00:06:39 +00:00
|
|
|
else {
|
2022-04-11 14:47:01 +00:00
|
|
|
const data = await prisma.attendee.create({
|
|
|
|
data: {
|
2022-04-27 17:25:36 +00:00
|
|
|
email: safePost.data.email,
|
|
|
|
name: safePost.data.name,
|
|
|
|
timeZone: safePost.data.timeZone,
|
|
|
|
booking: { connect: { id: safePost.data.bookingId } },
|
2022-04-11 14:47:01 +00:00
|
|
|
},
|
|
|
|
});
|
2022-04-26 20:23:33 +00:00
|
|
|
const attendee = schemaAttendeeReadPublic.parse(data);
|
2022-04-01 20:05:10 +00:00
|
|
|
|
2022-04-11 14:47:01 +00:00
|
|
|
if (attendee) {
|
|
|
|
res.status(201).json({
|
|
|
|
attendee,
|
|
|
|
message: "Attendee created successfully",
|
2022-04-07 01:29:53 +00:00
|
|
|
});
|
2022-04-11 14:47:01 +00:00
|
|
|
} else {
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new attendee",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
2022-04-23 00:06:39 +00:00
|
|
|
}
|
2022-04-07 01:29:53 +00:00
|
|
|
} 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);
|