fix: prevents cron to be clogged with calendars with missing credentialId (#18281)
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user