Skip to content

Commit aef09ed

Browse files
authored
Merge pull request #38 from topcoder-platform/issues-99
Issues-93:follow/unfollow categories, Issues-129, Issues-112
2 parents 328f1de + 0fe1db3 commit aef09ed

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = {
4040
KICK: 'kick'
4141
},
4242
TOPCODER: {
43-
ROLE_COPILOT: 'copilot'
43+
ROLE_COPILOT: 'copilot',
44+
ROLE_MANAGER: 'manager'
4445
},
4546
VANILLA: {
4647
CHALLENGES_FORUM: 'Challenges Forums',

src/modules/user_management/helpers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ function processPayload (payload, topic) {
3030
action: constants.USER_ACTIONS.KICK
3131
}
3232
case constants.KAFKA.TOPICS.CHALLENGE_NOTIFICATION_TOPIC:
33+
if (!(payload.type in actionMap)) {
34+
throw new Error(`Not supported ${payload.type}. Only message types ${JSON.stringify(Object.keys(eventTypes))} are processed from '${topic}'`)
35+
}
3336
return {
3437
challengeId: payload.data.challengeId,
3538
userId: payload.data.userId,
3639
action: actionMap[payload.type]
3740
}
3841
default:
39-
throw new Error('Received message from unrecognized topic')
42+
throw new Error(`Received message from unrecognized '${topic}'`)
4043
}
4144
}
4245

src/services/vanilla.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,15 @@ async function manageVanillaUser (data) {
8787
await vanillaClient.updateUser(vanillaUser.userID, userData)
8888
}
8989

90-
const { body: categories } = await vanillaClient.getCategoriesByParentUrlCode(challengeId)
90+
let categories = []
91+
const { body: nestedCategories } = await vanillaClient.getCategoriesByParentUrlCode(challengeId)
92+
categories = nestedCategories
93+
94+
// Some group might not have nested categories
95+
if (categories.length === 0) {
96+
const { body: parentCategory } = await vanillaClient.getCategoryByUrlcode(challengeId)
97+
categories.push(parentCategory)
98+
}
9199

92100
// Choose action to perform
93101
switch (action) {
@@ -100,6 +108,8 @@ async function manageVanillaUser (data) {
100108
for (const category of categories) {
101109
await vanillaClient.watchCategory(category.categoryID, vanillaUser.userID, { watched: true })
102110
logger.info(`The user ${vanillaUser.name} watches categoryID=${category.categoryID} associated with challenge ${challengeId}`)
111+
await vanillaClient.followCategory(category.categoryID, { followed: true, userID: vanillaUser.userID })
112+
logger.info(`The user ${vanillaUser.name} follows categoryID=${category.categoryID} associated with challenge ${challengeId}`)
103113
}
104114
break
105115
}
@@ -108,6 +118,8 @@ async function manageVanillaUser (data) {
108118
for (const category of categories) {
109119
await vanillaClient.watchCategory(category.categoryID, vanillaUser.userID, { watched: false })
110120
logger.info(`The user ${vanillaUser.name} stopped watching categoryID=${category.categoryID} associated with challenge ${challengeId}`)
121+
await vanillaClient.followCategory(category.categoryID, { followed: false, userID: vanillaUser.userID })
122+
logger.info(`The user ${vanillaUser.name} unfollows categoryID=${category.categoryID} associated with challenge ${challengeId}`)
111123
}
112124
await vanillaClient.removeUserFromGroup(group.groupID, vanillaUser.userID)
113125
logger.info(`The user '${vanillaUser.name}' was removed from the group '${group.name}'`)
@@ -169,7 +181,10 @@ async function createVanillaGroup (challenge) {
169181
}
170182

171183
const { body: project } = await topcoderApi.getProject(challenge.projectId)
172-
const copilots = _.filter(project.members, { role: constants.TOPCODER.ROLE_COPILOT })
184+
const members = _.filter(project.members, member => {
185+
return member.role === constants.TOPCODER.ROLE_COPILOT || member.role === constants.TOPCODER.ROLE_MANAGER
186+
})
187+
173188
const challengesForums = _.filter(template.categories, ['name', constants.VANILLA.CHALLENGES_FORUM])
174189
if (!challengesForums) {
175190
throw new Error(`The '${constants.VANILLA.CHALLENGES_FORUM}' category wasn't found in the template json file`)
@@ -253,8 +268,8 @@ async function createVanillaGroup (challenge) {
253268
await createDiscussions(group, challenge, groupTemplate.discussions, challengeCategory)
254269
}
255270

256-
for (const copilot of copilots) {
257-
await manageVanillaUser({ challengeId: challenge.id, action: constants.USER_ACTIONS.INVITE, handle: copilot.handle })
271+
for (const member of members) {
272+
await manageVanillaUser({ challengeId: challenge.id, action: constants.USER_ACTIONS.INVITE, handle: member.handle })
258273
}
259274

260275
challengeDetailsDiscussion.url = `${challengeCategory.url}`

src/utils/vanilla-client.util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ function getVanillaClient () {
2424
throw err
2525
})
2626
},
27+
followCategory: (categoryId, data) => {
28+
return request.put(`${config.VANILLA.API_URL}/categories/${categoryId}/follow`)
29+
.query({ access_token: config.VANILLA.ADMIN_ACCESS_TOKEN })
30+
.send(data)
31+
},
2732
updateCategory: (categoryId, data) => {
2833
return request.patch(`${config.VANILLA.API_URL}/categories/${categoryId}`)
2934
.query({ access_token: config.VANILLA.ADMIN_ACCESS_TOKEN })

0 commit comments

Comments
 (0)