2022-10-07 19:08:25 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaQueryIdParseInt } from "~/lib/validations/shared/queryIdTransformParseInt";
|
2022-10-07 19:08:25 +00:00
|
|
|
|
|
|
|
async function authMiddleware(req: NextApiRequest) {
|
|
|
|
const { userId, isAdmin, prisma } = req;
|
|
|
|
const query = schemaQueryIdParseInt.parse(req.query);
|
|
|
|
// @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) return;
|
|
|
|
// Find all user bookings, including attendees
|
|
|
|
const attendee = await prisma.attendee.findFirst({
|
|
|
|
where: { id: query.id, booking: { userId } },
|
|
|
|
});
|
|
|
|
// Flatten and merge all the attendees in one array
|
2022-10-14 23:41:28 +00:00
|
|
|
if (!attendee) throw new HttpError({ statusCode: 403, message: "Forbidden" });
|
2022-10-07 19:08:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 01:46:45 +00:00
|
|
|
export default authMiddleware;
|