From 91af873db99b9747cb2c065364737debf423616a Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Thu, 3 Aug 2023 13:17:38 +0100 Subject: [PATCH] =?UTF-8?q?perf:=20When=20a=20Google=20access=20token=20is?= =?UTF-8?q?=20expired=20and=20can't=20be=20refreshed,=20w=E2=80=A6=20(#105?= =?UTF-8?q?57)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../googlecalendar/lib/CalendarService.ts | 15 ++++++++++++++- packages/core/getCalendarsEvents.ts | 5 ++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/app-store/googlecalendar/lib/CalendarService.ts b/packages/app-store/googlecalendar/lib/CalendarService.ts index 60c97c4b6b..746fd4c902 100644 --- a/packages/app-store/googlecalendar/lib/CalendarService.ts +++ b/packages/app-store/googlecalendar/lib/CalendarService.ts @@ -58,7 +58,20 @@ export default class GoogleCalendarService implements Calendar { }); myGoogleAuth.setCredentials(googleCredentials); } catch (err) { - this.log.error("Error refreshing google token", err); + let message; + if (err instanceof Error) message = err.message; + else message = String(err); + // if not invalid_grant, default behaviour (which admittedly isn't great) + if (message !== "invalid_grant") return myGoogleAuth; + // when the error is invalid grant, it's unrecoverable and the credential marked invalid. + // TODO: Evaluate bubbling up and handling this in the CalendarManager. IMO this should be done + // but this is a bigger refactor. + await prisma.credential.update({ + where: { id: credential.id }, + data: { + invalid: true, + }, + }); } return myGoogleAuth; }; diff --git a/packages/core/getCalendarsEvents.ts b/packages/core/getCalendarsEvents.ts index b00bdc674d..21103a27ff 100644 --- a/packages/core/getCalendarsEvents.ts +++ b/packages/core/getCalendarsEvents.ts @@ -11,7 +11,10 @@ const getCalendarsEvents = async ( dateTo: string, selectedCalendars: SelectedCalendar[] ): Promise => { - const calendarCredentials = withCredentials.filter((credential) => credential.type.endsWith("_calendar")); + const calendarCredentials = withCredentials + .filter((credential) => credential.type.endsWith("_calendar")) + // filter out invalid credentials - these won't work. + .filter((credential) => !credential.invalid); const calendars = await Promise.all(calendarCredentials.map((credential) => getCalendar(credential))); performance.mark("getBusyCalendarTimesStart"); const results = calendars.map(async (c, i) => {