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-v2.js
290 lines (240 loc) · 6.81 KB
/
update-groups-v2.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
/**
* Purpose is same as update-groups.js => To copy groups info from groups
* api to ubahn. However, approach is different:
* - Read all users in ubahn first
* - Get their topcoder user ids
* - Use the user ids to get the group associations in groups api
* - Copy the associations into ubahn
*
* Run the script with `node update-groups-v2.js`
*/
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 all groups associated with the member
* @param {String} memberId The member id
*/
async function getGroupsOfUser (memberId) {
const membershipType = 'user'
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,
memberId,
membershipType
},
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} for member ${memberId}`)
console.log(error)
throw error
}
}
return groups
}
/**
* Returns all users in ubahn
*/
async function getAllUbahnUsers () {
const perPage = 12
let page = 1
const users = []
let once = false
const token = await getUbahnM2Mtoken()
while (true) {
try {
const res = await axios.get(config.UBAHN_USERS_API_URL, {
headers: {
Authorization: `Bearer ${token}`
},
params: {
page,
perPage
}
})
if (!once) {
const total = res.headers['x-total']
console.log(`Discovered ${total} user(s) in ubahn...`)
if (total > 0) {
console.log('Fetching all of them...')
}
once = true
}
if (res.data.length > 0) {
users.push(...res.data)
}
if (res.data.length !== perPage) {
break
}
page += 1
} catch (error) {
console.log(`Error when fetching users in ubahn at page ${page} and per page ${perPage}`)
console.log(error)
throw error
}
}
return users
}
/**
* Returns the external profile of the user
* @param {String} userId The ubahn user id
*/
async function getExternalProfile (userId) {
const url = `${config.UBAHN_USERS_API_URL}/${userId}/externalProfiles`
const token = await getUbahnM2Mtoken()
try {
const res = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`
}
})
return res.data
} catch (error) {
console.log(`Error fetching the external profile of user with id ${userId}`)
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 () {
// Get all users in ubahn
const users = await getAllUbahnUsers()
const final = {}
for (let i = 0; i < users.length; i++) {
const userId = users[i].id
console.log(`Getting external profile of user with id ${userId}`)
// Get external profiles of the user (to get the tc member ids of the ubahn users)
let externalProfiles = await getExternalProfile(userId)
externalProfiles = _.uniqBy(externalProfiles, (e) => e.externalId)
// Get groups of the user
for (let j = 0; j < externalProfiles.length; j++) {
const memberId = externalProfiles[j].externalId
console.log(`Getting groups of user with id ${memberId}`)
let groups = await getGroupsOfUser(memberId)
groups = groups.map(g => ({ id: g.id, name: g.name }))
if (groups.length > 0) {
if (!final[userId]) {
final[userId] = groups
} else {
final[userId] = final[userId].concat(groups)
}
}
}
}
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()