From 6e7359ae962801be103760c1fb89ea13014135c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edward=20Fern=C3=A1ndez?= <40343753+edanfesi@users.noreply.github.com> Date: Thu, 6 Jan 2022 17:06:31 -0500 Subject: [PATCH] fix code validation when the google calendar integration does not have all permission (#1425) Co-authored-by: Edward Fernandez --- lib/integrations/calendar/CalendarManager.ts | 11 +++++++---- .../calendar/services/BaseCalendarService.ts | 5 +++-- .../calendar/services/GoogleCalendarService.ts | 7 ++++--- .../calendar/services/Office365CalendarService.ts | 5 +++-- pages/api/integrations/googlecalendar/callback.ts | 15 +++++++++++---- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/integrations/calendar/CalendarManager.ts b/lib/integrations/calendar/CalendarManager.ts index a8dd52ba50..961b9cfc87 100644 --- a/lib/integrations/calendar/CalendarManager.ts +++ b/lib/integrations/calendar/CalendarManager.ts @@ -9,7 +9,7 @@ import notEmpty from "@lib/notEmpty"; import { ALL_INTEGRATIONS } from "../getIntegrations"; import { CALENDAR_INTEGRATIONS_TYPES } from "./constants/generals"; -import { CalendarServiceType } from "./constants/types"; +import { CalendarServiceType, EventBusyDate } from "./constants/types"; import { Calendar, CalendarEvent } from "./interfaces/Calendar"; import AppleCalendarService from "./services/AppleCalendarService"; import CalDavCalendarService from "./services/CalDavCalendarService"; @@ -111,9 +111,12 @@ export const getBusyCalendarTimes = async ( .map((credential) => getCalendar(credential)) .filter(notEmpty); - const results = await Promise.all( - calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars)) - ); + let results: EventBusyDate[][] = []; + try { + results = await Promise.all(calendars.map((c) => c.getAvailability(dateFrom, dateTo, selectedCalendars))); + } catch (error) { + log.warn(error); + } return results.reduce((acc, availability) => acc.concat(availability), []); }; diff --git a/lib/integrations/calendar/services/BaseCalendarService.ts b/lib/integrations/calendar/services/BaseCalendarService.ts index 7c400605da..c86124b946 100644 --- a/lib/integrations/calendar/services/BaseCalendarService.ts +++ b/lib/integrations/calendar/services/BaseCalendarService.ts @@ -31,8 +31,7 @@ export default abstract class BaseCalendarService implements Calendar { private credentials: Record = {}; private headers: Record = {}; protected integrationName = ""; - - log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); + private log: typeof logger; constructor(credential: Credential, integrationName: string, url?: string) { this.integrationName = integrationName; @@ -47,6 +46,8 @@ export default abstract class BaseCalendarService implements Calendar { this.credentials = { username, password }; this.headers = getBasicAuthHeaders({ username, password }); + + this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); } async createEvent(event: CalendarEvent): Promise { diff --git a/lib/integrations/calendar/services/GoogleCalendarService.ts b/lib/integrations/calendar/services/GoogleCalendarService.ts index a97b57f8db..55f68dc964 100644 --- a/lib/integrations/calendar/services/GoogleCalendarService.ts +++ b/lib/integrations/calendar/services/GoogleCalendarService.ts @@ -17,13 +17,14 @@ export default class GoogleCalendarService implements Calendar { private url = ""; private integrationName = ""; private auth: { getToken: () => Promise }; - - log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); + private log: typeof logger; constructor(credential: Credential) { this.integrationName = CALENDAR_INTEGRATIONS_TYPES.google; this.auth = this.googleAuth(credential); + + this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); } private googleAuth = (credential: Credential) => { @@ -299,7 +300,7 @@ export default class GoogleCalendarService implements Calendar { }) || [] ); }) - .catch((err) => { + .catch((err: Error) => { this.log.error("There was an error contacting google calendar service: ", err); reject(err); diff --git a/lib/integrations/calendar/services/Office365CalendarService.ts b/lib/integrations/calendar/services/Office365CalendarService.ts index ed53062f2a..f3b7a46dc8 100644 --- a/lib/integrations/calendar/services/Office365CalendarService.ts +++ b/lib/integrations/calendar/services/Office365CalendarService.ts @@ -17,13 +17,14 @@ const MS_GRAPH_CLIENT_SECRET = process.env.MS_GRAPH_CLIENT_SECRET || ""; export default class Office365CalendarService implements Calendar { private url = ""; private integrationName = ""; + private log: typeof logger; auth: { getToken: () => Promise }; - log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); - constructor(credential: Credential) { this.integrationName = CALENDAR_INTEGRATIONS_TYPES.office365; this.auth = this.o365Auth(credential); + + this.log = logger.getChildLogger({ prefix: [`[[lib] ${this.integrationName}`] }); } async createEvent(event: CalendarEvent): Promise { diff --git a/pages/api/integrations/googlecalendar/callback.ts b/pages/api/integrations/googlecalendar/callback.ts index 61dbc6014b..5499db83e4 100644 --- a/pages/api/integrations/googlecalendar/callback.ts +++ b/pages/api/integrations/googlecalendar/callback.ts @@ -19,7 +19,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) res.status(401).json({ message: "You must be logged in to do this" }); return; } - if (typeof code !== "string") { + if (code && typeof code !== "string") { res.status(400).json({ message: "`code` must be a string" }); return; } @@ -30,10 +30,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const { client_secret, client_id } = JSON.parse(credentials).web; const redirect_uri = BASE_URL + "/api/integrations/googlecalendar/callback"; - const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri); - const token = await oAuth2Client.getToken(code); - const key = token.res?.data; + const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uri); + + let key = ""; + + if (code) { + const token = await oAuth2Client.getToken(code); + + key = token.res?.data; + } + await prisma.credential.create({ data: { type: "google_calendar",