<commit_before>
"usse strict";

var express = require('express'),
Bluebird    = require('bluebird'),
validator   = require('validator'),
_           = require('underscore'),
router      = express.Router(),
Pager = require('./utils/pager')();

// Make sure that current user is authorized to deal with settings
router.all(/.*/, require('../middleware/ensure_user_is_admin'));

router.get(/email/, function(req, res){

  res.locals.custom_java_script.push( '/js/bootstrap-datepicker.js' );
  res.locals.custom_css.push( '/css/bootstrap-datepicker3.standalone.css' );

  var user_id = validator.toInt(req.param('user_id')),
  start_date  = validator.trim(req.param('start_date')),
  end_date    = validator.trim(req.param('end_date')),
  page        = validator.toInt(req.param('page')) || 1,
  model       = req.app.get('db_model'),
  filter      = {
    company_id: req.user.companyId,
  };

  var items_per_page = Pager.items_per_page;

  // if there is a valid start date provided pass it to the filter
  if ( validator.isDate(start_date) ) {
    if (! filter.hasOwnProperty('created_at')) filter.created_at = {};
    filter.created_at['$gte'] = start_date;
  }

  // ... same for end date
  if ( validator.isDate(end_date) ) {
    if (! filter.hasOwnProperty('created_at')) filter.created_at = {};
    filter.created_at['$lte'] = end_date;
  }

  if ( validator.isInt(user_id) ) {
    filter.user_id = user_id;
  }

  var promise_emails = model.EmailAudit.findAndCountAll({
      where    : filter,

      limit : items_per_page,
      offset : items_per_page * (page - 1),

      include : [{
        model : model.User,
        as    : 'user',
      }]
  });

  var promise_all_users = model.User.findAll({
    where : {
      companyId : req.user.companyId,
    },
    order : [
      ['lastname']
    ],
  });

  Bluebird.join(
    promise_emails,
    promise_all_users,
    function(email_result, all_users){
<commit_msg>Change the order email audit records are shown.<commit_after>
"usse strict";

var express = require('express'),
Bluebird    = require('bluebird'),
validator   = require('validator'),
_           = require('underscore'),
router      = express.Router(),
Pager = require('./utils/pager')();

// Make sure that current user is authorized to deal with settings
router.all(/.*/, require('../middleware/ensure_user_is_admin'));

router.get(/email/, function(req, res){

  res.locals.custom_java_script.push( '/js/bootstrap-datepicker.js' );
  res.locals.custom_css.push( '/css/bootstrap-datepicker3.standalone.css' );

  var user_id = validator.toInt(req.param('user_id')),
  start_date  = validator.trim(req.param('start_date')),
  end_date    = validator.trim(req.param('end_date')),
  page        = validator.toInt(req.param('page')) || 1,
  model       = req.app.get('db_model'),
  filter      = {
    company_id: req.user.companyId,
  };

  var items_per_page = Pager.items_per_page;

  // if there is a valid start date provided pass it to the filter
  if ( validator.isDate(start_date) ) {
    if (! filter.hasOwnProperty('created_at')) filter.created_at = {};
    filter.created_at['$gte'] = start_date;
  }

  // ... same for end date
  if ( validator.isDate(end_date) ) {
    if (! filter.hasOwnProperty('created_at')) filter.created_at = {};
    filter.created_at['$lte'] = end_date;
  }

  if ( validator.isInt(user_id) ) {
    filter.user_id = user_id;
  }

  var promise_emails = model.EmailAudit.findAndCountAll({
      where    : filter,

      limit : items_per_page,
      offset : items_per_page * (page - 1),
      order : [
        [ 'id', 'DESC']
      ],

      include : [{
        model : model.User,
        as    : 'user',
      }]
  });

  var promise_all_users = model.User.findAll({
    where : {
      companyId : req.user.companyId,
    },
    order : [
      ['lastname']
    ],
  });

  Bluebird.join(
    promise_emails,
    promise_all_users,
    function(email_result, all_users){

{'fraction_non_alphanumeric': 0.15283066768215284, 'fraction_numerical': 0.0015232292460015233, 'mean_word_length': 3.0286298568507157, 'pattern_counts': {'":': 0, '<': 3, '<?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': '306094', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1322, 'n_words': 244}