fix: prevents cron to be clogged with calendars with missing credentialId (#18281)

This commit is contained in:
Omar López
2024-12-19 23:53:51 +00:00
committed by GitHub
parent 795eecf300
commit ec920eb2a8
2 changed files with 23 additions and 5 deletions
+17 -5
View File
@@ -21,7 +21,7 @@ function logRejected(result: PromiseSettledResult<unknown>) {
}
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 });
}
@@ -323,4 +323,10 @@ export class SelectedCalendarRepository {
)
);
}
static async updateById(id: string, data: Prisma.SelectedCalendarUpdateInput) {
return await prisma.selectedCalendar.update({
where: { id },
data,
});
}
}