4
4
5
5
const _ = require ( 'lodash' )
6
6
const config = require ( 'config' )
7
+ const busApi = require ( 'tc-bus-api-wrapper' )
8
+ const busApiClient = busApi ( _ . pick ( config , [ 'AUTH0_URL' , 'AUTH0_AUDIENCE' , 'TOKEN_CACHE_TIME' , 'AUTH0_CLIENT_ID' ,
9
+ 'AUTH0_CLIENT_SECRET' , 'BUSAPI_URL' , 'KAFKA_ERROR_TOPIC' , 'AUTH0_PROXY_SERVER_URL' ] ) )
7
10
const m2mAuth = require ( 'tc-core-library-js' ) . auth . m2m
8
11
const m2m = m2mAuth ( _ . pick ( config , [ 'AUTH0_URL' , 'AUTH0_AUDIENCE' , 'TOKEN_CACHE_TIME' , 'AUTH0_PROXY_SERVER_URL' ] ) )
9
12
const superagent = require ( 'superagent' )
13
+ const { v4 : uuid } = require ( 'uuid' )
14
+
15
+ const logger = require ( './logger' )
10
16
11
17
/**
12
18
* Get Kafka options
@@ -116,6 +122,24 @@ async function getChallengeResources (challengeId, roleId) {
116
122
return allResources
117
123
}
118
124
125
+ /**
126
+ * Get challenge resources using v4 API
127
+ * @param {String } legacyId the legacy challenge ID
128
+ * @param {String } challengeId the challenge ID
129
+ * @return {Array } list of observers
130
+ */
131
+ async function getChallengeResourcesV4 ( legacyId , challengeId ) {
132
+ const token = await getM2MToken ( )
133
+ const url = `${ config . V4_RESOURCES_API } ` + legacyId + '/resources'
134
+ const res = await superagent . get ( url ) . set ( 'Authorization' , `Bearer ${ token } ` )
135
+ if ( res . status !== 200 ) {
136
+ throw new Error ( `Failed to get resources for challenge id ${ challengeId } : ${ JSON . stringify ( _ . get ( res . body , 'result.content' ) ) } ` )
137
+ }
138
+
139
+ // TODO: Make generic and not hardcoded `OBSERVER`
140
+ return _ . filter ( _ . get ( res . body , 'result.content' ) , { role : 'Observer' } )
141
+ }
142
+
119
143
/**
120
144
* Search members of given member ids
121
145
* @param {Array } memberIds the member ids
@@ -198,7 +222,7 @@ async function filterMemberForGroups (memberIds, groupIds) {
198
222
const memberList = [ ]
199
223
200
224
for ( const memberId of memberIds ) {
201
- const res = await Promise . all ( groupIds . map ( groupId => memberGroupsCall ( groupId , memberId ) ) )
225
+ const res = await Promise . allSettled ( groupIds . map ( groupId => checkMemberGroup ( groupId , memberId ) ) )
202
226
const memberGroups = _ . compact ( _ . flattenDeep ( _ . map ( res , 'value' ) ) )
203
227
204
228
if ( memberGroups . length !== groupIds . length ) memberList . push ( memberId )
@@ -208,24 +232,61 @@ async function filterMemberForGroups (memberIds, groupIds) {
208
232
}
209
233
210
234
/**
211
- * Return the memberId if member is part of the groups
235
+ * Check for membership against the provided group
212
236
* @param {String } groupId
213
237
* @param {String } memberId
214
238
* @returns {String } memberId in case of member of group
215
239
*/
216
- async function memberGroupsCall ( groupId , memberId ) {
240
+ async function checkMemberGroup ( groupId , memberId ) {
217
241
// M2M token is cached by 'tc-core-library-js' lib
218
242
const token = await getM2MToken ( )
219
243
const url = `${ config . GROUPS_API_URL } /${ groupId } /members/${ memberId } `
220
244
221
- try {
222
- return superagent
223
- . get ( url )
224
- . set ( 'Authorization' , `Bearer ${ token } ` )
225
- . timeout ( config . REQUEST_TIMEOUT )
226
- } catch ( error ) {
227
- return [ ]
245
+ return superagent
246
+ . get ( url )
247
+ . set ( 'Authorization' , `Bearer ${ token } ` )
248
+ . timeout ( config . REQUEST_TIMEOUT )
249
+ }
250
+
251
+ /**
252
+ * Remove resources using V4 API
253
+ * @param {String } groupId
254
+ * @param {String } memberId
255
+ * @returns {String } memberId in case of member of group
256
+ */
257
+ async function deleteResourcesV4 ( challengeId , resources ) {
258
+ const payloads = [ ]
259
+
260
+ resources . map ( resource => {
261
+ payloads . push ( {
262
+ id : uuid ( ) ,
263
+ challengeId,
264
+ memberId : resource [ 'properties' ] [ 'External Reference ID' ] ,
265
+ memberHandle : resource [ 'properties' ] [ 'Handle' ] ,
266
+ roleId : config . RESOURCE_ROLE_ID ,
267
+ created : resource [ 'properties' ] [ 'Handle' ] ,
268
+ createdBy : new Date ( ) . toUTCString ( )
269
+ } )
270
+ } )
271
+
272
+ await Promise . allSettled ( payloads . map ( payload => postEvent ( config . RESOURCE_DELETE_TOPIC , payload ) ) )
273
+ }
274
+
275
+ /**
276
+ * Send Kafka event message
277
+ * @params {String} topic the topic name
278
+ * @params {Object} payload the payload
279
+ */
280
+ async function postEvent ( topic , payload ) {
281
+ logger . info ( `Publish event to Kafka topic ${ topic } ` )
282
+ const message = {
283
+ topic,
284
+ originator : config . KAFKA_MESSAGE_ORIGINATOR ,
285
+ timestamp : new Date ( ) . toISOString ( ) ,
286
+ 'mime-type' : 'application/json' ,
287
+ payload
228
288
}
289
+ await busApiClient . postEvent ( message )
229
290
}
230
291
231
292
module . exports = {
@@ -236,5 +297,7 @@ module.exports = {
236
297
deleteResource,
237
298
getProjectChallenges,
238
299
getChallengeResources,
239
- filterMemberForGroups
300
+ filterMemberForGroups,
301
+ getChallengeResourcesV4,
302
+ deleteResourcesV4
240
303
}
0 commit comments