@@ -301,29 +301,51 @@ function * getM2Mtoken () {
301301}
302302
303303/**
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 }
307307 */
308- function * getLegacyChallengeId ( challengeId ) {
308+ function * getChallenge ( challengeId ) {
309309 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 ) ) {
310310 logger . debug ( `${ challengeId } detected as uuid. Fetching legacy challenge id` )
311311 const token = yield getM2Mtoken ( )
312312 try {
313313 const response = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
314314 . set ( 'Authorization' , `Bearer ${ token } ` )
315315 . 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
323317 } catch ( err ) {
324318 logger . error ( `Error while accessing ${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
325319 throw err
326320 }
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
327349 }
328350 return challengeId
329351}
@@ -335,47 +357,21 @@ function * getLegacyChallengeId (challengeId) {
335357 */
336358function * getV5ChallengeId ( challengeId ) {
337359 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
351362 }
352363 return challengeId
353364}
354365
355- /*
366+ /**
356367 * Get submission phase ID of a challenge from Challenge API
357- * @param challengeId Challenge ID
368+ * @param challenge Challenge
358369 * @returns {Integer } Submission phase ID of the given challengeId
359370 */
360- function * getSubmissionPhaseId ( challengeId ) {
371+ function getSubmissionPhaseId ( challenge ) {
361372 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' , [ ] )
379375 const checkPoint = _ . filter ( phases , { name : 'Checkpoint Submission' , isOpen : true } )
380376 const submissionPh = _ . filter ( phases , { name : 'Submission' , isOpen : true } )
381377 const finalFixPh = _ . filter ( phases , { name : 'Final Fix' , isOpen : true } )
@@ -393,14 +389,14 @@ function * getSubmissionPhaseId (challengeId) {
393389 return phaseId
394390}
395391
396- /*
392+ /**
397393 * Function to check user access to create a submission
398394 * @param authUser Authenticated user
399395 * @param subEntity Submission Entity
396+ * @param challengeDetails Challenge
400397 * @returns {Promise }
401398 */
402- function * checkCreateAccess ( authUser , subEntity ) {
403- let challengeDetails
399+ function * checkCreateAccess ( authUser , subEntity , challengeDetails ) {
404400 let resources
405401
406402 const challengeId = yield getV5ChallengeId ( subEntity . challengeId )
@@ -412,18 +408,6 @@ function * checkCreateAccess (authUser, subEntity) {
412408
413409 const token = yield getM2Mtoken ( )
414410
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-
427411 try {
428412 resources = yield request . get ( `${ config . RESOURCEAPI_V5_BASE_URL } /resources?challengeId=${ challengeId } ` )
429413 . set ( 'Authorization' , `Bearer ${ token } ` )
@@ -451,7 +435,7 @@ function * checkCreateAccess (authUser, subEntity) {
451435 } )
452436
453437 // Get phases and winner detail from challengeDetails
454- const phases = challengeDetails . body . phases
438+ const { phases } = challengeDetails
455439
456440 // Check if the User is assigned as the reviewer for the contest
457441 const reviewers = _ . filter ( currUserRoles , { role : 'Reviewer' } )
@@ -471,7 +455,7 @@ function * checkCreateAccess (authUser, subEntity) {
471455 throw new errors . HttpStatusError ( 403 , `Register for the contest before you can submit` )
472456 }
473457
474- const submissionPhaseId = yield getSubmissionPhaseId ( subEntity . challengeId )
458+ const submissionPhaseId = yield getSubmissionPhaseId ( challengeDetails )
475459
476460 if ( submissionPhaseId == null ) {
477461 throw new errors . HttpStatusError ( 403 , 'You cannot create a submission in the current phase' )
@@ -912,6 +896,7 @@ module.exports = {
912896 cleanseReviews,
913897 getRoleIdToRoleNameMap,
914898 getV5ChallengeId,
899+ getChallenge,
915900 adjustSubmissionChallengeId,
916901 getLatestChallenges,
917902 getLegacyScoreCardId
0 commit comments