Skip to content

Commit 9149397

Browse files
Validate winners
1 parent 4825070 commit 9149397

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

config/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module.exports = {
5454
// copilot resource role ids allowed to upload attachment
5555
COPILOT_RESOURCE_ROLE_IDS: process.env.COPILOT_RESOURCE_ROLE_IDS
5656
? process.env.COPILOT_RESOURCE_ROLE_IDS.split(',') : ['10ba038e-48da-487b-96e8-8d3b99b6d18b'],
57+
SUBMITTER_ROLE_ID: process.env.SUBMITTER_ROLE_ID || '732339e7-8e30-49d7-9198-cccf9451e221',
5758

5859
// health check timeout in milliseconds
5960
HEALTH_CHECK_TIMEOUT: process.env.HEALTH_CHECK_TIMEOUT || 3000,

src/services/ChallengeService.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,15 @@ function isDifferentPrizeSets (prizeSets = [], otherPrizeSets) {
716716
/**
717717
* Validate the winners array.
718718
* @param {Array} winners the Winner Array
719+
* @param {String} winchallengeIdners the challenge ID
719720
*/
720-
function validateWinners (winners) {
721+
async function validateWinners (winners, challengeId) {
722+
const challengeResources = await helper.getChallengeResources(challengeId)
723+
const registrants = _.filter(challengeResources, r => r.roleId === config.SUBMITTER_ROLE_ID)
721724
for (const winner of winners) {
725+
if (!_.find(registrants, r => _.toString(r.memberId) === _.toString(winner.userId))) {
726+
throw new errors.BadRequestError(`Member with userId: ${winner.userId} is not registered on the challenge`)
727+
}
722728
const diffWinners = _.differenceWith(winners, [winner], _.isEqual)
723729
if (diffWinners.length + 1 !== winners.length) {
724730
throw new errors.BadRequestError(`Duplicate member with placement: ${helper.toString(winner)}`)
@@ -857,7 +863,7 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
857863
}
858864

859865
if (data.winners && data.winners.length) {
860-
await validateWinners(data.winners)
866+
await validateWinners(data.winners, challengeId)
861867
}
862868

863869
data.updated = moment().utc()

0 commit comments

Comments
 (0)