@@ -268,35 +268,35 @@ async function getTeamJob (currentUser, id, jobId) {
268268 )
269269 }
270270
271+ // If the job has candidates, the following data for each candidate would be populated:
272+ //
273+ // - the `status`, `resume`, `userId` and `id` of the candidate
274+ // - the `handle`, `firstName` `lastName` and `skills` of the user(from GET /users/:userId) for the candidate
275+ // - the `photoURL` of the member(from GET /members) for the candidate
276+ //
271277 if ( job && job . candidates && job . candidates . length > 0 ) {
272- const usersPromises = [ ]
273- _ . map ( job . candidates , ( candidate ) => { usersPromises . push ( helper . getUserById ( candidate . userId , true ) ) } )
274- const candidates = await Promise . all ( usersPromises )
275-
276- const userHandles = _ . map ( candidates , 'handle' )
277- if ( userHandles && userHandles . length > 0 ) {
278- // Get user photo from /v5/members
279- const members = await helper . getMembers ( userHandles )
280-
281- for ( const item of candidates ) {
282- const candidate = _ . find ( job . candidates , { userId : item . id } )
283- // TODO this logic should be vice-verse, we should loop trough candidates and populate users data if found,
284- // not loop through users and populate candidates data if found
285- if ( candidate ) {
286- item . resume = candidate . resume
287- item . status = candidate . status
288- // return User id as `userId` and JobCandidate id as `id`
289- item . userId = item . id
290- item . id = candidate . id
291- }
292- const findMember = _ . find ( members , { handleLower : item . handle . toLowerCase ( ) } )
293- if ( findMember && findMember . photoURL ) {
294- item . photo_url = findMember . photoURL
295- }
278+ // find user data for candidates
279+ const users = await Promise . all (
280+ _ . map ( _ . uniq ( _ . map ( job . candidates , 'userId' ) ) , userId => helper . getUserById ( userId , true ) )
281+ )
282+ const userMap = _ . groupBy ( users , 'id' )
283+
284+ // find photo URLs for users
285+ const members = await helper . getMembers ( _ . map ( users , 'handle' ) )
286+ const photoURLMap = _ . groupBy ( members , 'handleLower' )
287+
288+ result . candidates = _ . map ( job . candidates , candidate => {
289+ const candidateData = _ . pick ( candidate , [ 'status' , 'resume' , 'userId' , 'id' ] )
290+ const userData = userMap [ candidate . userId ] [ 0 ]
291+ // attach user data to the candidate
292+ Object . assign ( candidateData , _ . pick ( userData , [ 'handle' , 'firstName' , 'lastName' , 'skills' ] ) )
293+ // attach photo URL to the candidate
294+ const handleLower = userData . handle . toLowerCase ( )
295+ if ( photoURLMap [ handleLower ] ) {
296+ candidateData . photo_url = photoURLMap [ handleLower ] [ 0 ] . photoURL
296297 }
297- }
298-
299- result . candidates = candidates
298+ return candidateData
299+ } )
300300 }
301301
302302 return result
0 commit comments