Skip to content

Commit 3357c68

Browse files
committed
Creating function for validate groups by getGroupById
1 parent f976de1 commit 3357c68

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

src/common/helper.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -898,27 +898,7 @@ async function _filterChallengesByGroupsAccess(currentUser, challenges) {
898898
const needToCheckForGroupAccess = !currentUser
899899
? true
900900
: !currentUser.isMachine && !hasAdminRole(currentUser);
901-
if(!needToCheckForGroupAccess)
902-
{
903-
for (const challenge of challenges) {
904-
if(challenge && challenge.groups && challenge.groups.length>0) {
905-
const promises = [];
906-
_.each(challenge.groups, (g) => {
907-
promises.push(
908-
(async () => {
909-
const group = await getGroupById(g);
910-
if ( !group || !group.status==='active') {
911-
throw new errors.BadRequestError("The groups provided are invalid "+g);
912-
}
913-
})()
914-
);
915-
});
916-
await Promise.all(promises);
917-
res.push(challenge);
918-
}
919-
}
920-
return res;
921-
}
901+
if (!needToCheckForGroupAccess) return challenges;
922902

923903
let userGroups;
924904

src/services/ChallengeService.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,24 @@ searchChallenges.schema = {
910910
})
911911
.unknown(true),
912912
};
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+
}
913931

914932
/**
915933
* Create challenge.
@@ -921,6 +939,15 @@ searchChallenges.schema = {
921939
async function createChallenge(currentUser, challenge, userToken) {
922940
await challengeHelper.validateCreateChallengeRequest(currentUser, challenge);
923941

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+
924951
if (challenge.legacy.selfService) {
925952
// if self-service, create a new project (what about if projectId is provided in the payload? confirm with business!)
926953
if (!challenge.projectId) {
@@ -1443,6 +1470,15 @@ async function updateChallenge(currentUser, challengeId, data) {
14431470

14441471
await validateChallengeUpdateRequest(currentUser, challenge, data);
14451472

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+
14461482
let sendActivationEmail = false;
14471483
let sendSubmittedEmail = false;
14481484
let sendCompletedEmail = false;

0 commit comments

Comments
 (0)