@@ -11,20 +11,18 @@ const utils = require('../utils/common.util')
11
11
const template = require ( config . TEMPLATES . TEMPLATE_FILE_PATH )
12
12
13
13
/**
14
- * Add/Remove a user to/from a category.
15
- *
14
+ * Add/Remove a user to/from a group.
16
15
* @param {Object } data the data from message payload
17
16
* @returns {undefined }
18
17
*/
19
18
async function manageVanillaUser ( data ) {
20
- const { challengeId, action, handle : username , role : projectRole } = data
21
- logger . info ( `Managing users for challengeID=${ challengeId } ...` )
19
+ const { challengeId, action, handle : username , projectRole, challengeRoles } = data
20
+ logger . info ( `Managing user for challengeID=${ challengeId } [action= ${ action } , handle= ${ username } , projectRole= ${ JSON . stringify ( projectRole ) } , challengeRoles= ${ JSON . stringify ( challengeRoles ) } ] ...` )
22
21
const { body : groups } = await vanillaClient . searchGroups ( challengeId )
23
22
const group = groups . length > 0 ? groups [ 0 ] : null
24
23
25
- // Only members and copilots will receive notifications
26
- const watch = ( ! projectRole || projectRole === constants . TOPCODER . PROJECT_ROLES . COPILOT ) ? 1 : 0
27
- const follow = 1
24
+ const watch = shouldWatchCategories ( projectRole , challengeRoles )
25
+ const follow = shouldFollowCategories ( projectRole , challengeRoles )
28
26
29
27
if ( ! group ) {
30
28
throw new Error ( 'The group wasn\'t not found by challengeID' )
@@ -77,7 +75,7 @@ async function manageVanillaUser (data) {
77
75
78
76
const { body : user } = await vanillaClient . addUser ( userData )
79
77
vanillaUser = user
80
- logger . info ( `New user with UserID=${ vanillaUser . userID } was added.` )
78
+ logger . info ( `New user [ UserID=${ vanillaUser . userID } , Name= ${ vanillaUser . name } ] was added.` )
81
79
} else {
82
80
// Get a full user profile with roles
83
81
const { body : user } = await vanillaClient . getUser ( vanillaUser . userID )
@@ -90,6 +88,7 @@ async function manageVanillaUser (data) {
90
88
roleID : [ ...currentVanillaRoleIDs , ...userTopcoderRoleIDs ]
91
89
}
92
90
await vanillaClient . updateUser ( vanillaUser . userID , userData )
91
+ // logger.info(`Roles were synchronized for User [UserID=${vanillaUser.userID}, Name=${vanillaUser.name}].`)
93
92
}
94
93
95
94
// Choose action to perform
@@ -100,12 +99,12 @@ async function manageVanillaUser (data) {
100
99
watch : watch ,
101
100
follow : follow
102
101
} )
103
- logger . info ( `The user ' ${ vanillaUser . name } ' was added to the group ' ${ group . name } ' ` )
102
+ logger . info ( `User [UserID= ${ vanillaUser . userID } , Name= ${ vanillaUser . name } was added to Group [GroupID= ${ group . groupID } , Name= ${ group . name } , Watch= ${ watch } , Follow= ${ follow } ] ` )
104
103
break
105
104
}
106
105
case constants . USER_ACTIONS . KICK : {
107
106
await vanillaClient . removeUserFromGroup ( group . groupID , vanillaUser . userID )
108
- logger . info ( `The user ' ${ vanillaUser . name } ' was removed from the group ' ${ group . name } ' ` )
107
+ logger . info ( `User [UserID= ${ vanillaUser . userID } , Name = ${ vanillaUser . name } was removed from Group [GroupID= ${ group . groupID } , Name= ${ group . name } ] ` )
109
108
break
110
109
}
111
110
default :
@@ -256,7 +255,12 @@ async function createVanillaGroup (challenge) {
256
255
}
257
256
258
257
for ( const member of members ) {
259
- await manageVanillaUser ( { challengeId : challenge . id , action : constants . USER_ACTIONS . INVITE , handle : member . handle , role : member . role } )
258
+ await manageVanillaUser ( {
259
+ challengeId : challenge . id ,
260
+ action : constants . USER_ACTIONS . INVITE ,
261
+ handle : member . handle ,
262
+ projectRole : member . role
263
+ } )
260
264
}
261
265
262
266
challengeDetailsDiscussion . url = `${ challengeCategory . url } `
@@ -319,6 +323,44 @@ async function createDiscussions (group, challenge, templateDiscussions, vanilla
319
323
}
320
324
}
321
325
326
+ /**
327
+ * Auto-watch categories
328
+ * @param projectRole string
329
+ * @param challengeRoles array
330
+ * @returns {boolean }
331
+ */
332
+ function shouldWatchCategories ( projectRole , challengeRoles ) {
333
+ // New user
334
+ if ( ! projectRole && _ . isEmpty ( challengeRoles ) ) {
335
+ return true
336
+ }
337
+
338
+ // Project Copilots / Challenge Copilots
339
+ return ( projectRole === constants . TOPCODER . PROJECT_ROLES . COPILOT ||
340
+ ( _ . isArray ( challengeRoles ) && _ . includes ( challengeRoles , constants . TOPCODER . CHALLENGE_ROLES . COPILOT ) )
341
+ )
342
+ }
343
+
344
+ /**
345
+ * Auto-follow categories
346
+ * @param projectRole string
347
+ * @param challengeRoles array
348
+ * @returns {boolean }
349
+ */
350
+ function shouldFollowCategories ( projectRole , challengeRoles ) {
351
+ // New user
352
+ if ( ! projectRole && _ . isEmpty ( challengeRoles ) ) {
353
+ return true
354
+ }
355
+
356
+ // Project Copilots or Managers / Challenge Copilots and Managers
357
+ return projectRole === constants . TOPCODER . PROJECT_ROLES . COPILOT ||
358
+ projectRole === constants . TOPCODER . PROJECT_ROLES . MANAGER ||
359
+ ( _ . isArray ( challengeRoles ) && ( _ . includes ( challengeRoles , constants . TOPCODER . CHALLENGE_ROLES . COPILOT ) ||
360
+ _ . includes ( challengeRoles , constants . TOPCODER . CHALLENGE_ROLES . MANAGER ) )
361
+ )
362
+ }
363
+
322
364
module . exports = {
323
365
manageVanillaUser,
324
366
createVanillaGroup,
0 commit comments