2023-02-16 22:39:57 +00:00
|
|
|
import type { GetServerSidePropsContext } from "next";
|
2023-03-14 04:19:05 +00:00
|
|
|
import { z } from "zod";
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { LocationObject } from "@calcom/app-store/locations";
|
|
|
|
import { privacyFilteredLocations } from "@calcom/app-store/locations";
|
2023-02-15 15:42:49 +00:00
|
|
|
import { getAppFromSlug } from "@calcom/app-store/utils";
|
2023-03-14 04:19:05 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2023-03-02 18:15:28 +00:00
|
|
|
import { getBookingFieldsWithSystemFields } from "@calcom/features/bookings/lib/getBookingFields";
|
2022-06-10 00:32:34 +00:00
|
|
|
import { parseRecurringEvent } from "@calcom/lib";
|
2022-04-08 16:50:10 +00:00
|
|
|
import {
|
|
|
|
getDefaultEvent,
|
|
|
|
getDynamicEventName,
|
|
|
|
getGroupName,
|
|
|
|
getUsernameList,
|
|
|
|
} from "@calcom/lib/defaultEvents";
|
2023-04-04 04:59:09 +00:00
|
|
|
import getBooking from "@calcom/lib/getBooking";
|
|
|
|
import type { GetBookingType } from "@calcom/lib/getBooking";
|
2022-04-06 17:20:30 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-03-28 09:40:13 +00:00
|
|
|
import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML";
|
2023-03-14 04:19:05 +00:00
|
|
|
import prisma, { bookEventTypeSelect } from "@calcom/prisma";
|
2023-02-15 15:42:49 +00:00
|
|
|
import {
|
|
|
|
customInputSchema,
|
|
|
|
EventTypeMetaDataSchema,
|
|
|
|
userMetadata as userMetadataSchema,
|
|
|
|
} from "@calcom/prisma/zod-utils";
|
2022-04-05 18:03:22 +00:00
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { inferSSRProps } from "@lib/types/inferSSRProps";
|
2022-02-11 22:20:10 +00:00
|
|
|
|
2023-04-18 18:45:32 +00:00
|
|
|
import PageWrapper from "@components/PageWrapper";
|
2022-02-11 22:20:10 +00:00
|
|
|
import BookingPage from "@components/booking/pages/BookingPage";
|
|
|
|
|
|
|
|
import { ssrInit } from "@server/lib/ssr";
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
export type BookPageProps = inferSSRProps<typeof getServerSideProps>;
|
|
|
|
|
|
|
|
export default function Book(props: BookPageProps) {
|
2022-04-06 17:20:30 +00:00
|
|
|
const { t } = useLocale();
|
2022-04-22 15:03:01 +00:00
|
|
|
return props.away ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="dark:bg-inverted h-screen">
|
2022-04-22 15:03:01 +00:00
|
|
|
<main className="mx-auto max-w-3xl px-4 py-24">
|
|
|
|
<div className="space-y-6" data-testid="event-types">
|
|
|
|
<div className="overflow-hidden rounded-sm border dark:border-gray-900">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-muted dark:text-inverted p-8 text-center">
|
|
|
|
<h2 className="font-cal dark:text-inverted text-default mb-2 text-3xl">
|
2022-04-22 15:03:01 +00:00
|
|
|
😴{" " + t("user_away")}
|
|
|
|
</h2>
|
|
|
|
<p className="mx-auto max-w-md">{t("user_away_description")}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
</div>
|
|
|
|
) : props.isDynamicGroupBooking && !props.profile.allowDynamicBooking ? (
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="dark:bg-inverted h-screen">
|
2022-04-06 17:20:30 +00:00
|
|
|
<main className="mx-auto max-w-3xl px-4 py-24">
|
|
|
|
<div className="space-y-6" data-testid="event-types">
|
|
|
|
<div className="overflow-hidden rounded-sm border dark:border-gray-900">
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-muted dark:text-inverted p-8 text-center">
|
|
|
|
<h2 className="font-cal dark:text-inverted text-default mb-2 text-3xl">
|
2022-04-06 17:20:30 +00:00
|
|
|
{" " + t("unavailable")}
|
|
|
|
</h2>
|
|
|
|
<p className="mx-auto max-w-md">{t("user_dynamic_booking_disabled")}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</main>
|
|
|
|
</div>
|
|
|
|
) : (
|
2022-08-26 00:48:50 +00:00
|
|
|
<BookingPage {...props} />
|
2022-04-06 17:20:30 +00:00
|
|
|
);
|
2021-03-22 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 12:16:54 +00:00
|
|
|
Book.isBookingPage = true;
|
2023-04-18 18:45:32 +00:00
|
|
|
Book.PageWrapper = PageWrapper;
|
2023-04-17 12:16:54 +00:00
|
|
|
|
2023-03-14 04:19:05 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
bookingUid: z.string().optional(),
|
|
|
|
count: z.coerce.number().optional(),
|
|
|
|
embed: z.string().optional(),
|
|
|
|
rescheduleUid: z.string().optional(),
|
|
|
|
slug: z.string().optional(),
|
|
|
|
/** This is the event "type" ID */
|
|
|
|
type: z.coerce.number().optional(),
|
|
|
|
user: z.string(),
|
|
|
|
seatReferenceUid: z.string().optional(),
|
|
|
|
date: z.string().optional(),
|
|
|
|
duration: z.coerce.number().optional(),
|
|
|
|
});
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
2021-10-20 16:00:11 +00:00
|
|
|
const ssr = await ssrInit(context);
|
2023-03-14 04:19:05 +00:00
|
|
|
const query = querySchema.parse(context.query);
|
|
|
|
const usernameList = getUsernameList(query.user);
|
|
|
|
const eventTypeSlug = query.slug;
|
|
|
|
const recurringEventCountQuery = query.count;
|
2022-04-06 17:20:30 +00:00
|
|
|
const users = await prisma.user.findMany({
|
2021-09-14 08:45:28 +00:00
|
|
|
where: {
|
2022-04-06 17:20:30 +00:00
|
|
|
username: {
|
|
|
|
in: usernameList,
|
|
|
|
},
|
2021-07-11 19:35:56 +00:00
|
|
|
},
|
2021-09-14 08:45:28 +00:00
|
|
|
select: {
|
2022-02-01 21:48:40 +00:00
|
|
|
id: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
bio: true,
|
|
|
|
avatar: true,
|
|
|
|
theme: true,
|
2021-11-16 08:51:46 +00:00
|
|
|
brandColor: true,
|
2022-03-05 15:37:46 +00:00
|
|
|
darkBrandColor: true,
|
2022-04-06 17:20:30 +00:00
|
|
|
allowDynamicBooking: true,
|
2022-04-22 15:03:01 +00:00
|
|
|
away: true,
|
2023-02-15 15:42:49 +00:00
|
|
|
metadata: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
});
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2022-04-06 17:20:30 +00:00
|
|
|
if (!users.length) return { notFound: true };
|
|
|
|
const [user] = users;
|
2023-03-14 04:19:05 +00:00
|
|
|
const isDynamicGroupBooking = users.length > 1 && !!eventTypeSlug;
|
2022-05-11 05:41:09 +00:00
|
|
|
|
|
|
|
// Dynamic Group link doesn't need a type but it must have a slug
|
2023-03-14 04:19:05 +00:00
|
|
|
if ((!isDynamicGroupBooking && !query.type) || (users.length > 1 && !eventTypeSlug)) {
|
2022-05-11 05:41:09 +00:00
|
|
|
return { notFound: true };
|
|
|
|
}
|
|
|
|
|
2023-03-14 04:19:05 +00:00
|
|
|
const eventTypeRaw = isDynamicGroupBooking
|
|
|
|
? getDefaultEvent(eventTypeSlug)
|
|
|
|
: await prisma.eventType.findUnique({
|
|
|
|
where: {
|
|
|
|
id: query.type,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
...bookEventTypeSelect,
|
|
|
|
},
|
|
|
|
});
|
2021-06-09 18:28:39 +00:00
|
|
|
|
2022-02-01 21:48:40 +00:00
|
|
|
if (!eventTypeRaw) return { notFound: true };
|
|
|
|
const eventType = {
|
|
|
|
...eventTypeRaw,
|
2022-11-28 18:14:01 +00:00
|
|
|
metadata: EventTypeMetaDataSchema.parse(eventTypeRaw.metadata || {}),
|
2023-03-02 18:15:28 +00:00
|
|
|
bookingFields: getBookingFieldsWithSystemFields(eventTypeRaw),
|
2022-06-10 00:32:34 +00:00
|
|
|
recurringEvent: parseRecurringEvent(eventTypeRaw.recurringEvent),
|
2022-02-01 21:48:40 +00:00
|
|
|
};
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2023-02-15 15:42:49 +00:00
|
|
|
const getLocations = () => {
|
2022-08-26 00:48:50 +00:00
|
|
|
let locations = eventTypeRaw.locations || [];
|
2023-02-15 15:42:49 +00:00
|
|
|
if (!isDynamicGroupBooking) return locations;
|
|
|
|
|
|
|
|
let sortedUsers: typeof users = [];
|
|
|
|
// sort and be in the same order as usernameList so first user is the first user in the list
|
|
|
|
if (users.length > 1) {
|
|
|
|
sortedUsers = users.sort((a, b) => {
|
|
|
|
const aIndex = (a.username && usernameList.indexOf(a.username)) || 0;
|
|
|
|
const bIndex = (b.username && usernameList.indexOf(b.username)) || 0;
|
|
|
|
return aIndex - bIndex;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the prefered location type from the first user
|
|
|
|
const firstUsersMetadata = userMetadataSchema.parse(sortedUsers[0].metadata || {});
|
|
|
|
const preferedLocationType = firstUsersMetadata?.defaultConferencingApp;
|
|
|
|
|
|
|
|
if (preferedLocationType?.appSlug) {
|
|
|
|
const foundApp = getAppFromSlug(preferedLocationType.appSlug);
|
|
|
|
const appType = foundApp?.appData?.location?.type;
|
|
|
|
if (appType) {
|
|
|
|
// Replace the location with the prefered location type
|
|
|
|
// This will still be default to daily if the app is not found
|
|
|
|
locations = [{ type: appType, link: preferedLocationType.appLink }] as LocationObject[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return locations;
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventTypeObject = [eventType].map((e) => {
|
|
|
|
let locations = getLocations();
|
2022-08-26 00:48:50 +00:00
|
|
|
locations = privacyFilteredLocations(locations as LocationObject[]);
|
2021-08-08 16:17:17 +00:00
|
|
|
return {
|
|
|
|
...e,
|
2022-08-26 00:48:50 +00:00
|
|
|
locations: locations,
|
2021-08-08 16:17:17 +00:00
|
|
|
periodStartDate: e.periodStartDate?.toString() ?? null,
|
|
|
|
periodEndDate: e.periodEndDate?.toString() ?? null,
|
2022-08-15 19:52:01 +00:00
|
|
|
schedulingType: null,
|
2022-12-01 21:53:52 +00:00
|
|
|
customInputs: customInputSchema.array().parse(e.customInputs || []),
|
2022-08-12 19:29:29 +00:00
|
|
|
users: users.map((u) => ({
|
|
|
|
id: u.id,
|
|
|
|
name: u.name,
|
|
|
|
username: u.username,
|
|
|
|
avatar: u.avatar,
|
|
|
|
image: u.avatar,
|
|
|
|
slug: u.username,
|
|
|
|
theme: u.theme,
|
|
|
|
})),
|
2023-03-28 09:40:13 +00:00
|
|
|
descriptionAsSafeHTML: markdownToSafeHTML(eventType.description),
|
2021-08-08 16:17:17 +00:00
|
|
|
};
|
2021-08-04 10:39:43 +00:00
|
|
|
})[0];
|
2021-07-15 14:10:26 +00:00
|
|
|
|
2023-03-14 04:19:05 +00:00
|
|
|
// If rescheduleUid and event has seats lets convert Uid to bookingUid
|
|
|
|
let rescheduleUid = query.rescheduleUid;
|
|
|
|
const rescheduleEventTypeHasSeats = query.rescheduleUid && eventTypeRaw.seatsPerTimeSlot;
|
|
|
|
let attendeeEmail: string;
|
|
|
|
let bookingUidWithSeats: string | null = null;
|
|
|
|
|
|
|
|
if (rescheduleEventTypeHasSeats) {
|
|
|
|
const bookingSeat = await prisma.bookingSeat.findFirst({
|
|
|
|
where: {
|
|
|
|
referenceUid: query.rescheduleUid,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
attendee: true,
|
|
|
|
booking: {
|
|
|
|
select: {
|
|
|
|
uid: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (bookingSeat) {
|
|
|
|
rescheduleUid = bookingSeat.booking.uid;
|
|
|
|
attendeeEmail = bookingSeat.attendee.email;
|
|
|
|
}
|
2023-03-27 09:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (query.duration) {
|
2023-03-14 04:19:05 +00:00
|
|
|
// If it's not reschedule but event Type has seats we should obtain
|
|
|
|
// the bookingUid regardless and use it to get the booking
|
|
|
|
const currentSeats = await prisma.booking.findFirst({
|
|
|
|
where: {
|
|
|
|
eventTypeId: eventTypeRaw.id,
|
|
|
|
startTime: dayjs(query.date).toISOString(),
|
|
|
|
endTime: dayjs(query.date).add(query.duration, "minutes").toISOString(),
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
uid: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (currentSeats && currentSeats) {
|
|
|
|
bookingUidWithSeats = currentSeats.uid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 21:25:24 +00:00
|
|
|
let booking: GetBookingType | null = null;
|
2023-03-14 04:19:05 +00:00
|
|
|
if (rescheduleUid || query.bookingUid || bookingUidWithSeats) {
|
2023-03-27 08:27:10 +00:00
|
|
|
booking = await getBooking(prisma, rescheduleUid || query.bookingUid || bookingUidWithSeats || "");
|
2022-02-10 17:42:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 04:19:05 +00:00
|
|
|
if (rescheduleEventTypeHasSeats && booking?.attendees && booking?.attendees.length > 0) {
|
|
|
|
const currentAttendee = booking?.attendees.find((attendee) => {
|
|
|
|
return attendee.email === attendeeEmail;
|
|
|
|
});
|
|
|
|
if (currentAttendee) {
|
|
|
|
booking.attendees = [currentAttendee] || [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const dynamicNames = isDynamicGroupBooking ? users.map((user) => user.name || "") : [];
|
2022-04-07 13:41:26 +00:00
|
|
|
|
2022-04-06 17:20:30 +00:00
|
|
|
const profile = isDynamicGroupBooking
|
|
|
|
? {
|
2022-04-07 13:41:26 +00:00
|
|
|
name: getGroupName(dynamicNames),
|
2022-04-06 17:20:30 +00:00
|
|
|
image: null,
|
|
|
|
slug: eventTypeSlug,
|
|
|
|
theme: null,
|
|
|
|
brandColor: "",
|
|
|
|
darkBrandColor: "",
|
2023-03-14 04:19:05 +00:00
|
|
|
allowDynamicBooking: !users.some((user) => !user.allowDynamicBooking),
|
2022-04-08 16:50:10 +00:00
|
|
|
eventName: getDynamicEventName(dynamicNames, eventTypeSlug),
|
2022-04-06 17:20:30 +00:00
|
|
|
}
|
|
|
|
: {
|
|
|
|
name: user.name || user.username,
|
|
|
|
image: user.avatar,
|
|
|
|
slug: user.username,
|
|
|
|
theme: user.theme,
|
|
|
|
brandColor: user.brandColor,
|
|
|
|
darkBrandColor: user.darkBrandColor,
|
2022-04-08 16:50:10 +00:00
|
|
|
eventName: null,
|
2022-04-06 17:20:30 +00:00
|
|
|
};
|
|
|
|
|
2022-05-05 21:16:25 +00:00
|
|
|
// Checking if number of recurring event ocurrances is valid against event type configuration
|
|
|
|
const recurringEventCount =
|
|
|
|
(eventType.recurringEvent?.count &&
|
|
|
|
recurringEventCountQuery &&
|
2023-03-14 04:19:05 +00:00
|
|
|
(recurringEventCountQuery <= eventType.recurringEvent.count
|
|
|
|
? recurringEventCountQuery
|
2022-05-05 21:16:25 +00:00
|
|
|
: eventType.recurringEvent.count)) ||
|
|
|
|
null;
|
|
|
|
|
2023-03-27 11:32:35 +00:00
|
|
|
const currentSlotBooking = await getBooking(prisma, bookingUidWithSeats || "");
|
2023-03-27 09:34:41 +00:00
|
|
|
|
2021-06-24 22:26:55 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-04-22 15:03:01 +00:00
|
|
|
away: user.away,
|
2022-04-06 17:20:30 +00:00
|
|
|
profile,
|
2021-07-15 14:10:26 +00:00
|
|
|
eventType: eventTypeObject,
|
2021-06-24 22:26:55 +00:00
|
|
|
booking,
|
2023-03-27 09:34:41 +00:00
|
|
|
currentSlotBooking: currentSlotBooking,
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringEventCount,
|
2021-10-20 16:00:11 +00:00
|
|
|
trpcState: ssr.dehydrate(),
|
2022-04-06 17:20:30 +00:00
|
|
|
isDynamicGroupBooking,
|
2022-04-28 15:44:26 +00:00
|
|
|
hasHashedBookingLink: false,
|
|
|
|
hashedLink: null,
|
2023-03-14 04:19:05 +00:00
|
|
|
isEmbed: !!query.embed,
|
2021-06-24 22:26:55 +00:00
|
|
|
},
|
|
|
|
};
|
2021-06-15 15:26:16 +00:00
|
|
|
}
|