2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-01 21:03:03 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-11 10:03:15 +00:00
|
|
|
import { BookingReferenceResponse, BookingReferencesResponse } from "@lib/types";
|
2022-04-11 13:42:50 +00:00
|
|
|
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
2022-04-11 10:03:15 +00:00
|
|
|
import {
|
|
|
|
schemaBookingReferenceBodyParams,
|
|
|
|
schemaBookingReferencePublic,
|
|
|
|
} from "@lib/validations/booking-reference";
|
2022-04-01 21:03:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-11 13:10:16 +00:00
|
|
|
* /v1/booking-references:
|
2022-04-01 21:03:03 +00:00
|
|
|
* get:
|
2022-04-11 10:03:15 +00:00
|
|
|
* summary: Get all booking references
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-01 21:03:03 +00:00
|
|
|
* tags:
|
2022-04-03 15:47:18 +00:00
|
|
|
* - booking-references
|
2022-04-01 21:03:03 +00:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
2022-04-11 10:03:15 +00:00
|
|
|
* description: No booking references were found
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new booking reference
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-11 10:03:15 +00:00
|
|
|
* tags:
|
|
|
|
* - booking-references
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, booking reference created
|
|
|
|
* model: BookingReference
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. BookingReference body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
2022-04-01 21:03:03 +00:00
|
|
|
*/
|
2022-04-11 10:03:15 +00:00
|
|
|
async function createOrlistAllBookingReferences(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<BookingReferencesResponse | BookingReferenceResponse>
|
|
|
|
) {
|
|
|
|
const { method } = req;
|
2022-04-11 13:42:50 +00:00
|
|
|
const userId = await getCalcomUserId(res);
|
|
|
|
const userWithBookings = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
include: { bookings: true },
|
|
|
|
});
|
|
|
|
if (!userWithBookings) throw new Error("User not found");
|
|
|
|
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
2022-04-11 10:03:15 +00:00
|
|
|
if (method === "GET") {
|
2022-04-11 13:42:50 +00:00
|
|
|
const data = await prisma.bookingReference.findMany({ where: { id: { in: userBookingIds } } });
|
2022-04-11 10:03:15 +00:00
|
|
|
const booking_references = data.map((bookingReference) =>
|
|
|
|
schemaBookingReferencePublic.parse(bookingReference)
|
|
|
|
);
|
|
|
|
if (booking_references) res.status(200).json({ booking_references });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No BookingReferences were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
|
|
|
const safe = schemaBookingReferenceBodyParams.safeParse(req.body);
|
2022-04-13 00:12:16 +00:00
|
|
|
if (!safe.success) {
|
|
|
|
throw new Error("Invalid request body");
|
|
|
|
}
|
2022-04-11 10:03:15 +00:00
|
|
|
|
2022-04-13 16:53:44 +00:00
|
|
|
// const booking_reference = schemaBookingReferencePublic.parse(data);
|
2022-04-13 00:12:16 +00:00
|
|
|
const userId = await getCalcomUserId(res);
|
|
|
|
const userWithBookings = await prisma.user.findUnique({
|
|
|
|
where: { id: userId },
|
|
|
|
include: { bookings: true },
|
|
|
|
});
|
|
|
|
if (!userWithBookings) {
|
|
|
|
throw new Error("User not found");
|
|
|
|
}
|
|
|
|
const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat();
|
|
|
|
if (userBookingIds.includes(safe.data.bookingId)) {
|
2022-04-13 16:53:44 +00:00
|
|
|
const booking_reference = await prisma.bookingReference.create({
|
|
|
|
data: { ...safe.data },
|
|
|
|
});
|
|
|
|
if (booking_reference) {
|
2022-04-13 00:12:16 +00:00
|
|
|
res.status(201).json({
|
|
|
|
booking_reference,
|
|
|
|
message: "BookingReference created successfully",
|
2022-04-11 10:03:15 +00:00
|
|
|
});
|
2022-04-13 00:12:16 +00:00
|
|
|
} else {
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new booking reference",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else res.status(401).json({ message: "Unauthorized" });
|
2022-04-11 10:03:15 +00:00
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
2022-04-01 21:03:03 +00:00
|
|
|
|
2022-04-11 10:03:15 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllBookingReferences);
|