Skip to content

Commit 78fe6f3

Browse files
committed
Fix issue 60. Unauthenticated users should only search challenges without groups.
1 parent f5b5a21 commit 78fe6f3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/services/ChallengeService.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function searchChallenges (currentUser, criteria) {
8282
if (criteria.tag) {
8383
boolQuery.push({ match_phrase: { tags: criteria.tag } })
8484
}
85-
if (criteria.group) {
85+
if (criteria.group && !_.isUndefined(currentUser)) {
8686
boolQuery.push({ match_phrase: { groups: criteria.group } })
8787
}
8888
if (criteria.gitRepoURL) {
@@ -121,12 +121,18 @@ async function searchChallenges (currentUser, criteria) {
121121

122122
const mustQuery = []
123123

124+
let mustNotQuery
125+
124126
const accessQuery = []
125127

126128
if (!_.isUndefined(currentUser) && currentUser.handle) {
127129
accessQuery.push({ match_phrase: { createdBy: currentUser.handle } })
128130
}
129131

132+
if (_.isUndefined(currentUser)) {
133+
mustNotQuery = { exists: { field: 'groups' } }
134+
}
135+
130136
if (criteria.memberId) {
131137
const ids = await helper.listChallengesByMember(criteria.memberId)
132138
accessQuery.push({ terms: { _id: ids } })
@@ -154,9 +160,10 @@ async function searchChallenges (currentUser, criteria) {
154160
size: criteria.perPage,
155161
from: (criteria.page - 1) * criteria.perPage, // Es Index starts from 0
156162
body: {
157-
query: mustQuery.length > 0 ? {
163+
query: mustQuery.length > 0 || !_.isUndefined(mustNotQuery) ? {
158164
bool: {
159-
must: mustQuery
165+
must: mustQuery,
166+
must_not: mustNotQuery
160167
}
161168
} : {
162169
match_all: {}

0 commit comments

Comments
 (0)