Files
calendar/packages/lib/server/repository/selectedSlots.ts
T
devin-ai-integration[bot]GitHubmorgan@cal.com <morgan@cal.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>morgan@cal.com <morgan@cal.com>Morgan
80ac118242 refactor: convert SelectedSlotsRepository to use dependency injection pattern (#22387)
* refactor: convert TeamRepository to use dependency injection pattern

- Add constructor injection for PrismaClient in TeamRepository
- Convert all static methods to instance methods using this.prismaClient
- Update all usage sites to use two-step pattern: const teamRepo = new TeamRepository(prisma); teamRepo.method(...)
- Optimize instance reuse within same function scopes where possible
- Update test files to work with new instance pattern
- Preserve all existing functionality and method signatures

Follows the same dependency injection pattern as UserRepository refactoring

Co-Authored-By: morgan@cal.com <morgan@cal.com>

* refactor: convert SelectedSlotsRepository to use dependency injection pattern

- Add constructor injection for PrismaClient/PrismaTransaction
- Convert all static methods to instance methods using this.prismaClient
- Update usage sites to use two-step instantiation pattern:
  const slotsRepo = new SelectedSlotsRepository(prisma); slotsRepo.method(...)
- Optimize instance reuse in util.ts for multiple method calls
- Update test mocks to handle new constructor-based pattern
- Add explicit type annotations to resolve TypeScript inference issues

Follows same dependency injection pattern as UserRepository and TeamRepository refactorings.

Co-Authored-By: morgan@cal.com <morgan@cal.com>

* chore: bump platform libs

* fix: devin mistake prisma client

* fix: devin typing mistakes

* chore: bump platform libs

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: morgan@cal.com <morgan@cal.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
2025-07-10 15:14:26 +00:00

115 lines
2.5 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import type { PrismaClient } from "@calcom/prisma";
type WhereCondition = {
slotUtcStartDate?: Date | string;
slotUtcEndDate?: Date | string;
eventTypeId?: number;
uid?: string | { not: string };
releaseAt?: { gt: Date };
};
export type FindManyArgs = {
where?: WhereCondition & {
OR?: WhereCondition[];
};
select?: Prisma.SelectedSlotsSelect;
};
export type TimeSlot = {
utcStartIso: string;
utcEndIso: string;
};
export class SelectedSlotsRepository {
constructor(private prismaClient: PrismaClient) {}
async findMany({ where, select }: FindManyArgs) {
return await this.prismaClient.selectedSlots.findMany({ where, select });
}
async findFirst({ where }: { where: Prisma.SelectedSlotsWhereInput }) {
return await this.prismaClient.selectedSlots.findFirst({
where,
});
}
async findReservedByOthers({
slot,
eventTypeId,
uid,
}: {
slot: TimeSlot;
eventTypeId: number;
uid: string;
}) {
return await this.findFirst({
where: {
slotUtcStartDate: slot.utcStartIso,
slotUtcEndDate: slot.utcEndIso,
eventTypeId,
uid: { not: uid },
releaseAt: { gt: new Date() },
},
});
}
async findManyReservedByOthers(slots: TimeSlot[], eventTypeId: number, uid: string) {
return await this.findMany({
where: {
OR: slots.map((slot) => ({
slotUtcStartDate: slot.utcStartIso,
slotUtcEndDate: slot.utcEndIso,
eventTypeId,
uid: { not: uid },
releaseAt: { gt: new Date() },
})),
},
select: {
slotUtcStartDate: true,
slotUtcEndDate: true,
},
});
}
async findManyUnexpiredSlots({
userIds,
currentTimeInUtc,
}: {
userIds: number[];
currentTimeInUtc: string;
}) {
return this.prismaClient.selectedSlots.findMany({
where: {
userId: { in: userIds },
releaseAt: { gt: currentTimeInUtc },
},
select: {
id: true,
slotUtcStartDate: true,
slotUtcEndDate: true,
userId: true,
isSeat: true,
eventTypeId: true,
uid: true,
},
});
}
async deleteManyExpiredSlots({
eventTypeId,
currentTimeInUtc,
}: {
eventTypeId: number;
currentTimeInUtc: string;
}) {
return this.prismaClient.selectedSlots.deleteMany({
where: {
eventTypeId: { equals: eventTypeId },
releaseAt: { lt: currentTimeInUtc },
},
});
}
}