Skip to content

Commit

Permalink
Bookmark API (#115)
Browse files Browse the repository at this point in the history
* get bookmark list

* Add unit test for bookmark

* Merge master

* Adjust comments

* Modify unchanged variable

* Delete console log
  • Loading branch information
SarahhhC authored Jan 5, 2017
1 parent 67808a4 commit 964a3cd
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/server/config/json/user.callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export default {
ERR_USER_NOT_FOUND: 'No such account exists.',
ERR_UNDEFINED_KEY: 'Key is undefined.',
ERR_WRONG_PASSWORD: 'The password is not correct.',
SUCCESS_BOOKMARK_ON: 'Succeed bookmark on.',
SUCCESS_EDIT: 'Edit success.',
SUCCESS_BOOKMARK_OFF: 'Succeed bookmark off.',
SUCCESS_RESET_PASSWORD: 'The password had been changed successfully.',
SUCCESS_REGISTER: 'Registration & Signin success.',
SUCCESS_SIGNIN: 'Sign in success.',
Expand Down
11 changes: 10 additions & 1 deletion src/server/controllers/match.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const MATCH_STATUS = {
};

export function getMentorList(req, res, next) {
let bookmarkList = [];
let pendingList = [];

let projectOption = {
Expand Down Expand Up @@ -49,14 +50,18 @@ export function getMentorList(req, res, next) {
Promise.all([
findConnection(matchOptions.option1, projectOption, localField.mentee),
findConnection(matchOptions.option2, projectOption, localField.mentor),
findConnection(matchOptions.option3, projectOption, 'mentee_id'),
findConnection(matchOptions.option3, projectOption, localField.mentee),
User.findOne({ _id: req.user._id }).exec(),
])
.then((results) => {
let exceptionList = [];

results[0].forEach(user => exceptionList.push(user.mentee_id));
results[1].forEach(user => exceptionList.push(user.mentor_id));
results[2].forEach(user => pendingList.push(user.mentor_id.toString()));
if (results[3].bookmark !== undefined) {
bookmarkList = results[3].bookmark;
}

return User.find({
_id: {
Expand All @@ -73,6 +78,10 @@ export function getMentorList(req, res, next) {
return new Promise((resolve, reject) => {
let userData = JSON.parse(JSON.stringify(userList));
userData.forEach((item) => {
if (bookmarkList.includes(item._id.toString())) {
item.bookmarked = true;
}

if (pendingList.includes(item._id.toString())) {
item.pending = true;
}
Expand Down
73 changes: 73 additions & 0 deletions src/server/controllers/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export function getProfileById(req, res, next) {
.then((matchAsMentor) => {
userProfile.relation.asMentor =
matchAsMentor ? matchAsMentor.status : matchController.MATCH_STATUS.REJECTED;
return isBookmarked(req.user._id, req.params._id);
})
.then((isBookmarked) => {
if (isBookmarked) {
userProfile.bookmarked = true;
} else {
userProfile.bookmarked = false;
}

res.status(200).json(userProfile);
})
.catch((err) => {
Expand Down Expand Up @@ -598,6 +607,70 @@ export function getMentoringRequestStatus(req, res, next) {
});
}

export function bookmarkOn(req, res, next) {
User.findOne({ _id: req.user._id }).exec()
.then((user) => {
user.bookmark.push(req.body.id);
return user.save();
})
.then((user) => {
res.status(200).json({ msg: userCallback.SUCCESS_BOOKMARK_ON });
})
.catch((err) => {
res.status(400).json(err);
});
}

export function bookmarkOff(req, res, next) {
User.findOne({ _id: req.user._id }).exec()
.then((user) => {
const index = user.bookmark.indexOf(req.body.id);
user.bookmark.splice(index, 1);
return user.save();
})
.then((user) => {
res.status(200).json({ msg: userCallback.SUCCESS_BOOKMARK_OFF });
})
.catch((err) => {
res.status(400).json(err);
});
}

export function getBookmark(req, res, next) {
User.findOne({ _id: req.user._id }).exec()
.then((user) => {
let bookmarkList = user.bookmark;
return User.find({
_id: {
$in: bookmarkList,
},
}, { password: 0 }).exec();
})
.then((list) => {
res.status(200).json(list);
})
.catch((err) => {
res.status(400).json(err);
});
}

function isBookmarked(userId, targetId) {
return new Promise((resolve, reject) => {
User.findOne({ _id: userId }).exec()
.then((me) => {
const bookmarkList = me.bookmark !== undefined ? me.bookmark : [];
if (bookmarkList.includes(targetId)) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
reject(err);
});
});
}

export function signOut(req, res, next) {
User.findOne({ _id: req.user._id }).exec()
.then((user) => {
Expand Down
1 change: 1 addition & 0 deletions src/server/models/users.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ let userSchema = new Schema({
languages: String,
mentorMode: Boolean,
deviceToken: Array,
bookmark: Array,
reg_date: {
type: Date,
default: Date.now,
Expand Down
52 changes: 50 additions & 2 deletions src/server/routes/users.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,35 @@ router.post('/editExpertise', apiProtector, user.editExpertise);
router.post('/editPersonality', apiProtector, user.editPersonality);

/**
* @api {post} /users/setRequestStatus Request Edit mentorMode
* @apiName setRequestStatus
* @api {post} /users/editMentorMode Request Edit mentorMode
* @apiName editMentorMode
* @apiGroup User
*
* @apiParam {Boolean} mentorMode Flag for requestGet.
*
*/
router.post('/editMentorMode', apiProtector, user.setMentoringRequestStatus);

/**
* @api {post} /users/bookmarkOn Request Bookmark On
* @apiName bookmarkOn
* @apiGroup User
*
* @apiParam {String} id user for bookmark on.
*
*/
router.post('/bookmarkOn', apiProtector, user.bookmarkOn);

/**
* @api {post} /users/bookmarkOff Request Bookmark Off
* @apiName bookmarkOff
* @apiGroup User
*
* @apiParam {String} id user for bookmark off.
*
*/
router.post('/bookmarkOff', apiProtector, user.bookmarkOff);

//GET method

/**
Expand Down Expand Up @@ -558,6 +578,34 @@ router.get('/personality', apiProtector, user.getPersonalityInfo);
*/
router.get('/mentorMode', apiProtector, user.getMentoringRequestStatus);

/**
* @api {get} /users/bookmark Request bookmark list
* @apiName getRequestStatus
* @apiGroup User
*
* @apiSuccessExample {json} Success
* HTTP/1.1 200 OK
* {[
* {
* Marked User1's Info
* },
* {
* Marked User2's Info
* },
* {
* Marked User3's Info
* },....
* ]}
*
* @apiErrorExample {json}
* HTTP/1.1 401 Not Authenticated
* {
* "err_point": {err_msg}
* }
*
*/
router.get('/bookmark', apiProtector, user.getBookmark);

/**
* @api {get} /users/accessToken Token: Check
* @apiName checkAccessToken
Expand Down
95 changes: 95 additions & 0 deletions src/test/controllers/users.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ describe('Test User API', () => {
json: true,
})
.then((result) => {
userData.USER_E_DATA = result.body.user;
result.statusCode.should.equal(200);
done();
})
Expand Down Expand Up @@ -608,6 +609,100 @@ describe('/mentorMode', () => {
});
});

describe('/bookmarkOn', () => {
it('request /bookmarkOn with session cookie.', (done) => {
rp({
method: 'POST',
uri: `${API_BASE_URL}/bookmarkOn`,
form: { id: userData.USER_E_DATA._id },
resolveWithFullResponse: true,
json: true,
headers: {
access_token: userData.USER_A_DATA.access_token,
},
})
.then((result) => {
result.statusCode.should.equal(200);
result.body.msg.should.equal(userCallback.SUCCESS_BOOKMARK_ON);
done();
})
.catch((err) => {
should.fail();
done();
});
});
});

describe('/bookmark', () => {
it('request /bookmark with session cookie.', (done) => {
rp({
method: 'GET',
uri: `${API_BASE_URL}/bookmark`,
resolveWithFullResponse: true,
json: true,
headers: {
access_token: userData.USER_A_DATA.access_token,
},
})
.then((result) => {
result.statusCode.should.equal(200);
result.body[0]._id.should.equal(userData.USER_E_DATA._id);
done();
})
.catch((err) => {
should.fail();
done();
});
});
});

describe('/bookmarkOff', () => {
it('request /bookmarkOff with session cookie.', (done) => {
rp({
method: 'POST',
uri: `${API_BASE_URL}/bookmarkOff`,
form: { id: userData.USER_E_DATA._id },
resolveWithFullResponse: true,
json: true,
headers: {
access_token: userData.USER_A_DATA.access_token,
},
})
.then((result) => {
result.statusCode.should.equal(200);
result.body.msg.should.equal(userCallback.SUCCESS_BOOKMARK_OFF);
done();
})
.catch((err) => {
should.fail();
done();
});
});
});

describe('/bookmark', () => {
it('request /bookmark with session cookie.', (done) => {
rp({
method: 'GET',
uri: `${API_BASE_URL}/bookmark`,
resolveWithFullResponse: true,
json: true,
headers: {
access_token: userData.USER_A_DATA.access_token,
},
})
.then((result) => {
result.statusCode.should.equal(200);
result.body.length.should.equal(0);
done();
})
.catch((err) => {
should.fail();
done();
});
});
});

describe('/accessToken', () => {
it('request /accessToken to check validation of access token.', (done) => {
rp({
Expand Down

0 comments on commit 964a3cd

Please sign in to comment.