2022-04-04 00:02:11 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { AttendeeResponse } from "@lib/types";
|
2022-04-26 20:23:33 +00:00
|
|
|
import { schemaAttendeeEditBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee";
|
2022-04-04 00:02:11 +00:00
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
|
2022-04-29 00:30:59 +00:00
|
|
|
export async function attendeeById(
|
2022-06-15 18:43:35 +00:00
|
|
|
{ method, query, body, userId, isAdmin }: NextApiRequest,
|
2022-04-29 00:30:59 +00:00
|
|
|
res: NextApiResponse<AttendeeResponse>
|
|
|
|
) {
|
2022-04-10 00:10:34 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
2022-04-11 13:20:38 +00:00
|
|
|
if (!safeQuery.success) {
|
2022-04-24 00:10:32 +00:00
|
|
|
res.status(400).json({ error: safeQuery.error });
|
2022-05-05 16:18:00 +00:00
|
|
|
return;
|
2022-04-11 13:20:38 +00:00
|
|
|
}
|
2022-04-29 00:49:38 +00:00
|
|
|
const userBookingsAttendeeIds = await prisma.booking
|
|
|
|
// Find all user bookings, including attendees
|
|
|
|
.findMany({
|
|
|
|
where: { userId },
|
|
|
|
include: { attendees: true },
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
// Flatten and merge all the attendees in one array
|
|
|
|
(bookings) =>
|
|
|
|
bookings
|
2022-04-29 15:36:25 +00:00
|
|
|
.map((bookings) => bookings.attendees)
|
|
|
|
.flat()
|
|
|
|
.map((attendee) => attendee.id)
|
2022-04-29 00:49:38 +00:00
|
|
|
);
|
2022-06-15 18:43:35 +00:00
|
|
|
// @note: Here we make sure to only return attendee's of the user's own bookings if the user is not an admin.
|
|
|
|
if (!isAdmin) {
|
|
|
|
if (!userBookingsAttendeeIds.includes(safeQuery.data.id))
|
|
|
|
res.status(401).json({ message: "Unauthorized" });
|
|
|
|
} else {
|
2022-04-11 13:20:38 +00:00
|
|
|
switch (method) {
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /attendees/{id}:
|
|
|
|
* get:
|
2022-05-05 16:18:00 +00:00
|
|
|
* operationId: getAttendeeById
|
2022-04-29 15:29:57 +00:00
|
|
|
* summary: Find an attendee
|
2022-04-29 00:30:59 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the attendee to get
|
2022-04-29 00:30:59 +00:00
|
|
|
* example: 3
|
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: Attendee was not found
|
|
|
|
*/
|
2022-04-11 13:20:38 +00:00
|
|
|
case "GET":
|
|
|
|
await prisma.attendee
|
|
|
|
.findUnique({ where: { id: safeQuery.data.id } })
|
2022-04-26 20:23:33 +00:00
|
|
|
.then((data) => schemaAttendeeReadPublic.parse(data))
|
2022-04-11 13:20:38 +00:00
|
|
|
.then((attendee) => res.status(200).json({ attendee }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Attendee with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /attendees/{id}:
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing attendee
|
2022-05-05 16:18:00 +00:00
|
|
|
* operationId: editAttendeeById
|
2022-04-29 00:30:59 +00:00
|
|
|
* requestBody:
|
|
|
|
* description: Edit an existing attendee related to one of your bookings
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* email:
|
|
|
|
* type: string
|
|
|
|
* example: email@example.com
|
|
|
|
* name:
|
|
|
|
* type: string
|
|
|
|
* example: John Doe
|
|
|
|
* timeZone:
|
|
|
|
* type: string
|
|
|
|
* example: Europe/London
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* example: 3
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the attendee to edit
|
2022-04-29 00:30:59 +00:00
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, attendee edited successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Attendee body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-11 13:20:38 +00:00
|
|
|
case "PATCH":
|
2022-04-26 20:12:28 +00:00
|
|
|
const safeBody = schemaAttendeeEditBodyParams.safeParse(body);
|
2022-04-11 13:20:38 +00:00
|
|
|
if (!safeBody.success) {
|
2022-04-24 00:10:32 +00:00
|
|
|
res.status(400).json({ message: "Bad request", error: safeBody.error });
|
2022-05-05 16:18:00 +00:00
|
|
|
return;
|
2022-04-11 13:20:38 +00:00
|
|
|
}
|
|
|
|
await prisma.attendee
|
|
|
|
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
2022-04-26 20:23:33 +00:00
|
|
|
.then((data) => schemaAttendeeReadPublic.parse(data))
|
2022-04-11 13:20:38 +00:00
|
|
|
.then((attendee) => res.status(200).json({ attendee }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Attendee with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-29 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /attendees/{id}:
|
|
|
|
* delete:
|
2022-05-05 16:18:00 +00:00
|
|
|
* operationId: removeAttendeeById
|
2022-04-29 00:30:59 +00:00
|
|
|
* summary: Remove an existing attendee
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-05-05 16:18:00 +00:00
|
|
|
* description: ID of the attendee to delete
|
2022-04-29 00:30:59 +00:00
|
|
|
* tags:
|
|
|
|
* - attendees
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, attendee removed successfuly
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Attendee id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-11 13:20:38 +00:00
|
|
|
case "DELETE":
|
|
|
|
await prisma.attendee
|
|
|
|
.delete({ where: { id: safeQuery.data.id } })
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({
|
|
|
|
message: `Attendee with id: ${safeQuery.data.id} deleted successfully`,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Attendee with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 00:02:11 +00:00
|
|
|
|
2022-04-11 13:20:38 +00:00
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
2022-04-23 00:06:39 +00:00
|
|
|
}
|
2022-04-04 00:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(attendeeById));
|