fix: allow gCal installs without a primary calendar (#17189)

This commit is contained in:
Omar López
2024-10-19 16:56:09 +00:00
committed by GitHub
parent 731f97ebe4
commit fcc2e0f2ae
@@ -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,