|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +const router = require('express-promise-router')(); |
| 5 | +const graph = require('../graph.js'); |
| 6 | +const moment = require('moment-timezone'); |
| 7 | +const iana = require('windows-iana'); |
| 8 | + |
| 9 | +/* GET /calendar */ |
| 10 | +// <GetRouteSnippet> |
| 11 | +router.get('/', |
| 12 | + async function(req, res) { |
| 13 | + if (!req.session.userId) { |
| 14 | + // Redirect unauthenticated requests to home page |
| 15 | + res.redirect('/') |
| 16 | + } else { |
| 17 | + const params = { |
| 18 | + active: { calendar: true } |
| 19 | + }; |
| 20 | + |
| 21 | + // Get the user |
| 22 | + const user = req.app.locals.users[req.session.userId]; |
| 23 | + // Convert user's Windows time zone ("Pacific Standard Time") |
| 24 | + // to IANA format ("America/Los_Angeles") |
| 25 | + // Moment needs IANA format |
| 26 | + const timeZoneId = iana.findOneIana(user.timeZone); |
| 27 | + console.log(`Time zone: ${timeZoneId.valueOf()}`); |
| 28 | + |
| 29 | + // Calculate the start and end of the current week |
| 30 | + // Get midnight on the start of the current week in the user's timezone, |
| 31 | + // but in UTC. For example, for Pacific Standard Time, the time value would be |
| 32 | + // 07:00:00Z |
| 33 | + var startOfWeek = moment.tz(timeZoneId.valueOf()).startOf('week').utc(); |
| 34 | + var endOfWeek = moment(startOfWeek).add(7, 'day'); |
| 35 | + console.log(`Start: ${startOfWeek.format()}`); |
| 36 | + |
| 37 | + // Get the access token |
| 38 | + var accessToken; |
| 39 | + try { |
| 40 | + accessToken = await getAccessToken(req.session.userId, req.app.locals.msalClient); |
| 41 | + } catch (err) { |
| 42 | + req.flash('error_msg', { |
| 43 | + message: 'Could not get access token. Try signing out and signing in again.', |
| 44 | + debug: JSON.stringify(err, Object.getOwnPropertyNames(err)) |
| 45 | + }); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (accessToken && accessToken.length > 0) { |
| 50 | + try { |
| 51 | + // Get the events |
| 52 | + const events = await graph.getCalendarView( |
| 53 | + accessToken, |
| 54 | + startOfWeek.format(), |
| 55 | + endOfWeek.format(), |
| 56 | + user.timeZone); |
| 57 | + |
| 58 | + params.events = events.value; |
| 59 | + } catch (err) { |
| 60 | + req.flash('error_msg', { |
| 61 | + message: 'Could not fetch events', |
| 62 | + debug: JSON.stringify(err, Object.getOwnPropertyNames(err)) |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + else { |
| 67 | + req.flash('error_msg', 'Could not get an access token'); |
| 68 | + } |
| 69 | + |
| 70 | + res.render('calendar', params); |
| 71 | + } |
| 72 | + } |
| 73 | +); |
| 74 | +// </GetRouteSnippet> |
| 75 | + |
| 76 | +async function getAccessToken(userId, msalClient) { |
| 77 | + // Look up the user's account in the cache |
| 78 | + const accounts = msalClient |
| 79 | + .getTokenCache() |
| 80 | + .getAllAccounts(); |
| 81 | + |
| 82 | + const userAccount = accounts.find(a => a.homeAccountId === userId); |
| 83 | + |
| 84 | + // Get the token silently |
| 85 | + const response = await msalClient.acquireTokenSilent({ |
| 86 | + scopes: process.env.OAUTH_SCOPES.split(','), |
| 87 | + redirectUri: process.env.OAUTH_REDIRECT_URI, |
| 88 | + account: userAccount |
| 89 | + }); |
| 90 | + |
| 91 | + return response.accessToken; |
| 92 | +} |
| 93 | + |
| 94 | +module.exports = router; |
0 commit comments