fix: get v2/calendars existing record conflict (#22286)
* refactor: upsert destination calendar * test: v2 connected calendars * chore: bump libraries * refactor: have only createIfNotExistsForUser * refactor: exclude eventTypeId from user calendar search * chore: bump libraries
This commit is contained in:
1 parent
2838053ba8
commit
35022a0478
5 files changed
+77
-21
No files matched your search
@@ -8,6 +8,7 @@ import type { DestinationCalendar, SelectedCalendar, User } from "@calcom/prisma
|
||||
import { AppCategories } from "@calcom/prisma/enums";
|
||||
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
|
||||
|
||||
import { DestinationCalendarRepository } from "./server/repository/destinationCalendar";
|
||||
import { EventTypeRepository } from "./server/repository/eventType";
|
||||
import { SelectedCalendarRepository } from "./server/repository/selectedCalendar";
|
||||
|
||||
@@ -134,20 +135,18 @@ async function handleNoDestinationCalendar({
|
||||
}
|
||||
}
|
||||
|
||||
user.destinationCalendar = await prisma.destinationCalendar.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
integration,
|
||||
externalId,
|
||||
primaryEmail,
|
||||
...(!isDelegationCredential({ credentialId })
|
||||
? {
|
||||
credentialId,
|
||||
}
|
||||
: {
|
||||
delegationCredentialId,
|
||||
}),
|
||||
},
|
||||
user.destinationCalendar = await DestinationCalendarRepository.createIfNotExistsForUser({
|
||||
userId: user.id,
|
||||
integration,
|
||||
externalId,
|
||||
primaryEmail,
|
||||
...(!isDelegationCredential({ credentialId })
|
||||
? {
|
||||
credentialId,
|
||||
}
|
||||
: {
|
||||
delegationCredentialId,
|
||||
}),
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -14,6 +14,18 @@ export class DestinationCalendarRepository {
|
||||
});
|
||||
}
|
||||
|
||||
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: {
|
||||
@@ -36,6 +48,21 @@ export class DestinationCalendarRepository {
|
||||
});
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user