summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts/polls
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-03-04 04:28:38 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-03-04 04:28:38 +0900
commit3c1b92baa1ac15c23fe63e2f50739105252ca516 (patch)
tree35a0ae58802891f05209c83e780440775075e088 /src/api/endpoints/posts/polls
parentMerge pull request #233 from syuilo/greenkeeper/inquirer-3.0.6 (diff)
downloadsharkey-3c1b92baa1ac15c23fe63e2f50739105252ca516.tar.gz
sharkey-3c1b92baa1ac15c23fe63e2f50739105252ca516.tar.bz2
sharkey-3c1b92baa1ac15c23fe63e2f50739105252ca516.zip
Follow linter
Diffstat (limited to 'src/api/endpoints/posts/polls')
-rw-r--r--src/api/endpoints/posts/polls/vote.ts101
1 files changed, 49 insertions, 52 deletions
diff --git a/src/api/endpoints/posts/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts
index d0caf7da95..36d0de975b 100644
--- a/src/api/endpoints/posts/polls/vote.ts
+++ b/src/api/endpoints/posts/polls/vote.ts
@@ -1,5 +1,3 @@
-'use strict';
-
/**
* Module dependencies
*/
@@ -15,68 +13,67 @@ import notify from '../../../common/notify';
* @param {any} user
* @return {Promise<any>}
*/
-module.exports = (params, user) =>
- new Promise(async (res, rej) => {
- // Get 'post_id' parameter
- const [postId, postIdErr] = it(params.post_id, 'id', true);
- if (postIdErr) return rej('invalid post_id param');
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'post_id' parameter
+ 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: postId
- });
+ // Get votee
+ const post = await Post.findOne({
+ _id: postId
+ });
- if (post === null) {
- return rej('post not found');
- }
+ if (post === null) {
+ return rej('post not found');
+ }
- if (post.poll == null) {
- return rej('poll not found');
- }
+ if (post.poll == null) {
+ return rej('poll not found');
+ }
- // Get 'choice' parameter
- 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');
+ // Get 'choice' parameter
+ 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({
- post_id: post._id,
- user_id: user._id
- });
+ // if already voted
+ const exist = await Vote.findOne({
+ post_id: post._id,
+ user_id: user._id
+ });
- if (exist !== null) {
- return rej('already voted');
- }
+ if (exist !== null) {
+ return rej('already voted');
+ }
- // Create vote
- await Vote.insert({
- created_at: new Date(),
- post_id: post._id,
- user_id: user._id,
- choice: choice
- });
+ // Create vote
+ await Vote.insert({
+ created_at: new Date(),
+ post_id: post._id,
+ user_id: user._id,
+ choice: choice
+ });
- // Send response
- res();
+ // Send response
+ res();
- const inc = {};
- inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1;
+ const inc = {};
+ inc[`poll.choices.${findWithAttr(post.poll.choices, 'id', choice)}.votes`] = 1;
- // Increment likes count
- Post.update({ _id: post._id }, {
- $inc: inc
- });
+ // Increment likes count
+ Post.update({ _id: post._id }, {
+ $inc: inc
+ });
- // Notify
- notify(post.user_id, user._id, 'poll_vote', {
- post_id: post._id,
- choice: choice
- });
+ // Notify
+ notify(post.user_id, user._id, 'poll_vote', {
+ post_id: post._id,
+ choice: choice
});
+});
function findWithAttr(array, attr, value) {
for (let i = 0; i < array.length; i += 1) {