Skip to content

Commit

Permalink
Tournament API (#117)
Browse files Browse the repository at this point in the history
* del console

* edit pending

* merge

* Add tournament api

* Add unit test for tournament

* Refactor code

* Adjust comments
  • Loading branch information
SarahhhC authored Jan 20, 2017
1 parent 964a3cd commit abe3bd6
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
85 changes: 85 additions & 0 deletions src/server/controllers/tournament.controller.js
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 2 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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'));

Expand Down
43 changes: 43 additions & 0 deletions src/server/routes/tournament.route.js
Original file line number Diff line number Diff line change
@@ -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;
78 changes: 78 additions & 0 deletions src/test/controllers/tournament.controller.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
1 change: 1 addition & 0 deletions src/test/controllers/users.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
Expand Down

0 comments on commit abe3bd6

Please sign in to comment.