2022-04-28 15:44:26 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import { GetServerSidePropsContext } from "next";
|
|
|
|
import { JSONObject } from "superjson/dist/types";
|
|
|
|
|
|
|
|
import { getLocationLabels } from "@calcom/app-store/utils";
|
2022-06-10 00:32:34 +00:00
|
|
|
import { parseRecurringEvent } from "@calcom/lib";
|
2022-06-06 13:30:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-06-10 23:32:54 +00:00
|
|
|
import { bookEventTypeSelect } from "@calcom/prisma/selects";
|
2022-04-28 15:44:26 +00:00
|
|
|
|
2022-06-10 00:32:34 +00:00
|
|
|
import { asStringOrNull, asStringOrThrow } from "@lib/asStringOrNull";
|
2022-04-28 15:44:26 +00:00
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
import { inferSSRProps } from "@lib/types/inferSSRProps";
|
|
|
|
|
|
|
|
import BookingPage from "@components/booking/pages/BookingPage";
|
|
|
|
|
|
|
|
import { ssrInit } from "@server/lib/ssr";
|
|
|
|
|
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
|
|
|
export type HashLinkPageProps = inferSSRProps<typeof getServerSideProps>;
|
|
|
|
|
|
|
|
export default function Book(props: HashLinkPageProps) {
|
2022-06-06 13:30:41 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const locationLabels = getLocationLabels(t);
|
|
|
|
|
|
|
|
return <BookingPage {...props} locationLabels={locationLabels} />;
|
2022-04-28 15:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
|
|
|
const ssr = await ssrInit(context);
|
|
|
|
const link = asStringOrThrow(context.query.link as string);
|
2022-05-05 21:16:25 +00:00
|
|
|
const recurringEventCountQuery = asStringOrNull(context.query.count);
|
2022-04-28 15:44:26 +00:00
|
|
|
|
|
|
|
const hashedLink = await prisma.hashedLink.findUnique({
|
|
|
|
where: {
|
|
|
|
link,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
eventTypeId: true,
|
|
|
|
eventType: {
|
2022-06-10 23:32:54 +00:00
|
|
|
select: bookEventTypeSelect,
|
2022-04-28 15:44:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const userId = hashedLink?.eventType.userId || hashedLink?.eventType.users[0]?.id;
|
|
|
|
|
|
|
|
if (!userId)
|
|
|
|
return {
|
|
|
|
notFound: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const users = await prisma.user.findMany({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
bio: true,
|
|
|
|
avatar: true,
|
|
|
|
theme: true,
|
|
|
|
brandColor: true,
|
|
|
|
darkBrandColor: true,
|
|
|
|
allowDynamicBooking: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!users.length) return { notFound: true };
|
|
|
|
const [user] = users;
|
|
|
|
const eventTypeRaw = hashedLink?.eventType;
|
|
|
|
|
|
|
|
if (!eventTypeRaw) return { notFound: true };
|
|
|
|
|
|
|
|
const credentials = await prisma.credential.findMany({
|
|
|
|
where: {
|
|
|
|
userId: {
|
|
|
|
in: users.map((user) => user.id),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
type: true,
|
|
|
|
key: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const web3Credentials = credentials.find((credential) => credential.type.includes("_web3"));
|
|
|
|
|
|
|
|
const eventType = {
|
|
|
|
...eventTypeRaw,
|
|
|
|
metadata: (eventTypeRaw.metadata || {}) as JSONObject,
|
2022-06-10 00:32:34 +00:00
|
|
|
recurringEvent: parseRecurringEvent(eventTypeRaw.recurringEvent),
|
2022-04-28 15:44:26 +00:00
|
|
|
isWeb3Active:
|
|
|
|
web3Credentials && web3Credentials.key
|
|
|
|
? (((web3Credentials.key as JSONObject).isWeb3Active || false) as boolean)
|
|
|
|
: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventTypeObject = [eventType].map((e) => {
|
|
|
|
return {
|
|
|
|
...e,
|
|
|
|
periodStartDate: e.periodStartDate?.toString() ?? null,
|
|
|
|
periodEndDate: e.periodEndDate?.toString() ?? null,
|
|
|
|
};
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
const profile = {
|
|
|
|
name: user.name || user.username,
|
|
|
|
image: user.avatar,
|
|
|
|
slug: user.username,
|
|
|
|
theme: user.theme,
|
|
|
|
brandColor: user.brandColor,
|
|
|
|
darkBrandColor: user.darkBrandColor,
|
|
|
|
eventName: null,
|
|
|
|
};
|
|
|
|
|
2022-05-05 21:16:25 +00:00
|
|
|
// Checking if number of recurring event ocurrances is valid against event type configuration
|
|
|
|
const recurringEventCount =
|
|
|
|
(eventTypeObject?.recurringEvent?.count &&
|
|
|
|
recurringEventCountQuery &&
|
|
|
|
(parseInt(recurringEventCountQuery) <= eventTypeObject.recurringEvent.count
|
2022-06-10 20:38:06 +00:00
|
|
|
? parseInt(recurringEventCountQuery)
|
2022-06-10 00:32:34 +00:00
|
|
|
: eventType.recurringEvent?.count)) ||
|
2022-05-05 21:16:25 +00:00
|
|
|
null;
|
|
|
|
|
2022-04-28 15:44:26 +00:00
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
profile,
|
|
|
|
eventType: eventTypeObject,
|
2022-05-02 20:39:35 +00:00
|
|
|
booking: null,
|
2022-04-28 15:44:26 +00:00
|
|
|
trpcState: ssr.dehydrate(),
|
2022-05-05 21:16:25 +00:00
|
|
|
recurringEventCount,
|
2022-04-28 15:44:26 +00:00
|
|
|
isDynamicGroupBooking: false,
|
|
|
|
hasHashedBookingLink: true,
|
|
|
|
hashedLink: link,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|