perf: Optimize team bookings query by fetching data for multiple users at once (#21137)
Co-authored-by: [email protected] <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
ab8556790e
commit
d770cfc65e
4 files changed
+646
-49
No files matched your search
@@ -17,6 +17,22 @@ type TeamBookingsParamsBase = {
|
||||
shouldReturnCount?: boolean;
|
||||
};
|
||||
|
||||
type TeamBookingsMultipleUsersParamsBase = {
|
||||
users: { id: number; email: string }[];
|
||||
teamId: number;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
excludedUid?: string | null;
|
||||
includeManagedEvents: boolean;
|
||||
shouldReturnCount?: boolean;
|
||||
};
|
||||
|
||||
type TeamBookingsMultipleUsersParamsWithCount = TeamBookingsMultipleUsersParamsBase & {
|
||||
shouldReturnCount: true;
|
||||
};
|
||||
|
||||
type TeamBookingsMultipleUsersParamsWithoutCount = TeamBookingsMultipleUsersParamsBase;
|
||||
|
||||
type TeamBookingsParamsWithCount = TeamBookingsParamsBase & {
|
||||
shouldReturnCount: true;
|
||||
};
|
||||
@@ -666,4 +682,116 @@ export class BookingRepository {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static async getAllAcceptedTeamBookingsOfUsers(
|
||||
params: TeamBookingsMultipleUsersParamsWithCount
|
||||
): Promise<number>;
|
||||
|
||||
static async getAllAcceptedTeamBookingsOfUsers(
|
||||
params: TeamBookingsMultipleUsersParamsWithoutCount
|
||||
): Promise<Array<Booking>>;
|
||||
|
||||
static async getAllAcceptedTeamBookingsOfUsers(params: TeamBookingsMultipleUsersParamsBase) {
|
||||
const { users, teamId, startDate, endDate, excludedUid, shouldReturnCount, includeManagedEvents } =
|
||||
params;
|
||||
|
||||
const baseWhere: Prisma.BookingWhereInput = {
|
||||
status: BookingStatus.ACCEPTED,
|
||||
startTime: {
|
||||
gte: startDate,
|
||||
},
|
||||
endTime: {
|
||||
lte: endDate,
|
||||
},
|
||||
...(excludedUid && {
|
||||
uid: {
|
||||
not: excludedUid,
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
const userIds = users.map((user) => user.id);
|
||||
const userEmails = users.map((user) => user.email);
|
||||
|
||||
const whereCollectiveRoundRobinOwner: Prisma.BookingWhereInput = {
|
||||
...baseWhere,
|
||||
userId: {
|
||||
in: userIds,
|
||||
},
|
||||
eventType: {
|
||||
teamId,
|
||||
},
|
||||
};
|
||||
|
||||
const whereCollectiveRoundRobinBookingsAttendee: Prisma.BookingWhereInput = {
|
||||
...baseWhere,
|
||||
attendees: {
|
||||
some: {
|
||||
email: {
|
||||
in: userEmails,
|
||||
},
|
||||
},
|
||||
},
|
||||
eventType: {
|
||||
teamId,
|
||||
},
|
||||
};
|
||||
|
||||
const whereManagedBookings: Prisma.BookingWhereInput = {
|
||||
...baseWhere,
|
||||
userId: {
|
||||
in: userIds,
|
||||
},
|
||||
eventType: {
|
||||
parent: {
|
||||
teamId,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (shouldReturnCount) {
|
||||
const collectiveRoundRobinBookingsOwner = await prisma.booking.count({
|
||||
where: whereCollectiveRoundRobinOwner,
|
||||
});
|
||||
|
||||
const collectiveRoundRobinBookingsAttendee = await prisma.booking.count({
|
||||
where: whereCollectiveRoundRobinBookingsAttendee,
|
||||
});
|
||||
|
||||
let managedBookings = 0;
|
||||
|
||||
if (includeManagedEvents) {
|
||||
managedBookings = await prisma.booking.count({
|
||||
where: whereManagedBookings,
|
||||
});
|
||||
}
|
||||
|
||||
const totalNrOfBooking =
|
||||
collectiveRoundRobinBookingsOwner + collectiveRoundRobinBookingsAttendee + managedBookings;
|
||||
|
||||
return totalNrOfBooking;
|
||||
}
|
||||
|
||||
const collectiveRoundRobinBookingsOwner = await prisma.booking.findMany({
|
||||
where: whereCollectiveRoundRobinOwner,
|
||||
});
|
||||
|
||||
const collectiveRoundRobinBookingsAttendee = await prisma.booking.findMany({
|
||||
where: whereCollectiveRoundRobinBookingsAttendee,
|
||||
});
|
||||
|
||||
let managedBookings: typeof collectiveRoundRobinBookingsAttendee = [];
|
||||
|
||||
if (includeManagedEvents) {
|
||||
managedBookings = await prisma.booking.findMany({
|
||||
where: whereManagedBookings,
|
||||
});
|
||||
}
|
||||
|
||||
return [
|
||||
...collectiveRoundRobinBookingsOwner,
|
||||
...collectiveRoundRobinBookingsAttendee,
|
||||
...managedBookings,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user