<commit_before>const express = require('express');
const router = express.Router(); // eslint-disable-line new-cap
const async = require('async');

const UserModel = require('../models/user');
const authenticate = require('../middlewares/authenticate');
const authorise = require('../middlewares/authorise');
const resHelpers = require('./helpers/response-helpers');

router.use('/users', authenticate);

// Create user
router.post('/users', authorise('admin'), (req, res) => {
  UserModel.create(req.body, (err, user) => {
    if (err) {
      return res.json(resHelpers.validationFail(
        'The user could not be created. Check validation.',
        err.errors,
        req.body
      ));
    }

    const userObj = user.toObject();
    delete userObj.password;

    res.json(resHelpers.success(
      'The user was created.',
      userObj
    ));
  });
});

// List users
router.get('/users', authorise('admin'), (req, res) => {
  const page = req.query.page || 1;
  const limit = 20;

  async.parallel({
    total: function total(cb) {
      UserModel.count(cb);
    },
    users: function users(cb) {
      UserModel
        .find()
        .select('name username createdAt updatedAt')
        .skip(limit * (page - 1))
        .limit(limit)
        .lean()<commit_msg>users: return role along with users<commit_after>const express = require('express');
const router = express.Router(); // eslint-disable-line new-cap
const async = require('async');

const UserModel = require('../models/user');
const authenticate = require('../middlewares/authenticate');
const authorise = require('../middlewares/authorise');
const resHelpers = require('./helpers/response-helpers');

router.use('/users', authenticate);

// Create user
router.post('/users', authorise('admin'), (req, res) => {
  UserModel.create(req.body, (err, user) => {
    if (err) {
      return res.json(resHelpers.validationFail(
        'The user could not be created. Check validation.',
        err.errors,
        req.body
      ));
    }

    const userObj = user.toObject();
    delete userObj.password;

    res.json(resHelpers.success(
      'The user was created.',
      userObj
    ));
  });
});

// List users
router.get('/users', authorise('admin'), (req, res) => {
  const page = req.query.page || 1;
  const limit = 20;

  async.parallel({
    total: function total(cb) {
      UserModel.count(cb);
    },
    users: function users(cb) {
      UserModel
        .find()
        .select('name username role createdAt updatedAt')
        .skip(limit * (page - 1))
        .limit(limit)
        .lean()
{'fraction_non_alphanumeric': 0.16286266924564796, 'fraction_numerical': 0.003094777562862669, 'mean_word_length': 2.8887218045112784, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4086849', 'n_tokens_mistral': 782, 'n_tokens_neox': 773, 'n_words': 187}