<commit_before>var Pick = require('../models/index.js').Pick;
var currentGame= require("../config/game");
var currentLoserGame = require("../config/loserGame");
var mail = require('../config/nodeMailer');
var User = require('../models/index.js').User;

  
function setChoice() {
  var game = currentGame.get();
  var loserGame = currentLoserGame.get();
  var currentPick = (game && game.weekGames) ? game.weekGames[game.weekGames.length -1].awayTeam : null;
  if(!currentPick && loserGame && loserGame.weekGames) {
    currentPick = loserGame.weekGames[loserGame.weekGames.length-1];
  }
  if(game){
    game.update({canEdit: false});
    return Pick.findAll({where: {GameId: game.id, week: game.weekNumber-1, hasWon:true, active: true}})
      .then(function(gamePicks) {
        if (gamePicks.length>0){
          return updatePicks(gamePicks, currentPick);
        }
        return Promise.resolve([]);
      });
      .then(checkLosers)
      .then(sendEmails);
  } else {
    return checkLosers([])
            .then(sendEmails);
  }
}


function checkLosers(users) {
  var loserGame = currentLoserGame.get();
  if(loserGame) {
    loserGame.update({canEdit: false});
    return Pick.findAll({where: {GameId: loserGame.id, week: loserGame.weekNumber -1, hasWon:true, active: true}})
      .then( function(gamePicks) {
        if (gamePicks.length>0){
          return userList(gamePicks, currentPick, users);

        }
        return Promise.resolve(users);
      });
  }
  return Promise.resolve(users);
}

function userList(gamePicks, currentPick, users){
  return updatePicks(gamePicks, currentPick)
    .then(function (loserPicks) {
      return filter(users.concat(loserPicks);

    });

}

function filter(array) {
  return array.filter(function(el, index, array) {
    console.log(array.indexOf(el) == index);
    return array.indexOf(el) == index;
  })

}

function updatePicks(gamePicks, newChoice){
  var emailList = [];
  gamePicks.forEach(function(pick){
    User.findOne({where:{id: pick.UserId}})
      .then(function(user){
        Pick.create({week: pick.week+1, hasPaid: pick.hasPaid, teamChoice: newChoice,GameId: pick.GameId, UserId:pick.UserId});
        pick.update({active: false});
        if (emailList.indexOf(user.emil) === -1) {
          emailList.push(user);
        }
      });
  })

  return Promise.resolve(emailList);
}

function sendEmails(AllEmails) {

  var message = {
    subject: "Pick Chosen!",
    text: "Good Morning! It is Saturday and we have gone through to make sure picks were chosen. If you got this email then your pick was not inputed and we chose for you. Hope you have a great weekend!",
  };

    message.to = allEmails;
    mail.sendEveryoneEmail(message);
}


function start() {
  currentGame.init()
    .then(currentLoserGame.init)
    .then(setChoice);
}

module.exports = {<commit_msg>making it so it will email out if the picks are chosen for them<commit_after>var Pick = require('../models/index.js').Pick;
var currentGame= require("../config/game");
var currentLoserGame = require("../config/loserGame");
var mail = require('../config/nodeMailer');
var User = require('../models/index.js').User;
var pickUsers = [];
  
function setChoice() {
  var game = currentGame.get();
  var loserGame = currentLoserGame.get();
  var currentPick = (game && game.weekGames) ? game.weekGames[game.weekGames.length -1].awayTeam : null;
  if(!currentPick && loserGame && loserGame.weekGames) {
    currentPick = loserGame.weekGames[loserGame.weekGames.length-1];
  }
  if(game){
    game.update({canEdit: false});
    return Pick.findAll({where: {GameId: game.id, week: game.weekNumber-1, hasWon:true, active: true}})
      .then(function(gamePicks) {
        if (gamePicks.length>0){
          return updatePicks(gamePicks, currentPick);
        }
      })
      .then(checkLosers)
      .then(sendEmails);
  } else {
    return checkLosers()
            .then(sendEmails);
  }
}


function checkLosers() {
  var loserGame = currentLoserGame.get();
  if(loserGame) {
    loserGame.update({canEdit: false});
    return Pick.findAll({where: {GameId: loserGame.id, week: loserGame.weekNumber -1, hasWon:true, active: true}})
      .then( function(gamePicks) {
        if (gamePicks.length>0){
          return updatePicks(gamePicks, currentPick)

        }
        return Promise.resolve();
      });
  }
  return Promise.resolve();
}

function updatePicks(gamePicks, newChoice){
  gamePicks.forEach(function(pick){
    return User.findOne({where:{id: pick.UserId}})
      .then(function(user){
        Pick.create({week: pick.week+1, hasPaid: pick.hasPaid, teamChoice: newChoice,GameId: pick.GameId, UserId:pick.UserId});
        pick.update({active: false});
        if (pickUsers.indexOf(user.email) === -1) {
          pickUsers.push(user.email);
        }
      });
  })

  return Promise.resolve();
}

function sendEmails() {

  var message = {
    subject: "Pick Chosen!",
    text: "Good Morning! It is Saturday and we have gone through to make sure picks were chosen. If you got this email then your pick was not inputed and we chose for you. Hope you have a great weekend!",
  };

  setTimeout(function() {
    console.log("I am thing", pickUsers);
    message.to = pickUsers;
    mail.sendEveryoneEmail(message);
  }, 10000);
}


function start() {
  currentGame.init()
    .then(currentLoserGame.init)
    .then(setChoice);
}

module.exports = {
{'fraction_non_alphanumeric': 0.14594894561598223, 'fraction_numerical': 0.00388457269700333, 'mean_word_length': 3.360483870967742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6677235', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1737, 'n_words': 381}