From 967ee2225a874dc114079882d737a25a487404be Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 22 Jun 2020 21:11:19 +0300 Subject: refactor: improve vote endpoint --- services/votes/votes.class.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'services') 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 { - 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; } } -- cgit v1.2.3