fix: allow new booking to overlap old one when rescheduling (#10424)

Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
Carina WollendorferandCarinaWolli authored and GitHub committed 2023-08-10 19:07:57 +00:00
1 parent c7dfa7bc89
commit a6daf17909
8 files changed
+107 -37

No files matched your search

@@ -1,3 +1,4 @@
import { useSearchParams } from "next/navigation";
import { shallow } from "zustand/shallow";
import { useSchedule } from "@calcom/features/schedules";
@@ -58,6 +59,8 @@ export const useScheduleForEvent = ({
(state) => [state.username, state.eventSlug, state.month, state.selectedDuration],
shallow
);
const serachParams = useSearchParams();
const rescheduleUid = serachParams.get("rescheduleUid");
return useSchedule({
username: usernameFromStore ?? username,
@@ -65,6 +68,7 @@ export const useScheduleForEvent = ({
eventId: event.data?.id ?? eventId,
timezone,
prefetchNextMonth,
rescheduleUid,
month: monthFromStore ?? month,
duration: durationFromStore ?? duration,
});
@@ -94,6 +94,7 @@ const log = logger.getChildLogger({ prefix: ["[api] book:user"] });
type User = Prisma.UserGetPayload<typeof userSelect>;
type BufferedBusyTimes = BufferedBusyTime[];
type BookingType = Prisma.PromiseReturnType<typeof getOriginalRescheduledBooking>;
interface IEventTypePaymentCredentialType {
appId: EventTypeAppsList;
@@ -357,22 +358,35 @@ async function ensureAvailableUsers(
eventType: Awaited<ReturnType<typeof getEventTypesFromDB>> & {
users: IsFixedAwareUser[];
},
input: { dateFrom: string; dateTo: string; timeZone: string },
input: { dateFrom: string; dateTo: string; timeZone: string; originalRescheduledBooking?: BookingType },
recurringDatesInfo?: {
allRecurringDates: string[] | undefined;
currentRecurringIndex: number | undefined;
}
) {
const availableUsers: IsFixedAwareUser[] = [];
const orginalBookingDuration = input.originalRescheduledBooking
? dayjs(input.originalRescheduledBooking.endTime).diff(
dayjs(input.originalRescheduledBooking.startTime),
"minutes"
)
: undefined;
/** Let's start checking for availability */
for (const user of eventType.users) {
const { dateRanges, busy: bufferedBusyTimes } = await getUserAvailability(
{
userId: user.id,
eventTypeId: eventType.id,
duration: orginalBookingDuration,
...input,
},
{ user, eventType }
{
user,
eventType,
rescheduleUid: input.originalRescheduledBooking?.uid ?? null,
}
);
if (!dateRanges.length) {
@@ -806,6 +820,34 @@ async function handler(
}
}
let rescheduleUid = reqBody.rescheduleUid;
let bookingSeat: Prisma.BookingSeatGetPayload<{ include: { booking: true; attendee: true } }> | null = null;
let originalRescheduledBooking: BookingType = null;
if (rescheduleUid) {
// rescheduleUid can be bookingUid and bookingSeatUid
bookingSeat = await prisma.bookingSeat.findUnique({
where: {
referenceUid: rescheduleUid,
},
include: {
booking: true,
attendee: true,
},
});
if (bookingSeat) {
rescheduleUid = bookingSeat.booking.uid;
}
originalRescheduledBooking = await getOriginalRescheduledBooking(
rescheduleUid,
!!eventType.seatsPerTimeSlot
);
if (!originalRescheduledBooking) {
throw new HttpError({ statusCode: 404, message: "Could not find original booking" });
}
}
if (!eventType.seatsPerTimeSlot) {
const availableUsers = await ensureAvailableUsers(
{
@@ -822,6 +864,7 @@ async function handler(
dateFrom: reqBody.start,
dateTo: reqBody.end,
timeZone: reqBody.timeZone,
originalRescheduledBooking,
},
{
allRecurringDates,
@@ -860,34 +903,6 @@ async function handler(
const allCredentials = await getAllCredentials(organizerUser, eventType);
let rescheduleUid = reqBody.rescheduleUid;
let bookingSeat: Prisma.BookingSeatGetPayload<{ include: { booking: true; attendee: true } }> | null = null;
type BookingType = Prisma.PromiseReturnType<typeof getOriginalRescheduledBooking>;
let originalRescheduledBooking: BookingType = null;
if (rescheduleUid) {
// rescheduleUid can be bookingUid and bookingSeatUid
bookingSeat = await prisma.bookingSeat.findUnique({
where: {
referenceUid: rescheduleUid,
},
include: {
booking: true,
attendee: true,
},
});
if (bookingSeat) {
rescheduleUid = bookingSeat.booking.uid;
}
originalRescheduledBooking = await getOriginalRescheduledBooking(
rescheduleUid,
!!eventType.seatsPerTimeSlot
);
if (!originalRescheduledBooking) {
throw new HttpError({ statusCode: 404, message: "Could not find original booking" });
}
}
const isOrganizerRescheduling = organizerUser.id === userId;
const attendeeInfoOnReschedule =
@@ -10,6 +10,7 @@ type UseScheduleWithCacheArgs = {
timezone?: string | null;
prefetchNextMonth?: boolean;
duration?: number | null;
rescheduleUid?: string | null;
};
export const useSchedule = ({
@@ -20,6 +21,7 @@ export const useSchedule = ({
eventId,
prefetchNextMonth,
duration,
rescheduleUid,
}: UseScheduleWithCacheArgs) => {
const monthDayjs = month ? dayjs(month) : dayjs();
const nextMonthDayjs = monthDayjs.add(1, "month");
@@ -40,6 +42,7 @@ export const useSchedule = ({
endTime: (prefetchNextMonth ? nextMonthDayjs : monthDayjs).endOf("month").toISOString(),
timeZone: timezone!,
duration: duration ? `${duration}` : undefined,
rescheduleUid,
},
{
trpc: {