2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-26 21:29:30 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-26 21:29:30 +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 { BookingResponse, BookingsResponse } from "@lib/types";
|
2022-04-27 17:25:36 +00:00
|
|
|
import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
|
2022-03-26 21:29:30 +00:00
|
|
|
|
2022-04-29 00:49:38 +00:00
|
|
|
async function createOrlistAllBookings(
|
2022-04-30 18:53:19 +00:00
|
|
|
{ method, body, userId }: NextApiRequest,
|
2022-04-29 00:49:38 +00:00
|
|
|
res: NextApiResponse<BookingsResponse | BookingResponse>
|
|
|
|
) {
|
2022-05-11 13:57:00 +00:00
|
|
|
console.log("userIduserId", userId);
|
2022-04-29 00:49:38 +00:00
|
|
|
if (method === "GET") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings:
|
|
|
|
* get:
|
|
|
|
* summary: Find all bookings
|
2022-05-10 17:52:59 +00:00
|
|
|
* operationId: listBookings
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No bookings were found
|
|
|
|
*/
|
2022-04-29 00:49:38 +00:00
|
|
|
const data = await prisma.booking.findMany({ where: { userId } });
|
|
|
|
const bookings = data.map((booking) => schemaBookingReadPublic.parse(booking));
|
|
|
|
if (bookings) res.status(200).json({ bookings });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No Bookings were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
2022-04-29 15:29:57 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings:
|
|
|
|
* post:
|
|
|
|
* summary: Creates a new booking
|
2022-05-10 17:52:59 +00:00
|
|
|
* operationId: addBooking
|
|
|
|
* requestBody:
|
|
|
|
* description: Edit an existing booking related to one of your event-types
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* title:
|
|
|
|
* type: string
|
|
|
|
* example: 15min
|
|
|
|
* startTime:
|
|
|
|
* type: string
|
|
|
|
* example: 1970-01-01T17:00:00.000Z
|
|
|
|
* endTime:
|
|
|
|
* type: string
|
|
|
|
* example: 1970-01-01T17:00:00.000Z
|
2022-04-29 15:29:57 +00:00
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, booking created
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Booking body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-04-30 18:53:19 +00:00
|
|
|
const safe = schemaBookingCreateBodyParams.safeParse(body);
|
2022-05-10 17:52:59 +00:00
|
|
|
if (!safe.success) {
|
|
|
|
console.log(safe.error);
|
|
|
|
res.status(400).json({ message: "Bad request. Booking body is invalid." });
|
|
|
|
return;
|
|
|
|
}
|
2022-05-11 13:57:00 +00:00
|
|
|
safe.data.userId = userId;
|
|
|
|
const data = await prisma.booking.create({ data: { ...safe.data } });
|
2022-04-27 17:25:36 +00:00
|
|
|
const booking = schemaBookingReadPublic.parse(data);
|
2022-04-01 21:03:03 +00:00
|
|
|
|
2022-06-09 11:39:20 +00:00
|
|
|
if (booking) {
|
|
|
|
res.status(201).json({ booking, message: "Booking created successfully" });
|
|
|
|
|
|
|
|
// Send Webhook call if hooked to BOOKING_CREATED & BOOKING_RESCHEDULED
|
|
|
|
// const eventTrigger: WebhookTriggerEvents = rescheduleUid ? "BOOKING_RESCHEDULED" : "BOOKING_CREATED";
|
|
|
|
// const subscriberOptions = {
|
|
|
|
// userId: user.id,
|
|
|
|
// eventTypeId,
|
|
|
|
// triggerEvent: eventTrigger,
|
|
|
|
// };
|
|
|
|
|
|
|
|
// const subscribers = await getSubscribers(subscriberOptions);
|
|
|
|
// const bookingId = booking?.id;
|
|
|
|
// const promises = subscribers.map((sub) =>
|
|
|
|
// sendPayload(eventTrigger, new Date().toISOString(), sub, {
|
|
|
|
// ...evt,
|
|
|
|
// bookingId,
|
|
|
|
// rescheduleUid,
|
|
|
|
// metadata: reqBody.metadata,
|
|
|
|
// }).catch((e) => {
|
|
|
|
// console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
|
|
|
|
// })
|
|
|
|
// );
|
|
|
|
// await Promise.all(promises);
|
|
|
|
}
|
2022-04-11 10:03:15 +00:00
|
|
|
else
|
2022-05-10 17:52:59 +00:00
|
|
|
(error: Error) => {
|
|
|
|
console.log(error);
|
2022-04-11 10:03:15 +00:00
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new booking",
|
|
|
|
error,
|
|
|
|
});
|
2022-05-10 17:52:59 +00:00
|
|
|
};
|
2022-04-11 10:03:15 +00:00
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-26 21:29:30 +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")(createOrlistAllBookings);
|