2022-02-10 17:42:06 +00:00
|
|
|
import { Prisma } from "@prisma/client";
|
2021-06-24 22:26:55 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import timezone from "dayjs/plugin/timezone";
|
2021-09-22 18:36:13 +00:00
|
|
|
import utc from "dayjs/plugin/utc";
|
|
|
|
import { GetServerSidePropsContext } from "next";
|
2022-02-01 21:48:40 +00:00
|
|
|
import { JSONObject } from "superjson/dist/types";
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2022-02-11 22:20:10 +00:00
|
|
|
import { asStringOrThrow } from "@lib/asStringOrNull";
|
|
|
|
import prisma from "@lib/prisma";
|
|
|
|
import { inferSSRProps } from "@lib/types/inferSSRProps";
|
|
|
|
|
|
|
|
import BookingPage from "@components/booking/pages/BookingPage";
|
|
|
|
|
|
|
|
import { ssrInit } from "@server/lib/ssr";
|
|
|
|
|
2021-05-26 18:40:22 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
export type BookPageProps = inferSSRProps<typeof getServerSideProps>;
|
|
|
|
|
|
|
|
export default function Book(props: BookPageProps) {
|
2021-09-14 08:45:28 +00:00
|
|
|
return <BookingPage {...props} />;
|
2021-03-22 13:48:48 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2021-09-14 08:45:28 +00:00
|
|
|
const user = await prisma.user.findUnique({
|
|
|
|
where: {
|
2022-03-03 16:42:30 +00:00
|
|
|
username: asStringOrThrow(context.query.user).toLowerCase(),
|
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,
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
|
|
|
});
|
2021-03-22 13:48:48 +00:00
|
|
|
|
2021-09-22 18:36:13 +00:00
|
|
|
if (!user) return { notFound: true };
|
|
|
|
|
2022-02-01 21:48:40 +00:00
|
|
|
const eventTypeRaw = await prisma.eventType.findUnique({
|
2021-06-24 22:26:55 +00:00
|
|
|
where: {
|
2021-09-22 18:36:13 +00:00
|
|
|
id: parseInt(asStringOrThrow(context.query.type)),
|
2021-06-24 22:26:55 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
title: true,
|
|
|
|
slug: true,
|
|
|
|
description: true,
|
|
|
|
length: true,
|
|
|
|
locations: true,
|
|
|
|
customInputs: true,
|
2021-07-15 14:10:26 +00:00
|
|
|
periodType: true,
|
|
|
|
periodDays: true,
|
|
|
|
periodStartDate: true,
|
|
|
|
periodEndDate: true,
|
2022-02-01 21:48:40 +00:00
|
|
|
metadata: true,
|
2021-07-15 14:10:26 +00:00
|
|
|
periodCountCalendarDays: true,
|
2021-09-22 18:36:13 +00:00
|
|
|
price: true,
|
|
|
|
currency: true,
|
2021-09-22 11:04:32 +00:00
|
|
|
disableGuests: true,
|
2021-09-14 08:45:28 +00:00
|
|
|
users: {
|
|
|
|
select: {
|
|
|
|
username: true,
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
bio: true,
|
|
|
|
avatar: true,
|
|
|
|
theme: true,
|
|
|
|
},
|
|
|
|
},
|
2021-06-24 22:26:55 +00:00
|
|
|
},
|
|
|
|
});
|
2021-06-09 18:28:39 +00:00
|
|
|
|
2022-02-01 21:48:40 +00:00
|
|
|
if (!eventTypeRaw) return { notFound: true };
|
|
|
|
|
|
|
|
const credentials = await prisma.credential.findMany({
|
|
|
|
where: {
|
|
|
|
userId: 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,
|
|
|
|
isWeb3Active:
|
|
|
|
web3Credentials && web3Credentials.key
|
|
|
|
? (((web3Credentials.key as JSONObject).isWeb3Active || false) as boolean)
|
|
|
|
: false,
|
|
|
|
};
|
2021-09-22 18:36:13 +00:00
|
|
|
|
2021-08-08 16:17:17 +00:00
|
|
|
const eventTypeObject = [eventType].map((e) => {
|
|
|
|
return {
|
|
|
|
...e,
|
|
|
|
periodStartDate: e.periodStartDate?.toString() ?? null,
|
|
|
|
periodEndDate: e.periodEndDate?.toString() ?? null,
|
|
|
|
};
|
2021-08-04 10:39:43 +00:00
|
|
|
})[0];
|
2021-07-15 14:10:26 +00:00
|
|
|
|
2022-02-10 17:42:06 +00:00
|
|
|
async function getBooking() {
|
|
|
|
return prisma.booking.findFirst({
|
2021-06-24 22:26:55 +00:00
|
|
|
where: {
|
2021-09-22 18:36:13 +00:00
|
|
|
uid: asStringOrThrow(context.query.rescheduleUid),
|
2021-06-24 22:26:55 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
description: true,
|
|
|
|
attendees: {
|
|
|
|
select: {
|
|
|
|
email: true,
|
|
|
|
name: true,
|
|
|
|
},
|
2021-03-22 13:48:48 +00:00
|
|
|
},
|
2021-06-24 22:26:55 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:42:06 +00:00
|
|
|
type Booking = Prisma.PromiseReturnType<typeof getBooking>;
|
|
|
|
let booking: Booking | null = null;
|
|
|
|
|
|
|
|
if (context.query.rescheduleUid) {
|
|
|
|
booking = await getBooking();
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:26:55 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2021-09-14 08:45:28 +00:00
|
|
|
profile: {
|
|
|
|
slug: user.username,
|
|
|
|
name: user.name,
|
|
|
|
image: user.avatar,
|
|
|
|
theme: user.theme,
|
2021-11-16 08:51:46 +00:00
|
|
|
brandColor: user.brandColor,
|
2022-03-05 15:37:46 +00:00
|
|
|
darkBrandColor: user.darkBrandColor,
|
2021-09-14 08:45:28 +00:00
|
|
|
},
|
2021-07-15 14:10:26 +00:00
|
|
|
eventType: eventTypeObject,
|
2021-06-24 22:26:55 +00:00
|
|
|
booking,
|
2021-10-20 16:00:11 +00:00
|
|
|
trpcState: ssr.dehydrate(),
|
2021-06-24 22:26:55 +00:00
|
|
|
},
|
|
|
|
};
|
2021-06-15 15:26:16 +00:00
|
|
|
}
|