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

Commit 902acc8

Browse files
Replace message payload with data from the challenge API
1 parent 4c0f873 commit 902acc8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/app.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
require('./bootstrap')
6+
const _ = require('lodash')
67
const config = require('config')
78
const Kafka = require('no-kafka')
89
const healthcheck = require('@topcoder-platform/topcoder-healthcheck-dropin')
@@ -45,6 +46,21 @@ const dataHandler = (messageSet, topic, partition) => Promise.each(messageSet, a
4546
return
4647
}
4748

49+
// do not trust the message payload
50+
// the message.payload will be replaced with the data from the API
51+
try {
52+
const challengeUuid = _.get(messageJSON, 'payload.id')
53+
if (_.isEmpty(challengeUuid)) {
54+
throw new Error('Invalid payload')
55+
}
56+
const m2mToken = await helper.getM2MToken()
57+
const v5Challenge = await helper.getRequest(`${config.V5_CHALLENGE_API_URL}/${challengeUuid}`, m2mToken)
58+
messageJSON.payload = v5Challenge.body
59+
} catch (err) {
60+
logger.debug('Failed to fetch challenge information')
61+
logger.logFullError(err)
62+
}
63+
4864
try {
4965
if (topic === config.CREATE_CHALLENGE_TOPIC) {
5066
await ProcessorService.processCreate(messageJSON)

src/services/ProcessorService.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,7 @@ async function processUpdate (message) {
443443
logger.info('Activated!')
444444
}
445445
if (message.payload.status === constants.challengeStatuses.Completed && challenge.currentStatus !== constants.challengeStatuses.Completed) {
446-
const challengeUuid = message.payload.id
447-
const v5Challenge = await helper.getRequest(`${config.V5_CHALLENGE_API_URL}/${challengeUuid}`, m2mToken)
448-
if (v5Challenge.body.task.isTask) {
446+
if (message.payload.task.isTask) {
449447
logger.info('Challenge is a TASK')
450448
if (!message.payload.winners || message.payload.winners.length === 0) {
451449
throw new Error('Cannot close challenge without winners')

0 commit comments

Comments
 (0)