@@ -32,10 +32,10 @@ controller.createNewTeam = (data, eventId, userId) => {
3232 . getTeamsForEvent ( eventId )
3333 . then ( teams => {
3434 console . log ( 'Step 1' )
35- if ( userHasTeam ( teams , userId ) ) {
35+ if ( userHasTeam ( teams . data , userId ) ) {
3636 throw new ForbiddenError ( 'You are already in a team' )
3737 }
38- return removeCandidateApplications ( teams , userId )
38+ return removeCandidateApplications ( teams . data , userId )
3939 } )
4040 . then ( teamsToSave => {
4141 console . log ( 'Step 2' )
@@ -138,7 +138,6 @@ controller.editTeam = (eventId, userId, edits) => {
138138const userHasTeam = ( teams , userId ) => {
139139 let hasTeam = false
140140 const teamMembers = teams . map ( team => team . members . concat ( team . owner ) )
141- console . log ( 'Team members: ' , teamMembers )
142141 teamMembers . map ( team => {
143142 if ( _ . includes ( team , userId ) ) {
144143 hasTeam = true
@@ -149,9 +148,7 @@ const userHasTeam = (teams, userId) => {
149148}
150149const removeCandidateApplications = ( teams , userId ) => {
151150 const teamsToSave = [ ]
152- console . log ( 'Teams to remove candidate: ' , teams )
153151 teams . map ( team => {
154- console . log ( 'Team candidates: ' , team . candidates )
155152 if (
156153 _ . includes (
157154 team . candidates . map ( candidate => candidate . userId ) ,
@@ -164,7 +161,6 @@ const removeCandidateApplications = (teams, userId) => {
164161 teamsToSave . push ( team )
165162 }
166163 } )
167- console . log ( 'Teams to save after removing candidate: ' , teamsToSave )
168164 return teamsToSave
169165}
170166controller . joinTeam = ( eventId , userId , code ) => {
@@ -178,10 +174,10 @@ controller.joinTeam = (eventId, userId, code) => {
178174 . getTeamsForEvent ( eventId )
179175 . then ( teams => {
180176 console . log ( 'Step 1' )
181- if ( userHasTeam ( teams , userId ) ) {
177+ if ( userHasTeam ( teams . data , userId ) ) {
182178 throw new ForbiddenError ( 'You are already in a team' )
183179 }
184- return teams
180+ return teams . data
185181 } )
186182 . then ( teams => {
187183 console . log ( 'Step 2' )
@@ -219,7 +215,7 @@ controller.joinTeam = (eventId, userId, code) => {
219215 } )
220216 } )
221217}
222-
218+ //TODO: optimize this process, slow with over 200 teams
223219controller . acceptCandidateToTeam = ( eventId , userId , code , candidateId ) => {
224220 let teamToReturn
225221 return controller
@@ -231,19 +227,19 @@ controller.acceptCandidateToTeam = (eventId, userId, code, candidateId) => {
231227 )
232228 }
233229 teamToReturn = team
230+
234231 return controller . getTeamsForEvent ( eventId )
235232 } )
236233 . then ( teams => {
237234 console . log ( 'Step 1' )
238- console . log ( 'Teams: ' , teams )
239- if ( userHasTeam ( teams , candidateId ) ) {
235+ if ( userHasTeam ( teams . data , candidateId ) ) {
240236 teamToReturn . candidates = teamToReturn . candidates . filter (
241237 candidate => candidate . userId !== candidateId ,
242238 )
243239 teamToReturn . save ( )
244240 throw new ForbiddenError ( 'Candidate is already in a team' )
245241 }
246- return teams
242+ return teams . data
247243 } )
248244 . then ( teams => {
249245 console . log ( 'Step 2' )
@@ -485,21 +481,40 @@ controller.attachUserApplicant = (teams, userId) => {
485481 } )
486482}
487483
488- controller . getTeamsForEvent = async ( eventId , userId , page , size ) => {
484+ controller . getTeamsForEvent = async ( eventId , userId , page , size , filter ) => {
489485 if ( page && size ) {
490- const found = await Team . find ( {
491- event : eventId ,
492- } )
493- . sort ( { createdAt : 'desc' } )
494- . skip ( parseInt ( size * page ) )
495- . limit ( parseInt ( size ) )
496- . then ( teams => {
497- if ( userId ) {
498- return controller . attachUserApplicant ( teams , userId )
499- }
486+ console . log ( "filter" , filter )
487+ if ( filter ) {
488+ const found = await Team . find ( {
489+ event : eventId ,
490+ challenge : filter ,
500491 } )
501- const count = await Team . find ( { event : eventId } ) . countDocuments ( )
502- return { data : found , count : count }
492+ . sort ( { createdAt : 'desc' } )
493+ . skip ( parseInt ( size * page ) )
494+ . limit ( parseInt ( size ) )
495+ . then ( teams => {
496+ if ( userId ) {
497+ return controller . attachUserApplicant ( teams , userId )
498+ }
499+ } )
500+ const count = await Team . find ( { event : eventId , challenge : filter } ) . countDocuments ( )
501+ console . log ( "with filter" , { data : found , count : count } )
502+ return { data : found , count : count }
503+ } else {
504+ const found = await Team . find ( {
505+ event : eventId ,
506+ } )
507+ . sort ( { createdAt : 'desc' } )
508+ . skip ( parseInt ( size * page ) )
509+ . limit ( parseInt ( size ) )
510+ . then ( teams => {
511+ if ( userId ) {
512+ return controller . attachUserApplicant ( teams , userId )
513+ }
514+ } )
515+ const count = await Team . find ( { event : eventId } ) . countDocuments ( )
516+ return { data : found , count : count }
517+ }
503518 } else {
504519 const found = await Team . find ( {
505520 event : eventId ,
@@ -512,6 +527,7 @@ controller.getTeamsForEvent = async (eventId, userId, page, size) => {
512527 return teams
513528 } )
514529 const count = await Team . find ( { event : eventId } ) . countDocuments ( )
530+ console . log ( "getting all teams" , count )
515531 return { data : found , count : count }
516532 }
517533 // TODO make the code not visible to participants on Redux store
0 commit comments