diff --git a/packages/features/insights/server/events.ts b/packages/features/insights/server/events.ts index f7314f4603..240d4b936a 100644 --- a/packages/features/insights/server/events.ts +++ b/packages/features/insights/server/events.ts @@ -75,45 +75,36 @@ class EventsInsights { return result; }; - static getBaseBookingForEventStatus = async (where: Prisma.BookingTimeStatusWhereInput) => { - const baseBookings = await prisma.bookingTimeStatus.findMany({ + static getBaseBookingCountForEventStatus = async (where: Prisma.BookingTimeStatusWhereInput) => { + const baseBookings = await prisma.bookingTimeStatus.count({ where, - select: { - id: true, - }, }); return baseBookings; }; - static getTotalCompletedEvents = async (bookingIds: number[]) => { + static getTotalCompletedEvents = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => { return await prisma.bookingTimeStatus.count({ where: { - id: { - in: bookingIds, - }, + ...whereConditional, timeStatus: "completed", }, }); }; - static getTotalRescheduledEvents = async (bookingIds: number[]) => { + static getTotalRescheduledEvents = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => { return await prisma.bookingTimeStatus.count({ where: { - id: { - in: bookingIds, - }, + ...whereConditional, timeStatus: "rescheduled", }, }); }; - static getTotalCancelledEvents = async (bookingIds: number[]) => { + static getTotalCancelledEvents = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => { return await prisma.bookingTimeStatus.count({ where: { - id: { - in: bookingIds, - }, + ...whereConditional, timeStatus: "cancelled", }, }); diff --git a/packages/features/insights/server/trpc-router.ts b/packages/features/insights/server/trpc-router.ts index b8a4d2b665..3206d4b8a6 100644 --- a/packages/features/insights/server/trpc-router.ts +++ b/packages/features/insights/server/trpc-router.ts @@ -222,53 +222,56 @@ export const insightsRouter = router({ }; } - const baseBookings = await EventsInsights.getBaseBookingForEventStatus({ + const baseWhereCondition = { ...whereConditional, createdAt: { gte: new Date(startDate), lte: new Date(endDate), }, - }); + }; + + const baseBookingsCount = await EventsInsights.getBaseBookingCountForEventStatus(baseWhereCondition); const startTimeEndTimeDiff = dayjs(endDate).diff(dayjs(startDate), "day"); - const baseBookingIds = baseBookings.map((b) => b.id); - const totalCompleted = await EventsInsights.getTotalCompletedEvents(baseBookingIds); + const totalCompleted = await EventsInsights.getTotalCompletedEvents(baseWhereCondition); - const totalRescheduled = await EventsInsights.getTotalRescheduledEvents(baseBookingIds); + const totalRescheduled = await EventsInsights.getTotalRescheduledEvents(baseWhereCondition); - const totalCancelled = await EventsInsights.getTotalCancelledEvents(baseBookingIds); + const totalCancelled = await EventsInsights.getTotalCancelledEvents(baseWhereCondition); const lastPeriodStartDate = dayjs(startDate).subtract(startTimeEndTimeDiff, "day"); const lastPeriodEndDate = dayjs(endDate).subtract(startTimeEndTimeDiff, "day"); - const lastPeriodBaseBookings = await EventsInsights.getBaseBookingForEventStatus({ + const lastPeriodBaseCondition = { ...whereConditional, createdAt: { gte: lastPeriodStartDate.toDate(), lte: lastPeriodEndDate.toDate(), }, teamId: teamId, - }); + }; - const lastPeriodBaseBookingIds = lastPeriodBaseBookings.map((b) => b.id); - - const lastPeriodTotalRescheduled = await EventsInsights.getTotalRescheduledEvents( - lastPeriodBaseBookingIds + const lastPeriodBaseBookingsCount = await EventsInsights.getBaseBookingCountForEventStatus( + lastPeriodBaseCondition ); - const lastPeriodTotalCancelled = await EventsInsights.getTotalCancelledEvents(lastPeriodBaseBookingIds); + const lastPeriodTotalRescheduled = await EventsInsights.getTotalRescheduledEvents( + lastPeriodBaseCondition + ); + + const lastPeriodTotalCancelled = await EventsInsights.getTotalCancelledEvents(lastPeriodBaseCondition); const result = { empty: false, created: { - count: baseBookings.length, - deltaPrevious: EventsInsights.getPercentage(baseBookings.length, lastPeriodBaseBookings.length), + count: baseBookingsCount, + deltaPrevious: EventsInsights.getPercentage(baseBookingsCount, lastPeriodBaseBookingsCount), }, completed: { count: totalCompleted, deltaPrevious: EventsInsights.getPercentage( - baseBookings.length - totalCancelled - totalRescheduled, - lastPeriodBaseBookings.length - lastPeriodTotalCancelled - lastPeriodTotalRescheduled + baseBookingsCount - totalCancelled - totalRescheduled, + lastPeriodBaseBookingsCount - lastPeriodTotalCancelled - lastPeriodTotalRescheduled ), }, rescheduled: {