perf: When a Google access token is expired and can't be refreshed, w… (#10557)

pull/10521/head^2
Alex van Andel 2023-08-03 13:17:38 +01:00 committed by GitHub
parent 2de4abba65
commit 91af873db9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -58,7 +58,20 @@ export default class GoogleCalendarService implements Calendar {
}); });
myGoogleAuth.setCredentials(googleCredentials); myGoogleAuth.setCredentials(googleCredentials);
} catch (err) { } 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; return myGoogleAuth;
}; };

View File

@ -11,7 +11,10 @@ const getCalendarsEvents = async (
dateTo: string, dateTo: string,
selectedCalendars: SelectedCalendar[] selectedCalendars: SelectedCalendar[]
): Promise<EventBusyDate[][]> => { ): Promise<EventBusyDate[][]> => {
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))); const calendars = await Promise.all(calendarCredentials.map((credential) => getCalendar(credential)));
performance.mark("getBusyCalendarTimesStart"); performance.mark("getBusyCalendarTimesStart");
const results = calendars.map(async (c, i) => { const results = calendars.map(async (c, i) => {