Skip to content

Commit d267b37

Browse files
refactor for performance improvements
1 parent c57f9d2 commit d267b37

File tree

2 files changed

+41
-45
lines changed

2 files changed

+41
-45
lines changed

src/common/helper.js

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -301,73 +301,66 @@ 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-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-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
}
327-
}
328-
return challengeId
329-
}
330-
331-
/**
332-
* Get v5 challenge id (uuid) if legacy challenge id
333-
* @param {Integer} challengeId Challenge ID
334-
* @returns {String} v5 uuid Challenge ID of the given challengeId
335-
*/
336-
function * getV5ChallengeId (challengeId) {
337-
if (!(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(challengeId))) {
321+
} else {
338322
logger.debug(`${challengeId} detected as legacy challenge id. Fetching legacy challenge id`)
339323
const token = yield getM2Mtoken()
340324
try {
341325
const response = yield request.get(`${config.CHALLENGEAPI_V5_URL}?legacyId=${challengeId}`)
342326
.set('Authorization', `Bearer ${token}`)
343327
.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
328+
return response.body[0]
347329
} catch (err) {
348330
logger.error(`Error while accessing ${config.CHALLENGEAPI_V5_URL}?legacyId=${challengeId}`)
349331
throw err
350332
}
351333
}
352-
return challengeId
353334
}
354335

355336
/**
356-
* Get challenge details from Challenge API
337+
* Get legacy challenge id if the challenge id is uuid form
357338
* @param {String} challengeId Challenge ID
358-
* @returns {Object} Challenge details
339+
* @returns {String} Legacy Challenge ID of the given challengeId
359340
*/
360-
function * getChallenge (challengeId) {
361-
try {
362-
const token = yield getM2Mtoken()
363-
const response = yield request.get(`${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
364-
.set('Authorization', `Bearer ${token}`)
365-
.set('Content-Type', 'application/json')
366-
return response.body
367-
} catch (e) {
368-
logger.error(`Error while accessing ${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
369-
throw e
341+
function * getLegacyChallengeId (challengeId) {
342+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-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
370349
}
350+
return challengeId
351+
}
352+
353+
/**
354+
* Get v5 challenge id (uuid) if legacy challenge id
355+
* @param {Integer} challengeId Challenge ID
356+
* @returns {String} v5 uuid Challenge ID of the given challengeId
357+
*/
358+
function * getV5ChallengeId (challengeId) {
359+
if (!(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(challengeId))) {
360+
const challenge = yield getChallenge(challengeId)
361+
return challenge.id
362+
}
363+
return challengeId
371364
}
372365

373366
/*
@@ -930,8 +923,8 @@ module.exports = {
930923
cleanseReviews,
931924
getRoleIdToRoleNameMap,
932925
getV5ChallengeId,
926+
getChallenge,
933927
adjustSubmissionChallengeId,
934928
getLatestChallenges,
935-
getLegacyScoreCardId,
936-
getChallenge
929+
getLegacyScoreCardId
937930
}

src/services/SubmissionService.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,15 @@ function * createSubmission (authUser, files, entity) {
281281

282282
// Submission api only works with legacy challenge id
283283
// If it is a v5 challenge id, get the associated legacy challenge id
284-
const challengeId = yield helper.getV5ChallengeId(entity.challengeId)
285-
const legacyChallengeId = yield helper.getLegacyChallengeId(entity.challengeId)
284+
const {
285+
id: challengeId,
286+
status,
287+
phases,
288+
legacyId: legacyChallengeId
289+
} = yield helper.getChallenge(entity.challengeId)
286290
const currDate = (new Date()).toISOString()
287-
const challenge = yield helper.getChallenge(challengeId)
288291

289-
if (challenge.status !== 'Active') {
292+
if (status !== 'Active') {
290293
throw new errors.HttpStatusError(400, 'Challenge is not active')
291294
}
292295

@@ -323,7 +326,7 @@ function * createSubmission (authUser, files, entity) {
323326

324327
if (item.submissionPhaseId) {
325328
// make sure the phase is open
326-
const openPhase = _.find(challenge.phases, { phaseId: item.submissionPhaseId, isOpen: true })
329+
const openPhase = _.find(phases, { phaseId: item.submissionPhaseId, isOpen: true })
327330
if (!openPhase) {
328331
throw new errors.HttpStatusError(400, `The phase ${item.submissionPhaseId} is not open`)
329332
}

0 commit comments

Comments
 (0)