feat: Add support for multiple google calendars

This commit is contained in:
Fernando Barrios 2021-05-01 22:53:16 -06:00
parent 3a6eae8b8f
commit 5f101ca5fc
3 changed files with 29 additions and 16 deletions

View File

@ -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.

View File

@ -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 = {

View File

@ -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') {