From 24bf4c5b0a876459a2bcd40fc85a0c4872248146 Mon Sep 17 00:00:00 2001 From: Pierre Theo Klein Date: Tue, 22 Jan 2019 14:07:31 -0500 Subject: [PATCH] =?UTF-8?q?Add=20teamId=20to=20expansion=20of=20query=20re?= =?UTF-8?q?sult,=20and=20keep=20team=20member=20ids=20in=20=E2=80=A6=20(#3?= =?UTF-8?q?00)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add teamId to expansion of query result, and keep team member ids in get team * Update test * Update comments * doc updates * Bug fix * ObjectId object -> string * accountId -> hackerId * _id -> .toString() --- controllers/team.controller.js | 1 - docs/api/api_data.js | 18 +++++----- docs/api/api_data.json | 18 +++++----- docs/api/api_project.js | 30 ++++++++--------- docs/api/api_project.json | 30 ++++++++--------- middlewares/team.middleware.js | 5 +-- package-lock.json | 61 ++++++++++++---------------------- package.json | 2 +- routes/api/team.js | 19 +++++++++-- services/search.service.js | 6 ++-- tests/team.test.js | 6 ++++ 11 files changed, 100 insertions(+), 96 deletions(-) diff --git a/controllers/team.controller.js b/controllers/team.controller.js index 359aeffe..943c3e55 100644 --- a/controllers/team.controller.js +++ b/controllers/team.controller.js @@ -20,7 +20,6 @@ const Constants = { */ function showTeam(req, res) { const teamData = req.body.team.toJSON(); - delete teamData.members; const memberNames = []; for (const member of req.body.teamMembers) { diff --git a/docs/api/api_data.js b/docs/api/api_data.js index d31debb9..93150e27 100644 --- a/docs/api/api_data.js +++ b/docs/api/api_data.js @@ -1257,7 +1257,7 @@ define({ }, { "type": "get", - "url": "/hacker/email/:email", + "url": "/hacker/:id", "title": "get a hacker's information", "name": "getHacker", "group": "Hacker", @@ -1268,8 +1268,8 @@ define({ "group": "param", "type": "String", "optional": false, - "field": "email", - "description": "

a hacker's unique email

" + "field": "id", + "description": "

a hacker's unique mongoID

" }] } }, @@ -1324,12 +1324,12 @@ define({ "filename": "routes/api/hacker.js", "groupTitle": "Hacker", "sampleRequest": [{ - "url": "https://api.mchacks.ca/api/hacker/email/:email" + "url": "https://api.mchacks.ca/api/hacker/:id" }] }, { "type": "get", - "url": "/hacker/:id", + "url": "/hacker/email/:email", "title": "get a hacker's information", "name": "getHacker", "group": "Hacker", @@ -1340,8 +1340,8 @@ define({ "group": "param", "type": "String", "optional": false, - "field": "id", - "description": "

a hacker's unique mongoID

" + "field": "email", + "description": "

a hacker's unique email

" }] } }, @@ -1396,7 +1396,7 @@ define({ "filename": "routes/api/hacker.js", "groupTitle": "Hacker", "sampleRequest": [{ - "url": "https://api.mchacks.ca/api/hacker/:id" + "url": "https://api.mchacks.ca/api/hacker/email/:email" }] }, { @@ -2645,7 +2645,7 @@ define({ }, "examples": [{ "title": "Success-Response: ", - "content": "{\n \"message\": \"Successfully retrieved team information\", \n \"data\": {...}\n }", + "content": "{\n \"message\": \"Team retrieval successful\", \n \"data\": { \n \"team\": {\n \"name\":\"foo\",\n \"members\": [\n ObjectId('...')\n ],\n \"devpostURL\": \"www.devpost.com/foo\",\n \"projectName\": \"fooey\"\n },\n \"members\": [\n {\n \"firstName\": \"John\",\n \"lastName\": \"Doe\"\n }\n ],\n }\n }", "type": "object" }] }, diff --git a/docs/api/api_data.json b/docs/api/api_data.json index 06066e0d..fab71f01 100644 --- a/docs/api/api_data.json +++ b/docs/api/api_data.json @@ -1256,7 +1256,7 @@ }, { "type": "get", - "url": "/hacker/email/:email", + "url": "/hacker/:id", "title": "get a hacker's information", "name": "getHacker", "group": "Hacker", @@ -1267,8 +1267,8 @@ "group": "param", "type": "String", "optional": false, - "field": "email", - "description": "

a hacker's unique email

" + "field": "id", + "description": "

a hacker's unique mongoID

" }] } }, @@ -1323,12 +1323,12 @@ "filename": "routes/api/hacker.js", "groupTitle": "Hacker", "sampleRequest": [{ - "url": "https://api.mchacks.ca/api/hacker/email/:email" + "url": "https://api.mchacks.ca/api/hacker/:id" }] }, { "type": "get", - "url": "/hacker/:id", + "url": "/hacker/email/:email", "title": "get a hacker's information", "name": "getHacker", "group": "Hacker", @@ -1339,8 +1339,8 @@ "group": "param", "type": "String", "optional": false, - "field": "id", - "description": "

a hacker's unique mongoID

" + "field": "email", + "description": "

a hacker's unique email

" }] } }, @@ -1395,7 +1395,7 @@ "filename": "routes/api/hacker.js", "groupTitle": "Hacker", "sampleRequest": [{ - "url": "https://api.mchacks.ca/api/hacker/:id" + "url": "https://api.mchacks.ca/api/hacker/email/:email" }] }, { @@ -2643,7 +2643,7 @@ }, "examples": [{ "title": "Success-Response: ", - "content": "{\n \"message\": \"Successfully retrieved team information\", \n \"data\": {...}\n }", + "content": "{\n \"message\": \"Team retrieval successful\", \n \"data\": { \n \"team\": {\n \"name\":\"foo\",\n \"members\": [\n ObjectId('...')\n ],\n \"devpostURL\": \"www.devpost.com/foo\",\n \"projectName\": \"fooey\"\n },\n \"members\": [\n {\n \"firstName\": \"John\",\n \"lastName\": \"Doe\"\n }\n ],\n }\n }", "type": "object" }] }, diff --git a/docs/api/api_project.js b/docs/api/api_project.js index 278259a4..59d642d6 100644 --- a/docs/api/api_project.js +++ b/docs/api/api_project.js @@ -1,16 +1,16 @@ -define({ - "name": "hackerAPI", - "version": "0.0.8", - "description": "Documentation for the API used for mchacks", - "defaultVersion": "0.0.8", - "title": "hackerAPI documentation", - "url": "https://api.mchacks.ca/api", - "sampleUrl": "https://api.mchacks.ca/api", - "apidoc": "0.3.0", - "generator": { - "name": "apidoc", - "time": "2019-01-21T04:13:36.482Z", - "url": "http://apidocjs.com", - "version": "0.17.7" - } +define({ + "name": "hackerAPI", + "version": "0.0.8", + "description": "Documentation for the API used for mchacks", + "defaultVersion": "0.0.8", + "title": "hackerAPI documentation", + "url": "https://api.mchacks.ca/api", + "sampleUrl": "https://api.mchacks.ca/api", + "apidoc": "0.3.0", + "generator": { + "name": "apidoc", + "time": "2019-01-21T04:13:36.482Z", + "url": "http://apidocjs.com", + "version": "0.17.7" + } }); \ No newline at end of file diff --git a/docs/api/api_project.json b/docs/api/api_project.json index 13eedf19..8d421e55 100644 --- a/docs/api/api_project.json +++ b/docs/api/api_project.json @@ -1,16 +1,16 @@ -{ - "name": "hackerAPI", - "version": "0.0.8", - "description": "Documentation for the API used for mchacks", - "defaultVersion": "0.0.8", - "title": "hackerAPI documentation", - "url": "https://api.mchacks.ca/api", - "sampleUrl": "https://api.mchacks.ca/api", - "apidoc": "0.3.0", - "generator": { - "name": "apidoc", - "time": "2019-01-21T04:13:36.482Z", - "url": "http://apidocjs.com", - "version": "0.17.7" - } +{ + "name": "hackerAPI", + "version": "0.0.8", + "description": "Documentation for the API used for mchacks", + "defaultVersion": "0.0.8", + "title": "hackerAPI documentation", + "url": "https://api.mchacks.ca/api", + "sampleUrl": "https://api.mchacks.ca/api", + "apidoc": "0.3.0", + "generator": { + "name": "apidoc", + "time": "2019-01-21T04:13:36.482Z", + "url": "http://apidocjs.com", + "version": "0.17.7" + } } \ No newline at end of file diff --git a/middlewares/team.middleware.js b/middlewares/team.middleware.js index 32c8f4af..830866e7 100644 --- a/middlewares/team.middleware.js +++ b/middlewares/team.middleware.js @@ -407,13 +407,14 @@ async function populateMemberAccountsById(req, res, next) { data: {} }); } - + let hackerIds = []; let teamMembers = []; for (const member of team.members) { teamMembers.push(member.accountId); + hackerIds.push(member._id); } - + team.members = hackerIds; req.body.team = team; req.body.teamMembers = teamMembers; return next(); diff --git a/package-lock.json b/package-lock.json index 1a161758..e3a011d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "dependencies": { "@google-cloud/common": { "version": "0.17.0", - "resolved": "https://registry.npmjs.org/@google-cloud/common/-/common-0.17.0.tgz", + "resolved": "http://registry.npmjs.org/@google-cloud/common/-/common-0.17.0.tgz", "integrity": "sha512-HRZLSU762E6HaKoGfJGa8W95yRjb9rY7LePhjaHK9ILAnFacMuUGVamDbTHu1csZomm1g3tZTtXfX/aAhtie/Q==", "requires": { "array-uniq": "^1.0.3", @@ -71,7 +71,7 @@ "dependencies": { "@google-cloud/common": { "version": "0.16.2", - "resolved": "https://registry.npmjs.org/@google-cloud/common/-/common-0.16.2.tgz", + "resolved": "http://registry.npmjs.org/@google-cloud/common/-/common-0.16.2.tgz", "integrity": "sha512-GrkaFoj0/oO36pNs4yLmaYhTujuA3i21FdQik99Fd/APix1uhf01VlpJY4lAteTDFLRNkRx6ydEh7OVvmeUHng==", "requires": { "array-uniq": "^1.0.3", @@ -96,7 +96,7 @@ }, "google-auto-auth": { "version": "0.9.7", - "resolved": "https://registry.npmjs.org/google-auto-auth/-/google-auto-auth-0.9.7.tgz", + "resolved": "http://registry.npmjs.org/google-auto-auth/-/google-auto-auth-0.9.7.tgz", "integrity": "sha512-Nro7aIFrL2NP0G7PoGrJqXGMZj8AjdBOcbZXRRm/8T3w08NUHIiNN3dxpuUYzDsZizslH+c8e+7HXL8vh3JXTQ==", "requires": { "async": "^2.3.0", @@ -2076,7 +2076,7 @@ "dependencies": { "pify": { "version": "2.3.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" } } @@ -2108,7 +2108,7 @@ }, "globby": { "version": "4.1.0", - "resolved": "https://registry.npmjs.org/globby/-/globby-4.1.0.tgz", + "resolved": "http://registry.npmjs.org/globby/-/globby-4.1.0.tgz", "integrity": "sha1-CA9UVJ7BuCpsYOYx/ILhIR2+lfg=", "requires": { "array-union": "^1.0.1", @@ -2121,7 +2121,7 @@ }, "pify": { "version": "2.3.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" } } @@ -3039,8 +3039,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -3061,14 +3060,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3083,20 +3080,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -3213,8 +3207,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -3226,7 +3219,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3241,7 +3233,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3249,14 +3240,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3275,7 +3264,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -3356,8 +3344,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -3369,7 +3356,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -3455,8 +3441,7 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -3492,7 +3477,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3512,7 +3496,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3556,20 +3539,18 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, "gcp-metadata": { "version": "0.6.3", - "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-0.6.3.tgz", + "resolved": "http://registry.npmjs.org/gcp-metadata/-/gcp-metadata-0.6.3.tgz", "integrity": "sha512-MSmczZctbz91AxCvqp9GHBoZOSbJKAICV7Ow/AIWSJZRrRchUd5NL1b2P4OfP+4m490BEUPhhARfpHdqCxuCvg==", "requires": { "axios": "^0.18.0", @@ -5205,7 +5186,7 @@ "dependencies": { "pify": { "version": "2.3.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" } } @@ -5421,7 +5402,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" } } @@ -6373,7 +6354,7 @@ }, "pify": { "version": "2.3.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" } } diff --git a/package.json b/package.json index 18509ed8..6483660f 100644 --- a/package.json +++ b/package.json @@ -48,4 +48,4 @@ "mocha": "^5.2.0", "nodemon": "^1.17.3" } -} \ No newline at end of file +} diff --git a/routes/api/team.js b/routes/api/team.js index 41b1098e..1a0b872f 100644 --- a/routes/api/team.js +++ b/routes/api/team.js @@ -122,8 +122,23 @@ module.exports = { * @apiSuccess {Object} data Team object * @apiSuccessExample {object} Success-Response: * { - "message": "Successfully retrieved team information", - "data": {...} + "message": "Team retrieval successful", + "data": { + "team": { + "name":"foo", + "members": [ + ObjectId('...') + ], + "devpostURL": "www.devpost.com/foo", + "projectName": "fooey" + }, + "members": [ + { + "firstName": "John", + "lastName": "Doe" + } + ], + } } * @apiError {String} message Error message diff --git a/services/search.service.js b/services/search.service.js index 7d0f3b3e..597a4cb1 100644 --- a/services/search.service.js +++ b/services/search.service.js @@ -17,10 +17,12 @@ function executeQuery(model, queryArray, page, limit, sort, sort_by, shouldExpan var query; switch (model.toLowerCase()) { case "hacker": - query = (shouldExpand) ? Hacker.find().populate({ + query = (shouldExpand) ? Hacker.find().populate([{ path: "accountId", select: " -password" - }) : Hacker.find(); + }, { + path: "teamId" + }]) : Hacker.find(); break; default: return []; diff --git a/tests/team.test.js b/tests/team.test.js index 3c37cb95..931e62ca 100644 --- a/tests/team.test.js +++ b/tests/team.test.js @@ -76,6 +76,12 @@ describe("GET team", function () { res.body.should.have.property("data"); res.body.data.should.have.property("team"); res.body.data.team.name.should.equal("FullTeam"); + res.body.data.team.should.have.property("members"); + res.body.data.team.members[0].should.equal(util.hacker.TeamHacker1._id.toString()); + res.body.data.team.members[1].should.equal(util.hacker.TeamHacker2._id.toString()); + res.body.data.team.members[2].should.equal(util.hacker.TeamHacker3._id.toString()); + res.body.data.team.members[3].should.equal(util.hacker.TeamHacker4._id.toString()); + res.body.data.should.have.property("members"); res.body.data.members[0].firstName.should.equal(util.account.hackerAccounts.stored.team[1].firstName); res.body.data.members[0].lastName.should.equal(util.account.hackerAccounts.stored.team[1].lastName);