@@ -1421,37 +1421,44 @@ async function validateWinners(winners, challengeResources) {
1421
1421
* @param {Object } data the new input challenge data
1422
1422
* @param {Array } challengeResources the challenge resources
1423
1423
*/
1424
- function validateTaskSelfAssign ( currentUser , challenge , data , challengeResources ) {
1425
- if ( currentUser . isMachine ) {
1424
+ function validateTask ( currentUser , challenge , data , challengeResources ) {
1425
+ if ( ! _ . get ( challenge , "legacy.pureV5Task" ) ) {
1426
+ // Not a Task
1426
1427
return ;
1427
1428
}
1428
1429
1429
- const finalStatus = data . status || challenge . status ;
1430
+ // Status changed to Active, indicating launch a Task
1431
+ const isLaunchTask =
1432
+ data . status === constants . challengeStatuses . Active &&
1433
+ challenge . status !== constants . challengeStatuses . Active ;
1430
1434
1431
- // Only validate when launch/complete a task
1432
- const isLaunchCompleteTask =
1433
- _ . get ( challenge , "legacy.pureV5Task" ) &&
1434
- ( finalStatus === constants . challengeStatuses . Active ||
1435
- finalStatus === constants . challengeStatuses . Completed ) ;
1436
- if ( ! isLaunchCompleteTask ) {
1437
- return ;
1435
+ // Status changed to Completed, indicating complete a Task
1436
+ const isCompleteTask =
1437
+ data . status === constants . challengeStatuses . Completed &&
1438
+ challenge . status !== constants . challengeStatuses . Completed ;
1439
+
1440
+ // When complete a Task, input data should have winners
1441
+ if ( isCompleteTask && ( ! data . winners || ! data . winners . length ) ) {
1442
+ throw new errors . BadRequestError ( "The winners is required to complete a Task" ) ;
1438
1443
}
1439
1444
1440
- // Whether task is assigned to current user
1441
- const assignedToCurrentUser =
1442
- _ . filter (
1443
- challengeResources ,
1444
- ( r ) =>
1445
- r . roleId === config . SUBMITTER_ROLE_ID &&
1446
- _ . toString ( r . memberId ) === _ . toString ( currentUser . userId )
1447
- ) . length > 0 ;
1448
-
1449
- if ( assignedToCurrentUser ) {
1450
- throw new errors . ForbiddenError (
1451
- `You are not allowed to ${
1452
- finalStatus === constants . challengeStatuses . Active ? "lanuch" : "complete"
1453
- } task assigned to yourself. Please contact manager to operate.`
1454
- ) ;
1445
+ if ( ! currentUser . isMachine && ( isLaunchTask || isCompleteTask ) ) {
1446
+ // Whether task is assigned to current user
1447
+ const assignedToCurrentUser =
1448
+ _ . filter (
1449
+ challengeResources ,
1450
+ ( r ) =>
1451
+ r . roleId === config . SUBMITTER_ROLE_ID &&
1452
+ _ . toString ( r . memberId ) === _ . toString ( currentUser . userId )
1453
+ ) . length > 0 ;
1454
+
1455
+ if ( assignedToCurrentUser ) {
1456
+ throw new errors . ForbiddenError (
1457
+ `You are not allowed to ${
1458
+ data . status === constants . challengeStatuses . Active ? "lanuch" : "complete"
1459
+ } task assigned to yourself. Please contact manager to operate.`
1460
+ ) ;
1461
+ }
1455
1462
}
1456
1463
}
1457
1464
@@ -1489,7 +1496,7 @@ async function updateChallenge(currentUser, challengeId, data) {
1489
1496
const challengeResources = await helper . getChallengeResources ( challengeId ) ;
1490
1497
1491
1498
await validateChallengeUpdateRequest ( currentUser , challenge , data , challengeResources ) ;
1492
- validateTaskSelfAssign ( currentUser , challenge , data , challengeResources ) ;
1499
+ validateTask ( currentUser , challenge , data , challengeResources ) ;
1493
1500
1494
1501
let sendActivationEmail = false ;
1495
1502
let sendSubmittedEmail = false ;
0 commit comments