blob: ed05cc7927ac946c9a54fbc8c7a3ee507300e676 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import { Model, model, Types } from 'mongoose';
import { Which } from 'which-types';
import { PollSchema, pollSchema } from './poll.schema';
pollSchema.methods.vote = function vote(userId: string, which: Which): PollSchema {
const participants: Types.ObjectId[] = ['left', 'right'].reduce((acc, option) => {
const { votes } = this.contents[option];
return acc.concat(votes);
}, []);
if (!participants.some(user => user.equals(userId))) {
this.contents[which].votes.push(userId);
}
return this.save();
};
export default model<PollSchema, Model<PollSchema>>('Poll', pollSchema);
|