@@ -1364,10 +1364,9 @@ function isDifferentPrizeSets(prizeSets = [], otherPrizeSets = []) {
1364
1364
/**
1365
1365
* Validate the winners array.
1366
1366
* @param {Array } winners the Winner Array
1367
- * @param {String } winchallengeIdners the challenge ID
1367
+ * @param {Array } challengeResources the challenge resources
1368
1368
*/
1369
- async function validateWinners ( winners , challengeId ) {
1370
- const challengeResources = await helper . getChallengeResources ( challengeId ) ;
1369
+ async function validateWinners ( winners , challengeResources ) {
1371
1370
const registrants = _ . filter ( challengeResources , ( r ) => r . roleId === config . SUBMITTER_ROLE_ID ) ;
1372
1371
for ( const prizeType of _ . values ( constants . prizeSetTypes ) ) {
1373
1372
const filteredWinners = _ . filter ( winners , ( w ) => w . type === prizeType ) ;
@@ -1410,6 +1409,48 @@ async function validateWinners(winners, challengeId) {
1410
1409
}
1411
1410
}
1412
1411
1412
+ /**
1413
+ * Task shouldn't be launched/completed when it is assigned to the current user self.
1414
+ * E.g: stop copilots from paying themselves, thus copilots will need to contact manager to launch/complete the task.
1415
+ * @param {Object } currentUser the user who perform operation
1416
+ * @param {Object } challenge the existing challenge
1417
+ * @param {Object } data the new input challenge data
1418
+ * @param {Array } challengeResources the challenge resources
1419
+ */
1420
+ function validateTaskSelfAssign ( currentUser , challenge , data , challengeResources ) {
1421
+ if ( currentUser . isMachine ) {
1422
+ return ;
1423
+ }
1424
+
1425
+ const finalStatus = data . status || challenge . status ;
1426
+
1427
+ // Only validate when launch/complete a task
1428
+ const isLaunchCompleteTask =
1429
+ _ . get ( challenge , "legacy.pureV5Task" ) &&
1430
+ ( finalStatus === constants . challengeStatuses . Active ||
1431
+ finalStatus === constants . challengeStatuses . Completed ) ;
1432
+ if ( ! isLaunchCompleteTask ) {
1433
+ return ;
1434
+ }
1435
+
1436
+ // Whether task is assigned to current user
1437
+ const assignedToCurrentUser =
1438
+ _ . filter (
1439
+ challengeResources ,
1440
+ ( r ) =>
1441
+ r . roleId === config . SUBMITTER_ROLE_ID &&
1442
+ _ . toString ( r . memberId ) === _ . toString ( currentUser . userId )
1443
+ ) . length > 0 ;
1444
+
1445
+ if ( assignedToCurrentUser ) {
1446
+ throw new errors . ForbiddenError (
1447
+ `You are not allowed to ${
1448
+ finalStatus === constants . challengeStatuses . Active ? "lanuch" : "complete"
1449
+ } task assigned to yourself. Please contact manager to operate.`
1450
+ ) ;
1451
+ }
1452
+ }
1453
+
1413
1454
/**
1414
1455
* Update challenge.
1415
1456
* @param {Object } currentUser the user who perform operation
@@ -1441,7 +1482,10 @@ async function updateChallenge(currentUser, challengeId, data) {
1441
1482
data = sanitizeData ( sanitizeChallenge ( data ) , challenge ) ;
1442
1483
logger . debug ( `Sanitized Data: ${ JSON . stringify ( data ) } ` ) ;
1443
1484
1444
- await validateChallengeUpdateRequest ( currentUser , challenge , data ) ;
1485
+ const challengeResources = await helper . getChallengeResources ( challengeId ) ;
1486
+
1487
+ await validateChallengeUpdateRequest ( currentUser , challenge , data , challengeResources ) ;
1488
+ validateTaskSelfAssign ( currentUser , challenge , data , challengeResources ) ;
1445
1489
1446
1490
let sendActivationEmail = false ;
1447
1491
let sendSubmittedEmail = false ;
@@ -1716,7 +1760,7 @@ async function updateChallenge(currentUser, challengeId, data) {
1716
1760
}
1717
1761
1718
1762
if ( data . winners && data . winners . length && data . winners . length > 0 ) {
1719
- await validateWinners ( data . winners , challengeId ) ;
1763
+ await validateWinners ( data . winners , challengeResources ) ;
1720
1764
}
1721
1765
1722
1766
// Only m2m tokens are allowed to modify the `task.*` information on a challenge
@@ -1777,7 +1821,6 @@ async function updateChallenge(currentUser, challengeId, data) {
1777
1821
1778
1822
if ( _ . get ( type , "isTask" ) ) {
1779
1823
if ( ! _ . isEmpty ( _ . get ( data , "task.memberId" ) ) ) {
1780
- const challengeResources = await helper . getChallengeResources ( challengeId ) ;
1781
1824
const registrants = _ . filter (
1782
1825
challengeResources ,
1783
1826
( r ) => r . roleId === config . SUBMITTER_ROLE_ID
0 commit comments