Refactor/booking logic (#183)
refs #175 Reuses the same logic for creating bookings from the web app. Co-authored-by: Leo Giovanetti <[email protected]>
This commit is contained in:
1 parent
f13694fd13
commit
8c24c5c714
9 files changed
+88
-266
No files matched your search
@@ -39,7 +39,7 @@ import { schemaQueryIdParseInt } from "@lib/validations/shared/queryIdTransformP
|
||||
* - bookings
|
||||
* responses:
|
||||
* 201:
|
||||
* description: OK, booking edited successfuly
|
||||
* description: OK, booking edited successfully
|
||||
* 400:
|
||||
* description: Bad request. Booking body is invalid.
|
||||
* 401:
|
||||
|
||||
+7
-121
@@ -1,14 +1,6 @@
|
||||
import { HttpError } from "@/../../packages/lib/http-error";
|
||||
import { WebhookTriggerEvents } from "@prisma/client";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import z from "zod";
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import { BookingResponse, BookingsResponse } from "@calcom/api/lib/types";
|
||||
import sendPayload from "@calcom/api/lib/utils/sendPayload";
|
||||
import getWebhooks from "@calcom/api/lib/utils/webhookSubscriptions";
|
||||
import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@calcom/api/lib/validations/booking";
|
||||
import { schemaEventTypeReadPublic } from "@calcom/api/lib/validations/event-type";
|
||||
import handleNewBooking from "@calcom/features/bookings/lib/handleNewBooking";
|
||||
import { defaultResponder } from "@calcom/lib/server";
|
||||
|
||||
/**
|
||||
@@ -53,117 +45,11 @@ import { defaultResponder } from "@calcom/lib/server";
|
||||
* 401:
|
||||
* description: Authorization information is missing or invalid.
|
||||
*/
|
||||
async function handler(
|
||||
{ body, userId, isAdmin, prisma }: NextApiRequest,
|
||||
res: NextApiResponse<BookingsResponse | BookingResponse>
|
||||
) {
|
||||
const booking = schemaBookingCreateBodyParams.parse(body);
|
||||
if (!isAdmin) {
|
||||
booking.userId = userId;
|
||||
}
|
||||
const eventTypeDb = await prisma.eventType.findUnique({
|
||||
where: { id: booking.eventTypeId },
|
||||
});
|
||||
if (!eventTypeDb) throw new HttpError({ statusCode: 400, message: "Invalid eventTypeId." });
|
||||
const eventType = schemaEventTypeReadPublic.parse(eventTypeDb);
|
||||
let bookings: z.infer<typeof schemaBookingReadPublic>[];
|
||||
if (!eventType) throw new HttpError({ statusCode: 400, message: "Could not create new booking" });
|
||||
if (eventType.recurringEvent) {
|
||||
console.log("Event type has recurring configuration");
|
||||
if (!booking.recurringCount) throw new HttpError({ statusCode: 400, message: "Missing recurringCount." });
|
||||
if (eventType.recurringEvent.count && booking.recurringCount > eventType?.recurringEvent.count) {
|
||||
throw new HttpError({ statusCode: 400, message: "Invalid recurringCount." });
|
||||
}
|
||||
// Event type is recurring, ceating each booking
|
||||
const recurringEventId = uuidv4();
|
||||
const allBookings = await Promise.all(
|
||||
Array.from(Array(booking.recurringCount).keys()).map(async () => {
|
||||
return await prisma.booking.create({
|
||||
data: {
|
||||
uid: uuidv4(),
|
||||
recurringEventId,
|
||||
eventTypeId: booking.eventTypeId,
|
||||
title: booking.title,
|
||||
startTime: booking.startTime,
|
||||
endTime: booking.endTime,
|
||||
userId: booking.userId,
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
bookings = allBookings.map((book) => schemaBookingReadPublic.parse(book));
|
||||
} else {
|
||||
// Event type not recurring, creating as single one
|
||||
const data = await prisma.booking.create({
|
||||
data: {
|
||||
uid: uuidv4(),
|
||||
eventTypeId: booking.eventTypeId,
|
||||
title: booking.title,
|
||||
startTime: booking.startTime,
|
||||
endTime: booking.endTime,
|
||||
userId: booking.userId,
|
||||
},
|
||||
});
|
||||
bookings = [schemaBookingReadPublic.parse(data)];
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
bookings.map(async (booking) => {
|
||||
const evt = {
|
||||
type: eventType?.title || booking.title,
|
||||
title: booking.title,
|
||||
description: "",
|
||||
additionalNotes: "",
|
||||
customInputs: {},
|
||||
startTime: booking.startTime.toISOString(),
|
||||
endTime: booking.endTime.toISOString(),
|
||||
organizer: {
|
||||
name: "",
|
||||
email: "",
|
||||
timeZone: "",
|
||||
language: {
|
||||
locale: "en",
|
||||
},
|
||||
},
|
||||
attendees: [],
|
||||
location: "",
|
||||
destinationCalendar: null,
|
||||
hideCalendar: false,
|
||||
uid: booking.uid,
|
||||
metadata: {},
|
||||
};
|
||||
console.log(`evt: ${evt}`);
|
||||
|
||||
// Send Webhook call if hooked to BOOKING_CREATED
|
||||
const triggerEvent = WebhookTriggerEvents.BOOKING_CREATED;
|
||||
console.log(`Trigger Event: ${triggerEvent}`);
|
||||
const subscriberOptions = {
|
||||
userId,
|
||||
eventTypeId: booking.eventTypeId as number,
|
||||
triggerEvent,
|
||||
};
|
||||
console.log(`subscriberOptions: ${subscriberOptions}`);
|
||||
|
||||
const subscribers = await getWebhooks(subscriberOptions, prisma);
|
||||
console.log(`subscribers: ${subscribers}`);
|
||||
const bookingId = booking?.id;
|
||||
await Promise.all(
|
||||
subscribers.map((sub) =>
|
||||
sendPayload(triggerEvent, new Date().toISOString(), sub, {
|
||||
...evt,
|
||||
bookingId,
|
||||
})
|
||||
)
|
||||
);
|
||||
console.log("All promises resolved! About to send the response");
|
||||
})
|
||||
);
|
||||
|
||||
if (bookings.length > 1) {
|
||||
res.status(201).json({ bookings, message: "Bookings created successfully." });
|
||||
} else {
|
||||
res.status(201).json({ booking: bookings[0], message: "Booking created successfully." });
|
||||
}
|
||||
async function handler(req: NextApiRequest) {
|
||||
const { userId, isAdmin } = req;
|
||||
if (isAdmin) req.userId = req.body.userId || userId;
|
||||
const booking = await handleNewBooking(req);
|
||||
return booking;
|
||||
}
|
||||
|
||||
export default defaultResponder(handler);
|
||||
Reference in new issue
Block a user