Remove bookingIds map and conditional using in: [ ] (#10655)

This commit is contained in:
alannnc 2023-08-10 10:36:29 -07:00 committed by GitHub
parent 2de5f57437
commit f3eb490e3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 34 deletions

View File

@ -75,45 +75,36 @@ class EventsInsights {
return result; return result;
}; };
static getBaseBookingForEventStatus = async (where: Prisma.BookingTimeStatusWhereInput) => { static getBaseBookingCountForEventStatus = async (where: Prisma.BookingTimeStatusWhereInput) => {
const baseBookings = await prisma.bookingTimeStatus.findMany({ const baseBookings = await prisma.bookingTimeStatus.count({
where, where,
select: {
id: true,
},
}); });
return baseBookings; return baseBookings;
}; };
static getTotalCompletedEvents = async (bookingIds: number[]) => { static getTotalCompletedEvents = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
return await prisma.bookingTimeStatus.count({ return await prisma.bookingTimeStatus.count({
where: { where: {
id: { ...whereConditional,
in: bookingIds,
},
timeStatus: "completed", timeStatus: "completed",
}, },
}); });
}; };
static getTotalRescheduledEvents = async (bookingIds: number[]) => { static getTotalRescheduledEvents = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
return await prisma.bookingTimeStatus.count({ return await prisma.bookingTimeStatus.count({
where: { where: {
id: { ...whereConditional,
in: bookingIds,
},
timeStatus: "rescheduled", timeStatus: "rescheduled",
}, },
}); });
}; };
static getTotalCancelledEvents = async (bookingIds: number[]) => { static getTotalCancelledEvents = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
return await prisma.bookingTimeStatus.count({ return await prisma.bookingTimeStatus.count({
where: { where: {
id: { ...whereConditional,
in: bookingIds,
},
timeStatus: "cancelled", timeStatus: "cancelled",
}, },
}); });

View File

@ -222,53 +222,56 @@ export const insightsRouter = router({
}; };
} }
const baseBookings = await EventsInsights.getBaseBookingForEventStatus({ const baseWhereCondition = {
...whereConditional, ...whereConditional,
createdAt: { createdAt: {
gte: new Date(startDate), gte: new Date(startDate),
lte: new Date(endDate), lte: new Date(endDate),
}, },
}); };
const baseBookingsCount = await EventsInsights.getBaseBookingCountForEventStatus(baseWhereCondition);
const startTimeEndTimeDiff = dayjs(endDate).diff(dayjs(startDate), "day"); const startTimeEndTimeDiff = dayjs(endDate).diff(dayjs(startDate), "day");
const baseBookingIds = baseBookings.map((b) => b.id); const totalCompleted = await EventsInsights.getTotalCompletedEvents(baseWhereCondition);
const totalCompleted = await EventsInsights.getTotalCompletedEvents(baseBookingIds);
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 lastPeriodStartDate = dayjs(startDate).subtract(startTimeEndTimeDiff, "day");
const lastPeriodEndDate = dayjs(endDate).subtract(startTimeEndTimeDiff, "day"); const lastPeriodEndDate = dayjs(endDate).subtract(startTimeEndTimeDiff, "day");
const lastPeriodBaseBookings = await EventsInsights.getBaseBookingForEventStatus({ const lastPeriodBaseCondition = {
...whereConditional, ...whereConditional,
createdAt: { createdAt: {
gte: lastPeriodStartDate.toDate(), gte: lastPeriodStartDate.toDate(),
lte: lastPeriodEndDate.toDate(), lte: lastPeriodEndDate.toDate(),
}, },
teamId: teamId, teamId: teamId,
}); };
const lastPeriodBaseBookingIds = lastPeriodBaseBookings.map((b) => b.id); const lastPeriodBaseBookingsCount = await EventsInsights.getBaseBookingCountForEventStatus(
lastPeriodBaseCondition
const lastPeriodTotalRescheduled = await EventsInsights.getTotalRescheduledEvents(
lastPeriodBaseBookingIds
); );
const lastPeriodTotalCancelled = await EventsInsights.getTotalCancelledEvents(lastPeriodBaseBookingIds); const lastPeriodTotalRescheduled = await EventsInsights.getTotalRescheduledEvents(
lastPeriodBaseCondition
);
const lastPeriodTotalCancelled = await EventsInsights.getTotalCancelledEvents(lastPeriodBaseCondition);
const result = { const result = {
empty: false, empty: false,
created: { created: {
count: baseBookings.length, count: baseBookingsCount,
deltaPrevious: EventsInsights.getPercentage(baseBookings.length, lastPeriodBaseBookings.length), deltaPrevious: EventsInsights.getPercentage(baseBookingsCount, lastPeriodBaseBookingsCount),
}, },
completed: { completed: {
count: totalCompleted, count: totalCompleted,
deltaPrevious: EventsInsights.getPercentage( deltaPrevious: EventsInsights.getPercentage(
baseBookings.length - totalCancelled - totalRescheduled, baseBookingsCount - totalCancelled - totalRescheduled,
lastPeriodBaseBookings.length - lastPeriodTotalCancelled - lastPeriodTotalRescheduled lastPeriodBaseBookingsCount - lastPeriodTotalCancelled - lastPeriodTotalRescheduled
), ),
}, },
rescheduled: { rescheduled: {