Skip to content

Commit ec5f00c

Browse files
committed
Moving code to challenge-helper
1 parent 3357c68 commit ec5f00c

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

src/common/challenge-helper.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ class ChallengeHelper {
8080
}
8181
}
8282

83+
/**
84+
* Validate Challenge groups.
85+
* @param {Object} groups the group of a challenge
86+
*/
87+
async validateGroups(groups) {
88+
const promises = [];
89+
_.each(groups, (g) => {
90+
promises.push(
91+
(async () => {
92+
const group = await helper.getGroupById(g);
93+
if (!group || group.status !== "active") {
94+
throw new errors.BadRequestError("The groups provided are invalid " + g);
95+
}
96+
})()
97+
);
98+
});
99+
await Promise.all(promises);
100+
}
101+
83102
async validateCreateChallengeRequest(currentUser, challenge) {
84103
// projectId is required for non self-service challenges
85104
if (challenge.legacy.selfService == null && challenge.projectId == null) {
@@ -98,7 +117,13 @@ class ChallengeHelper {
98117
// helper.ensureNoDuplicateOrNullElements(challenge.events, 'events')
99118

100119
// check groups authorization
101-
await helper.ensureAccessibleByGroupsAccess(currentUser, challenge);
120+
if (challenge.groups && challenge.groups.length > 0) {
121+
if (currentUser.isMachine || hasAdminRole(currentUser)) {
122+
await this.validateGroups(challenge.groups);
123+
} else {
124+
await helper.ensureAccessibleByGroupsAccess(currentUser, challenge);
125+
}
126+
}
102127

103128
if (challenge.constraints) {
104129
await ChallengeHelper.validateChallengeConstraints(challenge.constraints);
@@ -118,10 +143,13 @@ class ChallengeHelper {
118143
}
119144

120145
// check groups access to be updated group values
121-
if (data.groups) {
122-
await ensureAcessibilityToModifiedGroups(currentUser, data, challenge);
146+
if (data.groups && data.groups.length > 0) {
147+
if (currentUser.isMachine || hasAdminRole(currentUser)) {
148+
await this.validateGroups(data.groups);
149+
} else {
150+
await ensureAcessibilityToModifiedGroups(currentUser, data, challenge);
151+
}
123152
}
124-
125153
// Ensure descriptionFormat is either 'markdown' or 'html'
126154
if (data.descriptionFormat && !_.includes(["markdown", "html"], data.descriptionFormat)) {
127155
throw new errors.BadRequestError(

src/services/ChallengeService.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -910,24 +910,6 @@ 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-
}
931913

932914
/**
933915
* Create challenge.
@@ -939,15 +921,6 @@ async function validateGroups(groups) {
939921
async function createChallenge(currentUser, challenge, userToken) {
940922
await challengeHelper.validateCreateChallengeRequest(currentUser, challenge);
941923

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

14711444
await validateChallengeUpdateRequest(currentUser, challenge, data);
14721445

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-
14821446
let sendActivationEmail = false;
14831447
let sendSubmittedEmail = false;
14841448
let sendCompletedEmail = false;

0 commit comments

Comments
 (0)