Skip to content

Scheduling infrastructure for absolutely everyone.

fix: Credentials cron for DelegationCredential not working (#21239)

Hariom Balharacaf184151d84
diff --git a/apps/web/app/api/cron/credentials/route.ts b/apps/web/app/api/cron/credentials/route.ts
index 4879efed8c2..a7541681702 100644
--- a/apps/web/app/api/cron/credentials/route.ts
+++ b/apps/web/app/api/cron/credentials/route.ts
@@ -8,6 +8,7 @@ import { NextResponse } from "next/server";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
+import { safeStringify } from "@calcom/lib/safeStringify";
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
import { DelegationCredentialRepository } from "@calcom/lib/server/repository/delegationCredential";
@@ -42,7 +43,7 @@ async function handleCreateCredentials() {
log.info(
`Skipping credential creation for workspace platform ${workspacePlatform.slug} - only google is supported`
);
- return;
+ continue;
}
// We can't know by looking at a Credential record in DB, if it has access to the `user.email` calendar in Google Calendar.
@@ -55,15 +56,15 @@ async function handleCreateCredentials() {
});
const existingCredentialUserIds = new Set(existingCredentials.map((cred) => cred.userId));
-
- const membersNeedingCredentials = organization.delegatedMembers.filter(
+ const delegatedMembers = organization.delegatedMembers;
+ const membersNeedingCredentials = delegatedMembers.filter(
(member) => !existingCredentialUserIds.has(member.userId)
);
const toProcessMembers = membersNeedingCredentials.slice(0, batchSizeToCreateCredentials);
log.info(
- `Creating credentials for ${toProcessMembers.length} members in organization ${organization.id}`
+ `Creating credentials for ${toProcessMembers.length} members out of ${delegatedMembers.length} delegated members in organization ${organization.id}`
);
const credentialCreationPromises = toProcessMembers.map(async (member) => {
@@ -80,18 +81,24 @@ async function handleCreateCredentials() {
});
log.info(`Created credential for member ${member.userId}`);
} catch (error) {
- log.error(`Error creating credential for member ${member.userId}:`, error);
+ log.error(`Error creating credential for member ${member.userId}:`, safeStringify(error));
throw error;
}
});
const results = await Promise.allSettled(credentialCreationPromises);
const successCount = results.filter((r) => r.status === "fulfilled").length;
const failureCount = results.filter((r) => r.status === "rejected").length;
-
+ log.info(
+ `Created ${successCount} credentials for delegationCredentialId: ${delegationCredential.id} and ${failureCount} failures`
+ );
totalSuccess += successCount;
totalFailures += failureCount;
}
+ log.info(
+ `Completed creating credentials for all delegation credentials. Total Success: ${totalSuccess}, Total Failures: ${totalFailures}`
+ );
+
return {
executedAt: new Date().toISOString(),
success: totalSuccess,
diff --git a/apps/web/app/api/cron/selected-calendars/route.ts b/apps/web/app/api/cron/selected-calendars/route.ts
index a94fe0a27dc..d409f7eaa83 100644
--- a/apps/web/app/api/cron/selected-calendars/route.ts
+++ b/apps/web/app/api/cron/selected-calendars/route.ts
@@ -36,8 +36,10 @@ async function handleCreateSelectedCalendars() {
});
if (!allDelegationUserCredentials.length) {
+ const message = "No delegation credentials found";
+ log.info(message);
return {
- message: "No delegation credentials found",
+ message,
success: 0,
failures: 0,
};
@@ -158,17 +160,19 @@ async function handleCreateSelectedCalendars() {
);
const successCount = results.filter((r) => r.status === "fulfilled").length;
const failureCount = results.filter((r) => r.status === "rejected").length;
-
+ log.info(
+ `Processed ${successCount} selected calendars for delegationCredentialId: ${delegationCredentialId} and ${failureCount} failures`
+ );
totalSuccess += successCount;
totalFailures += failureCount;
}
log.info(
- `Completed processing all selected calendars. Total Success: ${totalSuccess}, Total Failures: ${totalFailures}`
+ `Completed creating selected calendars for all delegation credentials. Total Success: ${totalSuccess}, Total Failures: ${totalFailures}`
);
return {
- message: "All selected calendars processed",
+ message: "All selected calendars created",
executedAt: new Date().toISOString(),
success: totalSuccess,
failures: totalFailures,
diff --git a/packages/app-store/googlecalendar/lib/CalendarService.ts b/packages/app-store/googlecalendar/lib/CalendarService.ts
index 361a1460729..836d5a52b74 100644
--- a/packages/app-store/googlecalendar/lib/CalendarService.ts
+++ b/packages/app-store/googlecalendar/lib/CalendarService.ts
@@ -826,7 +826,6 @@ export default class GoogleCalendarService implements Calendar {
const primaryCalendar = this.filterPrimaryCalendar(cals);
if (!primaryCalendar) return [];
return [primaryCalendar.id];
- return cals.reduce((c, cal) => (cal.id ? [...c, cal.id] : c), [] as string[]);
};
try {
diff --git a/packages/features/calendar-cache/calendar-cache.repository.ts b/packages/features/calendar-cache/calendar-cache.repository.ts
index 82606729510..3319434d045 100644
--- a/packages/features/calendar-cache/calendar-cache.repository.ts
+++ b/packages/features/calendar-cache/calendar-cache.repository.ts
@@ -145,6 +145,8 @@ export class CalendarCacheRepository implements ICalendarCacheRepository {
},
},
update: {
+ // Ensure that on update userId is also set(It handles the case where userId is not set for legacy records)
+ userId,
value,
expiresAt: new Date(Date.now() + CACHING_TIME),
},