forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleGraphClient.js
54 lines (45 loc) · 1.69 KB
/
simpleGraphClient.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { Client } = require('@microsoft/microsoft-graph-client');
/**
* This class is a wrapper for the Microsoft Graph API.
* See: https://developer.microsoft.com/en-us/graph for more information.
*/
class SimpleGraphClient {
constructor(token) {
if (!token || !token.trim()) {
throw new Error('SimpleGraphClient: Invalid token received.');
}
this._token = token;
// Get an Authenticated Microsoft Graph client using the token issued to the user.
this.graphClient = Client.init({
authProvider: (done) => {
done(null, this._token); // First parameter takes an error if you can't get an access token.
}
});
}
// Get list of all teams in the organization
async getAllTeams() {
return await this.graphClient
.api('/groups?$filter=resourceProvisioningOptions/Any(x:x eq \'Team\')').version('beta')
.get().then((res) => {
return res;
});
}
// Add the user to the team whose team id is specified
async joinTeam(userId, teamId) {
const teamObject = {
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": [
"owner"
],
"[email protected]": "https://graph.microsoft.com/v1.0/users('" + userId + "')"
};
return await this.graphClient
.api('teams/' + teamId + '/members').version('beta')
.post(teamObject).then((res) => {
return res;
});
}
}
exports.SimpleGraphClient = SimpleGraphClient;