diff --git a/packages/features/calendar-cache/api/cron.ts b/packages/features/calendar-cache/api/cron.ts index c5ac348130..9044dc1773 100644 --- a/packages/features/calendar-cache/api/cron.ts +++ b/packages/features/calendar-cache/api/cron.ts @@ -21,7 +21,7 @@ function logRejected(result: PromiseSettledResult) { } function getUniqueCalendarsByExternalId< - T extends { externalId: string; eventTypeId: number | null; credentialId: number | null } + T extends { externalId: string; eventTypeId: number | null; credentialId: number | null; id: string } >(calendars: T[]) { type ExternalId = string; return calendars.reduce( @@ -30,6 +30,7 @@ function getUniqueCalendarsByExternalId< acc[sc.externalId] = { eventTypeIds: [sc.eventTypeId], credentialId: sc.credentialId, + id: sc.id, }; } else { acc[sc.externalId].eventTypeIds.push(sc.eventTypeId); @@ -41,6 +42,7 @@ function getUniqueCalendarsByExternalId< { eventTypeIds: SelectedCalendarEventTypeIds; credentialId: number | null; + id: string; } > ); @@ -51,8 +53,13 @@ const handleCalendarsToUnwatch = async () => { const calendarsWithEventTypeIdsGroupedTogether = getUniqueCalendarsByExternalId(calendarsToUnwatch); const result = await Promise.allSettled( Object.entries(calendarsWithEventTypeIdsGroupedTogether).map( - async ([externalId, { eventTypeIds, credentialId }]) => { - if (!credentialId) return; + async ([externalId, { eventTypeIds, credentialId, id }]) => { + if (!credentialId) { + // So we don't retry on next cron run + await SelectedCalendarRepository.updateById(id, { error: "Missing credentialId" }); + console.log("no credentialId for SelecedCalendar: ", id); + return; + } const cc = await CalendarCache.initFromCredentialId(credentialId); await cc.unwatchCalendar({ calendarId: externalId, eventTypeIds }); } @@ -68,8 +75,13 @@ const handleCalendarsToWatch = async () => { const calendarsWithEventTypeIdsGroupedTogether = getUniqueCalendarsByExternalId(calendarsToWatch); const result = await Promise.allSettled( Object.entries(calendarsWithEventTypeIdsGroupedTogether).map( - async ([externalId, { credentialId, eventTypeIds }]) => { - if (!credentialId) return; + async ([externalId, { credentialId, eventTypeIds, id }]) => { + if (!credentialId) { + // So we don't retry on next cron run + await SelectedCalendarRepository.updateById(id, { error: "Missing credentialId" }); + console.log("no credentialId for SelecedCalendar: ", id); + return; + } const cc = await CalendarCache.initFromCredentialId(credentialId); await cc.watchCalendar({ calendarId: externalId, eventTypeIds }); } diff --git a/packages/lib/server/repository/selectedCalendar.ts b/packages/lib/server/repository/selectedCalendar.ts index 74be09a764..f99563ac79 100644 --- a/packages/lib/server/repository/selectedCalendar.ts +++ b/packages/lib/server/repository/selectedCalendar.ts @@ -323,4 +323,10 @@ export class SelectedCalendarRepository { ) ); } + static async updateById(id: string, data: Prisma.SelectedCalendarUpdateInput) { + return await prisma.selectedCalendar.update({ + where: { id }, + data, + }); + } }