-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* del console * edit pending * merge * Add tournament api * Add unit test for tournament * Refactor code * Adjust comments
- Loading branch information
Showing
6 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters