@@ -176,6 +176,25 @@ async function parsePayload (payload, m2mToken, isCreated = true) {
176
176
}
177
177
}
178
178
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
+
179
198
/**
180
199
* Process create challenge message
181
200
* @param {Object } message the kafka message
@@ -269,6 +288,22 @@ async function processUpdate (message) {
269
288
if ( ! challenge ) {
270
289
throw new Error ( `Could not find challenge ${ message . payload . legacyId } ` )
271
290
}
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
+ }
272
307
// we can't switch the challenge type
273
308
// TODO: track is missing from the response.
274
309
// if (message.payload.legacy.track) {
0 commit comments