@@ -55,7 +55,42 @@ async function ensureUniqueHackerId(req, res, next) {
5555
5656/**
5757 * @async
58- * @function ensureSpance
58+ * @function createTeam
59+ * @param {{body: {teamDetails: {_id: ObjectId, name: string, members: ObjectId[], devpostURL?: string, projectName: string}}} } req
60+ * @param {* } res
61+ * @description create a team from information in req.body.teamDetails.
62+ */
63+ async function createTeam ( req , res , next ) {
64+ const teamDetails = req . body . teamDetails ;
65+
66+ const team = await Services . Team . createTeam ( teamDetails ) ;
67+
68+ if ( ! team ) {
69+ return res . status ( 500 ) . json ( {
70+ message : Constants . Error . TEAM_CREATE_500_MESSAGE ,
71+ data : { }
72+ } ) ;
73+ }
74+
75+ for ( const hackerId of teamDetails . members ) {
76+ const hacker = await Services . Hacker . updateOne ( hackerId , {
77+ teamId : team . _id
78+ } ) ;
79+
80+ if ( ! hacker ) {
81+ return res . status ( 500 ) . json ( {
82+ message : Constants . Error . HACKER_UPDATE_500_MESSAGE ,
83+ data : { }
84+ } ) ;
85+ }
86+ }
87+
88+ req . body . team = team ;
89+ return next ( ) ;
90+ }
91+
92+ /**
93+ * @function ensureSpace
5994 * @param {{body: {name: string}} } req
6095 * @param {JSON } res
6196 * @param {(err?)=>void } next
@@ -85,26 +120,49 @@ async function ensureSpace(req, res, next) {
85120}
86121
87122/**
88- * @async
89- * @function createTeam
90- * @param {{body: {teamDetails: {_id: ObjectId, name: string, members: ObjectId[], devpostURL?: string, projectName: string}}} } req
91- * @param {* } res
92- * @description create a team from information in req.body.teamDetails.
123+ * @function ensureFreeTeamName
124+ * @param {{body: {teamDetails: {name: String}}} } req
125+ * @param {JSON } res
126+ * @param {(err?)=>void } next
127+ * @return {void }
128+ * @description Checks to see that the team name is not in use.
93129 */
94- async function createTeam ( req , res , next ) {
130+ async function ensureFreeTeamName ( req , res , next ) {
95131 const teamDetails = req . body . teamDetails ;
96132
97- const team = await Services . Team . createTeam ( teamDetails ) ;
133+ const team = await Services . Team . findByName ( teamDetails . name ) ;
134+
135+ if ( team ) {
136+ return next ( {
137+ status : 409 ,
138+ message : Constants . Error . TEAM_NAME_409_MESSAGE ,
139+ data : teamDetails . name
140+ } ) ;
141+ }
142+
143+ return next ( ) ;
144+ }
145+
146+ /**
147+ * @async
148+ * @function findById
149+ * @param {{body: {id: ObjectId}} } req
150+ * @param {* } res
151+ * @return {JSON } Success or error status
152+ * @description Finds a team by it's mongoId that's specified in req.param.id in route parameters. The id is moved to req.body.id from req.params.id by validation.
153+ */
154+ async function findById ( req , res , next ) {
155+ const team = await Services . Team . findById ( req . body . id ) ;
98156
99157 if ( ! team ) {
100- return res . status ( 500 ) . json ( {
101- message : Constants . Error . TEAM_CREATE_500_MESSAGE ,
158+ return res . status ( 404 ) . json ( {
159+ message : Constants . Error . TEAM_404_MESSAGE ,
102160 data : { }
103161 } ) ;
104- } else {
105- req . body . team = team ;
106- return next ( ) ;
107162 }
163+
164+ req . body . team = team ;
165+ return next ( ) ;
108166}
109167
110168/**
@@ -145,7 +203,7 @@ async function updateHackerTeam(req, res, next) {
145203 return next ( {
146204 status : 409 ,
147205 message : Constants . Error . TEAM_JOIN_SAME_409_MESSAGE ,
148- data : req . body . teamName
206+ data : req . body . name
149207 } ) ;
150208 }
151209
@@ -155,7 +213,6 @@ async function updateHackerTeam(req, res, next) {
155213 await Services . Team . removeTeamIfEmpty ( previousTeamId ) ;
156214 }
157215
158-
159216 // add hacker to the new team and change teamId of hacker
160217 const update = await Services . Team . addMember ( receivingTeam . _id , hacker . _id ) ;
161218
@@ -258,12 +315,55 @@ function parseTeam(req, res, next) {
258315 return next ( ) ;
259316}
260317
318+ async function parseNewTeam ( req , res , next ) {
319+ const teamDetails = {
320+ _id : mongoose . Types . ObjectId ( ) ,
321+ name : req . body . name ,
322+ members : [ ] ,
323+ devpostURL : req . body . devpostURL ,
324+ projectName : req . body . projectName
325+ } ;
326+
327+ delete req . body . name ;
328+ delete req . body . members ;
329+ delete req . body . devpostURL ;
330+ delete req . body . projectName ;
331+
332+ // hacker should exist because of authorization
333+ const hacker = await Services . Hacker . findByAccountId ( req . user . id ) ;
334+
335+ if ( ! hacker ) {
336+ return next ( {
337+ status : 404 ,
338+ message : Constants . Error . HACKER_404_MESSAGE ,
339+ data : {
340+ id : req . user . id
341+ }
342+ } ) ;
343+ }
344+
345+ // hacker should not be in another team
346+ if ( hacker . teamId !== undefined ) {
347+ return next ( {
348+ status : 409 ,
349+ message : Constants . Error . TEAM_MEMBER_409_MESSAGE ,
350+ } ) ;
351+ }
352+
353+ teamDetails . members . push ( hacker . _id ) ;
354+
355+ req . body . teamDetails = teamDetails ;
356+ return next ( ) ;
357+ }
358+
261359module . exports = {
262360 parseTeam : parseTeam ,
263361 findById : Util . asyncMiddleware ( findById ) ,
264362 createTeam : Util . asyncMiddleware ( createTeam ) ,
265363 ensureUniqueHackerId : Util . asyncMiddleware ( ensureUniqueHackerId ) ,
266364 ensureSpace : Util . asyncMiddleware ( ensureSpace ) ,
267365 updateHackerTeam : Util . asyncMiddleware ( updateHackerTeam ) ,
366+ parseNewTeam : Util . asyncMiddleware ( parseNewTeam ) ,
367+ ensureFreeTeamName : Util . asyncMiddleware ( ensureFreeTeamName ) ,
268368 populateMemberAccountsById : Util . asyncMiddleware ( populateMemberAccountsById ) ,
269369} ;
0 commit comments