@@ -118,6 +118,52 @@ async function ensureAcessibilityToModifiedGroups (currentUser, data, challenge)
118118 }
119119}
120120
121+ /**
122+ * Search challenges by legacyId
123+ * @param {Object } currentUser the user who perform operation
124+ * @param {Number } legacyId the legacyId
125+ * @param {Number } page the page
126+ * @param {Number } perPage the perPage
127+ * @returns {Array } the search result
128+ */
129+ async function searchByLegacyId ( currentUser , legacyId , page , perPage ) {
130+ const esQuery = {
131+ index : config . get ( 'ES.ES_INDEX' ) ,
132+ type : config . get ( 'ES.ES_TYPE' ) ,
133+ size : perPage ,
134+ from : ( page - 1 ) * perPage ,
135+ body : {
136+ query : {
137+ term : {
138+ legacyId
139+ }
140+ }
141+ }
142+ }
143+
144+ logger . debug ( `es Query ${ JSON . stringify ( esQuery ) } ` )
145+ let docs
146+ try {
147+ docs = await esClient . search ( esQuery )
148+ } catch ( e ) {
149+ logger . error ( `Query Error from ES ${ JSON . stringify ( e ) } ` )
150+ docs = {
151+ hits : {
152+ hits : [ ]
153+ }
154+ }
155+ }
156+ const ids = _ . map ( docs . hits . hits , item => item . _source . id )
157+ const result = [ ]
158+ for ( const id of ids ) {
159+ try {
160+ const challenge = await getChallenge ( currentUser , id )
161+ result . push ( challenge )
162+ } catch ( e ) { }
163+ }
164+ return result
165+ }
166+
121167/**
122168 * Search challenges
123169 * @param {Object } currentUser the user who perform operation
@@ -129,6 +175,10 @@ async function searchChallenges (currentUser, criteria) {
129175
130176 const page = criteria . page || 1
131177 const perPage = criteria . perPage || 20
178+ if ( ! _ . isUndefined ( criteria . legacyId ) ) {
179+ const result = await searchByLegacyId ( currentUser , criteria . legacyId , page , perPage )
180+ return { total : result . length , page, perPage, result }
181+ }
132182 const boolQuery = [ ]
133183 let sortByScore = false
134184 const matchPhraseKeys = [
@@ -1175,13 +1225,16 @@ async function getChallenge (currentUser, id, checkIfExists) {
11751225 // delete challenge.typeId
11761226
11771227 // Remove privateDescription for unregistered users
1178- let memberChallengeIds
11791228 if ( currentUser ) {
11801229 if ( ! currentUser . isMachine && ! helper . hasAdminRole ( currentUser ) ) {
11811230 _ . unset ( challenge , 'billing' )
1182- memberChallengeIds = await helper . listChallengesByMember ( currentUser . userId )
1183- if ( ! _ . includes ( memberChallengeIds , challenge . id ) ) {
1231+ if ( _ . isEmpty ( challenge . privateDescription ) ) {
11841232 _ . unset ( challenge , 'privateDescription' )
1233+ } else if ( ! _ . get ( challenge , 'task.isTask' , false ) || ! _ . get ( challenge , 'task.isAssigned' , false ) ) {
1234+ const memberResources = await helper . listResourcesByMemberAndChallenge ( currentUser . userId , challenge . id )
1235+ if ( _ . isEmpty ( memberResources ) ) {
1236+ _ . unset ( challenge , 'privateDescription' )
1237+ }
11851238 }
11861239 }
11871240 } else {
0 commit comments