Skip to content

Commit 656d728

Browse files
Merge pull request #304 from topcoder-platform/develop
Prod release
2 parents a744de7 + fa2f96e commit 656d728

File tree

2 files changed

+67
-65
lines changed

2 files changed

+67
-65
lines changed

src/common/helper.js

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -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-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
}
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-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
327349
}
328350
return challengeId
329351
}
@@ -335,47 +357,21 @@ function * getLegacyChallengeId (challengeId) {
335357
*/
336358
function * getV5ChallengeId (challengeId) {
337359
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))) {
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

src/services/SubmissionService.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,19 @@ 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 challenge = yield helper.getChallenge(entity.challengeId)
285+
const {
286+
id: challengeId,
287+
status,
288+
phases,
289+
legacyId: legacyChallengeId
290+
} = challenge
286291
const currDate = (new Date()).toISOString()
287292

293+
if (status !== 'Active') {
294+
throw new errors.HttpStatusError(400, 'Challenge is not active')
295+
}
296+
288297
const item = {
289298
id: submissionId,
290299
type: entity.type,
@@ -313,7 +322,15 @@ function * createSubmission (authUser, files, entity) {
313322
if (entity.submissionPhaseId) {
314323
item.submissionPhaseId = entity.submissionPhaseId
315324
} else {
316-
item.submissionPhaseId = yield helper.getSubmissionPhaseId(entity.challengeId)
325+
item.submissionPhaseId = helper.getSubmissionPhaseId(challenge)
326+
}
327+
328+
if (item.submissionPhaseId) {
329+
// make sure the phase is open
330+
const openPhase = _.find(phases, { phaseId: item.submissionPhaseId, isOpen: true })
331+
if (!openPhase) {
332+
throw new errors.HttpStatusError(400, `The phase ${item.submissionPhaseId} is not open`)
333+
}
317334
}
318335

319336
if (entity.fileType) {
@@ -325,7 +342,7 @@ function * createSubmission (authUser, files, entity) {
325342
logger.info('Check User access before creating the submission')
326343
if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) {
327344
logger.info(`Calling checkCreateAccess for ${JSON.stringify(authUser)}`)
328-
yield helper.checkCreateAccess(authUser, item)
345+
yield helper.checkCreateAccess(authUser, item, challenge)
329346

330347
if (entity.submittedDate) {
331348
throw new errors.HttpStatusError(403, 'You are not allowed to set the `submittedDate` attribute on a submission')

0 commit comments

Comments
 (0)