feat: update v2 calendar services logic to handle multiple calendars (#16478)

* update calendar logic to handle multiple calendars

* add method to link calendar to credential

* remove logs

* update platform libraries

* update calendar logic to handle multiple calendars

* update credentials repository

* add selected calendars repository to destination calendars module

* add method to get user selected calendars and rename fn

* chore: PR feedback

* resolve merge conflicts

* fixup

* make sure we cover all edge cases for apple calendar

---------

Co-authored-by: Morgan <[email protected]>
This commit is contained in:
Rajiv SahalandMorgan authored and GitHub committed 2024-09-12 06:44:01 +00:00
1 parent 55e1e0fa3d
commit 91f5d49cd4
9 files changed
+200 -35

No files matched your search

@@ -9,12 +9,12 @@ import { APPS_TYPE_ID_MAPPING } from "@calcom/platform-constants";
export class CredentialsRepository {
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}
async createAppCredential(
async upsertAppCredential(
type: keyof typeof APPS_TYPE_ID_MAPPING,
key: Prisma.InputJsonValue,
userId: number
userId: number,
credentialId?: number | null
) {
const credential = await this.getByTypeAndUserId(type, userId);
return this.dbWrite.prisma.credential.upsert({
create: {
type,
@@ -27,7 +27,7 @@ export class CredentialsRepository {
invalid: false,
},
where: {
id: credential?.id ?? 0,
id: credentialId ?? 0,
},
});
}
@@ -36,6 +36,10 @@ export class CredentialsRepository {
return this.dbWrite.prisma.credential.findFirst({ where: { type, userId } });
}
getAllUserCredentialsByTypeAndId(type: string, userId: number) {
return this.dbRead.prisma.credential.findMany({ where: { type, userId } });
}
getUserCredentialsByIds(userId: number, credentialIds: number[]) {
return this.dbRead.prisma.credential.findMany({
where: {
@@ -60,6 +64,30 @@ export class CredentialsRepository {
},
});
}
async getUserCredentialById(userId: number, credentialId: number, type: string) {
return await this.dbRead.prisma.credential.findUnique({
where: {
userId,
type,
id: credentialId,
},
select: {
id: true,
type: true,
key: true,
userId: true,
teamId: true,
appId: true,
invalid: true,
user: {
select: {
email: true,
},
},
},
});
}
}
export type CredentialsWithUserEmail = Awaited<
@@ -6,6 +6,7 @@ import { DestinationCalendarsController } from "@/modules/destination-calendars/
import { DestinationCalendarsRepository } from "@/modules/destination-calendars/destination-calendars.repository";
import { DestinationCalendarsService } from "@/modules/destination-calendars/services/destination-calendars.service";
import { PrismaModule } from "@/modules/prisma/prisma.module";
import { SelectedCalendarsRepository } from "@/modules/selected-calendars/selected-calendars.repository";
import { UsersRepository } from "@/modules/users/users.repository";
import { Module } from "@nestjs/common";
@@ -19,6 +20,7 @@ import { Module } from "@nestjs/common";
UsersRepository,
CredentialsRepository,
AppsRepository,
SelectedCalendarsRepository,
],
controllers: [DestinationCalendarsController],
exports: [DestinationCalendarsRepository],
@@ -6,7 +6,7 @@ import { Injectable } from "@nestjs/common";
export class SelectedCalendarsRepository {
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}
createSelectedCalendar(externalId: string, credentialId: number, userId: number, integration: string) {
upsertSelectedCalendar(externalId: string, credentialId: number, userId: number, integration: string) {
return this.dbWrite.prisma.selectedCalendar.upsert({
create: {
userId,
@@ -38,6 +38,18 @@ export class SelectedCalendarsRepository {
});
}
getUserSelectedCalendar(userId: number, integration: string, externalId: string) {
return this.dbRead.prisma.selectedCalendar.findUnique({
where: {
userId_integration_externalId: {
userId,
externalId,
integration,
},
},
});
}
async addUserSelectedCalendar(
userId: number,
integration: string,
@@ -68,7 +68,6 @@ export class UsersRepository {
}
async findByIdWithProfile(userId: number): Promise<UserWithProfile | null> {
console.log("findByIdWithProfile");
return this.dbRead.prisma.user.findUnique({
where: {
id: userId,