Skip to content

Commit c3985e3

Browse files
authored
fix: including Prisma.sql for Prisma v6 upgrade (calcom#23775)
1 parent c28eb90 commit c3985e3

2 files changed

Lines changed: 172 additions & 127 deletions

File tree

packages/lib/server/service/InsightsBookingBaseService.ts

Lines changed: 106 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
isNumberFilterValue,
1313
} from "@calcom/features/data-table/lib/utils";
1414
import type { DateRange } from "@calcom/features/insights/server/insightsDateUtils";
15-
import { Prisma } from "@calcom/prisma/client";
1615
import type { PrismaClient } from "@calcom/prisma";
16+
import { Prisma } from "@calcom/prisma/client";
1717
import { MembershipRole } from "@calcom/prisma/enums";
1818

1919
import { MembershipRepository } from "../repository/membership";
@@ -168,12 +168,7 @@ export class InsightsBookingBaseService {
168168
async getBookingsByHourStats({ timeZone }: { timeZone: string }) {
169169
const baseConditions = await this.getBaseConditions();
170170

171-
const results = await this.prisma.$queryRaw<
172-
Array<{
173-
hour: string;
174-
count: number;
175-
}>
176-
>`
171+
const query = Prisma.sql`
177172
SELECT
178173
EXTRACT(HOUR FROM ("startTime" AT TIME ZONE 'UTC' AT TIME ZONE ${timeZone}))::int as "hour",
179174
COUNT(*)::int as "count"
@@ -184,6 +179,13 @@ export class InsightsBookingBaseService {
184179
ORDER BY 1
185180
`;
186181

182+
const results = await this.prisma.$queryRaw<
183+
Array<{
184+
hour: string;
185+
count: number;
186+
}>
187+
>(query);
188+
187189
// Create a map of results by hour for easy lookup
188190
const resultsMap = new Map(results.map((row) => [Number(row.hour), row.count]));
189191

@@ -216,11 +218,13 @@ export class InsightsBookingBaseService {
216218
}
217219
}
218220

219-
return await this.prisma.$queryRaw<Array<SelectedFields<TSelect>>>`
221+
const query = Prisma.sql`
220222
SELECT ${selectFields}
221223
FROM "BookingTimeStatusDenormalized"
222224
WHERE ${baseConditions}
223225
`;
226+
227+
return await this.prisma.$queryRaw<Array<SelectedFields<TSelect>>>(query);
224228
}
225229

226230
async getBaseConditions(): Promise<Prisma.Sql> {
@@ -449,33 +453,17 @@ export class InsightsBookingBaseService {
449453
const baseConditions = await this.getBaseConditions();
450454

451455
// Get total count first
452-
const totalCountResult = await this.prisma.$queryRaw<[{ count: number }]>`
456+
const totalCountQuery = Prisma.sql`
453457
SELECT COUNT(*)::int as count
454458
FROM "BookingTimeStatusDenormalized"
455459
WHERE ${baseConditions}
456460
`;
461+
462+
const totalCountResult = await this.prisma.$queryRaw<[{ count: number }]>(totalCountQuery);
457463
const totalCount = totalCountResult[0]?.count || 0;
458464

459465
// 1. Get booking data from BookingTimeStatusDenormalized
460-
const csvData = await this.prisma.$queryRaw<
461-
Array<{
462-
id: number;
463-
uid: string | null;
464-
title: string;
465-
createdAt: Date;
466-
timeStatus: string;
467-
eventTypeId: number | null;
468-
eventLength: number;
469-
startTime: Date;
470-
endTime: Date;
471-
paid: boolean;
472-
userEmail: string;
473-
userUsername: string;
474-
rating: number | null;
475-
ratingFeedback: string | null;
476-
noShowHost: boolean;
477-
}>
478-
>`
466+
const csvDataQuery = Prisma.sql`
479467
SELECT
480468
"id",
481469
"uid",
@@ -499,6 +487,26 @@ export class InsightsBookingBaseService {
499487
OFFSET ${offset}
500488
`;
501489

490+
const csvData = await this.prisma.$queryRaw<
491+
Array<{
492+
id: number;
493+
uid: string | null;
494+
title: string;
495+
createdAt: Date;
496+
timeStatus: string;
497+
eventTypeId: number | null;
498+
eventLength: number;
499+
startTime: Date;
500+
endTime: Date;
501+
paid: boolean;
502+
userEmail: string;
503+
userUsername: string;
504+
rating: number | null;
505+
ratingFeedback: string | null;
506+
noShowHost: boolean;
507+
}>
508+
>(csvDataQuery);
509+
502510
if (csvData.length === 0) {
503511
return { data: csvData, total: totalCount };
504512
}
@@ -641,15 +649,7 @@ export class InsightsBookingBaseService {
641649

642650
const baseConditions = await this.getBaseConditions();
643651

644-
const data = await this.prisma.$queryRaw<
645-
{
646-
date: Date;
647-
bookingsCount: number;
648-
timeStatus: string;
649-
noShowHost: boolean;
650-
noShowGuests: number;
651-
}[]
652-
>`
652+
const query = Prisma.sql`
653653
WITH booking_stats AS (
654654
SELECT
655655
DATE("createdAt" AT TIME ZONE ${timeZone}) as "date",
@@ -688,6 +688,16 @@ export class InsightsBookingBaseService {
688688
ORDER BY bs."date"
689689
`;
690690

691+
const data = await this.prisma.$queryRaw<
692+
{
693+
date: Date;
694+
bookingsCount: number;
695+
timeStatus: string;
696+
noShowHost: boolean;
697+
noShowGuests: number;
698+
}[]
699+
>(query);
700+
691701
// Initialize aggregate object with zero counts for all date ranges
692702
const aggregate: {
693703
[date: string]: {
@@ -777,12 +787,7 @@ export class InsightsBookingBaseService {
777787
async getPopularEventsStats() {
778788
const baseConditions = await this.getBaseConditions();
779789

780-
const bookingsFromSelected = await this.prisma.$queryRaw<
781-
Array<{
782-
eventTypeId: number;
783-
count: number;
784-
}>
785-
>`
790+
const query = Prisma.sql`
786791
SELECT
787792
"eventTypeId",
788793
COUNT(id)::int as count
@@ -793,6 +798,13 @@ export class InsightsBookingBaseService {
793798
LIMIT 10
794799
`;
795800

801+
const bookingsFromSelected = await this.prisma.$queryRaw<
802+
Array<{
803+
eventTypeId: number;
804+
count: number;
805+
}>
806+
>(query);
807+
796808
const eventTypeIds = bookingsFromSelected.map((booking) => booking.eventTypeId);
797809

798810
if (eventTypeIds.length === 0) {
@@ -866,12 +878,7 @@ export class InsightsBookingBaseService {
866878
additionalCondition = Prisma.sql`AND status = 'accepted'`;
867879
}
868880

869-
const bookingsFromTeam = await this.prisma.$queryRaw<
870-
Array<{
871-
userId: number;
872-
count: number;
873-
}>
874-
>`
881+
const query = Prisma.sql`
875882
SELECT
876883
"userId",
877884
COUNT(id)::int as count
@@ -882,6 +889,13 @@ export class InsightsBookingBaseService {
882889
LIMIT 10
883890
`;
884891

892+
const bookingsFromTeam = await this.prisma.$queryRaw<
893+
Array<{
894+
userId: number;
895+
count: number;
896+
}>
897+
>(query);
898+
885899
if (bookingsFromTeam.length === 0) {
886900
return [];
887901
}
@@ -927,12 +941,7 @@ export class InsightsBookingBaseService {
927941
async getMembersRatingStats(sortOrder: "ASC" | "DESC" = "DESC"): Promise<UserStatsData> {
928942
const baseConditions = await this.getBaseConditions();
929943

930-
const bookingsFromTeam = await this.prisma.$queryRaw<
931-
Array<{
932-
userId: number;
933-
count: number;
934-
}>
935-
>`
944+
const query = Prisma.sql`
936945
SELECT
937946
"userId",
938947
AVG("rating")::float as "count"
@@ -943,6 +952,13 @@ export class InsightsBookingBaseService {
943952
LIMIT 10
944953
`;
945954

955+
const bookingsFromTeam = await this.prisma.$queryRaw<
956+
Array<{
957+
userId: number;
958+
count: number;
959+
}>
960+
>(query);
961+
946962
if (bookingsFromTeam.length === 0) {
947963
return [];
948964
}
@@ -988,13 +1004,7 @@ export class InsightsBookingBaseService {
9881004
async getRecentRatingsStats() {
9891005
const baseConditions = await this.getBaseConditions();
9901006

991-
const bookingsFromTeam = await this.prisma.$queryRaw<
992-
Array<{
993-
userId: number | null;
994-
rating: number | null;
995-
ratingFeedback: string | null;
996-
}>
997-
>`
1007+
const query = Prisma.sql`
9981008
SELECT
9991009
"userId",
10001010
"rating",
@@ -1005,6 +1015,14 @@ export class InsightsBookingBaseService {
10051015
LIMIT 10
10061016
`;
10071017

1018+
const bookingsFromTeam = await this.prisma.$queryRaw<
1019+
Array<{
1020+
userId: number | null;
1021+
rating: number | null;
1022+
ratingFeedback: string | null;
1023+
}>
1024+
>(query);
1025+
10081026
if (bookingsFromTeam.length === 0) {
10091027
return [];
10101028
}
@@ -1062,19 +1080,7 @@ export class InsightsBookingBaseService {
10621080
async getBookingStats() {
10631081
const baseConditions = await this.getBaseConditions();
10641082

1065-
const stats = await this.prisma.$queryRaw<
1066-
Array<{
1067-
total_bookings: bigint;
1068-
completed_bookings: bigint;
1069-
rescheduled_bookings: bigint;
1070-
cancelled_bookings: bigint;
1071-
no_show_host_bookings: bigint;
1072-
avg_rating: number | null;
1073-
total_ratings: bigint;
1074-
ratings_above_3: bigint;
1075-
no_show_guests: bigint;
1076-
}>
1077-
>`
1083+
const query = Prisma.sql`
10781084
WITH booking_stats AS (
10791085
SELECT
10801086
COUNT(*) as total_bookings,
@@ -1107,6 +1113,20 @@ export class InsightsBookingBaseService {
11071113
FROM booking_stats bs, guest_stats gs
11081114
`;
11091115

1116+
const stats = await this.prisma.$queryRaw<
1117+
Array<{
1118+
total_bookings: bigint;
1119+
completed_bookings: bigint;
1120+
rescheduled_bookings: bigint;
1121+
cancelled_bookings: bigint;
1122+
no_show_host_bookings: bigint;
1123+
avg_rating: number | null;
1124+
total_ratings: bigint;
1125+
ratings_above_3: bigint;
1126+
no_show_guests: bigint;
1127+
}>
1128+
>(query);
1129+
11101130
const rawStats = stats[0];
11111131
return rawStats
11121132
? {
@@ -1136,15 +1156,7 @@ export class InsightsBookingBaseService {
11361156
async getRecentNoShowGuests() {
11371157
const baseConditions = await this.getBaseConditions();
11381158

1139-
const recentNoShowBookings = await this.prisma.$queryRaw<
1140-
Array<{
1141-
bookingId: number;
1142-
startTime: Date;
1143-
eventTypeName: string;
1144-
guestName: string;
1145-
guestEmail: string;
1146-
}>
1147-
>`
1159+
const query = Prisma.sql`
11481160
WITH booking_attendee_stats AS (
11491161
SELECT
11501162
b.id as booking_id,
@@ -1182,6 +1194,16 @@ export class InsightsBookingBaseService {
11821194
LIMIT 10
11831195
`;
11841196

1197+
const recentNoShowBookings = await this.prisma.$queryRaw<
1198+
Array<{
1199+
bookingId: number;
1200+
startTime: Date;
1201+
eventTypeName: string;
1202+
guestName: string;
1203+
guestEmail: string;
1204+
}>
1205+
>(query);
1206+
11851207
return recentNoShowBookings;
11861208
}
11871209

0 commit comments

Comments
 (0)