Skip to content

Commit 8c1dfcc

Browse files
committed
Issues-94
1 parent a9ff708 commit 8c1dfcc

File tree

8 files changed

+95
-3
lines changed

8 files changed

+95
-3
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ services:
1717
KAFKA_ADVERTISED_HOST_NAME: localhost
1818
KAFKA_ADVERTISED_PORT: 9092
1919
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
20-
KAFKA_CREATE_TOPICS: "challenge.notification.create:1:1,challenge.notification.events:1:1,challenge.action.resource.create:1:1,challenge.action.resource.delete:1:1"
20+
KAFKA_CREATE_TOPICS: "challenge.notification.create:1:1,challenge.notification.update:1:1,challenge.notification.events:1:1,challenge.action.resource.create:1:1,challenge.action.resource.delete:1:1"

src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = {
99
TOPICS: {
1010
// For challenge creation
1111
CHALLENGE_CREATE_TOPIC: 'challenge.notification.create',
12+
// For challenge update
13+
CHALLENGE_UPDATE_TOPIC: 'challenge.notification.update',
1214
// For member registrations and de-registrations
1315
CHALLENGE_NOTIFICATION_TOPIC: 'challenge.notification.events',
1416
// For co-pilots,PMs,etc.

src/modules/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const createChallenge = require('./create_challenge')
2+
const updateChallenge = require('./update_challenge')
23
const userManagement = require('./user_management')
34

45
module.exports = {
5-
kafkaModules: [createChallenge, userManagement]
6+
kafkaModules: [createChallenge, updateChallenge, userManagement]
67
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const config = require('config')
2+
const util = require('util')
3+
const logger = require('../../utils/logger.util')
4+
const { updateVanillaGroup } = require('../../services/vanilla')
5+
const { processPayload } = require('./helpers')
6+
7+
const services = []
8+
9+
if (config.VANILLA_ENABLED) {
10+
services.push(updateVanillaGroup)
11+
}
12+
13+
/**
14+
* Handle a set of messages from the Kafka topic
15+
* @param {Array} messageSet
16+
* @param {String} topic
17+
*/
18+
async function handler (messageSet, topic) {
19+
if (services.length === 0) {
20+
logger.warn('No enabled services to handle messages')
21+
return
22+
}
23+
for (const item of messageSet) {
24+
const challenge = processPayload(item, topic)
25+
for (const service of services) {
26+
await service(challenge)
27+
.catch(err => {
28+
logger.error(util.inspect(err))
29+
})
30+
}
31+
}
32+
}
33+
34+
module.exports = handler
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const config = require('config')
2+
const _ = require('lodash')
3+
4+
/**
5+
* Processes a payload from the topic, to be consumed by the handler
6+
* @param {Object} payload
7+
*/
8+
function processPayload (payload, topic) {
9+
// Set the url of the challenge
10+
payload.url = `${config.TOPCODER.ROOT_URL}/challenges/${payload.id}`
11+
return payload
12+
}
13+
14+
module.exports = {
15+
processPayload
16+
}

src/modules/update_challenge/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Kafka = require('no-kafka')
2+
const constants = require('../../constants')
3+
const handler = require('./handler')
4+
5+
module.exports = {
6+
topics: [constants.KAFKA.TOPICS.CHALLENGE_UPDATE_TOPIC],
7+
handler: handler,
8+
options: {
9+
time: Kafka.LATEST_OFFSET
10+
}
11+
}

src/services/vanilla.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,28 @@ async function createVanillaGroup (challenge) {
265265
}
266266
}
267267

268+
/**
269+
* Update a vanilla forum group.
270+
*
271+
* @param {Object} challenge the challenge data
272+
*/
273+
async function updateVanillaGroup (challenge) {
274+
logger.info(`The challenge with challengeID=${challenge.id}:`)
275+
276+
const { body: groups } = await vanillaClient.searchGroups(challenge.id)
277+
if (groups.length == 0) {
278+
throw new Error('The group wasn\'t found for this challenge')
279+
}
280+
281+
if (groups.length > 1) {
282+
throw new Error('Multiple groups were found for this challenge')
283+
}
284+
285+
const {body: updatedGroup} = await vanillaClient.updateGroup(groups[0].groupID, {name: challenge.name})
286+
287+
logger.info(`The group was updated: ${JSON.stringify(updatedGroup)}`)
288+
}
289+
268290
async function createDiscussions (group, challenge, templateDiscussions, vanillaCategory) {
269291
for (const discussion of templateDiscussions) {
270292
// create a discussion
@@ -284,5 +306,6 @@ async function createDiscussions (group, challenge, templateDiscussions, vanilla
284306

285307
module.exports = {
286308
manageVanillaUser,
287-
createVanillaGroup
309+
createVanillaGroup,
310+
updateVanillaGroup
288311
}

src/utils/vanilla-client.util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ function getVanillaClient () {
107107
.query({ access_token: config.VANILLA.ADMIN_ACCESS_TOKEN })
108108
.send(data)
109109
},
110+
updateGroup: (groupId, data) => {
111+
return request.patch(`${config.VANILLA.API_URL}/groups/${groupId}`)
112+
.query({ access_token: config.VANILLA.ADMIN_ACCESS_TOKEN })
113+
.send(data)
114+
},
110115
searchGroups: (query) => {
111116
const queryParams = { access_token: config.VANILLA.ADMIN_ACCESS_TOKEN }
112117
queryParams.challengeID = query

0 commit comments

Comments
 (0)