Skip to content

Create Challenge : Validate Group Ids are valid or not #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,24 @@ searchChallenges.schema = {
})
.unknown(true),
};
/**
* Validate Challenge groups.
* @param {Object} groups the group of a challenge
*/
async function validateGroups(groups) {
const promises = [];
_.each(groups, (g) => {
promises.push(
(async () => {
const group = await helper.getGroupById(g);
if (!group || group.status !== "active") {
throw new errors.BadRequestError("The groups provided are invalid " + g);
}
})()
);
});
await Promise.all(promises);
}

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

//Validate the groups if Valid or Not
if (
challenge.groups &&
challenge.groups.length > 0 &&
(currentUser.isMachine || hasAdminRole(currentUser))
) {
await validateGroups(challenge.groups);
}

if (challenge.legacy.selfService) {
// if self-service, create a new project (what about if projectId is provided in the payload? confirm with business!)
if (!challenge.projectId) {
Expand Down Expand Up @@ -1443,6 +1470,15 @@ async function updateChallenge(currentUser, challengeId, data) {

await validateChallengeUpdateRequest(currentUser, challenge, data);

//Validate the groups if Valid or Not
if (
data.groups &&
data.groups.length > 0 &&
(currentUser.isMachine || hasAdminRole(currentUser))
) {
await validateGroups(data.groups);
}

let sendActivationEmail = false;
let sendSubmittedEmail = false;
let sendCompletedEmail = false;
Expand Down