This repository was archived by the owner on Mar 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-groups.js
336 lines (276 loc) · 7.71 KB
/
update-groups.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Script that reads groups and their members, and updates the group info in ubahn
* for that member
* - Reads all groups
* - Reads all members in that group
* - Updates the group association for that member in Ubahn
*/
const _ = require('lodash')
const config = require('config')
const axios = require('axios')
const m2mAuth = require('tc-core-library-js').auth.m2m
const elasticsearch = require('@elastic/elasticsearch')
const ubahnM2MConfig = _.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL'])
const topcoderM2MConfig = _.pick(config, ['AUTH0_URL', 'AUTH0_TOPCODER_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL'])
const ubahnM2M = m2mAuth({ ...ubahnM2MConfig, AUTH0_AUDIENCE: ubahnM2MConfig.AUTH0_AUDIENCE })
const topcoderM2M = m2mAuth({ ...topcoderM2MConfig, AUTH0_AUDIENCE: topcoderM2MConfig.AUTH0_TOPCODER_AUDIENCE })
let esClient
async function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
* Get M2M token.
* @returns {Promise<unknown>}
*/
async function getTCM2MToken () {
return topcoderM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}
/**
* Returns m2m token for use with ubahn's apis
*/
async function getUbahnM2Mtoken () {
return ubahnM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}
/**
* Returns the member handle for the member id
* @param {Number} memberId The member id
*/
async function getMemberRecord (memberId) {
const token = await getTCM2MToken()
try {
const res = await axios.get(config.USERS_API_URL, {
params: {
filter: `id=${memberId}`
},
headers: {
Authorization: `Bearer ${token}`
}
})
const user = _.pick(_.get(res, 'data.result.content[0]', {}), ['handle'])
return user
} catch (error) {
if (error.response.status === 400) {
console.log(`Error getting the member handle for member with id ${memberId}`)
return {}
}
console.log(`Error getting the member handle for member with id ${memberId}`)
console.log(error)
throw error
}
}
/**
* Returns members in the group identified by the groupId
* @param {String} groupId The group id
*/
async function getMembersInGroup (groupId) {
const url = `${config.GROUPS_API_URL}/${groupId}/members`
const perPage = 12
let page = 1
const members = []
let once = false
const token = await getTCM2MToken()
while (true) {
try {
const res = await axios.get(url, {
params: {
page,
perPage
},
headers: {
Authorization: `Bearer ${token}`
}
})
if (!once) {
const total = res.headers['x-total']
console.log(`Discovered ${total} members in the group...`)
if (total > 0) {
console.log('Fetching all of them...')
}
once = true
}
if (res.data.length > 0) {
members.push(...res.data)
}
if (res.data.length !== perPage) {
break
}
page += 1
} catch (error) {
if (error.response && error.response.status === 404) {
console.log(`Error when fetching the members in group ${groupId}. Status is 404.`)
break
}
console.log(`Error when fetching members in group with id ${groupId} at page ${page} and per page ${perPage}`)
console.log(error)
throw error
}
}
return members
}
/**
* Returns all groups
*/
async function getAllGroups () {
const url = `${config.GROUPS_API_URL}`
const perPage = 12
let page = 1
const groups = []
let once = false
const token = await getTCM2MToken()
while (true) {
try {
const res = await axios.get(url, {
params: {
page,
perPage
},
headers: {
Authorization: `Bearer ${token}`
}
})
if (!once) {
const total = res.headers['x-total']
console.log(`Discovered ${total} groups...`)
if (total > 0) {
console.log('Fetching all of them...')
}
once = true
}
if (res.data.length > 0) {
groups.push(...res.data)
}
if (res.data.length !== perPage) {
break
}
page += 1
} catch (error) {
console.log(`Error when fetching groups at page ${page} and per page ${perPage}`)
console.log(error)
throw error
}
}
return groups
}
/**
* Returns the user's details in ubahn
* @param {String} handle The member's handle
*/
async function getUbahnUser (handle) {
const token = await getUbahnM2Mtoken()
try {
const res = await axios.get(config.UBAHN_USERS_API_URL, {
params: {
handle
},
headers: {
Authorization: `Bearer ${token}`
}
})
return res.data
} catch (error) {
console.log(`Error getting the user details from ubahn with handle ${handle}`)
console.log(error)
throw error
}
}
/**
* Returns the Elasticsearch client
*/
async function getESClient () {
if (esClient) {
return esClient
}
const host = config.ES.HOST
const cloudId = config.ES.ELASTICCLOUD.id
if (!esClient) {
if (cloudId) {
// Elastic Cloud configuration
esClient = new elasticsearch.Client({
cloud: {
id: cloudId
},
auth: {
username: config.ES.ELASTICCLOUD.username,
password: config.ES.ELASTICCLOUD.password
}
})
} else {
esClient = new elasticsearch.Client({
node: host
})
}
}
return esClient
}
/**
* Updates the groups in the user
* @param {String} userId The user id
* @param {Array} groups The array of groups
*/
async function updateGroupsForUser (userId, groups) {
const client = await getESClient()
const { body: user } = await client.getSource({
index: config.get('ES.USER_INDEX'),
type: config.get('ES.USER_TYPE'),
id: userId
})
const propertyName = config.get('ES.USER_GROUP_PROPERTY_NAME')
// if (!user[propertyName]) {
// user[propertyName] = []
// }
// let groupsTotal = user[propertyName].concat(groups)
// groupsTotal = _.uniqBy(groupsTotal, (g) => g.id)
user[propertyName] = _.uniqBy(groups, (g) => g.id)
await client.index({
index: config.get('ES.USER_INDEX'),
type: config.get('ES.USER_TYPE'),
id: userId,
body: user,
refresh: 'wait_for',
pipeline: config.get('ES.USER_PIPELINE_ID')
})
}
/**
* Main function
*/
async function start () {
const groups = await getAllGroups()
const final = {}
for (let i = 0; i < groups.length; i++) {
// Get all members in the group(s)
const mg = await getMembersInGroup(groups[i].id)
const mem = _.filter(mg, (m) => {
return (m.membershipType === 'user')
})
console.log(`${mem.length} members in the group will be processed now`)
for (let j = 0; j < mem.length; j++) {
const memberId = mem[j].memberId
// eslint-disable-next-line prefer-const
let { handle } = await getMemberRecord(memberId)
if (!handle) {
console.log(`Could not find handle of user ${memberId}`)
continue
}
const ubahnuser = await getUbahnUser(handle)
if (!ubahnuser || ubahnuser.length === 0 || ubahnuser[0].handle !== handle) {
// Ignore users that do not exist in ubahn
console.log('Ignoring user since it is not found in ubahn')
continue
}
if (!final[ubahnuser[0].id]) {
final[ubahnuser[0].id] = []
}
final[ubahnuser[0].id].push({
id: groups[i].id,
name: groups[i].name
})
}
}
const keys = Object.keys(final)
for (let i = 0; i < keys.length; i++) {
await updateGroupsForUser(keys[i], final[keys[i]])
await sleep()
}
console.log('All groups copied over to ubahn as relevant')
}
start()