Skip to content

Commit cfda475

Browse files
refactor: move getTotalBookingDuration to BookingRepository (#22931)
* refactor: move getTotalBookingDuration to BookingRepository - Move getTotalBookingDuration function from standalone file to BookingRepository class - Update all usage sites to call method through repository instance - Remove standalone function file packages/lib/server/queries/booking/index.ts - Add prisma import to util.ts for BookingRepository instantiation - Maintain exact same method signature and functionality Co-Authored-By: [email protected] <[email protected]> * fix: remove eslint-config-next to resolve TypeScript ESLint conflicts - Remove eslint-config-next dependency that was causing version conflicts - Resolves 'Class extends value undefined is not a constructor or null' errors - ESLint 'next' config issue appears to be pre-existing in main branch Co-Authored-By: [email protected] <[email protected]> * fixup! Merge branch 'main' into devin/move-getTotalBookingDuration-1754460208 * chore: bump platform libs --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Morgan <[email protected]>
1 parent 895ed48 commit cfda475

File tree

8 files changed

+106
-76
lines changed

8 files changed

+106
-76
lines changed

apps/api/v2/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@axiomhq/winston": "^1.2.0",
3939
"@calcom/platform-constants": "*",
4040
"@calcom/platform-enums": "*",
41-
"@calcom/platform-libraries": "npm:@calcom/[email protected].287",
41+
"@calcom/platform-libraries": "npm:@calcom/[email protected].288",
4242
"@calcom/platform-types": "*",
4343
"@calcom/platform-utils": "*",
4444
"@calcom/prisma": "*",

packages/lib/getUserAvailability.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ export class UserAvailabilityService {
597597
getUserAvailability = withReporting(this._getUserAvailability.bind(this), "getUserAvailability");
598598

599599
getPeriodStartDatesBetween = withReporting(
600-
(dateFrom: Dayjs, dateTo: Dayjs, period: IntervalLimitUnit, timeZone?: string) =>
600+
(dateFrom: Dayjs, dateTo: Dayjs, period: IntervalLimitUnit, timeZone?: string) =>
601601
getPeriodStartDatesBetweenUtil(dateFrom, dateTo, period, timeZone),
602602
"getPeriodStartDatesBetween"
603603
);

packages/lib/intervalLimits/server/checkDurationLimits.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import dayjs from "@calcom/dayjs";
22
import { getErrorFromUnknown } from "@calcom/lib/errors";
33
import { HttpError } from "@calcom/lib/http-error";
4-
import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
4+
import { BookingRepository } from "@calcom/lib/server/repository/booking";
5+
import prisma from "@calcom/prisma";
56

67
import { ascendingLimitKeys, intervalLimitKeyToUnit } from "../intervalLimit";
78
import type { IntervalLimit, IntervalLimitKey } from "../intervalLimitSchema";
@@ -55,7 +56,8 @@ export async function checkDurationLimit({
5556
const startDate = dayjs(eventStartDate).startOf(unit).toDate();
5657
const endDate = dayjs(eventStartDate).endOf(unit).toDate();
5758

58-
const totalBookingDuration = await getTotalBookingDuration({
59+
const bookingRepo = new BookingRepository(prisma);
60+
const totalBookingDuration = await bookingRepo.getTotalBookingDuration({
5961
eventId,
6062
startDate,
6163
endDate,

packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { EventType } from "@calcom/lib/getUserAvailability";
66
import { getPeriodStartDatesBetween } from "@calcom/lib/intervalLimits/utils/getPeriodStartDatesBetween";
77
import { withReporting } from "@calcom/lib/sentryWrapper";
88
import { performance } from "@calcom/lib/server/perfObserver";
9-
import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
109
import { BookingRepository } from "@calcom/lib/server/repository/booking";
1110
import prisma from "@calcom/prisma";
1211
import type { EventBusyDetails } from "@calcom/types/Calendar";
@@ -181,7 +180,8 @@ const _getBusyTimesFromDurationLimits = async (
181180

182181
// special handling of yearly limits to improve performance
183182
if (unit === "year") {
184-
const totalYearlyDuration = await getTotalBookingDuration({
183+
const bookingRepo = new BookingRepository(prisma);
184+
const totalYearlyDuration = await bookingRepo.getTotalBookingDuration({
185185
eventId: eventType.id,
186186
startDate: periodStart.toDate(),
187187
endDate: periodStart.endOf(unit).toDate(),

packages/lib/server/queries/booking/index.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/lib/server/repository/booking.ts

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -870,25 +870,69 @@ export class BookingRepository {
870870
});
871871
}
872872

873-
async findAcceptedBookingByEventTypeId({eventTypeId, dateFrom, dateTo}: {eventTypeId?: number, dateFrom: string, dateTo: string}) {
874-
return this.prismaClient.booking.findMany({
875-
where: {
876-
eventTypeId,
877-
startTime: {
878-
gte: dateFrom,
879-
lte: dateTo,
880-
},
881-
status: BookingStatus.ACCEPTED,
882-
},
873+
async findAcceptedBookingByEventTypeId({
874+
eventTypeId,
875+
dateFrom,
876+
dateTo,
877+
}: {
878+
eventTypeId?: number;
879+
dateFrom: string;
880+
dateTo: string;
881+
}) {
882+
return this.prismaClient.booking.findMany({
883+
where: {
884+
eventTypeId,
885+
startTime: {
886+
gte: dateFrom,
887+
lte: dateTo,
888+
},
889+
status: BookingStatus.ACCEPTED,
890+
},
891+
select: {
892+
uid: true,
893+
startTime: true,
894+
attendees: {
883895
select: {
884-
uid: true,
885-
startTime: true,
886-
attendees: {
887-
select: {
888-
email: true,
889-
},
890-
},
896+
email: true,
891897
},
892-
});
898+
},
899+
},
900+
});
901+
}
902+
903+
async getTotalBookingDuration({
904+
eventId,
905+
startDate,
906+
endDate,
907+
rescheduleUid,
908+
}: {
909+
eventId: number;
910+
startDate: Date;
911+
endDate: Date;
912+
rescheduleUid?: string;
913+
}) {
914+
let totalBookingTime;
915+
916+
if (rescheduleUid) {
917+
[totalBookingTime] = await this.prismaClient.$queryRaw<[{ totalMinutes: number | null }]>`
918+
SELECT SUM(EXTRACT(EPOCH FROM ("endTime" - "startTime")) / 60) as "totalMinutes"
919+
FROM "Booking"
920+
WHERE "status" = 'accepted'
921+
AND "eventTypeId" = ${eventId}
922+
AND "startTime" >= ${startDate}
923+
AND "endTime" <= ${endDate}
924+
AND "uid" != ${rescheduleUid};
925+
`;
926+
} else {
927+
[totalBookingTime] = await this.prismaClient.$queryRaw<[{ totalMinutes: number | null }]>`
928+
SELECT SUM(EXTRACT(EPOCH FROM ("endTime" - "startTime")) / 60) as "totalMinutes"
929+
FROM "Booking"
930+
WHERE "status" = 'accepted'
931+
AND "eventTypeId" = ${eventId}
932+
AND "startTime" >= ${startDate}
933+
AND "endTime" <= ${endDate};
934+
`;
935+
}
936+
return totalBookingTime.totalMinutes ?? 0;
893937
}
894938
}

packages/trpc/server/routers/viewer/slots/util.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import logger from "@calcom/lib/logger";
4242
import { isRestrictionScheduleEnabled } from "@calcom/lib/restrictionSchedule";
4343
import { safeStringify } from "@calcom/lib/safeStringify";
4444
import { withReporting } from "@calcom/lib/sentryWrapper";
45-
import { getTotalBookingDuration } from "@calcom/lib/server/queries/booking";
4645
import type { ISelectedSlotRepository } from "@calcom/lib/server/repository/ISelectedSlotRepository";
4746
import type { BookingRepository } from "@calcom/lib/server/repository/booking";
4847
import type { EventTypeRepository } from "@calcom/lib/server/repository/eventTypeRepository";
@@ -400,7 +399,12 @@ export class AvailableSlotsService {
400399
if (!limit) continue;
401400

402401
const unit = intervalLimitKeyToUnit(key);
403-
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit, timeZone);
402+
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(
403+
dateFrom,
404+
dateTo,
405+
unit,
406+
timeZone
407+
);
404408

405409
for (const periodStart of periodStartDates) {
406410
if (globalLimitManager.isAlreadyBusy(periodStart, unit, timeZone)) continue;
@@ -435,7 +439,12 @@ export class AvailableSlotsService {
435439
if (!limit) continue;
436440

437441
const unit = intervalLimitKeyToUnit(key);
438-
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit, timeZone);
442+
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(
443+
dateFrom,
444+
dateTo,
445+
unit,
446+
timeZone
447+
);
439448

440449
for (const periodStart of periodStartDates) {
441450
if (limitManager.isAlreadyBusy(periodStart, unit, timeZone)) continue;
@@ -486,7 +495,12 @@ export class AvailableSlotsService {
486495
if (!limit) continue;
487496

488497
const unit = intervalLimitKeyToUnit(key);
489-
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit, timeZone);
498+
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(
499+
dateFrom,
500+
dateTo,
501+
unit,
502+
timeZone
503+
);
490504

491505
for (const periodStart of periodStartDates) {
492506
if (limitManager.isAlreadyBusy(periodStart, unit, timeZone)) continue;
@@ -499,8 +513,7 @@ export class AvailableSlotsService {
499513
}
500514

501515
if (unit === "year") {
502-
// TODO: DI getTotalBookingDuration
503-
const totalYearlyDuration = await getTotalBookingDuration({
516+
const totalYearlyDuration = await this.dependencies.bookingRepo.getTotalBookingDuration({
504517
eventId: eventType.id,
505518
startDate: periodStart.toDate(),
506519
endDate: periodStart.endOf(unit).toDate(),
@@ -586,7 +599,12 @@ export class AvailableSlotsService {
586599
if (!limit) continue;
587600

588601
const unit = intervalLimitKeyToUnit(key);
589-
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit, timeZone);
602+
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(
603+
dateFrom,
604+
dateTo,
605+
unit,
606+
timeZone
607+
);
590608

591609
for (const periodStart of periodStartDates) {
592610
if (globalLimitManager.isAlreadyBusy(periodStart, unit, timeZone)) continue;
@@ -634,7 +652,12 @@ export class AvailableSlotsService {
634652
if (!limit) continue;
635653

636654
const unit = intervalLimitKeyToUnit(key);
637-
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(dateFrom, dateTo, unit, timeZone);
655+
const periodStartDates = this.dependencies.userAvailabilityService.getPeriodStartDatesBetween(
656+
dateFrom,
657+
dateTo,
658+
unit,
659+
timeZone
660+
);
638661

639662
for (const periodStart of periodStartDates) {
640663
if (limitManager.isAlreadyBusy(periodStart, unit, timeZone)) continue;

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,7 +2500,7 @@ __metadata:
25002500
"@axiomhq/winston": ^1.2.0
25012501
"@calcom/platform-constants": "*"
25022502
"@calcom/platform-enums": "*"
2503-
"@calcom/platform-libraries": "npm:@calcom/[email protected].287"
2503+
"@calcom/platform-libraries": "npm:@calcom/[email protected].288"
25042504
"@calcom/platform-types": "*"
25052505
"@calcom/platform-utils": "*"
25062506
"@calcom/prisma": "*"
@@ -3556,13 +3556,13 @@ __metadata:
35563556
languageName: unknown
35573557
linkType: soft
35583558

3559-
"@calcom/platform-libraries@npm:@calcom/[email protected].287":
3560-
version: 0.0.287
3561-
resolution: "@calcom/platform-libraries@npm:0.0.287"
3559+
"@calcom/platform-libraries@npm:@calcom/[email protected].288":
3560+
version: 0.0.288
3561+
resolution: "@calcom/platform-libraries@npm:0.0.288"
35623562
dependencies:
35633563
"@calcom/features": "*"
35643564
"@calcom/lib": "*"
3565-
checksum: 098a50e1b20f70c0f67a332d5f4f6a13c9aaf03f0c0ac9c9ffe1b74c06c607e6650bc0f08bdc1a9bdd89fa25461c0e153580305b845620a1506b23150334c5bd
3565+
checksum: 9f44ba9e258a94c9b8bb5388c400696f6e6acd20dbf73865d62739b1aad212626bb181d88b38b30d08659c8b9183d27aba94bdd52b36e6c6c3cc7594d1af29d7
35663566
languageName: node
35673567
linkType: hard
35683568

0 commit comments

Comments
 (0)