cal.pub0.org/pages/api/bookings/_get.ts

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Prisma } from "@prisma/client";
2022-10-11 20:09:22 +00:00
import type { NextApiRequest } from "next";
import { defaultResponder } from "@calcom/lib/server";
import { schemaBookingReadPublic } from "@lib/validations/booking";
2022-10-11 20:09:22 +00:00
import { schemaQuerySingleOrMultipleUserIds } from "@lib/validations/shared/queryUserId";
/**
* @swagger
* /bookings:
* get:
* summary: Find all bookings
* operationId: listBookings
* tags:
* - bookings
* responses:
* 200:
* description: OK
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/ArrayOfBookings"
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No bookings were found
*/
2022-10-11 20:09:22 +00:00
async function handler(req: NextApiRequest) {
const { userId, isAdmin, prisma } = req;
const args: Prisma.BookingFindManyArgs = isAdmin ? {} : { where: { userId } };
2022-10-19 18:35:34 +00:00
args.include = {
attendees: true,
user: true,
};
2022-10-11 20:09:22 +00:00
/** Only admins can query other users */
if (isAdmin && req.query.userId) {
const query = schemaQuerySingleOrMultipleUserIds.parse(req.query);
const userIds = Array.isArray(query.userId) ? query.userId : [query.userId || userId];
args.where = { userId: { in: userIds } };
}
const data = await prisma.booking.findMany(args);
2022-10-11 20:09:22 +00:00
return { bookings: data.map((booking) => schemaBookingReadPublic.parse(booking)) };
}
export default defaultResponder(handler);