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