<commit_before>//Import error handler
var Error = require('../../Utils/Error.js');
//Import AES library
var AES = require('../../Utils/AES.js');

//Schema imports
var Worker = require('../MongoSchemas/Worker.js');
var WorkGroup = require('../MongoSchemas/WorkGroup.js');

var checkForSubmittedData = function(array, userID) {
  for(var i = 0; i < array.length; i++) {
    for(var j = 0; j < array[i].data.length; j++) {
      if(array[j].worker != userID) {
        return false;
      }
    }
  }
  return true;
}

//Export the request handler
module.exports = function(message, mongoose, socket, eventEmitter, key, userID, groupMax){
  //Get encryption information
  var payload = message.payload;
  var tag = message.tag;
  var iv = message.iv;
  //Declare decrypted variable for try/catch
  var decrypted;
  //Try decrypting, otherwise pass error onto user
  try {
    decrypted = JSON.parse(AES.decrypt(key, iv, tag, payload));
  } catch (e) {
    Error.sendError(socket, 'SECURITY_DECRYPTION_FAILURE', true);
    return;
  }
  if(!decrypted ||
    !decrypted.request ||
    decrypted.request.toUpperCase() !== 'REQUEST') {
      //Stop execution, we cannot verify the user...
      Error.sendError(socket, 'STAGE_HANDSHAKE_POST_COMPLETE_FAILURE', true);
      return;
  }
  //Make sure that the user does not currently have a work group that they have
  //not submitted work to
  WorkGroup.find({workers: userID}, function(error, workgroups) {
    //Pass database errors onto client
    if(error) {
      Error.sendError(socket, 'DATABASE_GENERIC', true);
      //Stop execution
      return;
    }
    //If the user has no pending work
    if(workgroups.length == 0 || checkForSubmittedData(workgroups)) {
      //Find non full work groups
      WorkGroup.findOne(
        {
          //All conditions should be true
          $and: [
            //Only workgroups which are not full
            {['workers.' + groupMax]: {$exists: false}},
            //And not in the workers section of the work group
            {'workers': {$ne: userID}}
            }
          ]
        },
        function(error, workgroup) {
          //Pass database errors onto the client
          if(error) {
            Error.sendError(socket, 'DATABASE_GENERIC', true);
            //Stop execution
            return;
          }
          //If there are no non full work groups, create a new work group
          if(!workgroup) {
            //Emit a create_work event and pass in a callback with a work
            //argument
            eventEmitter.emit('create_work', function(work) {
              //If there's no work left
              if(!work) {
                Error.sendError(socket, 'STAGE_REQUEST_NO_WORK', true);
                //Stop execution
                return;
              }<commit_msg>Fixed query having too many closing curly braces<commit_after>//Import error handler
var Error = require('../../Utils/Error.js');
//Import AES library
var AES = require('../../Utils/AES.js');

//Schema imports
var Worker = require('../MongoSchemas/Worker.js');
var WorkGroup = require('../MongoSchemas/WorkGroup.js');

var checkForSubmittedData = function(array, userID) {
  for(var i = 0; i < array.length; i++) {
    for(var j = 0; j < array[i].data.length; j++) {
      if(array[j].worker != userID) {
        return false;
      }
    }
  }
  return true;
}

//Export the request handler
module.exports = function(message, mongoose, socket, eventEmitter, key, userID, groupMax){
  //Get encryption information
  var payload = message.payload;
  var tag = message.tag;
  var iv = message.iv;
  //Declare decrypted variable for try/catch
  var decrypted;
  //Try decrypting, otherwise pass error onto user
  try {
    decrypted = JSON.parse(AES.decrypt(key, iv, tag, payload));
  } catch (e) {
    Error.sendError(socket, 'SECURITY_DECRYPTION_FAILURE', true);
    return;
  }
  if(!decrypted ||
    !decrypted.request ||
    decrypted.request.toUpperCase() !== 'REQUEST') {
      //Stop execution, we cannot verify the user...
      Error.sendError(socket, 'STAGE_HANDSHAKE_POST_COMPLETE_FAILURE', true);
      return;
  }
  //Make sure that the user does not currently have a work group that they have
  //not submitted work to
  WorkGroup.find({workers: userID}, function(error, workgroups) {
    //Pass database errors onto client
    if(error) {
      Error.sendError(socket, 'DATABASE_GENERIC', true);
      //Stop execution
      return;
    }
    //If the user has no pending work
    if(workgroups.length == 0 || checkForSubmittedData(workgroups)) {
      //Find non full work groups
      WorkGroup.findOne(
        {
          //All conditions should be true
          $and: [
            //Only workgroups which are not full
            {['workers.' + groupMax]: {$exists: false}},
            //And not in the workers section of the work group
            {'workers': {$ne: userID}}
          ]
        },
        function(error, workgroup) {
          //Pass database errors onto the client
          if(error) {
            Error.sendError(socket, 'DATABASE_GENERIC', true);
            //Stop execution
            return;
          }
          //If there are no non full work groups, create a new work group
          if(!workgroup) {
            //Emit a create_work event and pass in a callback with a work
            //argument
            eventEmitter.emit('create_work', function(work) {
              //If there's no work left
              if(!work) {
                Error.sendError(socket, 'STAGE_REQUEST_NO_WORK', true);
                //Stop execution
                return;
              }
{'fraction_non_alphanumeric': 0.11933131780188512, 'fraction_numerical': 0.0010670460608216254, 'mean_word_length': 2.222922636103152, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1964278', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1596, 'n_words': 495}