perf: batch booking queries in output service (#25900)
Replace N sequential queries with single batch queries in: - getOutputRecurringBookings - getOutputRecurringSeatedBookings Uses existing batch repository methods. Reduces database roundtrips from O(n) to O(1) for recurring booking lookups Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
This commit is contained in:
@@ -203,17 +203,17 @@ export class OutputBookingsService_2024_08_13 {
|
||||
}
|
||||
|
||||
async getOutputRecurringBookings(bookingsIds: number[]) {
|
||||
const transformed = [];
|
||||
|
||||
for (const bookingId of bookingsIds) {
|
||||
const databaseBooking = await this.bookingsRepository.getByIdWithAttendeesAndUserAndEvent(bookingId);
|
||||
const databaseBookings = await this.bookingsRepository.getByIdsWithAttendeesAndUserAndEvent(bookingsIds);
|
||||
|
||||
const bookingsMap = new Map(databaseBookings.map(booking => [booking.id, booking]));
|
||||
|
||||
const transformed = bookingsIds.map(bookingId => {
|
||||
const databaseBooking = bookingsMap.get(bookingId);
|
||||
if (!databaseBooking) {
|
||||
throw new Error(`Booking with id=${bookingId} was not found in the database`);
|
||||
}
|
||||
|
||||
transformed.push(this.getOutputRecurringBooking(databaseBooking));
|
||||
}
|
||||
|
||||
return this.getOutputRecurringBooking(databaseBooking);
|
||||
});
|
||||
return transformed.sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime());
|
||||
}
|
||||
|
||||
@@ -362,17 +362,17 @@ export class OutputBookingsService_2024_08_13 {
|
||||
}
|
||||
|
||||
async getOutputRecurringSeatedBookings(bookingsIds: number[], showAttendees: boolean) {
|
||||
const transformed = [];
|
||||
|
||||
for (const bookingId of bookingsIds) {
|
||||
const databaseBooking =
|
||||
await this.bookingsRepository.getByIdWithAttendeesWithBookingSeatAndUserAndEvent(bookingId);
|
||||
const databaseBookings = await this.bookingsRepository.getByIdsWithAttendeesWithBookingSeatAndUserAndEvent(bookingsIds);
|
||||
|
||||
const bookingsMap = new Map(databaseBookings.map(booking => [booking.id, booking]));
|
||||
|
||||
const transformed = bookingsIds.map(bookingId => {
|
||||
const databaseBooking = bookingsMap.get(bookingId);
|
||||
if (!databaseBooking) {
|
||||
throw new Error(`Booking with id=${bookingId} was not found in the database`);
|
||||
}
|
||||
|
||||
transformed.push(this.getOutputRecurringSeatedBooking(databaseBooking, showAttendees));
|
||||
}
|
||||
return this.getOutputRecurringSeatedBooking(databaseBooking, showAttendees);
|
||||
});
|
||||
|
||||
return transformed.sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user