@@ -342,6 +342,29 @@ async function searchChallenges (currentUser, criteria) {
342342 }
343343 }
344344
345+ const accessQuery = [ ]
346+ let memberChallengeIds
347+
348+ // FIXME: This is wrong!
349+ // if (!_.isUndefined(currentUser) && currentUser.handle) {
350+ // accessQuery.push({ match_phrase: { createdBy: currentUser.handle } })
351+ // }
352+
353+ if ( criteria . memberId ) {
354+ // logger.error(`memberId ${criteria.memberId}`)
355+ memberChallengeIds = await helper . listChallengesByMember ( criteria . memberId )
356+ // logger.error(`response ${JSON.stringify(ids)}`)
357+ accessQuery . push ( { terms : { _id : memberChallengeIds } } )
358+ }
359+
360+ if ( accessQuery . length > 0 ) {
361+ mustQuery . push ( {
362+ bool : {
363+ should : accessQuery
364+ }
365+ } )
366+ }
367+
345368 // FIXME: Tech Debt
346369 let excludeTasks = true
347370 // if you're an admin or m2m, security rules wont be applied
@@ -372,6 +395,7 @@ async function searchChallenges (currentUser, criteria) {
372395 mustQuery . push ( {
373396 bool : {
374397 should : [
398+ ...( _ . get ( memberChallengeIds , 'length' , 0 ) > 0 ? { terms : { _id : memberChallengeIds } } : { } ) ,
375399 { bool : { must_not : { exists : { field : 'task.isTask' } } } } ,
376400 { match_phrase : { 'task.isTask' : false } } ,
377401 {
@@ -400,28 +424,6 @@ async function searchChallenges (currentUser, criteria) {
400424 } )
401425 }
402426
403- const accessQuery = [ ]
404-
405- // FIXME: This is wrong!
406- // if (!_.isUndefined(currentUser) && currentUser.handle) {
407- // accessQuery.push({ match_phrase: { createdBy: currentUser.handle } })
408- // }
409-
410- if ( criteria . memberId ) {
411- // logger.error(`memberId ${criteria.memberId}`)
412- const ids = await helper . listChallengesByMember ( criteria . memberId )
413- // logger.error(`response ${JSON.stringify(ids)}`)
414- accessQuery . push ( { terms : { _id : ids } } )
415- }
416-
417- if ( accessQuery . length > 0 ) {
418- mustQuery . push ( {
419- bool : {
420- should : accessQuery
421- }
422- } )
423- }
424-
425427 if ( boolQuery . length > 0 ) {
426428 mustQuery . push ( {
427429 bool : {
@@ -722,6 +724,12 @@ async function createChallenge (currentUser, challenge, userToken) {
722724 const { track, type } = await validateChallengeData ( challenge )
723725 if ( _ . get ( type , 'isTask' ) ) {
724726 _ . set ( challenge , 'task.isTask' , true )
727+ if ( _ . isUndefined ( _ . get ( challenge , 'task.isAssigned' ) ) ) {
728+ _ . set ( challenge , 'task.isAssigned' , false )
729+ }
730+ if ( _ . isUndefined ( _ . get ( challenge , 'task.memberId' ) ) ) {
731+ _ . set ( challenge , 'task.memberId' , null )
732+ }
725733 }
726734 if ( challenge . phases && challenge . phases . length > 0 ) {
727735 await helper . validatePhases ( challenge . phases )
0 commit comments