refactor: create DestinationCalendarRepository in /features/calendars (#25851)
* mv destination calendar repo * fix
This commit is contained in:
1 parent
972f983824
commit
e394a03365
4 files changed
+5
-6
No files matched your search
@@ -3,10 +3,10 @@ import {
|
||||
getCalendarCredentials,
|
||||
getConnectedCalendars,
|
||||
} from "@calcom/features/calendars/lib/CalendarManager";
|
||||
import { DestinationCalendarRepository } from "@calcom/features/calendars/repositories/DestinationCalendarRepository";
|
||||
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
|
||||
import { isDelegationCredential } from "@calcom/lib/delegationCredential";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { DestinationCalendarRepository } from "@calcom/lib/server/repository/destinationCalendar";
|
||||
import { SelectedCalendarRepository } from "@calcom/lib/server/repository/selectedCalendar";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import prisma from "@calcom/prisma";
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { buildCredentialPayloadForPrisma } from "@calcom/lib/server/buildCredentialPayloadForCalendar";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["DestinationCalendarRepository"] });
|
||||
|
||||
export class DestinationCalendarRepository {
|
||||
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,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,11 @@ import {
|
||||
import { describe, test, expect, beforeEach } from "vitest";
|
||||
|
||||
import { PrismaAppRepository } from "@calcom/features/apps/repository/PrismaAppRepository";
|
||||
import { DestinationCalendarRepository } from "@calcom/features/calendars/repositories/DestinationCalendarRepository";
|
||||
import { CredentialRepository } from "@calcom/features/credentials/repositories/CredentialRepository";
|
||||
import { EventTypeRepository } from "@calcom/features/eventtypes/repositories/eventTypeRepository";
|
||||
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
|
||||
import { DestinationCalendarRepository } from "@calcom/lib/server/repository/destinationCalendar";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
const testUser = {
|
||||
email: "[email protected]",
|
||||
|
||||
Reference in new issue
Block a user