feat: Redirect to original event link for rescheduling cancelled booking (#18605)

* feat: redirect to original event link for rescheduling cancelled booking

* allow cancelled booking rescheduling when allowRescheduleForCancelledBooking is true

* chore

* fix test

* chore

---------

Co-authored-by: Tushar Bhatt <95581504+TusharBhatt1@users.noreply.github.com>
This commit is contained in:
Kartik SainiandTushar Bhatt authored and GitHub committed 2025-04-15 14:09:18 -04:00
1 parent d66f983f56
commit 1584dd3179
5 files changed
+96 -29

No files matched your search

@@ -97,7 +97,20 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
} as const;
}
// If booking is already CANCELLED or REJECTED, we can't reschedule this booking. Take the user to the booking page which would show it's correct status and other details.
const eventType = booking.eventType ? booking.eventType : getDefaultEvent(dynamicEventSlugRef);
const enrichedBookingUser = booking.user
? await UserRepository.enrichUserWithItsProfile({ user: booking.user })
: null;
const eventUrl = await buildEventUrlFromBooking({
eventType,
dynamicGroupSlugRef: booking.dynamicGroupSlugRef ?? null,
profileEnrichedBookingUser: enrichedBookingUser,
});
// If booking is already REJECTED, we can't reschedule this booking. Take the user to the booking page which would show it's correct status and other details.
// If the booking is CANCELLED and allowRescheduleForCancelledBooking is false, we redirect the user to the original event link.
// A booking that has been rescheduled to a new booking will also have a status of CANCELLED
const isDisabledRescheduling = booking.eventType?.disableRescheduling;
if (
@@ -107,7 +120,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
) {
return {
redirect: {
destination: `/booking/${uid}`,
destination: booking.status === BookingStatus.CANCELLED ? eventUrl : `/booking/${uid}`,
permanent: false,
},
};
@@ -122,18 +135,6 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
};
}
const eventType = booking.eventType ? booking.eventType : getDefaultEvent(dynamicEventSlugRef);
const enrichedBookingUser = booking.user
? await UserRepository.enrichUserWithItsProfile({ user: booking.user })
: null;
const eventUrl = await buildEventUrlFromBooking({
eventType,
dynamicGroupSlugRef: booking.dynamicGroupSlugRef ?? null,
profileEnrichedBookingUser: enrichedBookingUser,
});
const isBookingInPast = booking.endTime && new Date(booking.endTime) < new Date();
if (isBookingInPast && !eventType.allowReschedulingPastBookings) {
const destinationUrlSearchParams = new URLSearchParams();
@@ -184,6 +185,10 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
destinationUrlSearchParams.set("rescheduleUid", seatReferenceUid || bookingUid);
if (allowRescheduleForCancelledBooking) {
destinationUrlSearchParams.set("allowRescheduleForCancelledBooking", "true");
}
// TODO: I think we should just forward all the query params here including coep flag
if (coepFlag) {
destinationUrlSearchParams.set("flag.coep", coepFlag as string);
@@ -2,6 +2,7 @@ import type { GetServerSidePropsContext } from "next";
import { z } from "zod";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import type { GetBookingType } from "@calcom/features/bookings/lib/get-booking";
import { getBookingForReschedule } from "@calcom/features/bookings/lib/get-booking";
import { getSlugOrRequestedSlug, orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
import { getOrganizationSEOSettings } from "@calcom/features/ee/organizations/lib/orgSettings";
@@ -9,7 +10,7 @@ import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import type { User } from "@calcom/prisma/client";
import { RedirectType } from "@calcom/prisma/client";
import { BookingStatus, RedirectType } from "@calcom/prisma/client";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
@@ -24,6 +25,7 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
const session = await getServerSession({ req });
const { slug: teamSlug, type: meetingSlug } = paramsSchema.parse(params);
const { rescheduleUid, isInstantMeeting: queryIsInstantMeeting, email } = query;
const allowRescheduleForCancelledBooking = query.allowRescheduleForCancelledBooking === "true";
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(req, params?.orgSlug);
const isOrgContext = currentOrgDomain && isValidOrgDomain;
@@ -61,7 +63,18 @@ export const getServerSideProps = async (context: GetServerSidePropsContext) =>
const orgSlug = isValidOrgDomain ? currentOrgDomain : null;
const name = team.parent?.name ?? team.name ?? null;
const booking = rescheduleUid ? await getBookingForReschedule(`${rescheduleUid}`, session?.user?.id) : null;
let booking: GetBookingType | null = null;
if (rescheduleUid) {
booking = await getBookingForReschedule(`${rescheduleUid}`, session?.user?.id);
if (booking?.status === BookingStatus.CANCELLED && !allowRescheduleForCancelledBooking) {
return {
redirect: {
permanent: false,
destination: `/team/${teamSlug}/${meetingSlug}`,
},
};
}
}
const fromRedirectOfNonOrgLink = context.query.orgRedirection === "true";
const isUnpublished = team.parent ? !team.parent.slug : !team.slug;
+6 -2
View File
@@ -246,9 +246,13 @@ test.describe("pro user", () => {
const cancelledHeadline = page.locator('[data-testid="cancelled-headline"]');
await expect(cancelledHeadline).toBeVisible();
const bookingCancelledId = new URL(page.url()).pathname.split("/booking/")[1];
const { slug: eventSlug } = await pro.getFirstEventAsOwner();
await page.goto(`/reschedule/${bookingCancelledId}`);
// Should be redirected to the booking details page which shows the cancelled headline
await expect(page.locator('[data-testid="cancelled-headline"]')).toBeVisible();
// Should be redirected to the original event link
await expect(page).toHaveURL(new RegExp(`/${pro.username}/${eventSlug}`));
});
test("can book an event that requires confirmation and then that booking can be accepted by organizer", async ({
@@ -3,8 +3,8 @@ import type { Session } from "next-auth";
import { z } from "zod";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { getBookingForReschedule, getBookingForSeatedEvent } from "@calcom/features/bookings/lib/get-booking";
import type { GetBookingType } from "@calcom/features/bookings/lib/get-booking";
import { getBookingForReschedule, getBookingForSeatedEvent } from "@calcom/features/bookings/lib/get-booking";
import { orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains";
import type { getPublicEvent } from "@calcom/features/eventtypes/lib/getPublicEvent";
import { getUsernameList } from "@calcom/lib/defaultEvents";
@@ -12,7 +12,7 @@ import { EventRepository } from "@calcom/lib/server/repository/event";
import { UserRepository } from "@calcom/lib/server/repository/user";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { RedirectType } from "@calcom/prisma/client";
import { BookingStatus, RedirectType } from "@calcom/prisma/client";
import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect";
@@ -33,10 +33,12 @@ async function processReschedule({
props,
rescheduleUid,
session,
allowRescheduleForCancelledBooking,
}: {
props: Props;
session: Session | null;
rescheduleUid: string | string[] | undefined;
allowRescheduleForCancelledBooking?: boolean;
}) {
if (!rescheduleUid) return;
@@ -52,7 +54,12 @@ async function processReschedule({
}
// if no booking found, no eventTypeId (dynamic) or it matches this eventData - return void (success).
if (booking === null || !booking.eventTypeId || booking?.eventTypeId === props.eventData?.id) {
if (
booking === null ||
!booking.eventTypeId ||
(booking?.eventTypeId === props.eventData?.id &&
(booking.status !== BookingStatus.CANCELLED || allowRescheduleForCancelledBooking))
) {
props.booking = booking;
props.rescheduleUid = Array.isArray(rescheduleUid) ? rescheduleUid[0] : rescheduleUid;
return;
@@ -82,20 +89,32 @@ async function processReschedule({
async function processSeatedEvent({
props,
bookingUid,
allowRescheduleForCancelledBooking,
}: {
props: Props;
bookingUid: string | string[] | undefined;
allowRescheduleForCancelledBooking?: boolean;
}) {
if (!bookingUid) return;
props.booking = await getBookingForSeatedEvent(`${bookingUid}`);
props.bookingUid = Array.isArray(bookingUid) ? bookingUid[0] : bookingUid;
const booking = await getBookingForSeatedEvent(`${bookingUid}`);
if (booking?.status === BookingStatus.CANCELLED && !allowRescheduleForCancelledBooking) {
return {
redirect: {
permanent: false,
destination: `${props.slug}`,
},
};
} else {
props.booking = booking;
props.bookingUid = Array.isArray(bookingUid) ? bookingUid[0] : bookingUid;
}
}
async function getDynamicGroupPageProps(context: GetServerSidePropsContext) {
const session = await getServerSession({ req: context.req });
const { user: usernames, type: slug } = paramsSchema.parse(context.params);
const { rescheduleUid, bookingUid } = context.query;
const allowRescheduleForCancelledBooking = context.query.allowRescheduleForCancelledBooking === "true";
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req, context.params?.orgSlug);
const org = isValidOrgDomain ? currentOrgDomain : null;
if (!org) {
@@ -162,12 +181,24 @@ async function getDynamicGroupPageProps(context: GetServerSidePropsContext) {
};
if (rescheduleUid) {
const processRescheduleResult = await processReschedule({ props, rescheduleUid, session });
const processRescheduleResult = await processReschedule({
props,
rescheduleUid,
session,
allowRescheduleForCancelledBooking,
});
if (processRescheduleResult) {
return processRescheduleResult;
}
} else if (bookingUid) {
await processSeatedEvent({ props, bookingUid });
const processSeatResult = await processSeatedEvent({
props,
bookingUid,
allowRescheduleForCancelledBooking,
});
if (processSeatResult) {
return processSeatResult;
}
}
return {
@@ -180,6 +211,7 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
const { user: usernames, type: slug } = paramsSchema.parse(context.params);
const username = usernames[0];
const { rescheduleUid, bookingUid } = context.query;
const allowRescheduleForCancelledBooking = context.query.allowRescheduleForCancelledBooking === "true";
const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig(context.req, context.params?.orgSlug);
const isOrgContext = currentOrgDomain && isValidOrgDomain;
@@ -243,14 +275,25 @@ async function getUserPageProps(context: GetServerSidePropsContext) {
rescheduleUid: null,
orgBannerUrl: eventData?.owner?.profile?.organization?.bannerUrl ?? null,
};
if (rescheduleUid) {
const processRescheduleResult = await processReschedule({ props, rescheduleUid, session });
const processRescheduleResult = await processReschedule({
props,
rescheduleUid,
session,
allowRescheduleForCancelledBooking,
});
if (processRescheduleResult) {
return processRescheduleResult;
}
} else if (bookingUid) {
await processSeatedEvent({ props, bookingUid });
const processSeatResult = await processSeatedEvent({
props,
bookingUid,
allowRescheduleForCancelledBooking,
});
if (processSeatResult) {
return processSeatResult;
}
}
return {
@@ -61,6 +61,7 @@ async function getBooking(prisma: PrismaClient, uid: string, isSeatedEvent?: boo
smsReminderNumber: true,
location: true,
eventTypeId: true,
status: true,
eventType: {
select: {
disableRescheduling: true,
@@ -231,6 +232,7 @@ export const getBookingForSeatedEvent = async (uid: string) => {
uid: true,
startTime: true,
endTime: true,
status: true,
attendees: {
select: {
id: true,