perf: improve insights where query and type fix (#18211)

* perf: insights perf improvement

* removed additional const
This commit is contained in:
Alex van Andel
2024-12-16 15:50:20 +00:00
committed by GitHub
parent 423b17a819
commit 04d7537e2f
2 changed files with 10 additions and 12 deletions
@@ -158,13 +158,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
take: 10,
});
const eventTypeIds = bookingsFromSelected
.filter((booking) => typeof booking.eventTypeId === "number")
.map((booking) => booking.eventTypeId);
const eventTypeIds = bookingsFromSelected.reduce((acc: number[], booking) => {
if (typeof booking.eventTypeId !== "number") return acc;
return [...acc, booking.eventTypeId];
}, []);
const eventTypeWhereConditional: Prisma.EventTypeWhereInput = {
id: {
in: eventTypeIds as number[],
in: eventTypeIds,
},
};
+5 -8
View File
@@ -483,7 +483,6 @@ class EventsInsights {
// Obtain the where conditional
let whereConditional: Prisma.BookingTimeStatusWhereInput = {};
let teamConditional: Prisma.TeamWhereInput = {};
if (startDate && endDate) {
whereConditional.createdAt = {
@@ -522,14 +521,12 @@ class EventsInsights {
if (teamsFromOrg.length === 0) {
return {};
}
teamConditional = {
id: {
in: [organizationId, ...teamsFromOrg.map((t) => t.id)],
},
};
const teamIds: number[] = [organizationId, ...teamsFromOrg.map((t) => t.id)];
const usersFromOrg = await prisma.membership.findMany({
where: {
team: teamConditional,
teamId: {
in: teamIds,
},
accepted: true,
},
select: {
@@ -548,7 +545,7 @@ class EventsInsights {
},
{
teamId: {
in: [organizationId, ...teamsFromOrg.map((t) => t.id)],
in: teamIds,
},
isTeamBooking: true,
},