Skip to content

Commit

Permalink
migrate /reschedule/[uid]
Browse files Browse the repository at this point in the history
  • Loading branch information
hbjORbj committed Nov 15, 2023
1 parent efed74c commit 5658134
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { Params } from "next/dist/shared/lib/router/utils/route-matcher";
import { cookies, headers } from "next/headers";
import { redirect, notFound } from "next/navigation";
import { URLSearchParams } from "url";
import { z } from "zod";

import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { getDefaultEvent } from "@calcom/lib/defaultEvents";
import { maybeGetBookingUidFromSeat } from "@calcom/lib/server/maybeGetBookingUidFromSeat";
import prisma, { bookingMinimalSelect } from "@calcom/prisma";

type PageProps = Readonly<{
params: Params;
}>;

export default async function Page({ params }: PageProps) {
await getProps(params);

return null;
}

async function getProps(params: Params) {
const req = { headers: headers(), cookies: cookies() };
const session = await getServerSession({ req });

const { uid: bookingUid, seatReferenceUid } = z
.object({ uid: z.string(), seatReferenceUid: z.string().optional() })
.parse(params);

const { uid, seatReferenceUid: maybeSeatReferenceUid } = await maybeGetBookingUidFromSeat(
prisma,
bookingUid
);
const booking = await prisma.booking.findUnique({
where: {
uid,
},
select: {
...bookingMinimalSelect,
eventType: {
select: {
users: {
select: {
username: true,
},
},
slug: true,
team: {
select: {
slug: true,
},
},
seatsPerTimeSlot: true,
userId: true,
owner: {
select: {
id: true,
},
},
hosts: {
select: {
user: {
select: {
id: true,
},
},
},
},
},
},
dynamicEventSlugRef: true,
dynamicGroupSlugRef: true,
user: true,
},
});
const dynamicEventSlugRef = booking?.dynamicEventSlugRef || "";

if (!booking) {
return notFound();
}

if (!booking?.eventType && !booking?.dynamicEventSlugRef) {
// TODO: Show something in UI to let user know that this booking is not rescheduleable
return notFound();
}

// if booking event type is for a seated event and no seat reference uid is provided, throw not found
if (booking?.eventType?.seatsPerTimeSlot && !maybeSeatReferenceUid) {
const userId = session?.user?.id;

if (!userId && !seatReferenceUid) {
return redirect(`/auth/login?callbackUrl=/reschedule/${bookingUid}`);
}
const userIsHost = booking?.eventType.hosts.find((host) => {
if (host.user.id === userId) return true;
});

const userIsOwnerOfEventType = booking?.eventType.owner?.id === userId;

if (!userIsHost && !userIsOwnerOfEventType) {
return notFound();
}
}

const eventType = booking.eventType ? booking.eventType : getDefaultEvent(dynamicEventSlugRef);

const eventPage = `${
eventType.team
? `team/${eventType.team.slug}`
: dynamicEventSlugRef
? booking.dynamicGroupSlugRef
: booking.user?.username || "rick" /* This shouldn't happen */
}/${eventType?.slug}`;
const destinationUrl = new URLSearchParams();

destinationUrl.set("rescheduleUid", seatReferenceUid || bookingUid);

return redirect(
`/${eventPage}?${destinationUrl.toString()}${eventType.seatsPerTimeSlot ? "&bookingUid=null" : ""}`
);
}
5 changes: 5 additions & 0 deletions apps/web/pages/reschedule/[uid].tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GetServerSidePropsContext } from "next";
import type { Params } from "next/dist/shared/lib/router/utils/route-matcher";
import { URLSearchParams } from "url";
import { z } from "zod";

Expand All @@ -7,6 +8,10 @@ import { getDefaultEvent } from "@calcom/lib/defaultEvents";
import { maybeGetBookingUidFromSeat } from "@calcom/lib/server/maybeGetBookingUidFromSeat";
import prisma, { bookingMinimalSelect } from "@calcom/prisma";

type PageProps = Readonly<{
params: Params;
}>;

export default function Type() {
// Just redirect to the schedule page to reschedule it.
return null;
Expand Down

0 comments on commit 5658134

Please sign in to comment.