Skip to content

Commit 9399c95

Browse files
authored
Feature/278 team leave (#279)
* Add route * Change success name, add tests * Fix api_data * Add removeTeamIfEmpty * Add leave to hackerRole * Bug fix * General team bugfixes * fn name change * Remove console.log * Change function name
1 parent 23eb14d commit 9399c95

12 files changed

Lines changed: 215 additions & 41 deletions

File tree

constants/role.constant.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const hackerRole = {
4242

4343
Constants.Routes.teamRoutes.join,
4444
Constants.Routes.teamRoutes.post,
45-
Constants.Routes.teamRoutes.get
45+
Constants.Routes.teamRoutes.get,
46+
Constants.Routes.teamRoutes.leave
4647
]
4748
};
4849

constants/routes.constant.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ const teamRoutes = {
146146
requestType: Constants.REQUEST_TYPES.PATCH,
147147
uri: "/api/team/join/",
148148
},
149+
"leave": {
150+
requestType: Constants.REQUEST_TYPES.PATCH,
151+
uri: "/api/team/leave/",
152+
},
149153
};
150154

151155
const volunteerRoutes = {

constants/success.constant.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const TEAM_GET_BY_ID = "Team found by id.";
3838
const TEAM_CREATE = "Team creation successful.";
3939
const TEAM_JOIN = "Team join successful.";
4040
const TEAM_READ = "Team retrieval successful.";
41+
const TEAM_HACKER_LEAVE = "Removal from team successful.";
4142

4243
const VOLUNTEER_CREATE = "Volunteer creation successful.";
4344

@@ -79,6 +80,7 @@ module.exports = {
7980
TEAM_CREATE: TEAM_CREATE,
8081
TEAM_JOIN: TEAM_JOIN,
8182
TEAM_READ: TEAM_READ,
83+
TEAM_HACKER_LEAVE: TEAM_HACKER_LEAVE,
8284

8385
VOLUNTEER_CREATE: VOLUNTEER_CREATE,
8486
};

controllers/team.controller.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,24 @@ function createdTeam(req, res) {
7171
});
7272
}
7373

74+
/**
75+
* @function leftTeam
76+
* @param {*} req
77+
* @param {*} res
78+
* @return {JSON} Success status
79+
* @description return success message of removing self from team.
80+
*/
81+
82+
function leftTeam(req, res) {
83+
return res.status(200).json({
84+
message: Constants.Success.TEAM_HACKER_LEAVE,
85+
data: {},
86+
});
87+
}
88+
7489
module.exports = {
7590
joinedTeam: joinedTeam,
7691
createdTeam: createdTeam,
7792
showTeam: showTeam,
93+
leftTeam: leftTeam,
7894
};

