Skip to content

Commit 4e92b6e

Browse files
authored
chore: Log availability snapshot when booking is created (calcom#22782)
* Add userAvailability type * Add user availability to return value of getUsersAvailability * Log availabilitySnapshot when booking is created * Type fix
1 parent 7b2f811 commit 4e92b6e

4 files changed

Lines changed: 14 additions & 4 deletions

File tree

packages/features/bookings/lib/handleNewBooking.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ async function handler(
658658

659659
let luckyUserResponse;
660660
let isFirstSeat = true;
661+
let availableUsers: IsFixedAwareUser[] = [];
661662

662663
if (eventType.seatsPerTimeSlot) {
663664
const booking = await prisma.booking.findFirst({
@@ -757,7 +758,6 @@ async function handler(
757758
}
758759

759760
if (!input.bookingData.allRecurringDates || input.bookingData.isFirstRecurringSlot) {
760-
let availableUsers: IsFixedAwareUser[] = [];
761761
try {
762762
availableUsers = await ensureAvailableUsers(
763763
{ ...eventTypeWithUsers, users: [...qualifiedRRUsers, ...fixedUsers] as IsFixedAwareUser[] },
@@ -1391,6 +1391,12 @@ async function handler(
13911391
if (booking?.userId) {
13921392
const usersRepository = new UsersRepository();
13931393
await usersRepository.updateLastActiveAt(booking.userId);
1394+
const organizerUserAvailability = availableUsers.find((user) => user.id === booking?.userId);
1395+
1396+
logger.info(`Booking created`, {
1397+
bookingUid: booking.uid,
1398+
availabilitySnapshot: organizerUserAvailability?.availabilityData,
1399+
});
13941400
}
13951401

13961402
// If it's a round robin event, record the reason for the host assignment

packages/features/bookings/lib/handleNewBooking/ensureAvailableUsers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ const _ensureAvailableUsers = async (
9999
afterEventBuffer: eventType.afterEventBuffer,
100100
bypassBusyCalendarTimes: false,
101101
shouldServeCache,
102+
withSource: true,
102103
},
103104
initialData: {
104105
eventType,
@@ -210,7 +211,8 @@ const _ensureAvailableUsers = async (
210211
}
211212
}
212213

213-
usersAvailability.forEach(({ oooExcludedDateRanges: dateRanges, busy: bufferedBusyTimes }, index) => {
214+
usersAvailability.forEach((userAvailability, index) => {
215+
const { oooExcludedDateRanges: dateRanges, busy: bufferedBusyTimes } = userAvailability;
214216
const user = eventType.users[index];
215217

216218
loggerWithEventDetails.debug(
@@ -239,7 +241,7 @@ const _ensureAvailableUsers = async (
239241
eventLength: duration,
240242
});
241243
if (!foundConflict) {
242-
availableUsers.push(user);
244+
availableUsers.push({ ...user, availabilityData: userAvailability });
243245
}
244246
} catch (error) {
245247
loggerWithEventDetails.error("Unable set isAvailableToBeBooked. Using true. ", error);

packages/features/bookings/lib/handleNewBooking/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { TFunction } from "i18next";
44

55
import type { EventTypeAppsList } from "@calcom/app-store/utils";
66
import type { PaymentAppData } from "@calcom/lib/getPaymentAppData";
7+
import type { GetUserAvailabilityResult } from "@calcom/lib/getUserAvailability";
78
import type { userSelect } from "@calcom/prisma";
89
import type { SelectedCalendar } from "@calcom/prisma/client";
910
import type { CredentialForCalendarService } from "@calcom/types/Credential";
@@ -40,6 +41,7 @@ export type IsFixedAwareUser = User & {
4041
weight?: number;
4142
userLevelSelectedCalendars: SelectedCalendar[];
4243
allSelectedCalendars: SelectedCalendar[];
44+
availabilityData?: GetUserAvailabilityResult;
4345
};
4446

4547
export type { PaymentAppData };

packages/lib/getUserAvailability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export type CurrentSeats = Awaited<ReturnType<typeof _getCurrentSeats>>;
266266

267267
export const getCurrentSeats = withReporting(_getCurrentSeats, "getCurrentSeats");
268268

269-
type GetUserAvailabilityResult = ReturnType<typeof _getUserAvailability>;
269+
export type GetUserAvailabilityResult = Awaited<ReturnType<typeof _getUserAvailability>>;
270270

271271
/** This should be called getUsersWorkingHoursAndBusySlots (...and remaining seats, and final timezone) */
272272
const _getUserAvailability = async function getUsersWorkingHoursLifeTheUniverseAndEverythingElse(

0 commit comments

Comments
 (0)