-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
executable file
·95 lines (81 loc) · 2.4 KB
/
graph.js
File metadata and controls
executable file
·95 lines (81 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var graph = require('@microsoft/microsoft-graph-client');
require('isomorphic-fetch');
module.exports = {
getUserDetails: async function (accessToken) {
const client = getAuthenticatedClient(accessToken);
const user = await client
.api('/me')
.select('displayName,mail,mailboxSettings,userPrincipalName')
.get();
return user;
},
// <GetCalendarViewSnippet>
getCalendarView: async function (accessToken, start, end, timeZone) {
const client = getAuthenticatedClient(accessToken);
const events = await client
.api('/me/calendarview')
// Add Prefer header to get back times in user's timezone
.header("Prefer", `outlook.timezone="${timeZone}"`)
// Add the begin and end of the calendar window
.query({ startDateTime: start, endDateTime: end })
// Get just the properties used by the app
.select('subject,organizer,start,end')
// Order by start time
.orderby('start/dateTime')
// Get at most 50 results
.top(50)
.get();
return events;
},
// </GetCalendarViewSnippet>
// <CreateEventSnippet>
createEvent: async function (accessToken, formData, timeZone) {
const client = getAuthenticatedClient(accessToken);
// Build a Graph event
const newEvent = {
subject: formData.subject,
start: {
dateTime: formData.start,
timeZone: timeZone
},
end: {
dateTime: formData.end,
timeZone: timeZone
},
body: {
contentType: 'text',
content: formData.body
}
};
// Add attendees if present
if (formData.attendees) {
newEvent.attendees = [];
formData.attendees.forEach(attendee => {
newEvent.attendees.push({
type: 'required',
emailAddress: {
address: attendee
}
});
});
}
// POST /me/events
await client
.api('/me/events')
.post(newEvent);
},
// </CreateEventSnippet>
};
function getAuthenticatedClient(accessToken) {
// Initialize Graph client
const client = graph.Client.init({
// Use the provided access token to authenticate
// requests
authProvider: (done) => {
done(null, accessToken);
}
});
return client;
}