Skip to content

Commit 967ee22

Browse files
committed
refactor: improve vote endpoint
1 parent ef94c64 commit 967ee22

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

services/votes/votes.class.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { PollSchema } from '../../models/polls/poll.schema';
33

44
export default class Votes {
55
async create(data: any, params: any): Promise<PollSchema | null> {
6-
return PollModel.findById(params.route.id)
7-
.then(poll => poll?.vote(params.user._id, data.which))
8-
.catch(e => {
9-
console.error(e);
10-
return null;
11-
});
6+
const poll = await PollModel.findById(params.route.id);
7+
if (poll) {
8+
const which: 'left' | 'right' = data.which;
9+
const { user } = params;
10+
poll.contents[which].votes.push(user._id);
11+
return poll.save();
12+
}
13+
return null;
1214
}
1315
}
1416

0 commit comments

Comments
 (0)