aboutsummaryrefslogtreecommitdiff
path: root/models/polls/poll.model.ts
blob: ecd8ad86a08b53ce16cd4c7f798d86c414523e39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { Model, model } from 'mongoose';
import { PollSchema, pollSchema } from './poll.schema';

pollSchema.methods.vote = function(userId: string, which: 'left' | 'right'): PollSchema {
  const participants = ['left', 'right'].reduce((acc, option) => {
    const { votes } = this.contents[option];
    return acc.concat(votes);
  }, []);

  if (!participants.indexOf(userId) === -1) {
    this.contents[which].votes.push(userId);
  }

  return this.save();
}

export default model<PollSchema, Model<PollSchema>>('Poll', pollSchema);