@@ -165,6 +165,34 @@ async function findById(req, res, next) {
165165 return next ( ) ;
166166}
167167
168+ /**
169+ * @async
170+ * @function deleteUserFromTeam
171+ * @param {{user: {id: ObjectId}} req
172+ * @param {* } res
173+ * @return {JSON } Success or error status
174+ * @description Removes the hacker associated with req.user.id from the team under teamId. If hacker is not part of a team, it does nothing.
175+ */
176+ async function deleteUserFromTeam ( req , res , next ) {
177+ const hacker = await Services . Hacker . findByAccountId ( req . user . id ) ;
178+
179+ if ( ! hacker ) {
180+ return next ( {
181+ status : 404 ,
182+ message : Constants . Error . HACKER_404_MESSAGE ,
183+ data : {
184+ id : req . user . id
185+ }
186+ } ) ;
187+ }
188+ const oldTeamId = hacker . teamId ;
189+ if ( oldTeamId ) {
190+ await Services . Team . removeMember ( oldTeamId , hacker . _id ) ;
191+ await Services . Team . removeTeamIfEmpty ( oldTeamId ) ;
192+ }
193+ next ( ) ;
194+ }
195+
168196/**
169197 * @async
170198 * @function updateHackerTeam
@@ -343,7 +371,7 @@ async function parseNewTeam(req, res, next) {
343371 }
344372
345373 // hacker should not be in another team
346- if ( hacker . teamId !== undefined ) {
374+ if ( hacker . teamId !== undefined && hacker . teamId !== null ) {
347375 return next ( {
348376 status : 409 ,
349377 message : Constants . Error . TEAM_MEMBER_409_MESSAGE ,
@@ -366,4 +394,5 @@ module.exports = {
366394 parseNewTeam : Util . asyncMiddleware ( parseNewTeam ) ,
367395 ensureFreeTeamName : Util . asyncMiddleware ( ensureFreeTeamName ) ,
368396 populateMemberAccountsById : Util . asyncMiddleware ( populateMemberAccountsById ) ,
397+ deleteUserFromTeam : Util . asyncMiddleware ( deleteUserFromTeam ) ,
369398} ;
0 commit comments