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

pull/10546/head^2
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;
};
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",
},
});

View File

@ -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: {