chore: make generic function for separate or queries (#16522)

* trying to create generic function for ors

* chore: Add generic function to run separate queries for OR

* Refactored getTotalNoShows to use separate queries

* Finished noshows

* Made the no shows logic handle single result
This commit is contained in:
Keith Williams authored and GitHub committed 2024-09-06 12:27:05 +00:00
1 parent 4280f556cb
commit 5c75cfc17f
1 file changed
+62 -43
+62 -43
View File
@@ -5,49 +5,54 @@ import type { Prisma } from "@calcom/prisma/client";
import type { RawDataInput } from "./raw-data.schema";
interface ITimeRange {
start: Dayjs;
end: Dayjs;
}
type TimeViewType = "week" | "month" | "year" | "day";
class EventsInsights {
static countGroupedByStatus = async (where: Prisma.BookingTimeStatusWhereInput) => {
let data;
if (!!where["OR"]) {
const queries = [];
const existingWhereOr = where["OR"];
const { OR: throwAwayOr, ...whereWithoutOr } = where;
for (let i = 0; i < existingWhereOr.length; i++) {
const newWhere = {
...whereWithoutOr,
...existingWhereOr[i],
};
queries.push(
prisma.bookingTimeStatus.groupBy({
where: newWhere,
by: ["timeStatus"],
_count: {
_all: true,
},
})
);
}
const results = await Promise.all(queries);
data = results.flat();
} else {
data = await prisma.bookingTimeStatus.groupBy({
where,
by: ["timeStatus"],
_count: {
_all: true,
},
});
static runSeparateQueriesForOrStatements = async (
where: Prisma.BookingTimeStatusWhereInput,
// eslint-disable-next-line @typescript-eslint/ban-types
queryReference: Function,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalArgs: any
) => {
if (!where["OR"]) {
return await queryReference(originalArgs);
}
const queries = [];
const existingWhereOr = where["OR"];
const { OR: _throwAwayOr, ...whereWithoutOr } = where;
for (let i = 0; i < existingWhereOr.length; i++) {
const newWhere = {
...whereWithoutOr,
...existingWhereOr[i],
};
queries.push(
queryReference({
...originalArgs,
where: newWhere,
})
);
}
const results = await Promise.all(queries);
return results.flat();
};
static countGroupedByStatus = async (where: Prisma.BookingTimeStatusWhereInput) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const queryReference = (args: any) => prisma.bookingTimeStatus.groupBy(args);
const data = await EventsInsights.runSeparateQueriesForOrStatements(where, queryReference, {
where,
by: ["timeStatus"],
_count: {
_all: true,
},
});
return data.reduce(
// @ts-expect-error Element implicitly has any type
(aggregate: { [x: string]: number }, item) => {
if (typeof item.timeStatus === "string") {
aggregate[item.timeStatus] += item._count._all;
@@ -79,12 +84,26 @@ class EventsInsights {
};
static getTotalNoShows = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
return await prisma.bookingTimeStatus.count({
where: {
...whereConditional,
noShowHost: true,
},
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const queryReference = (args: any) => prisma.bookingTimeStatus.count(args);
const originalWhereConditional = {
...whereConditional,
noShowHost: true,
};
const results = await EventsInsights.runSeparateQueriesForOrStatements(
originalWhereConditional,
queryReference,
{
where: {
...originalWhereConditional,
},
}
);
return results.length > 1
? results.reduce((total: number, item: number) => total + (item || 0), 0)
: results;
};
static getTotalCSAT = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {