2022-10-06 20:40:41 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
2022-10-11 20:09:22 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-10-06 20:40:41 +00:00
|
|
|
|
2022-10-20 17:27:24 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-10-06 20:40:41 +00:00
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaBookingReadPublic } from "~/lib/validations/booking";
|
|
|
|
import { schemaQuerySingleOrMultipleUserIds } from "~/lib/validations/shared/queryUserId";
|
2022-10-06 20:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings:
|
|
|
|
* get:
|
|
|
|
* summary: Find all bookings
|
2023-02-07 15:41:08 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: query
|
|
|
|
* name: apiKey
|
|
|
|
* required: true
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* description: Your API key
|
2023-02-13 12:52:11 +00:00
|
|
|
* - in: query
|
|
|
|
* name: userId
|
|
|
|
* required: false
|
|
|
|
* schema:
|
|
|
|
* oneOf:
|
|
|
|
* - type: integer
|
|
|
|
* - type: array
|
|
|
|
* items:
|
|
|
|
* type: integer
|
2022-10-06 20:40:41 +00:00
|
|
|
* operationId: listBookings
|
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
2022-10-19 16:03:54 +00:00
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* $ref: "#/components/schemas/ArrayOfBookings"
|
2022-10-06 20:40:41 +00:00
|
|
|
* 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;
|
2022-10-20 17:27:24 +00:00
|
|
|
const args: Prisma.BookingFindManyArgs = {};
|
2022-10-19 18:35:34 +00:00
|
|
|
args.include = {
|
|
|
|
attendees: true,
|
|
|
|
user: true,
|
|
|
|
};
|
2022-10-20 17:27:24 +00:00
|
|
|
|
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];
|
2022-10-20 17:27:24 +00:00
|
|
|
const users = await prisma.user.findMany({
|
|
|
|
where: { id: { in: userIds } },
|
|
|
|
select: { email: true },
|
|
|
|
});
|
|
|
|
const userEmails = users.map((u) => u.email);
|
|
|
|
args.where = {
|
|
|
|
OR: [
|
|
|
|
{ userId: { in: userIds } },
|
|
|
|
{
|
|
|
|
attendees: {
|
|
|
|
some: {
|
|
|
|
email: { in: userEmails },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
} else if (!isAdmin) {
|
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
select: {
|
|
|
|
email: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (!user) {
|
|
|
|
throw new HttpError({ message: "User not found", statusCode: 500 });
|
|
|
|
}
|
|
|
|
args.where = {
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
attendees: {
|
|
|
|
some: {
|
|
|
|
email: user.email,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2022-10-11 20:09:22 +00:00
|
|
|
}
|
2022-10-06 20:40:41 +00:00
|
|
|
const data = await prisma.booking.findMany(args);
|
2022-10-11 20:09:22 +00:00
|
|
|
return { bookings: data.map((booking) => schemaBookingReadPublic.parse(booking)) };
|
2022-10-06 20:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(handler);
|