From fcc2e0f2ae1df63fa94baba0f213211967100c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20L=C3=B3pez?= Date: Sat, 19 Oct 2024 09:56:09 -0700 Subject: [PATCH] fix: allow gCal installs without a primary calendar (#17189) --- .../app-store/googlecalendar/api/callback.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/app-store/googlecalendar/api/callback.ts b/packages/app-store/googlecalendar/api/callback.ts index 2231ac3068..2cb3883b47 100644 --- a/packages/app-store/googlecalendar/api/callback.ts +++ b/packages/app-store/googlecalendar/api/callback.ts @@ -75,10 +75,10 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { }); const cals = await calendar.calendarList.list({ fields: "items(id,summary,primary,accessRole)" }); - const primaryCal = cals.data.items?.find((cal) => cal.primary); - // Primary calendar won't be null, this check satisfies typescript. + let primaryCal = cals.data.items?.find((cal) => cal.primary); if (!primaryCal?.id) { - throw new HttpError({ message: "Internal Error", statusCode: 500 }); + // If the primary calendar is not set, set it to the first calendar + primaryCal = cals.data.items?.[0]; } // Only attempt to update the user's profile photo if the user has granted the required scope @@ -95,6 +95,16 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { }, }); + // If we still don't have a primary calendar skip creating the selected calendar. + // It can be toggled on later. + if (!primaryCal?.id) { + res.redirect( + getSafeRedirectUrl(state?.returnTo) ?? + getInstalledAppPath({ variant: "calendar", slug: "google-calendar" }) + ); + return; + } + const selectedCalendarWhereUnique = { userId: req.session.user.id, externalId: primaryCal.id,