<commit_before>var db = require('./db.js');
var models = require('./models.js');
var Promise = require('bluebird');

module.exports.findOrCreate = function (Model, attributes) {

  return new Promise (function (resolve, reject) {
    Model.forge(attributes).fetch()
    .then(function (model) {
      if (!model) {
        model = new Model(attributes);
      }
      model.save()
      .then(function () {
        resolve(model);
      })
      .catch(function (error) {
        reject(error);
      });
    })
    .catch(function (error) {
      reject(error);
    });
  });
};

module.exports.getFriends = function (ids_array) {
  return db.knex
  .select()
  .from('users')
  .whereIn('facebook_id', ids_array)
  .then(function (friends) {
    return friends;
  })
  .catch(function (error) {
    return error;
  })
};<commit_msg>(chore) write getFriends and inviteFriends helpers<commit_after>var db = require('./db.js');
var models = require('./models.js');
var Promise = require('bluebird');

module.exports.findOrCreate = function (Model, attributes) {

  return new Promise (function (resolve, reject) {
    Model.forge(attributes).fetch()
    .then(function (model) {
      if (!model) {
        model = new Model(attributes);
      }
      model.save()
      .then(function () {
        resolve(model);
      })
      .catch(function (error) {
        reject(error);
      });
    })
    .catch(function (error) {
      reject(error);
    });
  });
};

module.exports.getFriends = function (ids_obj) {
  var ids_array = [];
  for (var id in ids_obj) {
    ids_array.push(id);
  }
  var friends = [];
  var friendCount = 0;
  return new Promise(function (res, rej) {
    return ids_array.forEach(function (id) {
      return db.knex
      .select()
      .from('users')
      .where('facebook_id', id)
      .then(function (user) {
        if (user.length) {
          friends.push(user[0]);
          if (++friendCount === ids_array.length) {
            res(friends);
          }
        } else {
          new models.User({'facebook_id': id}).save()
          .then(function (user) {
            friends.push(user);
            if (++friendCount === ids_array.length) {
              res(friends);
              res();
            }
          })
          .catch(function (error) {
            console.log(error);
          })
        }
      })
    });
  });
};

module.exports.inviteFriends = function (game, friends) {
  return new Promise(function (res, rej) {
    var inviteCount = 0;
    friends.forEach(function (friend) {
      module.exports.findOrCreate(models.UserGame, {game_id: game.id, user_id: friend.id})
      .then(function (userGame) {
        if (++inviteCount === friends.length) {
          res(game);
        }
      })
      .catch(function (error) {
        rej(error);
      });
    });
  });
};
{'fraction_non_alphanumeric': 0.15984474241354976, 'fraction_numerical': 0.001058574453069866, 'mean_word_length': 2.178251121076233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '450871', 'n_tokens_mistral': 886, 'n_tokens_neox': 876, 'n_words': 176}