<commit_before>var utils = require('../config/utils');

var getInfo = function(req) {
  var userId = req.params.id;
  var models = req.app.get('models');
  var Review = models.Review;
  return {userId: userId, Review: Review};
};

var recursiveAdder = function(options){
  for(var i = 0; i < options.reviews.length; i++){
    var review = options.reviews[i];
    review.authorId = options.utils.userId
    options.utils.Review.sync().then(function() {
      return options.utils.Review.create(review);
    }).then(function(newReview) {
      options.savedReviews.push(newReview.dataValues);
      return options.savedReviews;
    }).catch(function(err){
      console.log('Error: ', err)
    });
  }
  return options;
}

module.exports = {

  getReviews: function(req, res){
    var reviewUtils = getInfo(req);

    reviewUtils.Review.sync().then(function() {
      return reviewUtils.Review.findAll({
        where: {subjectId: reviewUtils.userId}
      });
    })
    .then(function(results) {
      var totalStar = results.reduce(function(previous, current) {
        return previous + current.starRating;
      }, 0);
      var averageStar = totalStar / results.length;

      var reviews = results.map(function(data) {
        return {
          authorId: data.authorId,
          eventId: data.eventId,
          starRating: data.starRating,
          text: data.text, 
          createdAt: data.createdAt
        };
      });

      var reviewData = {
        averageRating: averageStar,
        reviews: reviews
      };

      utils.sendResponse(res,200,reviewData);
    });
  },

  postReview: function(req, res){
    var reviewUtils = getInfo(req);
    var reviews = [];
    var count = 0;

    for(var i = 0; i < req.body.subjects.length; i++){
      reviews.push(req.body.subjects[i]);
    }

    var options = {
      count: count,
      reviews: reviews,
      savedReviews: [],
      utils: reviewUtils,
    }


    recursiveAdder(options);

  }
};<commit_msg>implemented asyncmap to solve all problems :)<commit_after>var utils = require('../config/utils');

var getInfo = function(req) {
  var userId = req.params.id;
  var models = req.app.get('models');
  var Review = models.Review;
  return {userId: userId, Review: Review};
};

var recursiveAdder = function(options, callback){
  var results = [];
  var invoker = function(index){
    if(options.reviews[index]){
      var review = options.reviews[index];
      review.authorId = options.utils.userId

      options.utils.Review.sync().then(function() {
        return options.utils.Review.create(review);
      }).then(function(newReview) {
        results.push(newReview.dataValues);
        invoker(index+1);
      }).catch(function(err){
        console.log('Error: ', err)
      });
      
    }else{
      return callback(results);
    }
  }
  invoker(0);
}


module.exports = {

  getReviews: function(req, res){
    var reviewUtils = getInfo(req);

    reviewUtils.Review.sync().then(function() {
      return reviewUtils.Review.findAll({
        where: {subjectId: reviewUtils.userId}
      });
    })
    .then(function(results) {
      var totalStar = results.reduce(function(previous, current) {
        return previous + current.starRating;
      }, 0);
      var averageStar = totalStar / results.length;

      var reviews = results.map(function(data) {
        return {
          authorId: data.authorId,
          eventId: data.eventId,
          starRating: data.starRating,
          text: data.text, 
          createdAt: data.createdAt
        };
      });

      var reviewData = {
        averageRating: averageStar,
        reviews: reviews
      };

      utils.sendResponse(res,200,reviewData);
    });
  },

  postReview: function(req, res){
    var reviewUtils = getInfo(req);
    var reviews = [];
    var count = 0;

    for(var i = 0; i < req.body.subjects.length; i++){
      reviews.push(req.body.subjects[i]);
    }

    var options = {
      count: count,
      reviews: reviews,
      utils: reviewUtils,
    }

    recursiveAdder(options, function(results){
      utils.sendResponse(res,200, results);
    });

  }
};
{'fraction_non_alphanumeric': 0.13714562636297553, 'fraction_numerical': 0.004361521686455052, 'mean_word_length': 2.531223267750214, 'pattern_counts': {'":': 0, '<': 6, '<?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': '1980660', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1256, 'n_words': 252}