diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-03 08:00:10 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-03 08:00:10 +0900 |
| commit | 583b64331b4d3d36f642801c847109b8634df1d9 (patch) | |
| tree | 1bccc089c3feae69e2690b22093ee9b2155b53db /src/api/endpoints/posts/polls | |
| parent | wip (diff) | |
| download | sharkey-583b64331b4d3d36f642801c847109b8634df1d9.tar.gz sharkey-583b64331b4d3d36f642801c847109b8634df1d9.tar.bz2 sharkey-583b64331b4d3d36f642801c847109b8634df1d9.zip | |
wip
Diffstat (limited to 'src/api/endpoints/posts/polls')
| -rw-r--r-- | src/api/endpoints/posts/polls/vote.ts (renamed from src/api/endpoints/posts/polls/vote.js) | 32 |
1 files changed, 10 insertions, 22 deletions
diff --git a/src/api/endpoints/posts/polls/vote.js b/src/api/endpoints/posts/polls/vote.ts index 9f9a5171a0..d0caf7da95 100644 --- a/src/api/endpoints/posts/polls/vote.js +++ b/src/api/endpoints/posts/polls/vote.ts @@ -3,7 +3,7 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; +import it from '../../../it'; import Vote from '../../../models/poll-vote'; import Post from '../../../models/post'; import notify from '../../../common/notify'; @@ -18,19 +18,12 @@ import notify from '../../../common/notify'; module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'post_id' parameter - const postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } - - // Validate id - if (!mongo.ObjectID.isValid(postId)) { - return rej('incorrect post_id'); - } + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); // Get votee const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { @@ -42,15 +35,12 @@ module.exports = (params, user) => } // Get 'choice' parameter - const choice = params.choice; - if (choice == null) { - return rej('choice is required'); - } - - // Validate choice - if (!post.poll.choices.some(x => x.id == choice)) { - return rej('invalid choice'); - } + const [choice, choiceError] = + it(params.choice).expect.string() + .required() + .validate(c => post.poll.choices.some(x => x.id == c)) + .qed(); + if (choiceError) return rej('invalid choice param'); // if already voted const exist = await Vote.findOne({ @@ -76,8 +66,6 @@ module.exports = (params, user) => const inc = {}; inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1; - console.log(inc); - // Increment likes count Post.update({ _id: post._id }, { $inc: inc |