Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.

Commit 936d8a5

Browse files
Add support for activating/closing a challenge
1 parent 7d94bea commit 936d8a5

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

config/default.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ module.exports = {
3838
// PHASE IDs
3939
REGISTRATION_PHASE_ID: process.env.REGISTRATION_PHASE_ID || 'a93544bc-c165-4af4-b55e-18f3593b457a',
4040
SUBMISSION_PHASE_ID: process.env.SUBMISSION_PHASE_ID || '6950164f-3c5e-4bdc-abc8-22aaf5a1bd49',
41-
CHECKPOINT_SUBMISSION_PHASE_ID: process.env.CHECKPOINT_SUBMISSION_PHASE_ID || 'd8a2cdbe-84d1-4687-ab75-78a6a7efdcc8'
41+
CHECKPOINT_SUBMISSION_PHASE_ID: process.env.CHECKPOINT_SUBMISSION_PHASE_ID || 'd8a2cdbe-84d1-4687-ab75-78a6a7efdcc8',
42+
43+
// Challenge Type IDs
44+
TASK_TYPE_ID: process.env.TASK_TYPE_ID || 'e885273d-aeda-42c0-917d-bfbf979afbba'
4245
}

src/services/ProcessorService.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ async function parsePayload (payload, m2mToken, isCreated = true) {
176176
}
177177
}
178178

179+
/**
180+
* Activate challenge
181+
* @param {Number} challengeId the challenge ID
182+
*/
183+
async function activateChallenge (challengeId) {
184+
const m2mToken = await helper.getM2MToken()
185+
return helper.postRequest(`${config.V4_CHALLENGE_API_URL}/${challengeId}/activate`, null, m2mToken)
186+
}
187+
188+
/**
189+
* Close challenge
190+
* @param {Number} challengeId the challenge ID
191+
* @param {Number} winnerId the winner ID
192+
*/
193+
async function closeChallenge (challengeId, winnerId) {
194+
const m2mToken = await helper.getM2MToken()
195+
return helper.postRequest(`${config.V4_CHALLENGE_API_URL}/${challengeId}/close?winnerId=${winnerId}`, null, m2mToken)
196+
}
197+
179198
/**
180199
* Process create challenge message
181200
* @param {Object} message the kafka message
@@ -269,6 +288,22 @@ async function processUpdate (message) {
269288
if (!challenge) {
270289
throw new Error(`Could not find challenge ${message.payload.legacyId}`)
271290
}
291+
if (message.payload.status) {
292+
if (message.payload.status === constants.challengeStatuses.Active && challenge.status !== constants.challengeStatuses.Active) {
293+
await activateChallenge(message.payload.legacyId)
294+
}
295+
if (message.payload.status === constants.challengeStatuses.Completed && challenge.status !== constants.challengeStatuses.Completed) {
296+
const challengeUuid = message.payload.id
297+
const v5Challenge = await helper.getRequest(`${config.V5_CHALLENGE_API_URL}/${challengeUuid}`, m2mToken)
298+
if (v5Challenge.typeId === config.TASK_TYPE_ID) {
299+
if (!message.payload.winners || message.payload.winners.length === 0) {
300+
throw new Error('Cannot close challenge without winners')
301+
}
302+
const winnerId = _.find(message.payload.winners, winner => winner.placement === 1).userId
303+
await closeChallenge(message.payload.legacyId, winnerId)
304+
}
305+
}
306+
}
272307
// we can't switch the challenge type
273308
// TODO: track is missing from the response.
274309
// if (message.payload.legacy.track) {

0 commit comments

Comments
 (0)