@@ -910,6 +910,24 @@ searchChallenges.schema = {
910
910
} )
911
911
. unknown ( true ) ,
912
912
} ;
913
+ /**
914
+ * Validate Challenge groups.
915
+ * @param {Object } groups the group of a challenge
916
+ */
917
+ async function validateGroups ( groups ) {
918
+ const promises = [ ] ;
919
+ _ . each ( groups , ( g ) => {
920
+ promises . push (
921
+ ( async ( ) => {
922
+ const group = await helper . getGroupById ( g ) ;
923
+ if ( ! group || group . status !== "active" ) {
924
+ throw new errors . BadRequestError ( "The groups provided are invalid " + g ) ;
925
+ }
926
+ } ) ( )
927
+ ) ;
928
+ } ) ;
929
+ await Promise . all ( promises ) ;
930
+ }
913
931
914
932
/**
915
933
* Create challenge.
@@ -921,6 +939,15 @@ searchChallenges.schema = {
921
939
async function createChallenge ( currentUser , challenge , userToken ) {
922
940
await challengeHelper . validateCreateChallengeRequest ( currentUser , challenge ) ;
923
941
942
+ //Validate the groups if Valid or Not
943
+ if (
944
+ challenge . groups &&
945
+ challenge . groups . length > 0 &&
946
+ ( currentUser . isMachine || hasAdminRole ( currentUser ) )
947
+ ) {
948
+ await validateGroups ( challenge . groups ) ;
949
+ }
950
+
924
951
if ( challenge . legacy . selfService ) {
925
952
// if self-service, create a new project (what about if projectId is provided in the payload? confirm with business!)
926
953
if ( ! challenge . projectId ) {
@@ -1443,6 +1470,15 @@ async function updateChallenge(currentUser, challengeId, data) {
1443
1470
1444
1471
await validateChallengeUpdateRequest ( currentUser , challenge , data ) ;
1445
1472
1473
+ //Validate the groups if Valid or Not
1474
+ if (
1475
+ data . groups &&
1476
+ data . groups . length > 0 &&
1477
+ ( currentUser . isMachine || hasAdminRole ( currentUser ) )
1478
+ ) {
1479
+ await validateGroups ( data . groups ) ;
1480
+ }
1481
+
1446
1482
let sendActivationEmail = false ;
1447
1483
let sendSubmittedEmail = false ;
1448
1484
let sendCompletedEmail = false ;
0 commit comments