From abe3bd66fb93fe497097f813bd9cb7cf7387bebf Mon Sep 17 00:00:00 2001 From: SarahhhC Date: Fri, 20 Jan 2017 15:07:48 +0900 Subject: [PATCH] Tournament API (#117) * del console * edit pending * merge * Add tournament api * Add unit test for tournament * Refactor code * Adjust comments --- gulpfile.js | 1 + .../controllers/tournament.controller.js | 85 +++++++++++++++++++ src/server/index.js | 2 + src/server/routes/tournament.route.js | 43 ++++++++++ .../controllers/tournament.controller.test.js | 78 +++++++++++++++++ src/test/controllers/users.controller.test.js | 1 + 6 files changed, 210 insertions(+) create mode 100644 src/server/controllers/tournament.controller.js create mode 100644 src/server/routes/tournament.route.js create mode 100644 src/test/controllers/tournament.controller.test.js diff --git a/gulpfile.js b/gulpfile.js index 8f83a01..a5d20d1 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -29,6 +29,7 @@ gulp.task('test', ['pre-test'], () => { 'dist-test/test/utils.js', 'dist-test/test/controllers/users.controller.test.js', 'dist-test/test/controllers/survey.controller.test.js', + 'dist-test/test/controllers/tournament.controller.test.js', 'dist-test/test/controllers/match.controller.test.js', 'dist-test/test/controllers/users.signout.test.js',]) .pipe(mocha()) diff --git a/src/server/controllers/tournament.controller.js b/src/server/controllers/tournament.controller.js new file mode 100644 index 0000000..4ab3965 --- /dev/null +++ b/src/server/controllers/tournament.controller.js @@ -0,0 +1,85 @@ +import mongoose from 'mongoose'; + +/* + * Methods for getting tournament list. + */ + +const ObjectId = mongoose.Types.ObjectId; + +const Match = mongoose.model('match'); +const User = mongoose.model('user'); + +export function getTournamentList(req, res, next) { + const matchOptions = { + MATCH_AS_MENTOR: { + mentor_id: ObjectId(req.user._id), + }, + MATCH_AS_MENTEE: { + mentee_id: ObjectId(req.user._id), + }, + }; + + const projectOption = { + mentee_id: 1, + mentor_id: 1, + }; + + const localField = { + mentee: 'mentee_id', + mentor: 'mentor_id', + }; + + Promise.all([ + findConnection(matchOptions.MATCH_AS_MENTOR, projectOption, localField.mentee), + findConnection(matchOptions.MATCH_AS_MENTEE, projectOption, localField.mentor), + ]) + .then((exceptionList) => { + const defaultFindOption = { + _id: { + $ne: req.user._id, + $nin: exceptionList[0], + $nin: exceptionList[1], + }, + mentorMode: { + $ne: false, + }, + }; + + if (req.params.area === 'All') { + return User.find(defaultFindOption, { password: 0 }) + .sort({ stamp_login: -1 }).exec(); + } else { + return User.find({ + _id: defaultFindOption._id, + mentorMode: defaultFindOption.mentorMode, + 'career.area': req.params.area, + }, { password: 0 }) + .sort({ stamp_login: -1 }).exec(); + } + }) + .then((userList) => { + res.status(200).json(userList); + }) + .catch((err) => { + res.status(400).json({ err }); + }); +} + +function findConnection(matchOption, projectOption, localField) { + return Match.aggregate([ + { + $match: matchOption, + }, + { + $project: projectOption, + }, + { + $lookup: { + from: 'users', + localField, + foreignField: '_id', + as: 'list', + }, + }, + ]).exec(); +} diff --git a/src/server/index.js b/src/server/index.js index ae6093e..698ba0c 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -8,6 +8,7 @@ import match from './routes/match.route'; import methodOverride from 'method-override'; import morgan from 'morgan'; import survey from './routes/survey.route'; +import tournament from './routes/tournament.route'; import users from './routes/users.route'; export default (cb) => { @@ -33,6 +34,7 @@ export default (cb) => { app.use('/survey', survey); app.use('/match', match); app.use('/chat', chat); + app.use('/tournament', tournament); app.use(express.static(__dirname + '/apidoc')); diff --git a/src/server/routes/tournament.route.js b/src/server/routes/tournament.route.js new file mode 100644 index 0000000..46fcbc7 --- /dev/null +++ b/src/server/routes/tournament.route.js @@ -0,0 +1,43 @@ +import express from 'express'; +import jwtUtil from '../utils/jwt.util'; +import * as tournament from '../controllers/tournament.controller'; + +const apiProtector = jwtUtil.apiProtector; +const router = express.Router(); + +//GET method + +/** + * @api {get} /tournament/list/:area Get tournament list. + * @apiName getTournamentList + * @apiGroup Tournament + * + * @apiParam {String} type Define the area as "Designer" or other else. + * + * @apiSuccessExample {json} Success + * HTTP/1.1 200 OK + * { + * "count" : 28, + * "list" : [ + * { + * Mentor1's Info + * }, + * { + * Mentor2's Info + * }, + * { + * Mentor3's Info + * },.... + * ] + * } + * + * @apiErrorExample {json} + * HTTP/1.1 401 Not Authenticated + * { + * "err_point": {err_msg} + * } + * + */ +router.get('/list/:area', apiProtector, tournament.getTournamentList); + +export default router; diff --git a/src/test/controllers/tournament.controller.test.js b/src/test/controllers/tournament.controller.test.js new file mode 100644 index 0000000..59e39e9 --- /dev/null +++ b/src/test/controllers/tournament.controller.test.js @@ -0,0 +1,78 @@ +import should from 'should'; +import '../../server/models/users.model'; +import rp from 'request-promise'; +import userData from '../fixtures/userData'; + +/* + * Test for Tournament API + */ + +const API_BASE_URL = `http://localhost:${process.env.PORT}/tournament`; + +describe('Test Tournament API', () => { + + describe('/list/:area', () => { + it('request /list/:id with invalid area.', (done) => { + rp({ + method: 'GET', + uri: `${API_BASE_URL}/list/INVALID_AREA`, + resolveWithFullResponse: true, + json: true, + headers: { + access_token: userData.USER_E_DATA.access_token, + }, + }) + .then((result) => { + result.statusCode.should.equal(200); + result.body.length.should.equal(0); + done(); + }) + .catch((err) => { + should.fail(err); + done(); + }); + }); + + it('request /list/:area with valid All area.', (done) => { + rp({ + method: 'GET', + uri: `${API_BASE_URL}/list/All`, + resolveWithFullResponse: true, + json: true, + headers: { + access_token: userData.USER_E_DATA.access_token, + }, + }) + .then((result) => { + result.statusCode.should.equal(200); + result.body.length.should.equal(1); + done(); + }) + .catch((err) => { + should.fail(); + done(); + }); + }); + + it('request /list/:area with valid Design area.', (done) => { + rp({ + method: 'GET', + uri: `${API_BASE_URL}/list/Design`, + resolveWithFullResponse: true, + json: true, + headers: { + access_token: userData.USER_E_DATA.access_token, + }, + }) + .then((result) => { + result.statusCode.should.equal(200); + result.body.length.should.equal(1); + done(); + }) + .catch((err) => { + should.fail(); + done(); + }); + }); + }); +}); diff --git a/src/test/controllers/users.controller.test.js b/src/test/controllers/users.controller.test.js index c0c5f9d..066b21a 100644 --- a/src/test/controllers/users.controller.test.js +++ b/src/test/controllers/users.controller.test.js @@ -263,6 +263,7 @@ describe('Test User API', () => { }) .then((result) => { userData.USER_E_DATA = result.body.user; + userData.USER_E_DATA.access_token = result.body.access_token; result.statusCode.should.equal(200); done(); })