2022-10-11 14:25:57 +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-11 14:25:57 +00:00
|
|
|
|
|
|
|
async function authMiddleware(req: NextApiRequest) {
|
|
|
|
const { userId, isAdmin, prisma } = req;
|
|
|
|
const { id } = schemaQueryIdParseInt.parse(req.query);
|
|
|
|
// Here we make sure to only return references of the user's own bookings if the user is not an admin.
|
|
|
|
if (isAdmin) return;
|
|
|
|
// Find all references where the user has bookings
|
|
|
|
const bookingReference = await prisma.bookingReference.findFirst({
|
|
|
|
where: { id, booking: { userId } },
|
|
|
|
});
|
2022-10-14 23:41:28 +00:00
|
|
|
if (!bookingReference) throw new HttpError({ statusCode: 403, message: "Forbidden" });
|
2022-10-11 14:25:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default authMiddleware;
|