chore: Remove separate OR queries for Insights (#16568)
Co-authored-by: Anik Dhabal Babu <[email protected]>
This commit is contained in:
1 parent
1a86e7c506
commit
bde40dceb3
1 file changed
+4
-62
@@ -1,5 +1,3 @@
|
|||||||
import type { AcceleratePromise } from "@prisma/extension-accelerate";
|
|
||||||
|
|
||||||
import type { Dayjs } from "@calcom/dayjs";
|
import type { Dayjs } from "@calcom/dayjs";
|
||||||
import dayjs from "@calcom/dayjs";
|
import dayjs from "@calcom/dayjs";
|
||||||
import { readonlyPrisma as prisma } from "@calcom/prisma";
|
import { readonlyPrisma as prisma } from "@calcom/prisma";
|
||||||
@@ -10,49 +8,8 @@ import type { RawDataInput } from "./raw-data.schema";
|
|||||||
type TimeViewType = "week" | "month" | "year" | "day";
|
type TimeViewType = "week" | "month" | "year" | "day";
|
||||||
|
|
||||||
class EventsInsights {
|
class EventsInsights {
|
||||||
static runSeparateQueriesForOrStatements = async <T, R>(
|
|
||||||
where: Prisma.BookingTimeStatusWhereInput,
|
|
||||||
|
|
||||||
queryReference: (args: T) => AcceleratePromise<R>,
|
|
||||||
|
|
||||||
originalArgs: T
|
|
||||||
) => {
|
|
||||||
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) => {
|
static countGroupedByStatus = async (where: Prisma.BookingTimeStatusWhereInput) => {
|
||||||
const queryReference = (args: Prisma.BookingTimeStatusGroupByArgs) =>
|
const data = await prisma.bookingTimeStatus.groupBy({
|
||||||
// casting since TS is not able to infer the type of the output of the query properly
|
|
||||||
// Typescript infers this as AcceleratePromise<{}[]> which is not specific enough
|
|
||||||
prisma.bookingTimeStatus.groupBy(args) as unknown as AcceleratePromise<
|
|
||||||
Prisma.BookingTimeStatusGroupByOutputType[]
|
|
||||||
>;
|
|
||||||
|
|
||||||
const data = await EventsInsights.runSeparateQueriesForOrStatements<
|
|
||||||
Prisma.BookingTimeStatusGroupByArgs,
|
|
||||||
Prisma.BookingTimeStatusGroupByOutputType[]
|
|
||||||
>(where, queryReference, {
|
|
||||||
where,
|
where,
|
||||||
by: ["timeStatus"],
|
by: ["timeStatus"],
|
||||||
_count: {
|
_count: {
|
||||||
@@ -92,27 +49,12 @@ class EventsInsights {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static getTotalNoShows = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
|
static getTotalNoShows = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
|
||||||
const queryReference = (args: Prisma.BookingTimeStatusCountArgs) => prisma.bookingTimeStatus.count(args);
|
return await prisma.bookingTimeStatus.count({
|
||||||
const originalWhereConditional = {
|
|
||||||
...whereConditional,
|
|
||||||
noShowHost: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const results = await EventsInsights.runSeparateQueriesForOrStatements<
|
|
||||||
Prisma.BookingTimeStatusCountArgs,
|
|
||||||
number
|
|
||||||
>(originalWhereConditional, queryReference, {
|
|
||||||
where: {
|
where: {
|
||||||
...originalWhereConditional,
|
...whereConditional,
|
||||||
|
noShowHost: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// we don't know if WHERE clause contains OR, so we need to check if it's an array
|
|
||||||
if (Array.isArray(results)) {
|
|
||||||
return results.reduce((total: number, item: number) => total + (item || 0), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static getTotalCSAT = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
|
static getTotalCSAT = async (whereConditional: Prisma.BookingTimeStatusWhereInput) => {
|
||||||
|
|||||||
Reference in new issue
Block a user