<commit_before>var passport = require('passport');
var mongoose = require('mongoose');
var HashMap = require('hashmap');
var Cache = require('../cache/Students');
var User = mongoose.model('User');
var School = mongoose.model('School');
var DBAssignments = mongoose.model('Assignment');
var DBUserProgress = mongoose.model('userProgress');
var DBUserAudit = mongoose.model('UserAudit');

var sendJSONresponse = function(res, status, content) {
  res.status(status);
  res.json(content);
};

module.exports.register = function(req, res) {

  // if(!req.body.name || !req.body.email || !req.body.password) {
  //   sendJSONresponse(res, 400, {
  //     "message": "All fields required"
  //   });
  //   return;
  // }
  console.log("In authentication register with: " + user);
  var user = new User();

  user.name = req.body.name;
  user.email = req.body.email;
  user.role = req.body.role;

  if(user.role === "teacher") {
    user.school = new mongoose.mongo.ObjectId(req.body.school);
  }
  if(user.role === "student") {
    user.teacher = new mongoose.mongo.ObjectId(req.body.teacher);
  }

  user.setPassword(req.body.password);

  user.save(function(err) {
    var token;
    token = user.generateJwt();
    res.status(200);
    res.json({
      "token" : token
    });
  });

};

module.exports.login = function(req, res) {
  sendRespose = function(user) {
      token = user.generateJwt();
      res.status(200);
      res.json({
        "token" : token
      });
  }

  passport.authenticate('local', function(err, user, info){
    var token;
    // If Passport throws/catches an error
    if (err) {
      res.status(404).json(err);
      return;
    }

    // If a user is found
    if(user) {
      if(user.role == 'student') {
        loadStudentActivity(user, sendRespose);
      }
      else {
        sendRespose(user);
      }
    } 
    else {
      // If user is not found
      res.status(401).json(info);
    }
  })(req, res);

};

module.exports.logout = function(req, res) {

};

var student = undefined;
var assignments = undefined;

loadStudentActivity = function(user, sendRespose) {
  student = new Cache.Student(user._id);
  loadAssignments(user, sendRespose);
}

loadAssignments = function(user, sendRespose) {
    DBAssignments
    .aggregate({ $match: { assignee: user._id }})
    .exec(function (err, data) {
        if (err) {
            console.log('Returned from aggregate with error ' + err);
        }
        else {
            for(var i=0; i<data.length; i++) {
              student.addAssignment(new Cache.Assignment(data[i]));
            }
            loadUserProgress(user, sendRespose);
        }
    })        
}

loadUserProgress = function(user, sendRespose) {
    DBUserProgress<commit_msg>Implemented Cache on new student registration<commit_after>var passport = require('passport');
var mongoose = require('mongoose');
var HashMap = require('hashmap');
var Cache = require('../cache/Students');
var User = mongoose.model('User');
var School = mongoose.model('School');
var DBAssignments = mongoose.model('Assignment');
var DBUserProgress = mongoose.model('userProgress');
var DBUserAudit = mongoose.model('UserAudit');

var student = undefined;
var assignments = undefined;

var sendJSONresponse = function(res, status, content) {
  res.status(status);
  res.json(content);
};

module.exports.register = function(req, res) {
  sendRespose = function(user) {
      token = user.generateJwt();
      res.status(200);
      res.json({
        "token" : token
      });
  }
  console.log("In authentication register with: " + user);
  var user = new User();

  user.name = req.body.name;
  user.email = req.body.email;
  user.role = req.body.role;

  if(user.role === "teacher") {
    user.school = new mongoose.mongo.ObjectId(req.body.school);
  }
  if(user.role === "student") {
    user.teacher = new mongoose.mongo.ObjectId(req.body.teacher);
  }

  user.setPassword(<PASSWORD>.body.password);

  user.save(function(err) {
    if(user.role === "student") {
      loadStudentActivity(user, sendRespose);
    }
    else {
      sendRespose(user);
    }
  });

};

module.exports.login = function(req, res) {
  sendRespose = function(user) {
      token = user.generateJwt();
      res.status(200);
      res.json({
        "token" : token
      });
  }

  passport.authenticate('local', function(err, user, info){
    var token;
    // If Passport throws/catches an error
    if (err) {
      res.status(404).json(err);
      return;
    }

    // If a user is found
    if(user) {
      if(user.role == 'student') {
        loadStudentActivity(user, sendRespose);
      }
      else {
        sendRespose(user);
      }
    } 
    else {
      // If user is not found
      res.status(401).json(info);
    }
  })(req, res);

};

module.exports.logout = function(req, res) {

};


loadStudentActivity = function(user, sendRespose) {
  student = new Cache.Student(user._id);
  loadAssignments(user, sendRespose);
}

loadAssignments = function(user, sendRespose) {
    DBAssignments
    .aggregate({ $match: { assignee: user._id }})
    .exec(function (err, data) {
        if (err) {
            console.log('Returned from aggregate with error ' + err);
        }
        else {
            for(var i=0; i<data.length; i++) {
              student.addAssignment(new Cache.Assignment(data[i]));
            }
            loadUserProgress(user, sendRespose);
        }
    })        
}

loadUserProgress = function(user, sendRespose) {
    DBUserProgress
{'fraction_non_alphanumeric': 0.15179058353026723, 'fraction_numerical': 0.00527176876931467, 'mean_word_length': 2.7762525737817434, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 4, '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': '6629992', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1792, 'n_words': 356}