feat: Rerouting a booking with corrected responses (#17074)

* POC working with no TS error

* cleanup

* Fix query booking

* Simplify things

* Support rerouting to different event

* self-review fixes

* Some improvements

* add more tests

* Remove members relation query

* Udits feedback

* Dont show CTAs when non-eventTypeRedirectUrl action route is chosen

---------

Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
Hariom BalharaandUdit Takkar authored and GitHub committed 2024-10-18 17:11:50 -04:00
1 parent 4f14775a4d
commit b8d4fccbab
33 files changed
+1990 -98

No files matched your search

@@ -47,7 +47,7 @@ export const mapBookingToMutationInput = ({
const routingFormResponseIdParam = searchParams.get("cal.routingFormResponseId");
const routingFormResponseId = routingFormResponseIdParam ? Number(routingFormResponseIdParam) : undefined;
const skipContactOwner = searchParams.get("cal.skipContactOwner") === "true";
const reroutingFormResponses = searchParams.get("cal.reroutingFormResponses");
return {
...values,
user: username,
@@ -72,6 +72,8 @@ export const mapBookingToMutationInput = ({
routedTeamMemberIds,
routingFormResponseId,
skipContactOwner,
// In case of rerouting, the form responses are actually the responses that we need to update.
reroutingFormResponses: reroutingFormResponses ? JSON.parse(reroutingFormResponses) : undefined,
};
};
@@ -235,6 +235,8 @@ async function handler(
rescheduleReason,
luckyUsers,
routedTeamMemberIds,
reroutingFormResponses,
routingFormResponseId,
...reqBody
} = bookingData;
@@ -918,24 +920,12 @@ async function handler(
})
);
// update original rescheduled booking (no seats event)
if (!eventType.seatsPerTimeSlot && originalRescheduledBooking?.uid) {
await prisma.booking.update({
where: {
id: originalRescheduledBooking.id,
},
data: {
rescheduled: true,
status: BookingStatus.CANCELLED,
rescheduledBy: reqBody.rescheduledBy,
},
});
}
try {
booking = await createBooking({
uid,
routedFromRoutingFormResponseId: reqBody.routingFormResponseId,
rescheduledBy: reqBody.rescheduledBy,
routingFormResponseId: routingFormResponseId,
reroutingFormResponses: reroutingFormResponses ?? null,
reqBody: {
user: reqBody.user,
metadata: reqBody.metadata,
@@ -1,6 +1,8 @@
import { Prisma } from "@prisma/client";
import type short from "short-uuid";
import type { z } from "zod";
import type { routingFormResponseInDbSchema } from "@calcom/app-store/routing-forms/zod";
import dayjs from "@calcom/dayjs";
import { isPrismaObjOrUndefined } from "@calcom/lib";
import prisma from "@calcom/prisma";
@@ -21,7 +23,9 @@ type ReqBodyWithEnd = TgetBookingDataSchema & { end: string };
type CreateBookingParams = {
uid: short.SUUID;
routedFromRoutingFormResponseId: number | undefined;
routingFormResponseId: number | undefined;
reroutingFormResponses: z.infer<typeof routingFormResponseInDbSchema> | null;
rescheduledBy: string | undefined;
reqBody: {
user: ReqBodyWithEnd["user"];
metadata: ReqBodyWithEnd["metadata"];
@@ -78,21 +82,20 @@ export async function createBooking({
input,
evt,
originalRescheduledBooking,
routedFromRoutingFormResponseId,
}: CreateBookingParams) {
routingFormResponseId,
reroutingFormResponses,
rescheduledBy,
}: CreateBookingParams & { rescheduledBy: string | undefined }) {
updateEventDetails(evt, originalRescheduledBooking, input.changedOrganizer);
const associatedBookingForFormResponse = routedFromRoutingFormResponseId
? await getAssociatedBookingForFormResponse(routedFromRoutingFormResponseId)
const associatedBookingForFormResponse = routingFormResponseId
? await getAssociatedBookingForFormResponse(routingFormResponseId)
: null;
const newBookingData = buildNewBookingData({
const bookingAndAssociatedData = buildNewBookingData({
uid,
// We allow only the first booking to be connected to the form response
// Other bookings could happen due to user doing a booking by using browser back button to reach the same booking form with same query params and changing the time
// Such case isn't what the Routing Form redirected the user to, so we avoid this at the moment.
routedFromRoutingFormResponseId: associatedBookingForFormResponse
? undefined
: routedFromRoutingFormResponseId,
rescheduledBy,
routingFormResponseId: shouldConnectBookingToFormResponse() ? routingFormResponseId : undefined,
reroutingFormResponses,
reqBody,
eventType,
input,
@@ -101,19 +104,38 @@ export async function createBooking({
});
return await saveBooking(
newBookingData,
bookingAndAssociatedData,
originalRescheduledBooking,
eventType.paymentAppData,
eventType.organizerUser
);
function shouldConnectBookingToFormResponse() {
const isRerouting = !!reroutingFormResponses;
// During rerouting, we want to connect the new booking to the existing form response for booking being rescheduled(original booking)
if (isRerouting) {
return true;
}
// If not rerouting and there is already an associated booking for the form response, we don't want to connect the new booking to the form response
// We allow only the first booking to be connected to the form response
// Other bookings could happen due to user doing a booking by using browser back button to reach the same booking form with same query params and changing the time
// Such case isn't what the Routing Form redirected the user to, so we avoid this at the moment.
if (associatedBookingForFormResponse) {
return false;
}
return true;
}
}
async function saveBooking(
newBookingData: Prisma.BookingCreateInput,
bookingAndAssociatedData: ReturnType<typeof buildNewBookingData>,
originalRescheduledBooking: OriginalRescheduledBooking,
paymentAppData: PaymentAppData,
organizerUser: CreateBookingParams["eventType"]["organizerUser"]
) {
const { newBookingData, reroutingFormResponseUpdateData, originalBookingUpdateDataForCancellation } =
bookingAndAssociatedData;
const createBookingObj = {
include: {
user: {
@@ -143,7 +165,21 @@ async function saveBooking(
});
}
return prisma.booking.create(createBookingObj);
/**
* Reschedule(Cancellation + Creation) with an update of reroutingFormResponse should be atomic
*/
return prisma.$transaction(async (tx) => {
if (originalBookingUpdateDataForCancellation) {
await tx.booking.update(originalBookingUpdateDataForCancellation);
}
const booking = await tx.booking.create(createBookingObj);
if (reroutingFormResponseUpdateData) {
await tx.app_RoutingForms_FormResponse.update(reroutingFormResponseUpdateData);
}
return booking;
});
}
function getEventTypeRel(eventTypeId: EventTypeId) {
@@ -164,12 +200,25 @@ function getAttendeesData(evt: Pick<CalendarEvent, "attendees" | "team">) {
}));
}
function buildNewBookingData(params: CreateBookingParams): Prisma.BookingCreateInput {
const { uid, evt, reqBody, eventType, input, originalRescheduledBooking, routedFromRoutingFormResponseId } =
params;
function buildNewBookingData(params: CreateBookingParams) {
const {
uid,
evt,
reqBody,
eventType,
input,
originalRescheduledBooking,
routingFormResponseId,
reroutingFormResponses,
rescheduledBy,
} = params;
const attendeesData = getAttendeesData(evt);
const eventTypeRel = getEventTypeRel(eventType.id);
const reroutingFormResponseUpdateData = getReroutingFormResponseUpdateData({
reroutingFormResponses,
routingFormResponseId,
});
const newBookingData: Prisma.BookingCreateInput = {
uid,
@@ -206,8 +255,8 @@ function buildNewBookingData(params: CreateBookingParams): Prisma.BookingCreateI
}
: undefined,
routedFromRoutingFormReponse: routedFromRoutingFormResponseId
? { connect: { id: routedFromRoutingFormResponseId } }
routedFromRoutingFormReponse: routingFormResponseId
? { connect: { id: routingFormResponseId } }
: undefined,
};
@@ -215,6 +264,8 @@ function buildNewBookingData(params: CreateBookingParams): Prisma.BookingCreateI
newBookingData.recurringEventId = reqBody.recurringEventId;
}
let originalBookingUpdateDataForCancellation: Prisma.BookingUpdateArgs | undefined = undefined;
if (originalRescheduledBooking) {
newBookingData.metadata = {
...(typeof originalRescheduledBooking.metadata === "object" && originalRescheduledBooking.metadata),
@@ -235,12 +286,53 @@ function buildNewBookingData(params: CreateBookingParams): Prisma.BookingCreateI
(attendee) => attendee.email === input.bookerEmail
);
}
if (originalRescheduledBooking.recurringEventId) {
newBookingData.recurringEventId = originalRescheduledBooking.recurringEventId;
}
if (!evt.seatsPerTimeSlot && originalRescheduledBooking?.uid) {
originalBookingUpdateDataForCancellation = {
where: {
id: originalRescheduledBooking.id,
},
data: {
rescheduled: true,
status: BookingStatus.CANCELLED,
rescheduledBy: rescheduledBy,
},
};
}
}
return newBookingData;
return {
newBookingData,
reroutingFormResponseUpdateData,
originalBookingUpdateDataForCancellation,
};
function getReroutingFormResponseUpdateData({
reroutingFormResponses,
routingFormResponseId,
}: {
reroutingFormResponses: z.infer<typeof routingFormResponseInDbSchema> | null;
routingFormResponseId: number | undefined | null;
}) {
if (!routingFormResponseId) {
return null;
}
if (!reroutingFormResponses) {
return null;
}
return {
where: { id: routingFormResponseId },
data: {
response: reroutingFormResponses,
},
};
}
}
export type Booking = Prisma.PromiseReturnType<typeof createBooking>;