<commit_before>import Poll from '../models/poll.model';

export default class PollsController {

  create = (req, res) => {
    let poll = new Poll(req.body);
    poll.createdBy = req.user;
    poll.save((err) => {
      if (err) return res.status(500).send(err);
      res.send(poll);
    })
  };

  canVote = (req, res, next) => {
    let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    let userVoted;
    let ipVoted;

    if (req.user) {
      userVoted = req.poll.users.some(user => {
        return user.equals(req.user.id);
      });
    } else {
      userVoted = false;
    }

    ipVoted = req.poll.ips.indexOf(ip) !== -1;

    if (userVoted || ipVoted) {
      return res.status(403).send({message: 'You already voted'});
    }
    return next();
  };

  canDelete = (req, res, next) => {
    if (!req.poll.createdBy.equals(req.user._id)) {
      return res.status(403).send({message: 'User is not authorized'})
    }
    return next();
  };

  getPoll(req, res, next) {
    Poll.findById(req.params.id).exec((err, poll) => {
      if (err) return res.status(500).send(err);
      if (!poll) return res.status(500).send({message: 'Poll doesn\'t exist'});
      req.poll = poll;
      return next();
    })
  }

  get = (req, res) => {
    res.send(req.poll);
  };

  delete = (req, res) => {
    req.poll.remove((err) => {
      if (err) return res.status(500).send(err);
      return res.send({});
    })
  };

  list = (req, res) => {
    Poll.find({}).populate('createdBy', 'name').exec((err, polls) => {
      if (err) return res.status(500).send(err);
      res.send(polls);
    })
  };

  vote = (req, res) => {
    const option = req.body,
      poll = req.poll;
    if (option.id) {
      const optionToVote = poll.options.id(option.id);
      optionToVote.votes++;
    } else {
      poll.options.push({value: option.value, votes: 1});
    }
    if (req.user) {
      poll.users.push(req.user._id);
    }
    console.log(req.headers);
    poll.ips.push(req.headers['x-forwarded-for'] || req.connection.remoteAddress);
    poll.save((err) => {
      if (err) return res.status(500).send(err);
      return res.send(poll);
    })
  };
}<commit_msg>delete console.log<commit_after>import Poll from '../models/poll.model';

export default class PollsController {

  create = (req, res) => {
    let poll = new Poll(req.body);
    poll.createdBy = req.user;
    poll.save((err) => {
      if (err) return res.status(500).send(err);
      res.send(poll);
    })
  };

  canVote = (req, res, next) => {
    let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    let userVoted;
    let ipVoted;

    if (req.user) {
      userVoted = req.poll.users.some(user => {
        return user.equals(req.user.id);
      });
    } else {
      userVoted = false;
    }

    ipVoted = req.poll.ips.indexOf(ip) !== -1;

    if (userVoted || ipVoted) {
      return res.status(403).send({message: 'You already voted'});
    }
    return next();
  };

  canDelete = (req, res, next) => {
    if (!req.poll.createdBy.equals(req.user._id)) {
      return res.status(403).send({message: 'User is not authorized'})
    }
    return next();
  };

  getPoll(req, res, next) {
    Poll.findById(req.params.id).exec((err, poll) => {
      if (err) return res.status(500).send(err);
      if (!poll) return res.status(500).send({message: 'Poll doesn\'t exist'});
      req.poll = poll;
      return next();
    })
  }

  get = (req, res) => {
    res.send(req.poll);
  };

  delete = (req, res) => {
    req.poll.remove((err) => {
      if (err) return res.status(500).send(err);
      return res.send({});
    })
  };

  list = (req, res) => {
    Poll.find({}).populate('createdBy', 'name').exec((err, polls) => {
      if (err) return res.status(500).send(err);
      res.send(polls);
    })
  };

  vote = (req, res) => {
    const option = req.body,
      poll = req.poll;
    if (option.id) {
      const optionToVote = poll.options.id(option.id);
      optionToVote.votes++;
    } else {
      poll.options.push({value: option.value, votes: 1});
    }
    if (req.user) {
      poll.users.push(req.user._id);
    }
    poll.ips.push(req.headers['x-forwarded-for'] || req.connection.remoteAddress);
    poll.save((err) => {
      if (err) return res.status(500).send(err);
      return res.send(poll);
    })
  };
}
{'fraction_non_alphanumeric': 0.19049804911636448, 'fraction_numerical': 0.011934817535001148, 'mean_word_length': 2.4532488114104596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504603', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1556, 'n_words': 296}