refactor: convert EventTypeRepository to use dependency injection pattern (#22397)
- Add constructor with PrismaClient dependency injection - Convert all 14 static methods to instance methods using this.prismaClient - Update all usage sites to use two-step instantiation pattern: const eventTypeRepo = new EventTypeRepository(prisma); eventTypeRepo.method(...) - Keep getSelectedCalendarsFromUser as static utility method - Follow same pattern as UserRepository, TeamRepository, SelectedSlotsRepository, and BookingRepository - Maintain all existing method signatures and functionality Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
This commit is contained in:
1 parent
b5911a9a2d
commit
758143ef70
14 files changed
+68
-59
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import type { EventType as PrismaEventType } from "@prisma/client";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
|
||||
import logger from "@calcom/lib/logger";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { prisma, availabilityUserSelect } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
import { EventTypeMetaDataSchema, rrSegmentQueryValueSchema } from "@calcom/prisma/zod-utils";
|
||||
@@ -68,7 +69,9 @@ function usersWithSelectedCalendars<
|
||||
}
|
||||
|
||||
export class EventTypeRepository {
|
||||
private static generateCreateEventTypeData = (eventTypeCreateData: IEventType) => {
|
||||
constructor(private prismaClient: PrismaClient) {}
|
||||
|
||||
private generateCreateEventTypeData = (eventTypeCreateData: IEventType) => {
|
||||
const {
|
||||
userId,
|
||||
profileId,
|
||||
@@ -122,8 +125,8 @@ export class EventTypeRepository {
|
||||
};
|
||||
};
|
||||
|
||||
static async create(data: IEventType) {
|
||||
return await prisma.eventType.create({
|
||||
async create(data: IEventType) {
|
||||
return await this.prismaClient.eventType.create({
|
||||
data: this.generateCreateEventTypeData(data),
|
||||
include: {
|
||||
calVideoSettings: true,
|
||||
@@ -131,13 +134,13 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async createMany(data: IEventType[]) {
|
||||
return await prisma.eventType.createMany({
|
||||
async createMany(data: IEventType[]) {
|
||||
return await this.prismaClient.eventType.createMany({
|
||||
data: data.map((d) => this.generateCreateEventTypeData(d)),
|
||||
});
|
||||
}
|
||||
|
||||
static async findAllByUpId(
|
||||
async findAllByUpId(
|
||||
{ upId, userId }: { upId: string; userId: number },
|
||||
{
|
||||
orderBy,
|
||||
@@ -184,7 +187,7 @@ export class EventTypeRepository {
|
||||
|
||||
if (!profileId) {
|
||||
// Lookup is by userId
|
||||
return await prisma.eventType.findMany({
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
userId: lookupTarget.id,
|
||||
...where,
|
||||
@@ -200,7 +203,7 @@ export class EventTypeRepository {
|
||||
if (profile?.movedFromUser) {
|
||||
// Because the user has been moved to this profile, we need to get all user events except those that belong to some other profile
|
||||
// This is because those event-types that are created after moving to profile would have profileId but existing event-types would have profileId set to null
|
||||
return await prisma.eventType.findMany({
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
// Existing events
|
||||
@@ -228,7 +231,7 @@ export class EventTypeRepository {
|
||||
orderBy,
|
||||
});
|
||||
} else {
|
||||
return await prisma.eventType.findMany({
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
@@ -252,7 +255,7 @@ export class EventTypeRepository {
|
||||
}
|
||||
}
|
||||
|
||||
static async findAllByUpIdWithMinimalData(
|
||||
async findAllByUpIdWithMinimalData(
|
||||
{ upId, userId }: { upId: string; userId: number },
|
||||
{
|
||||
orderBy,
|
||||
@@ -288,7 +291,7 @@ export class EventTypeRepository {
|
||||
|
||||
if (!profileId) {
|
||||
// Lookup is by userId
|
||||
return await prisma.eventType.findMany({
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
userId: lookupTarget.id,
|
||||
...where,
|
||||
@@ -304,7 +307,7 @@ export class EventTypeRepository {
|
||||
if (profile?.movedFromUser) {
|
||||
// Because the user has been moved to this profile, we need to get all user events except those that belong to some other profile
|
||||
// This is because those event-types that are created after moving to profile would have profileId but existing event-types would have profileId set to null
|
||||
return await prisma.eventType.findMany({
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
// Existing events
|
||||
@@ -332,7 +335,7 @@ export class EventTypeRepository {
|
||||
orderBy,
|
||||
});
|
||||
} else {
|
||||
return await prisma.eventType.findMany({
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
@@ -356,7 +359,7 @@ export class EventTypeRepository {
|
||||
}
|
||||
}
|
||||
|
||||
static async findTeamEventTypes({
|
||||
async findTeamEventTypes({
|
||||
teamId,
|
||||
parentId,
|
||||
userId,
|
||||
@@ -397,7 +400,7 @@ export class EventTypeRepository {
|
||||
},
|
||||
};
|
||||
|
||||
const teamMembership = await prisma.membership.findFirst({
|
||||
const teamMembership = await this.prismaClient.membership.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
@@ -437,16 +440,16 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findAllByUserId({ userId }: { userId: number }) {
|
||||
return await prisma.eventType.findMany({
|
||||
async findAllByUserId({ userId }: { userId: number }) {
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static async findTitleById({ id }: { id: number }) {
|
||||
return await prisma.eventType.findUnique({
|
||||
async findTitleById({ id }: { id: number }) {
|
||||
return await this.prismaClient.eventType.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
@@ -456,8 +459,8 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findByIdWithUserAccess({ id, userId }: { id: number; userId: number }) {
|
||||
return await prisma.eventType.findUnique({
|
||||
async findByIdWithUserAccess({ id, userId }: { id: number; userId: number }) {
|
||||
return await this.prismaClient.eventType.findUnique({
|
||||
where: {
|
||||
id,
|
||||
OR: [{ userId }, { hosts: { some: { userId } } }, { users: { some: { id: userId } } }],
|
||||
@@ -465,7 +468,7 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findById({ id, userId }: { id: number; userId: number }) {
|
||||
async findById({ id, userId }: { id: number; userId: number }) {
|
||||
const userSelect = {
|
||||
name: true,
|
||||
avatarUrl: true,
|
||||
@@ -716,7 +719,7 @@ export class EventTypeRepository {
|
||||
// This is more efficient than using a complex join with team.members in the query
|
||||
const userTeamIds = await MembershipRepository.findUserTeamIds({ userId });
|
||||
|
||||
return await prisma.eventType.findFirst({
|
||||
return await this.prismaClient.eventType.findFirst({
|
||||
where: {
|
||||
AND: [
|
||||
{
|
||||
@@ -745,24 +748,16 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findByIdMinimal({ id }: { id: number }) {
|
||||
return await prisma.eventType.findUnique({
|
||||
async findByIdMinimal({ id }: { id: number }) {
|
||||
return await this.prismaClient.eventType.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static async findFirstEventTypeId({
|
||||
slug,
|
||||
teamId,
|
||||
userId,
|
||||
}: {
|
||||
slug: string;
|
||||
teamId?: number;
|
||||
userId?: number;
|
||||
}) {
|
||||
return prisma.eventType.findFirst({
|
||||
async findFirstEventTypeId({ slug, teamId, userId }: { slug: string; teamId?: number; userId?: number }) {
|
||||
return this.prismaClient.eventType.findFirst({
|
||||
where: {
|
||||
slug,
|
||||
...(teamId ? { teamId } : {}),
|
||||
@@ -774,8 +769,8 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findByIdIncludeHostsAndTeam({ id }: { id: number }) {
|
||||
const eventType = await prisma.eventType.findUnique({
|
||||
async findByIdIncludeHostsAndTeam({ id }: { id: number }) {
|
||||
const eventType = await this.prismaClient.eventType.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
@@ -818,8 +813,8 @@ export class EventTypeRepository {
|
||||
};
|
||||
}
|
||||
|
||||
static async findAllByTeamIdIncludeManagedEventTypes({ teamId }: { teamId?: number }) {
|
||||
return await prisma.eventType.findMany({
|
||||
async findAllByTeamIdIncludeManagedEventTypes({ teamId }: { teamId?: number }) {
|
||||
return await this.prismaClient.eventType.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
@@ -835,8 +830,8 @@ export class EventTypeRepository {
|
||||
});
|
||||
}
|
||||
|
||||
static async findForSlots({ id }: { id: number }) {
|
||||
const eventType = await prisma.eventType.findUnique({
|
||||
async findForSlots({ id }: { id: number }) {
|
||||
const eventType = await this.prismaClient.eventType.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user