Skip to content

Scheduling infrastructure for absolutely everyone.

chore: improve perf for fetching bookings in insights (#17281)

Benny Jood53cabd16392
diff --git a/packages/features/insights/server/events.ts b/packages/features/insights/server/events.ts
index 38d3bc78a3e..1f699ec2b17 100644
--- a/packages/features/insights/server/events.ts
+++ b/packages/features/insights/server/events.ts
@@ -207,39 +207,48 @@ class EventsInsights {
where: whereConditional,
});
- const csvDataWithBookerInfo = await Promise.all(
- csvData.map(async (bookingTimeStatus) => {
- if (!bookingTimeStatus.uid) {
- return bookingTimeStatus;
- }
+ const uids = csvData.filter((b) => b.uid !== null).map((b) => b.uid as string);
+
+ if (uids.length === 0) {
+ return csvData;
+ }
- const booking = await prisma.booking.findUnique({
- where: { uid: bookingTimeStatus.uid },
+ const bookings = await prisma.booking.findMany({
+ where: {
+ uid: {
+ in: uids,
+ },
+ },
+ select: {
+ uid: true,
+ attendees: {
select: {
- attendees: {
- select: {
- name: true,
- email: true,
- },
- },
+ name: true,
+ email: true,
},
- });
+ },
+ },
+ });
+ const bookingMap = new Map(bookings.map((booking) => [booking.uid, booking.attendees[0] || null]));
- if (!booking || !booking.attendees.length) {
- return bookingTimeStatus;
- }
- const { attendees } = booking;
- const booker = attendees[0];
-
- return {
- ...bookingTimeStatus,
- bookerEmail: booker.email,
- bookerName: booker.name,
- };
- })
- );
+ return csvData.map((bookingTimeStatus) => {
+ if (!bookingTimeStatus.uid) {
+ // should not be reached because we filtered above
+ return bookingTimeStatus;
+ }
- return csvDataWithBookerInfo;
+ const booker = bookingMap.get(bookingTimeStatus.uid);
+
+ if (!booker) {
+ return bookingTimeStatus;
+ }
+
+ return {
+ ...bookingTimeStatus,
+ bookerEmail: booker.email,
+ bookerName: booker.name,
+ };
+ });
};
/*