2023-08-24 09:14:10 +00:00
|
|
|
import type { Prisma } 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-27 08:27:10 +00:00
|
|
|
import { bookingResponsesDbSchema } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
|
2023-03-02 18:15:28 +00:00
|
|
|
import slugify from "@calcom/lib/slugify";
|
2023-08-24 09:14:10 +00:00
|
|
|
import type { PrismaClient } from "@calcom/prisma";
|
2023-03-02 18:15:28 +00:00
|
|
|
|
|
|
|
type BookingSelect = {
|
|
|
|
description: true;
|
|
|
|
customInputs: true;
|
|
|
|
attendees: {
|
|
|
|
select: {
|
|
|
|
email: true;
|
|
|
|
name: true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
location: 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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-27 08:27:10 +00:00
|
|
|
async function getBooking(prisma: PrismaClient, uid: string) {
|
2023-03-02 18:15:28 +00:00
|
|
|
const rawBooking = await prisma.booking.findFirst({
|
2022-04-14 21:25:24 +00:00
|
|
|
where: {
|
|
|
|
uid,
|
|
|
|
},
|
|
|
|
select: {
|
2023-03-14 04:19:05 +00:00
|
|
|
id: true,
|
|
|
|
uid: true,
|
2022-04-14 21:25:24 +00:00
|
|
|
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-14 04:19:05 +00:00
|
|
|
bookingSeat: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
2022-04-14 21:25:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
if (!rawBooking) {
|
|
|
|
return rawBooking;
|
|
|
|
}
|
|
|
|
|
2023-03-27 08:27:10 +00:00
|
|
|
const booking = getBookingWithResponses(rawBooking);
|
2023-03-02 18:15:28 +00:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}>
|
|
|
|
>(
|
2023-03-27 08:27:10 +00:00
|
|
|
booking: T
|
2023-03-02 18:15:28 +00:00
|
|
|
) => {
|
|
|
|
return {
|
|
|
|
...booking,
|
2023-03-27 08:27:10 +00:00
|
|
|
responses: bookingResponsesDbSchema.parse(booking.responses || getResponsesFromOldBooking(booking)),
|
|
|
|
} as Omit<T, "responses"> & { responses: z.infer<typeof bookingResponsesDbSchema> };
|
2023-03-02 18:15:28 +00:00
|
|
|
};
|
2023-04-04 04:59:09 +00:00
|
|
|
|
2022-04-14 21:25:24 +00:00
|
|
|
export default getBooking;
|