Skip to content

Scheduling infrastructure for absolutely everyone.

feat: Add support for multiple google calendars

Fernando Barrios5f101ca5fc66
diff --git a/README.md b/README.md
index 783110893c3..d07614cb84c 100644
--- a/README.md
+++ b/README.md
@@ -146,7 +146,7 @@ Contributions are what make the open source community such an amazing place to b
2. In the search box, type calendar and select the Google Calendar API search result.
3. Enable the selected API.
4. Next, go to the [OAuth consent screen](https://console.cloud.google.com/apis/credentials/consent) from the side pane. Select the app type (Internal or External) and enter the basic app details on the first page.
-5. In the second page on Scopes, select Add or Remove Scopes. Search for Calendar.event and select the scope with scope value `.../auth/calendar.events` and select Update.
+5. In the second page on Scopes, select Add or Remove Scopes. Search for Calendar.event and select the scope with scope value `.../auth/calendar.events`, `.../auth/calendar.readonly`, `.../auth/calendar` and select Update.
6. In the third page (Test Users), add the Google account(s) you'll using. Make sure the details are correct on the last page of the wizard and your consent screen will be configured.
7. Now select [Credentials](https://console.cloud.google.com/apis/credentials) from the side pane and then select Create Credentials. Select the OAuth Client ID option.
8. Select Web Application as the Application Type.
diff --git a/lib/calendarClient.ts b/lib/calendarClient.ts
index dcfaefc9597..7642c72cd34 100644
--- a/lib/calendarClient.ts
+++ b/lib/calendarClient.ts
@@ -139,20 +139,33 @@ const GoogleCalendar = (credential) => {
return {
getAvailability: (dateFrom, dateTo) => new Promise( (resolve, reject) => {
const calendar = google.calendar({ version: 'v3', auth: myGoogleAuth });
- calendar.freebusy.query({
- requestBody: {
- timeMin: dateFrom,
- timeMax: dateTo,
- items: [ {
- "id": "primary"
- } ]
- }
- }, (err, apires) => {
- if (err) {
- reject(err);
- }
- resolve(apires.data.calendars.primary.busy)
- });
+ calendar.calendarList
+ .list()
+ .then(cals => {
+ const items = cals.data.items.filter(
+ item => !/calendar.google.com/.test(item.id)
+ );
+ calendar.freebusy.query({
+ requestBody: {
+ timeMin: dateFrom,
+ timeMax: dateTo,
+ items: items
+ }
+ }, (err, apires) => {
+ if (err) {
+ reject(err);
+ }
+ resolve(
+ Object.values(apires.data.calendars).flatMap(
+ (item) => item["busy"]
+ )
+ )
+ });
+ })
+ .catch((err) => {
+ reject(err);
+ });
+
}),
createEvent: (event: CalendarEvent) => new Promise( (resolve, reject) => {
const payload = {
diff --git a/pages/api/integrations/googlecalendar/add.ts b/pages/api/integrations/googlecalendar/add.ts
index 5d2c4ae0a35..c568e652313 100644
--- a/pages/api/integrations/googlecalendar/add.ts
+++ b/pages/api/integrations/googlecalendar/add.ts
@@ -4,7 +4,7 @@ import prisma from '../../../../lib/prisma';
const {google} = require('googleapis');
const credentials = process.env.GOOGLE_API_CREDENTIALS;
-const scopes = ['https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/calendar.events'];
+const scopes = ['https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/calendar.events', 'https://www.googleapis.com/auth/calendar'];
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'GET') {