@@ -301,29 +301,51 @@ function * getM2Mtoken () {
301
301
}
302
302
303
303
/**
304
- * Get legacy challenge id if the challenge id is uuid form
305
- * @param {String } challengeId Challenge ID
306
- * @returns {String } Legacy Challenge ID of the given challengeId
304
+ * Function to get challenge by id
305
+ * @param {String } challengeId Challenge id
306
+ * @returns {Promise }
307
307
*/
308
- function * getLegacyChallengeId ( challengeId ) {
308
+ function * getChallenge ( challengeId ) {
309
309
if ( / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 0 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i. test ( challengeId ) ) {
310
310
logger . debug ( `${ challengeId } detected as uuid. Fetching legacy challenge id` )
311
311
const token = yield getM2Mtoken ( )
312
312
try {
313
313
const response = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
314
314
. set ( 'Authorization' , `Bearer ${ token } ` )
315
315
. set ( 'Content-Type' , 'application/json' )
316
- if ( _ . get ( response . body , 'legacy.pureV5' ) ) {
317
- // pure V5 challenges don't have a legacy ID
318
- return null
319
- }
320
- const legacyId = parseInt ( response . body . legacyId , 10 )
321
- logger . debug ( `Legacy challenge id is ${ legacyId } for v5 challenge id ${ challengeId } ` )
322
- return legacyId
316
+ return response . body
323
317
} catch ( err ) {
324
318
logger . error ( `Error while accessing ${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
325
319
throw err
326
320
}
321
+ } else {
322
+ logger . debug ( `${ challengeId } detected as legacy challenge id. Fetching legacy challenge id` )
323
+ const token = yield getM2Mtoken ( )
324
+ try {
325
+ const response = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } ?legacyId=${ challengeId } ` )
326
+ . set ( 'Authorization' , `Bearer ${ token } ` )
327
+ . set ( 'Content-Type' , 'application/json' )
328
+ return response . body [ 0 ]
329
+ } catch ( err ) {
330
+ logger . error ( `Error while accessing ${ config . CHALLENGEAPI_V5_URL } ?legacyId=${ challengeId } ` )
331
+ throw err
332
+ }
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Get legacy challenge id if the challenge id is uuid form
338
+ * @param {String } challengeId Challenge ID
339
+ * @returns {String } Legacy Challenge ID of the given challengeId
340
+ */
341
+ function * getLegacyChallengeId ( challengeId ) {
342
+ if ( / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 0 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i. test ( challengeId ) ) {
343
+ const challenge = yield getChallenge ( challengeId )
344
+ if ( _ . get ( challenge , 'legacy.pureV5' ) ) {
345
+ return null
346
+ }
347
+ const legacyId = parseInt ( challenge . legacyId , 10 )
348
+ return legacyId
327
349
}
328
350
return challengeId
329
351
}
@@ -335,47 +357,21 @@ function * getLegacyChallengeId (challengeId) {
335
357
*/
336
358
function * getV5ChallengeId ( challengeId ) {
337
359
if ( ! ( / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 0 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i. test ( challengeId ) ) ) {
338
- logger . debug ( `${ challengeId } detected as legacy challenge id. Fetching legacy challenge id` )
339
- const token = yield getM2Mtoken ( )
340
- try {
341
- const response = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } ?legacyId=${ challengeId } ` )
342
- . set ( 'Authorization' , `Bearer ${ token } ` )
343
- . set ( 'Content-Type' , 'application/json' )
344
- const v5Uuid = _ . get ( response , 'body[0].id' )
345
- logger . debug ( `V5 challenge id is ${ v5Uuid } for legacy challenge id ${ challengeId } ` )
346
- return v5Uuid
347
- } catch ( err ) {
348
- logger . error ( `Error while accessing ${ config . CHALLENGEAPI_V5_URL } ?legacyId=${ challengeId } ` )
349
- throw err
350
- }
360
+ const challenge = yield getChallenge ( challengeId )
361
+ return challenge . id
351
362
}
352
363
return challengeId
353
364
}
354
365
355
- /*
366
+ /**
356
367
* Get submission phase ID of a challenge from Challenge API
357
- * @param challengeId Challenge ID
368
+ * @param challenge Challenge
358
369
* @returns {Integer } Submission phase ID of the given challengeId
359
370
*/
360
- function * getSubmissionPhaseId ( challengeId ) {
371
+ function getSubmissionPhaseId ( challenge ) {
361
372
let phaseId = null
362
- let response
363
- challengeId = yield getV5ChallengeId ( challengeId )
364
-
365
- try {
366
- logger . info ( `Calling to challenge API to find submission phase Id for ${ challengeId } ` )
367
- const token = yield getM2Mtoken ( )
368
- response = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
369
- . set ( 'Authorization' , `Bearer ${ token } ` )
370
- . set ( 'Content-Type' , 'application/json' )
371
- logger . info ( `returned from finding submission phase Id for ${ challengeId } ` )
372
- } catch ( ex ) {
373
- logger . error ( `Error while accessing ${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
374
- logger . debug ( 'Setting submissionPhaseId to Null' )
375
- response = null
376
- }
377
- if ( response ) {
378
- const phases = _ . get ( response . body , 'phases' , [ ] )
373
+ if ( challenge ) {
374
+ const phases = _ . get ( challenge , 'phases' , [ ] )
379
375
const checkPoint = _ . filter ( phases , { name : 'Checkpoint Submission' , isOpen : true } )
380
376
const submissionPh = _ . filter ( phases , { name : 'Submission' , isOpen : true } )
381
377
const finalFixPh = _ . filter ( phases , { name : 'Final Fix' , isOpen : true } )
@@ -393,14 +389,14 @@ function * getSubmissionPhaseId (challengeId) {
393
389
return phaseId
394
390
}
395
391
396
- /*
392
+ /**
397
393
* Function to check user access to create a submission
398
394
* @param authUser Authenticated user
399
395
* @param subEntity Submission Entity
396
+ * @param challengeDetails Challenge
400
397
* @returns {Promise }
401
398
*/
402
- function * checkCreateAccess ( authUser , subEntity ) {
403
- let challengeDetails
399
+ function * checkCreateAccess ( authUser , subEntity , challengeDetails ) {
404
400
let resources
405
401
406
402
const challengeId = yield getV5ChallengeId ( subEntity . challengeId )
@@ -412,18 +408,6 @@ function * checkCreateAccess (authUser, subEntity) {
412
408
413
409
const token = yield getM2Mtoken ( )
414
410
415
- try {
416
- logger . info ( `Calling to challenge API for fetch phases and winners for ${ challengeId } ` )
417
- challengeDetails = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
418
- . set ( 'Authorization' , `Bearer ${ token } ` )
419
- . set ( 'Content-Type' , 'application/json' )
420
- logger . info ( `returned for ${ challengeId } with ${ JSON . stringify ( challengeDetails ) } ` )
421
- } catch ( ex ) {
422
- logger . error ( `Error while accessing ${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
423
- logger . error ( ex )
424
- throw new errors . HttpStatusError ( 503 , `Could not fetch details of challenge with id ${ challengeId } ` )
425
- }
426
-
427
411
try {
428
412
resources = yield request . get ( `${ config . RESOURCEAPI_V5_BASE_URL } /resources?challengeId=${ challengeId } ` )
429
413
. set ( 'Authorization' , `Bearer ${ token } ` )
@@ -451,7 +435,7 @@ function * checkCreateAccess (authUser, subEntity) {
451
435
} )
452
436
453
437
// Get phases and winner detail from challengeDetails
454
- const phases = challengeDetails . body . phases
438
+ const { phases } = challengeDetails
455
439
456
440
// Check if the User is assigned as the reviewer for the contest
457
441
const reviewers = _ . filter ( currUserRoles , { role : 'Reviewer' } )
@@ -471,7 +455,7 @@ function * checkCreateAccess (authUser, subEntity) {
471
455
throw new errors . HttpStatusError ( 403 , `Register for the contest before you can submit` )
472
456
}
473
457
474
- const submissionPhaseId = yield getSubmissionPhaseId ( subEntity . challengeId )
458
+ const submissionPhaseId = yield getSubmissionPhaseId ( challengeDetails )
475
459
476
460
if ( submissionPhaseId == null ) {
477
461
throw new errors . HttpStatusError ( 403 , 'You cannot create a submission in the current phase' )
@@ -912,6 +896,7 @@ module.exports = {
912
896
cleanseReviews,
913
897
getRoleIdToRoleNameMap,
914
898
getV5ChallengeId,
899
+ getChallenge,
915
900
adjustSubmissionChallengeId,
916
901
getLatestChallenges,
917
902
getLegacyScoreCardId
0 commit comments