-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-api.js
141 lines (128 loc) · 4.79 KB
/
graph-api.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import 'isomorphic-fetch';
import { Client } from '@microsoft/microsoft-graph-client';
import MuAuthenticationProvider from './mu-authentication-provider';
const CUSTOMER_VISIT_START_HOUR = process.env.VISIT_START_HOUR || 17;
const PLANNING_START_HOUR = process.env.PLANNING_START_HOUR || 19;
function toMsDate(date, hours, minutes = 0, seconds = 0) {
const clone = new Date(date);
clone.setHours(hours);
clone.setMinutes(minutes);
clone.setSeconds(seconds);
const isoDate = clone.toISOString();
const dateStr = isoDate.substr(0, 'YYYY-MM-DDTHH:mm:ss'.length);
return {
dateTime: dateStr,
timeZone: 'Romance Standard Time'
};
}
function toMsEvent(event, requiresReschedule = true) {
let htmlBody = '';
if (event.description) {
htmlBody += `<p>${event.description}</p>`;
}
if (event.url) {
htmlBody += `<p>RKB: <a href=${event.url}>${event.url}</a></p>`;
}
const msEvent = {
subject: event.subject,
body: {
contentType: 'html',
content: htmlBody
},
location: {
displayName: event.location,
address: {
street: event.street,
postalCode: event['postal-code'],
city: event.city,
// countryOrRegion: event.country
},
},
isReminderOn: false
};
if (requiresReschedule) {
let hour;
if (event.resource.type == 'http://data.rollvolet.be/vocabularies/crm/Request') {
hour = CUSTOMER_VISIT_START_HOUR;
} else {
hour = PLANNING_START_HOUR;
}
msEvent.start = toMsDate(event.date, hour);
msEvent.end = toMsDate(event.date, hour + 1);
}
return msEvent;
}
/**
* Client to interact with the MS Graph API. Requests are executed on behalf of a user.
* The client uses the mu-authentication-provider which fetches an access-token from
* the triplestore based on the user's session.
*
* Note: This client is not responsible for inserting/deleting data in the triplestore,
* only for interacting with the O365 Cloud using the Graph API.
*/
export default class GraphApiClient {
constructor(sessionUri) {
this.client = Client.initWithMiddleware({
authProvider: new MuAuthenticationProvider(sessionUri)
});
}
async createCalendarEvent(calendarId, event, resource) {
const basePath = calendarId ? `/users/${calendarId}` : `/me`;
console.log(`Creating calendar event on ${event.date} in MS calendar ${basePath}`);
const msEvent = toMsEvent(Object.assign({}, event, { resource }));
const path = `${basePath}/calendar/events`;
const response = await this.client.api(path).post(msEvent);
return response;
}
async getCalendarEvent(calendarId, event) {
const basePath = calendarId ? `/users/${calendarId}` : `/me`;
console.log(`Fetching calendar event with id ${event['ms-identifier']} in MS calendar ${basePath}`);
const path = `${basePath}/calendar/events/${event['ms-identifier']}`;
try {
const response = await this.client.api(path).get();
const date = response.start.dateTime.substr(0, "YYYY-MM-DD".length);
return {
'ms-identifier': response.id,
date: date
};
} catch (e) {
if (e && e.statusCode == 404) {
console.log(`Event with id ${event['ms-identifier']} not found in MS calendar ${basePath}.`);
return null;
} else {
throw e;
}
}
}
async updateCalendarEvent(calendarId, event, resource, requiresReschedule) {
const basePath = calendarId ? `/users/${calendarId}` : `/me`;
console.log(`Updating calendar event with id ${event['ms-identifier']} in MS calendar ${basePath}`);
const msEvent = toMsEvent(Object.assign({}, event, { resource }), requiresReschedule);
const path = `${basePath}/calendar/events/${event['ms-identifier']}`;
try {
const response = await this.client.api(path).update(msEvent);
return response;
} catch (e) {
if (e && e.statusCode == 404) {
console.log(`Event with id ${event['ms-identifier']} not found in MS calendar ${basePath}. Going to create a new calendar event.`);
const newEvent = await this.createCalendarEvent(calendarId, event, resource);
return newEvent;
} else {
throw e;
}
}
}
async deleteCalendarEvent(calendarId, msId) {
const basePath = calendarId ? `/users/${calendarId}` : `/me`;
console.log(`Deleting calendar event with id ${msId} from MS calendar ${basePath}`);
try {
await this.client.api(`${basePath}/calendar/events/${msId}`).delete();
} catch (e) {
if (e && e.statusCode == 404) {
console.log(`Event with id ${msId} not found in MS calendar ${calendarId}. Nothing to delete via Graph API.`);
} else {
console.log(`Something went wrong while deleting calendar event with id ${msId} from MS calendar ${basePath}. Event may need to be deleted manually in the calendar.`);
}
}
}
}