<commit_before>var express = require('express');
var router = express.Router();
var db = require('../db');
var conf = require('../../../config');

router.get('/users/:id', function(req, res) {
  if (!req.params.id) {
    res.json({
      status: false,
      level: "danger",
      msg: "Не задан идентификатор пользователя."
    });
    return;
  }

  if (!req.session.login) {
    findUserById(req.params.id, false, res);
    return;
  }

  db.findUserByEmail(req.session.email, function(err, user) {
    if (err || !user || !user.isAdministrator) {
      findUserById(req.params.id, false, res);
    } else {
      findUserById(req.params.id, true, res);
    }
  });
});

function findUserById(id, admin, res) {
  db.findUserByIdWithoutPassword(id, admin, function(err, user) {
    if (err) {
      var msg = 'Ошибка базы данных' 
        + (conf.mode !== 'production' ? ': ' + err.message : '.');
      res.json({
        status: false,
        level: "danger",
        msg: msg
      });
      return;
    }
    
    if (!user) {
      res.json({
        status: false,
        level: "info",
        msg: "Пользователь не найден."
      });
      return;
    }
    
    user.id = user._id;
    delete user._id;
    
    res.json({
      status: true,
      level: "success",
      msg: "Пользователь найден.",
      user: user
    });
  });
}


router.get('/users/', function(req, res, next) {
  if (!req.session.login) {
    findAllUsers(false, res);
    return;
  }
  
  db.findUserByEmail(req.session.email, function(err, user) {
    if (err || !user || !user.isAdministrator) {
      findAllUsers(false, res);
    } else {
      findAllUsers(true, res);
    }
  });
});

function findAllUsers(admin, res) {
  db.findAllUsersWithoutPassword(admin, function(err, users) {
    if (err) {
      var msg = 'Ошибка базы данных' 
        + (conf.mode !== 'production' ? ': ' + err.message : '.');
      res.json({
        status: false,
        level: "danger",
        msg: msg
      });
      return;
    }
    
    res.json({
      status: true,
      level: "success",
      msg: "Список всех пользователей получен.",
      users: users
    });
  });
}
  
router.post('/findUserByEmail', function(req, res, next) {
  if(!req.body.email) {
    res.json({
      status: false,
      level: "danger",
      msg: "Укажите email!"
    })
    return;
  }
  if(!req.session.login) {
    findUserByEmail(req.body.email, false, res);
    return;
  }
  db.findUserByEmail(req.session.email, function(err, user) {
    if(err || !user || !user.isAdministrator) {
      findUserByEmail(req.body.email, false, res);
      return;
    } else {
      findUserByEmail(req.body.email, true, res);
      return;
    }
  })
});

function findUserByEmail(email, admin, res) {
  db.findUserByEmailWithoutPassword(email, admin, function(err, user) {
    if (err) {
      var msg = 'Ошибка базы данных' 
        + (conf.mode !== 'production' ? ': ' + err.message : '.');
      res.json({
        status: false,
        level: "danger",
        msg: msg
      });
      return;
    }
    
    if (!user) {
      res.json({
        status: false,
        level: "info",
        msg: "Пользователь не найден."
      });
      return;
    }
    
    res.json({
      status: true,
      level: "success",
      msg: "Пользователь найден.",
      user: user
    });
  });
}<commit_msg>POST /users/findUserByEmail -> GET /users/users/?email=<email><commit_after>var express = require('express');
var router = express.Router();
var db = require('../db');
var conf = require('../../../config');

router.get('/users/:id', function(req, res) {
  if (!req.params.id) {
    res.json({
      status: false,
      level: "danger",
      msg: "Не задан идентификатор пользователя."
    });
    return;
  }

  if (!req.session.login) {
    findUserById(req.params.id, false, res);
    return;
  }

  db.findUserByEmail(req.session.email, function(err, user) {
    if (err || !user || !user.isAdministrator) {
      findUserById(req.params.id, false, res);
    } else {
      findUserById(req.params.id, true, res);
    }
  });
});

function findUserById(id, admin, res) {
  db.findUserByIdWithoutPassword(id, admin, function(err, user) {
    if (err) {
      var msg = 'Ошибка базы данных' 
        + (conf.mode !== 'production' ? ': ' + err.message : '.');
      res.json({
        status: false,
        level: "danger",
        msg: msg
      });
      return;
    }
    
    if (!user) {
      res.json({
        status: false,
        level: "info",
        msg: "Пользователь не найден."
      });
      return;
    }
    
    user.id = user._id;
    delete user._id;
    
    res.json({
      status: true,
      level: "success",
      msg: "Пользователь найден.",
      user: user
    });
  });
}


router.get('/users/', function(req, res, next) {
  if (!!req.query['email']) { // by email
    if (!req.session.login) {
      findUserByEmail(req.query['email'], false, res);
      return;
    }
    
    db.findUserByEmail(req.session.email, function(err, user) {
      if (err || !user || !user.isAdministrator) {
        findUserByEmail(req.query['email'], false, res);
      } else {
        findUserByEmail(req.query['email'], true, res);
      }
    });
  }
  else { // all
    if (!req.session.login) {
      findAllUsers(false, res);
      return;
    }

    db.findUserByEmail(req.session.email, function(err, user) {
      if (err || !user || !user.isAdministrator) {
        findAllUsers(false, res);
      } else {
        findAllUsers(true, res);
      }
    });
  }
});

function findAllUsers(admin, res) {
  db.findAllUsersWithoutPassword(admin, function(err, users) {
    if (err) {
      var msg = 'Ошибка базы данных' 
        + (conf.mode !== 'production' ? ': ' + err.message : '.');
      res.json({
        status: false,
        level: "danger",
        msg: msg
      });
      return;
    }
    
    res.json({
      status: true,
      level: "success",
      msg: "Список всех пользователей получен.",
      users: users
    });
  });
}

function findUserByEmail(email, admin, res) {
  db.findUserByEmailWithoutPassword(email, admin, function(err, user) {
    if (err) {
      var msg = 'Ошибка базы данных' 
        + (conf.mode !== 'production' ? ': ' + err.message : '.');
      res.json({
        status: false,
        level: "danger",
        msg: msg
      });
      return;
    }
    
    if (!user) {
      res.json({
        status: false,
        level: "info",
        msg: "Пользователь не найден."
      });
      return;
    }
    
    res.json({
      status: true,
      level: "success",
      msg: "Пользователь найден.",
      user: user
    });
  });
}
{'fraction_non_alphanumeric': 0.16734082397003747, 'fraction_numerical': 0.0, 'mean_word_length': 2.072250345144961, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '5189308', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2332, 'n_words': 473}