2023-02-16 22:39:57 +00:00
|
|
|
import type { Prisma, PrismaClient } from "@prisma/client";
|
2023-03-02 18:15:28 +00:00
|
|
|
import type { z } from "zod";
|
2022-04-14 21:25:24 +00:00
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
import { getBookingResponsesPartialSchema } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
|
|
|
|
import slugify from "@calcom/lib/slugify";
|
|
|
|
import type { eventTypeBookingFields } from "@calcom/prisma/zod-utils";
|
|
|
|
|
|
|
|
type BookingSelect = {
|
|
|
|
description: true;
|
|
|
|
customInputs: true;
|
|
|
|
attendees: {
|
|
|
|
select: {
|
|
|
|
email: true;
|
|
|
|
name: true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
location: true;
|
|
|
|
smsReminderNumber: true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Backward Compatibility for booking created before we had managed booking questions
|
|
|
|
function getResponsesFromOldBooking(
|
|
|
|
rawBooking: Prisma.BookingGetPayload<{
|
|
|
|
select: BookingSelect;
|
|
|
|
}>
|
|
|
|
) {
|
|
|
|
const customInputs = rawBooking.customInputs || {};
|
|
|
|
const responses = Object.keys(customInputs).reduce((acc, label) => {
|
|
|
|
acc[slugify(label) as keyof typeof acc] = customInputs[label as keyof typeof customInputs];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
return {
|
2023-03-03 16:35:58 +00:00
|
|
|
// It is possible to have no attendees in a booking when the booking is cancelled.
|
|
|
|
name: rawBooking.attendees[0]?.name || "Nameless",
|
|
|
|
email: rawBooking.attendees[0]?.email || "",
|
2023-03-02 18:15:28 +00:00
|
|
|
guests: rawBooking.attendees.slice(1).map((attendee) => {
|
|
|
|
return attendee.email;
|
|
|
|
}),
|
|
|
|
notes: rawBooking.description || "",
|
|
|
|
location: {
|
|
|
|
value: rawBooking.location || "",
|
|
|
|
optionValue: rawBooking.location || "",
|
|
|
|
},
|
|
|
|
...responses,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getBooking(
|
|
|
|
prisma: PrismaClient,
|
|
|
|
uid: string,
|
|
|
|
bookingFields: z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">
|
|
|
|
) {
|
|
|
|
const rawBooking = await prisma.booking.findFirst({
|
2022-04-14 21:25:24 +00:00
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
startTime: true,
|
|
|
|
description: true,
|
2022-05-18 21:05:49 +00:00
|
|
|
customInputs: true,
|
2023-03-02 18:15:28 +00:00
|
|
|
responses: true,
|
2022-09-09 19:01:37 +00:00
|
|
|
smsReminderNumber: true,
|
2022-10-17 13:47:11 +00:00
|
|
|
location: true,
|
2022-04-14 21:25:24 +00:00
|
|
|
attendees: {
|
|
|
|
select: {
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
if (!rawBooking) {
|
|
|
|
return rawBooking;
|
|
|
|
}
|
|
|
|
|
|
|
|
const booking = getBookingWithResponses(rawBooking, {
|
|
|
|
bookingFields,
|
|
|
|
});
|
|
|
|
|
2022-04-14 21:25:24 +00:00
|
|
|
if (booking) {
|
|
|
|
// @NOTE: had to do this because Server side cant return [Object objects]
|
|
|
|
// probably fixable with json.stringify -> json.parse
|
|
|
|
booking["startTime"] = (booking?.startTime as Date)?.toISOString() as unknown as Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
return booking;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type GetBookingType = Prisma.PromiseReturnType<typeof getBooking>;
|
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
export const getBookingWithResponses = <
|
|
|
|
T extends Prisma.BookingGetPayload<{
|
|
|
|
select: BookingSelect & {
|
|
|
|
responses: true;
|
|
|
|
};
|
|
|
|
}>
|
|
|
|
>(
|
|
|
|
booking: T,
|
|
|
|
eventType: {
|
|
|
|
bookingFields: z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">;
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
return {
|
|
|
|
...booking,
|
|
|
|
responses: getBookingResponsesPartialSchema({
|
|
|
|
bookingFields: eventType.bookingFields,
|
|
|
|
}).parse(booking.responses || getResponsesFromOldBooking(booking)),
|
|
|
|
};
|
|
|
|
};
|
2022-04-14 21:25:24 +00:00
|
|
|
export default getBooking;
|