Skip to content

Scheduling infrastructure for absolutely everyone.

fix code validation when the google calendar integration does not have all permission (#1425)

Edward Fernández6e7359ae9628
diff --git a/lib/integrations/calendar/CalendarManager.ts b/lib/integrations/calendar/CalendarManager.ts
index a8dd52ba506..961b9cfc87c 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 7c400605da6..c86124b9460 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<string, string> = {};
private headers: Record<string, string> = {};
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<NewCalendarEventType> {
diff --git a/lib/integrations/calendar/services/GoogleCalendarService.ts b/lib/integrations/calendar/services/GoogleCalendarService.ts
index a97b57f8dbc..55f68dc9643 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<MyGoogleAuth> };
-
- 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 ed53062f2a4..f3b7a46dc8c 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<string> };
- 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<NewCalendarEventType> {
diff --git a/pages/api/integrations/googlecalendar/callback.ts b/pages/api/integrations/googlecalendar/callback.ts
index 61dbc6014b9..5499db83e45 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;
+ let key = "";
+
+ if (code) {
+ const token = await oAuth2Client.getToken(code);
+
+ key = token.res?.data;
+ }
+
await prisma.credential.create({
data: {
type: "google_calendar",