Files
Benny JooGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
96bec9f7b9 refactor: Move repositories from @calcom/lib to @calcom/features domain folders (#27570)
* refactor: move repositories from lib to features domain folders

- Move HolidayRepository to features/holidays/repositories
- Move PrismaTrackingRepository to features/bookings/repositories
- Move PrismaBookingPaymentRepository to features/bookings/repositories
- Move PrismaRoutingFormResponseRepository to features/routing-forms/repositories
- Move PrismaAssignmentReasonRepository to features/assignment-reason/repositories
- Move VerificationTokenRepository to features/auth/repositories
- Move WorkspacePlatformRepository to features/workspace-platform/repositories
- Move DTO files to their respective feature domains
- Merge lib DestinationCalendarRepository into features version
- Merge lib SelectedCalendarRepository into features version
- Update all import paths across the codebase

This follows the vertical slice architecture pattern by organizing
repositories by domain rather than by technical layer.

Co-Authored-By: [email protected] <[email protected]>

* fix: update VerificationTokenService import path to new location

Co-Authored-By: [email protected] <[email protected]>

* fix: update test file imports to use new repository locations

Co-Authored-By: [email protected] <[email protected]>

* mv

* fix structure

* fix

* refactor: merge unit tests for SelectedCalendarRepository into single file

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-09 08:41:04 -03:00

147 lines
3.8 KiB
TypeScript

import logger from "@calcom/lib/logger";
import { buildCredentialPayloadForPrisma } from "@calcom/lib/server/buildCredentialPayloadForCalendar";
import type { PrismaClient } from "@calcom/prisma";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
const log = logger.getSubLogger({ prefix: ["DestinationCalendarRepository"] });
export class DestinationCalendarRepository {
private prismaClient: PrismaClient;
constructor(prismaClient?: PrismaClient) {
this.prismaClient = prismaClient ?? prisma;
}
async getCustomReminderByCredentialId(credentialId: number): Promise<number | null> {
const destinationCalendar = await this.prismaClient.destinationCalendar.findFirst({
where: { credentialId },
select: { customCalendarReminder: true },
});
return destinationCalendar?.customCalendarReminder ?? null;
}
async updateCustomReminder({
userId,
credentialId,
integration,
customCalendarReminder,
}: {
userId: number;
credentialId: number;
integration: string;
customCalendarReminder: number | null;
}) {
return await this.prismaClient.destinationCalendar.updateMany({
where: {
userId,
credentialId,
integration,
},
data: {
customCalendarReminder,
},
});
}
static async create(data: Prisma.DestinationCalendarCreateInput) {
return await prisma.destinationCalendar.create({
data,
});
}
static async createIfNotExistsForUser(
data: { userId: number } & Prisma.DestinationCalendarUncheckedCreateInput
) {
const conflictingCalendar = await DestinationCalendarRepository.findConflictingForUser(data);
if (conflictingCalendar) {
return conflictingCalendar;
}
return await prisma.destinationCalendar.create({
data,
});
}
static async getByUserId(userId: number) {
return await prisma.destinationCalendar.findFirst({
where: {
userId,
},
});
}
static async getByEventTypeId(eventTypeId: number) {
return await prisma.destinationCalendar.findFirst({
where: {
eventTypeId,
},
});
}
static async find({ where }: { where: Prisma.DestinationCalendarWhereInput }) {
return await prisma.destinationCalendar.findFirst({
where,
});
}
private static async findConflictingForUser(data: {
userId: number;
integration: string;
externalId: string;
}) {
return await DestinationCalendarRepository.find({
where: {
userId: data.userId,
integration: data.integration,
externalId: data.externalId,
eventTypeId: null,
},
});
}
static async upsert({
where,
update,
create,
}: {
where: Prisma.DestinationCalendarUpsertArgs["where"];
update: {
integration?: string;
externalId?: string;
credentialId?: number | null;
primaryEmail?: string | null;
delegationCredentialId?: string | null;
};
create: {
integration: string;
externalId: string;
credentialId: number | null;
primaryEmail?: string | null;
delegationCredentialId?: string | null;
};
}) {
log.debug("upsert", { where, update, create });
const credentialPayloadForUpdate = buildCredentialPayloadForPrisma({
credentialId: update.credentialId,
delegationCredentialId: update.delegationCredentialId,
});
const credentialPayloadForCreate = buildCredentialPayloadForPrisma({
credentialId: create.credentialId,
delegationCredentialId: create.delegationCredentialId,
});
return await prisma.destinationCalendar.upsert({
where,
update: {
...update,
...credentialPayloadForUpdate,
},
create: {
...create,
...credentialPayloadForCreate,
},
});
}
}