@@ -97,6 +97,11 @@ async function parsePayload (payload, m2mToken, isCreated = true) {
97
97
if ( payload . typeId ) {
98
98
const typeRes = await helper . getRequest ( `${ config . V5_CHALLENGE_TYPE_API_URL } /${ payload . typeId } ` , m2mToken )
99
99
data . subTrack = typeRes . body . abbreviation // FIXME: thomas
100
+ // TASK is named as FIRST_2_FINISH on legacy
101
+ if ( data . subTrack === constants . challengeAbbreviations . TASK ) {
102
+ data . task = true
103
+ data . subTrack = constants . challengeAbbreviations . FIRST_2_FINISH
104
+ }
100
105
data . legacyTypeId = typeRes . body . legacyId
101
106
}
102
107
if ( payload . description ) {
@@ -117,8 +122,8 @@ async function parsePayload (payload, m2mToken, isCreated = true) {
117
122
const registrationPhase = _ . find ( payload . phases , p => p . phaseId === config . REGISTRATION_PHASE_ID )
118
123
const submissionPhase = _ . find ( payload . phases , p => p . phaseId === config . SUBMISSION_PHASE_ID )
119
124
data . registrationStartsAt = new Date ( ) . toISOString ( )
120
- data . registrationEndsAt = new Date ( Date . now ( ) + registrationPhase . duration ) . toISOString ( )
121
- data . registrationDuration = registrationPhase . duration
125
+ data . registrationEndsAt = new Date ( Date . now ( ) + ( registrationPhase || submissionPhase ) . duration ) . toISOString ( )
126
+ data . registrationDuration = ( registrationPhase || submissionPhase ) . duration
122
127
data . submissionEndsAt = new Date ( Date . now ( ) + submissionPhase . duration ) . toISOString ( )
123
128
data . submissionDuration = submissionPhase . duration
124
129
@@ -151,7 +156,7 @@ async function parsePayload (payload, m2mToken, isCreated = true) {
151
156
if ( ! challengePrizes ) {
152
157
throw new Error ( 'Challenge prize information is invalid.' )
153
158
}
154
- data . prizes = _ . map ( challengePrizes [ 0 ] . prizes , 'value' ) . sort ( ( a , b ) => b - a )
159
+ data . prizes = _ . map ( challengePrizes . prizes , 'value' ) . sort ( ( a , b ) => b - a )
155
160
}
156
161
if ( payload . tags ) {
157
162
const techResult = await getTechnologies ( m2mToken )
@@ -287,19 +292,28 @@ async function processUpdate (message) {
287
292
if ( ! challenge ) {
288
293
throw new Error ( `Could not find challenge ${ message . payload . legacyId } ` )
289
294
}
295
+ await helper . putRequest ( `${ config . V4_CHALLENGE_API_URL } /${ message . payload . legacyId } ` , { param : saveDraftContestDTO } , m2mToken )
296
+
290
297
if ( message . payload . status ) {
291
- if ( message . payload . status === constants . challengeStatuses . Active && challenge . status !== constants . challengeStatuses . Active ) {
298
+ logger . info ( `The status has changed from ${ challenge . currentStatus } to ${ message . payload . status } ` )
299
+ if ( message . payload . status === constants . challengeStatuses . Active && challenge . currentStatus !== constants . challengeStatuses . Active ) {
300
+ logger . info ( 'Activating challenge...' )
292
301
await activateChallenge ( message . payload . legacyId )
302
+ logger . info ( 'Activated!' )
293
303
}
294
- if ( message . payload . status === constants . challengeStatuses . Completed && challenge . status !== constants . challengeStatuses . Completed ) {
304
+ if ( message . payload . status === constants . challengeStatuses . Completed && challenge . currentStatus !== constants . challengeStatuses . Completed ) {
295
305
const challengeUuid = message . payload . id
296
306
const v5Challenge = await helper . getRequest ( `${ config . V5_CHALLENGE_API_URL } /${ challengeUuid } ` , m2mToken )
297
- if ( v5Challenge . typeId === config . TASK_TYPE_ID ) {
307
+ if ( v5Challenge . body . typeId === config . TASK_TYPE_ID ) {
308
+ logger . info ( 'Challenge type is TASK' )
298
309
if ( ! message . payload . winners || message . payload . winners . length === 0 ) {
299
310
throw new Error ( 'Cannot close challenge without winners' )
300
311
}
301
312
const winnerId = _ . find ( message . payload . winners , winner => winner . placement === 1 ) . userId
313
+ logger . info ( `Will close the challenge with ID ${ message . payload . legacyId } . Winner ${ winnerId } !` )
302
314
await closeChallenge ( message . payload . legacyId , winnerId )
315
+ } else {
316
+ logger . info ( `Challenge type is ${ v5Challenge . body . typeId } .. Skip closing challenge...` )
303
317
}
304
318
}
305
319
}
@@ -313,8 +327,6 @@ async function processUpdate (message) {
313
327
// throw new Error('You can\'t change challenge track')
314
328
// }
315
329
// }
316
-
317
- await helper . putRequest ( `${ config . V4_CHALLENGE_API_URL } /${ message . payload . legacyId } ` , { param : saveDraftContestDTO } , m2mToken )
318
330
} catch ( e ) {
319
331
logger . error ( 'processUpdate Catch' , e )
320
332
throw e
0 commit comments