Skip to content

Commit ba90b64

Browse files
check winners by type
1 parent 25a4eac commit ba90b64

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/services/ChallengeService.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,25 +1246,28 @@ function isDifferentPrizeSets (prizeSets = [], otherPrizeSets = []) {
12461246
async function validateWinners (winners, challengeId) {
12471247
const challengeResources = await helper.getChallengeResources(challengeId)
12481248
const registrants = _.filter(challengeResources, r => r.roleId === config.SUBMITTER_ROLE_ID)
1249-
for (const winner of winners) {
1250-
if (!_.find(registrants, r => _.toString(r.memberId) === _.toString(winner.userId))) {
1251-
throw new errors.BadRequestError(`Member with userId: ${winner.userId} is not registered on the challenge`)
1252-
}
1253-
const diffWinners = _.differenceWith(winners, [winner], _.isEqual)
1254-
if (diffWinners.length + 1 !== winners.length) {
1255-
throw new errors.BadRequestError(`Duplicate member with placement: ${helper.toString(winner)}`)
1256-
}
1257-
1258-
// find another member with the placement
1259-
const placementExists = _.find(diffWinners, function (w) { return w.placement === winner.placement })
1260-
if (placementExists && (placementExists.userId !== winner.userId || placementExists.handle !== winner.handle)) {
1261-
throw new errors.BadRequestError(`Only one member can have a placement: ${winner.placement}`)
1262-
}
1263-
1264-
// find another placement for a member
1265-
const memberExists = _.find(diffWinners, function (w) { return w.userId === winner.userId && w.type === winner.type })
1266-
if (memberExists && memberExists.placement !== winner.placement) {
1267-
throw new errors.BadRequestError(`The same member ${winner.userId} cannot have multiple placements`)
1249+
for (const prizeType of constants.prizeSetTypes) {
1250+
const filteredWinners = _.filter(winners, w => w.type === prizeType)
1251+
for (const winner of filteredWinners) {
1252+
if (!_.find(registrants, r => _.toString(r.memberId) === _.toString(winner.userId))) {
1253+
throw new errors.BadRequestError(`Member with userId: ${winner.userId} is not registered on the challenge`)
1254+
}
1255+
const diffWinners = _.differenceWith(filteredWinners, [winner], _.isEqual)
1256+
if (diffWinners.length + 1 !== filteredWinners.length) {
1257+
throw new errors.BadRequestError(`Duplicate member with placement: ${helper.toString(winner)}`)
1258+
}
1259+
1260+
// find another member with the placement
1261+
const placementExists = _.find(diffWinners, function (w) { return w.placement === winner.placement })
1262+
if (placementExists && (placementExists.userId !== winner.userId || placementExists.handle !== winner.handle)) {
1263+
throw new errors.BadRequestError(`Only one member can have a placement: ${winner.placement}`)
1264+
}
1265+
1266+
// find another placement for a member
1267+
const memberExists = _.find(diffWinners, function (w) { return w.userId === winner.userId && w.type === winner.type })
1268+
if (memberExists && memberExists.placement !== winner.placement) {
1269+
throw new errors.BadRequestError(`The same member ${winner.userId} cannot have multiple placements`)
1270+
}
12681271
}
12691272
}
12701273
}

0 commit comments

Comments
 (0)