Skip to content

Scheduling infrastructure for absolutely everyone.

fix: Wrong primary calendar selected for Google Calendar (#17301)

Benny Jooa4d733fb9db6
diff --git a/packages/app-store/googlecalendar/api/callback.ts b/packages/app-store/googlecalendar/api/callback.ts
index 2cb3883b47f..4d614d53c4d 100644
--- a/packages/app-store/googlecalendar/api/callback.ts
+++ b/packages/app-store/googlecalendar/api/callback.ts
@@ -42,11 +42,9 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri);
- let key;
-
if (code) {
const token = await oAuth2Client.getToken(code);
- key = token.tokens;
+ const key = token.tokens;
const grantedScopes = token.tokens.scope?.split(" ") ?? [];
// Check if we have granted all required permissions
const hasMissingRequiredScopes = REQUIRED_SCOPES.some((scope) => !grantedScopes.includes(scope));
@@ -75,7 +73,12 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
});
const cals = await calendar.calendarList.list({ fields: "items(id,summary,primary,accessRole)" });
- let primaryCal = cals.data.items?.find((cal) => cal.primary);
+ const tokenInfo = key.access_token ? await oAuth2Client.getTokenInfo(key.access_token) : null;
+ const userEmail = tokenInfo?.email ?? null;
+
+ let primaryCal = cals.data.items?.find((cal) => {
+ cal.primary || cal.accessRole === "owner" || cal.id === userEmail;
+ });
if (!primaryCal?.id) {
// If the primary calendar is not set, set it to the first calendar
primaryCal = cals.data.items?.[0];