aboutsummaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-06-22 21:11:19 +0300
committereug-vs <eug-vs@keemail.me>2020-06-22 21:11:19 +0300
commit967ee2225a874dc114079882d737a25a487404be (patch)
treed88c6cc28dd75467448e03a8b559f63ee5fc06b3 /services
parentef94c648b14bd09bf28b8f47ed015b319aa8f0cc (diff)
downloadwhich-api-967ee2225a874dc114079882d737a25a487404be.tar.gz
refactor: improve vote endpoint
Diffstat (limited to 'services')
-rw-r--r--services/votes/votes.class.ts14
1 files changed, 8 insertions, 6 deletions
diff --git a/services/votes/votes.class.ts b/services/votes/votes.class.ts
index d95c148..0490b7e 100644
--- a/services/votes/votes.class.ts
+++ b/services/votes/votes.class.ts
@@ -3,12 +3,14 @@ import { PollSchema } from '../../models/polls/poll.schema';
export default class Votes {
async create(data: any, params: any): Promise<PollSchema | null> {
- return PollModel.findById(params.route.id)
- .then(poll => poll?.vote(params.user._id, data.which))
- .catch(e => {
- console.error(e);
- return null;
- });
+ const poll = await PollModel.findById(params.route.id);
+ if (poll) {
+ const which: 'left' | 'right' = data.which;
+ const { user } = params;
+ poll.contents[which].votes.push(user._id);
+ return poll.save();
+ }
+ return null;
}
}