Skip to content
Snippets Groups Projects
Commit e14ea1fe authored by syuilo's avatar syuilo
Browse files

wip

parent 7602e8f9
No related branches found
No related tags found
No related merge requests found
import $ from 'cafy'; import ID from '../../../../../cafy-id';
import UserList from '../../../../../models/user-list';
import User from '../../../../../models/user';
/**
* Add a user to a user list
*/
module.exports = async (params, me) => new Promise(async (res, rej) => {
// Get 'listId' parameter
const [listId, listIdErr] = $(params.listId).type(ID).$;
if (listIdErr) return rej('invalid listId param');
// Fetch the list
const userList = await UserList.findOne({
_id: listId,
userId: me._id,
});
if (userList == null) {
return rej('list not found');
}
// Get 'userId' parameter
const [userId, userIdErr] = $(params.userId).type(ID).$;
if (userIdErr) return rej('invalid userId param');
// Fetch the user
const user = await User.findOne({
_id: userId
});
if (user == null) {
return rej('user not found');
}
if (userList.userIds.map(id => id.toHexString()).includes(user._id.toHexString())) {
return rej('the user already added');
}
// Push the user
await UserList.update({ _id: userList._id }, {
$push: {
userIds: user._id
}
});
res();
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment