Created
September 6, 2018 13:30
-
-
Save Meistercoach83/22978f3286a657e10934aa14641a800a to your computer and use it in GitHub Desktop.
Firestore & Google-Cal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as functions from 'firebase-functions'; | |
import * as admin from 'firebase-admin'; | |
import * as moment from 'moment'; | |
const { google } = require('googleapis'); | |
const GOOGLE_API_KEY = functions.config().google.calendar.key; | |
const calendar = google.calendar({ version: 'v3', auth: GOOGLE_API_KEY }); | |
const db = admin.firestore(); | |
const currentDate = moment(); | |
const timeMin = currentDate.subtract(1, 'year').toISOString(); | |
const timeMax = currentDate.add(2, 'year').toISOString(); | |
export const getGoogleCalendarEvents = functions.region('europe-west1').https.onRequest(async (req, resp) => { | |
const eventList = []; | |
try { | |
const applicationRef = db.collection('applications'); | |
const activeAppRef = await applicationRef.where('isCurrentApplication', '==', true).get(); | |
if (activeAppRef.size === 0) { | |
resp.send('No current Application found'); | |
} | |
const promises = []; | |
for (let cal of activeAppRef.docs[0].data().assignedCalendars) { | |
console.log(cal.link); | |
promises.push(getEventList(cal.link)); | |
} | |
const snapshots = await Promise.all(promises); | |
snapshots.forEach(snap => { | |
eventList.push(snap.data().items); | |
}); | |
return resp.send(eventList); | |
} catch (error) { | |
console.log(error); | |
return resp.status(500).send(error); | |
} | |
}); | |
function getEventList(cal): Promise<any> { | |
return calendar.events.list({ | |
calendarId: cal.link, | |
timeMin: timeMin, | |
timeMax: timeMax, | |
// maxResults: 100, | |
singleEvents: true, | |
orderBy: 'startTime' | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment