forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleGraphClient.js
140 lines (121 loc) · 4.1 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
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
// 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.
}
});
}
/**
* Creates a new tag by provided details.
*/
async createTeamworkTagAsync(teamId, teamTag) {
var postData = {
displayName: teamTag.displayName,
description: teamTag.description,
members: []
};
for (var memberToBeAdded of teamTag.membersToBeAdded) {
postData.members.push({
userId: memberToBeAdded.userId
});
}
return await this.graphClient
.api('/teams/'+ teamId +'/tags').version('beta')
.post(postData).then((res) => {
return res;
});
}
/**
* Creates a new tag by provided details.
*/
async updateTeamworkTagAsync(teamId, teamTag) {
var updateTagData = {
displayName: teamTag.displayName,
description: teamTag.description
}
var response = await this.graphClient
.api('/teams/'+ teamId +'/tags/'+ teamTag.id).version('beta')
.update(updateTagData).then(async() => {
await this.addRemoveTagMembersAsync(teamId, teamTag);
});
}
/**
* Add and remove the users as per the updated details.
* @param {string} teamId Id of the team
* @param {object} teamTag Updated details of the tag.
*/
async addRemoveTagMembersAsync(teamId, teamTag) {
for (var member of teamTag.membersToBeAdded) {
try {
var teamworkTagMember =
{
userId: member.userId
};
await this.graphClient
.api('/teams/'+ teamId +'/tags/'+ teamTag.id +'/members').version('beta')
.post(teamworkTagMember);
}
catch (ex) {
console.error("Member not added with user Id: " + member.userId, ex);
continue;
}
}
for (var member of teamTag.membersToBeDeleted)
{
try {
await this.graphClient
.api('/teams/'+ teamId +'/tags/'+ teamTag.id +'/members/'+member.id).version('beta')
.delete();
}
catch (ex)
{
console.error("Member not deleted with user Id: " + member.userId, ex);
continue;
}
}
}
/**
* Gets list of tags present for the specified team Id.
*/
async listTeamworkTagsAsync(teamId) {
return await this.graphClient
.api('/teams/'+ teamId +'/tags').version('beta')
.get().then((res) => {
return res;
});
}
/**
* Deletes the specified tag from team based on tagId and teamId.
*/
async deleteTeamworkTagsAsync(teamId, tagId) {
return await this.graphClient
.api('/teams/'+ teamId +'/tags/'+ tagId).version('beta')
.delete().then((res) => {
return res;
});
}
/**
* Gets the members of the specified tag Id.
*/
async getTeamworkTagsMembersAsync(teamId, tagId) {
return await this.graphClient
.api('/teams/'+ teamId +'/tags/'+ tagId+'/members').version('beta')
.get().then((res) => {
return res;
});
}
}
exports.SimpleGraphClient = SimpleGraphClient;