Skip to content

Commit 433a103

Browse files
YiFeiZhang2pierreTklein
authored andcommitted
Feature/team get (#270)
* Add routes and roles * add showTeam with member names * create tests * Create middleware to display member names and add auth * documentation and function commenting * add some more test details * docs and typo * Change findMemberNames to use Mongoose.populate * Change documentation to so param type is ObjectId * Move id for get to be in req.param * Fix documentation, make test cases more detailed
1 parent 9083e77 commit 433a103

14 files changed

Lines changed: 172 additions & 56 deletions

File tree

constants/role.constant.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const hackerRole = {
4141
Constants.Routes.hackerRoutes.getSelf,
4242

4343
Constants.Routes.teamRoutes.join,
44+
Constants.Routes.teamRoutes.get
4445
]
4546
};
4647

@@ -52,6 +53,8 @@ const volunteerRole = {
5253

5354
Constants.Routes.hackerRoutes.patchAnyCheckInById,
5455
Constants.Routes.hackerRoutes.patchSelfCheckInById,
56+
57+
Constants.Routes.teamRoutes.get
5558
]
5659
};
5760

constants/routes.constant.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ const sponsorRoutes = {
134134
};
135135

136136
const teamRoutes = {
137-
"getSelfById": {
138-
requestType: Constants.REQUEST_TYPES.GET,
139-
uri: "/api/team/" + Constants.ROLE_CATEGORIES.SELF,
140-
},
141-
"getAnyById": {
137+
"get": {
142138
requestType: Constants.REQUEST_TYPES.GET,
143139
uri: "/api/team/" + Constants.ROLE_CATEGORIES.ALL,
144140
},

controllers/team.controller.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,27 @@ const Constants = {
1919
* @description Returns the JSON of team object located in req.body.team
2020
*/
2121
function showTeam(req, res) {
22+
const teamData = req.body.team.toJSON();
23+
delete teamData.members;
24+
25+
const memberNames = [];
26+
for (const member of req.body.teamMembers) {
27+
const strippedMemberJSON = member.toStrippedJSON();
28+
29+
const memberName = {
30+
"firstName": strippedMemberJSON.firstName,
31+
"lastName": strippedMemberJSON.lastName,
32+
};
33+
34+
memberNames.push(memberName);
35+
}
36+
2237
return res.status(200).json({
2338
message: Constants.Success.TEAM_READ,
24-
data: req.body.team.toJSON()
39+
data: {
40+
team: teamData,
41+
members: memberNames,
42+
}
2543
});
2644
}
2745

docs/api/api_data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ define({
17791779
{
17801780
"type": "post",
17811781
"url": "/api/role/",
1782-
"title": "create a new hacker",
1782+
"title": "create a new role",
17831783
"name": "createRole",
17841784
"group": "Role",
17851785
"version": "1.1.1",
@@ -2263,7 +2263,7 @@ define({
22632263
"type": "ObjectId",
22642264
"optional": false,
22652265
"field": "id",
2266-
"description": "<p>a team's unique mongoId</p>"
2266+
"description": "<p>MongoId of the team</p>"
22672267
}]
22682268
}
22692269
},

docs/api/api_data.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@
17781778
{
17791779
"type": "post",
17801780
"url": "/api/role/",
1781-
"title": "create a new hacker",
1781+
"title": "create a new role",
17821782
"name": "createRole",
17831783
"group": "Role",
17841784
"version": "1.1.1",
@@ -2262,7 +2262,7 @@
22622262
"type": "ObjectId",
22632263
"optional": false,
22642264
"field": "id",
2265-
"description": "<p>a team's unique mongoId</p>"
2265+
"description": "<p>MongoId of the team</p>"
22662266
}]
22672267
}
22682268
},

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-04T19:05:11.619Z",
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-07T19:14:21.760Z",
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-04T19:05:11.619Z",
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-07T19:14:21.760Z",
13+
"url": "http://apidocjs.com",
14+
"version": "0.17.7"
15+
}
1616
}

middlewares/team.middleware.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const mongoose = require("mongoose");
55
const Services = {
66
Logger: require("../services/logger.service"),
77
Team: require("../services/team.service"),
8-
Hacker: require("../services/hacker.service")
8+
Hacker: require("../services/hacker.service"),
9+
Account: require("../services/account.service"),
910
};
1011
const Util = require("./util.middleware");
1112
const Constants = {
@@ -192,6 +193,42 @@ async function findById(req, res, next) {
192193
next();
193194
}
194195

196+
/**
197+
* @async
198+
* @function populateMemberAccountsById
199+
* @param {{body: {id: ObjectId}}} req
200+
* @param {*} res
201+
* @return {JSON} Success or error status
202+
* @description
203+
* Find the team by id and populates the accounts of the members.
204+
* The team information is stored in req.body.team, and the member information is stored in req.body.teamMembers
205+
*/
206+
async function populateMemberAccountsById(req, res, next) {
207+
const team = await Services.Team.findById(req.body.id).populate({
208+
path: "members",
209+
populate: {
210+
path: "accountId"
211+
}
212+
});
213+
214+
if (!team) {
215+
return res.status(404).json({
216+
message: Constants.Error.TEAM_404_MESSAGE,
217+
data: {}
218+
});
219+
}
220+
221+
let teamMembers = [];
222+
223+
for (const member of team.members) {
224+
teamMembers.push(member.accountId);
225+
}
226+
227+
req.body.team = team;
228+
req.body.teamMembers = teamMembers;
229+
return next();
230+
}
231+
195232
/**
196233
* @function parseTeam
197234
* @param {{body: {name: string, members: Object[], devpostURL: string, projectName: string}}} req
@@ -228,4 +265,5 @@ module.exports = {
228265
ensureUniqueHackerId: Util.asyncMiddleware(ensureUniqueHackerId),
229266
ensureSpace: Util.asyncMiddleware(ensureSpace),
230267
updateHackerTeam: Util.asyncMiddleware(updateHackerTeam),
268+
populateMemberAccountsById: Util.asyncMiddleware(populateMemberAccountsById),
231269
};

middlewares/validators/team.validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ module.exports = {
1313

1414
joinTeamValidator: [
1515
VALIDATOR.asciiValidator("body", "name", false),
16-
]
16+
],
1717
};

routes/api/role.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
const roleRouter = express.Router();
1919

2020
/**
21-
* @api {post} /api/role/ create a new hacker
21+
* @api {post} /api/role/ create a new role
2222
* @apiName createRole
2323
* @apiGroup Role
2424
* @apiVersion 1.1.1

0 commit comments

Comments
 (0)