docs/api/api_data.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,43 @@ define({
22612261
"url": "https://api.mchacks.ca/api/team/"
22622262
}]
22632263
},
2264+
{
2265+
"type": "patch",
2266+
"url": "/team/leave/",
2267+
"title": "Allows a logged in hacker to leave current team",
2268+
"name": "deleteSelfFromTeam",
2269+
"group": "Team",
2270+
"version": "1.1.1",
2271+
"success": {
2272+
"fields": {
2273+
"Success 200": [{
2274+
"group": "Success 200",
2275+
"type": "string",
2276+
"optional": false,
2277+
"field": "message",
2278+
"description": "<p>Success message</p>"
2279+
},
2280+
{
2281+
"group": "Success 200",
2282+
"type": "object",
2283+
"optional": false,
2284+
"field": "data",
2285+
"description": "<p>{}</p>"
2286+
}
2287+
]
2288+
},
2289+
"examples": [{
2290+
"title": "Success-Response: ",
2291+
"content": "{\n \"message\": \"Removal from team successful.\", \n \"data\": {}\n}",
2292+
"type": "object"
2293+
}]
2294+
},
2295+
"filename": "routes/api/team.js",
2296+
"groupTitle": "Team",
2297+
"sampleRequest": [{
2298+
"url": "https://api.mchacks.ca/api/team/leave/"
2299+
}]
2300+
},
22642301
{
22652302
"type": "get",
22662303
"url": "/team/:id",
@@ -2375,9 +2412,6 @@ define({
23752412
"type": "object"
23762413
}]
23772414
},
2378-
"permission": [{
2379-
"name": "Administrator"
2380-
}],
23812415
"filename": "routes/api/team.js",
23822416
"groupTitle": "Team",
23832417
"sampleRequest": [{

docs/api/api_data.json

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,43 @@
22602260
"url": "https://api.mchacks.ca/api/team/"
22612261
}]
22622262
},
2263+
{
2264+
"type": "patch",
2265+
"url": "/team/leave/",
2266+
"title": "Allows a logged in hacker to leave current team",
2267+
"name": "deleteSelfFromTeam",
2268+
"group": "Team",
2269+
"version": "1.1.1",
2270+
"success": {
2271+
"fields": {
2272+
"Success 200": [{
2273+
"group": "Success 200",
2274+
"type": "string",
2275+
"optional": false,
2276+
"field": "message",
2277+
"description": "<p>Success message</p>"
2278+
},
2279+
{
2280+
"group": "Success 200",
2281+
"type": "object",
2282+
"optional": false,
2283+
"field": "data",
2284+
"description": "<p>{}</p>"
2285+
}
2286+
]
2287+
},
2288+
"examples": [{
2289+
"title": "Success-Response: ",
2290+
"content": "{\n \"message\": \"Removal from team successful.\", \n \"data\": {}\n}",
2291+
"type": "object"
2292+
}]
2293+
},
2294+
"filename": "routes/api/team.js",
2295+
"groupTitle": "Team",
2296+
"sampleRequest": [{
2297+
"url": "https://api.mchacks.ca/api/team/leave/"
2298+
}]
2299+
},
22632300
{
22642301
"type": "get",
22652302
"url": "/team/:id",
@@ -2374,9 +2411,6 @@
23742411
"type": "object"
23752412
}]
23762413
},
2377-
"permission": [{
2378-
"name": "Administrator"
2379-
}],
23802414
"filename": "routes/api/team.js",
23812415
"groupTitle": "Team",
23822416
"sampleRequest": [{

docs/api/api_project.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
define({
2-
"name": "hackerAPI",
3-
"version": "0.0.8",
4-
"description": "Documentation for the API used for mchacks",
5-
"defaultVersion": "0.0.8",
6-
"title": "hackerAPI documentation",
7-
"url": "https://api.mchacks.ca/api",
8-
"sampleUrl": "https://api.mchacks.ca/api",
9-
"apidoc": "0.3.0",
10-
"generator": {
11-
"name": "apidoc",
12-
"time": "2019-01-07T03:26:47.443Z",
13-
"url": "http://apidocjs.com",
14-
"version": "0.17.7"
15-
}
1+
define({
2+
"name": "hackerAPI",
3+
"version": "0.0.8",
4+
"description": "Documentation for the API used for mchacks",
5+
"defaultVersion": "0.0.8",
6+
"title": "hackerAPI documentation",
7+
"url": "https://api.mchacks.ca/api",
8+
"sampleUrl": "https://api.mchacks.ca/api",
9+
"apidoc": "0.3.0",
10+
"generator": {
11+
"name": "apidoc",
12+
"time": "2019-01-08T22:07:07.661Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
});

docs/api/api_project.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{
2-
"name": "hackerAPI",
3-
"version": "0.0.8",
4-
"description": "Documentation for the API used for mchacks",
5-
"defaultVersion": "0.0.8",
6-
"title": "hackerAPI documentation",
7-
"url": "https://api.mchacks.ca/api",
8-
"sampleUrl": "https://api.mchacks.ca/api",
9-
"apidoc": "0.3.0",
10-
"generator": {
11-
"name": "apidoc",
12-
"time": "2019-01-07T03:26:47.443Z",
13-
"url": "http://apidocjs.com",
14-
"version": "0.17.7"
15-
}
1+
{
2+
"name": "hackerAPI",
3+
"version": "0.0.8",
4+
"description": "Documentation for the API used for mchacks",
5+
"defaultVersion": "0.0.8",
6+
"title": "hackerAPI documentation",
7+
"url": "https://api.mchacks.ca/api",
8+
"sampleUrl": "https://api.mchacks.ca/api",
9+
"apidoc": "0.3.0",
10+
"generator": {
11+
"name": "apidoc",
12+
"time": "2019-01-08T22:07:07.661Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
}

middlewares/team.middleware.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
};

routes/api/team.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ module.exports = {
7272
* "message": "Team join successful.",
7373
* "data": {}
7474
* }
75-
* @apiPermission Administrator
7675
*/
7776
teamRouter.route("/join/").patch(
7877
Middleware.Auth.ensureAuthenticated(),
@@ -87,6 +86,27 @@ module.exports = {
8786
Controllers.Team.joinedTeam
8887
);
8988

89+
/**
90+
* @api {patch} /team/leave/ Allows a logged in hacker to leave current team
91+
* @apiName deleteSelfFromTeam
92+
* @apiGroup Team
93+
* @apiVersion 1.1.1
94+
*
95+
* @apiSuccess {string} message Success message
96+
* @apiSuccess {object} data {}
97+
* @apiSuccessExample {object} Success-Response:
98+
* {
99+
* "message": "Removal from team successful.",
100+
* "data": {}
101+
* }
102+
*/
103+
teamRouter.route("/leave").patch(
104+
Middleware.Auth.ensureAuthenticated(),
105+
Middleware.Auth.ensureAuthorized(),
106+
Middleware.Team.deleteUserFromTeam,
107+
Controllers.Team.leftTeam
108+
);
109+
90110
/**
91111
* @api {get} /team/:id get a team's information
92112
* @apiName getTeam

0 commit comments

Comments
 (0)