<commit_before>'use strict';

var _ = require('lodash');
var moment = require('moment');
require('moment-timezone');
var Promise = require('bluebird');
var excel4node = require('excel4node');
var Packer = require('zip-stream');
var Bookshelf = require('../db');

var path = require('path');
var fs = require('fs');

var i18n = require('../i18n');

var CDR = require('../models/CDR');
var config = require('../config');

var getRecording = Promise.promisify(require('../getRecording'));

var toLocalTZ, fromLocalTZ;

if (config.tz.match(/^[+-]\d{4}$/)) {
  // utcOffset
  toLocalTZ = m => m.utcOffset(config.tz);
  fromLocalTZ = s => moment(s).utcOffset(config.tz, true);
} else {
  // tz
  if (!moment.tz.zone(config.tz)) {
    console.log(`WebCDR failed to start: incorrect timezone ${config.tz}`);
    process.exit(1);
  }

  toLocalTZ = m => m.tz(config.tz);
  fromLocalTZ = s => moment.tz(s, config.tz);
}

var router = require('express').Router();

router.get('/cdrs', function (req, res) {
  console.log('query', req.query);
  var page = parseInt(req.query.page, 10);
  var perPage = parseInt(req.query.per_page, 10);

  var filter = function () {
    this.where(function () {
      if (req.query.number) {
        var like = ['%', req.query.number, '%'].join('');
        this.where('src', 'like', like)
          .orWhere('dst', 'like', like);
      } else {
        this.whereRaw('1=1');
      }
    }).andWhere(function () {
      if (req.query.status) {
        this.whereIn('disposition', req.query.status);
      } else {
        this.whereRaw('1=1');
      }
    }).andWhere(function () {
      var df = 'YYYY-MM-DD HH:mm:ss'; // mysql format

      var start = req.query.start
        ? toLocalTZ(moment(req.query.start))
        : toLocalTZ(moment()).startOf('day');

      var end = req.query.end
        ? toLocalTZ(moment(req.query.end))
        : toLocalTZ(moment()).endOf('day');

      this.whereBetween('calldate', [start.format(df), end.format(df)]);
    }).andWhere(function () {
      if (req.query.direction) {
        var sqlDirection = req.query.direction.map(function (d) {
          switch (d) {
            case 'in': return '(LENGTH(src) > 5 AND LENGTH(dst) <= 5 )';
            case 'out': return '(LENGTH(src) <= 5 AND LENGTH(dst) > 5 )';
            case 'int': return '(LENGTH(src) <= 5 AND LENGTH(dst) <= 5 )';
          }
        }).join(' OR ');
        this.whereRaw(sqlDirection);
      } else {
        this.whereRaw('1=1');
      }
    }).andWhere(function () {
      if (req.user.acl) {
        this.where(function () {
          this.whereIn('src', req.user.acl);
        }).orWhere(function () {
          this.whereIn('dst', req.user.acl);
        });
        if (req.user.acl_in) {
          this.orWhere(function () {
            this.where('direction', 'in');
          });
        }
      } else {
        this.whereRaw('1=1');
      }
    });
  };

  var countPromise = Bookshelf.knex(config.cdr.table).select(Bookshelf.knex.raw('COUNT(*) AS cnt'));
  filter.call(countPromise);

  var dataPromise = CDR.collection()
    .query(filter)
    .query(function (qb) {
      if (page && perPage) {
        qb.offset((page - 1) * perPage);
        qb.limit(perPage);<commit_msg>Fix displaying incoming calls for users with ACLs<commit_after>'use strict';

var _ = require('lodash');
var moment = require('moment');
require('moment-timezone');
var Promise = require('bluebird');
var excel4node = require('excel4node');
var Packer = require('zip-stream');
var Bookshelf = require('../db');

var path = require('path');
var fs = require('fs');

var i18n = require('../i18n');

var CDR = require('../models/CDR');
var config = require('../config');

var getRecording = Promise.promisify(require('../getRecording'));

var toLocalTZ, fromLocalTZ;

if (config.tz.match(/^[+-]\d{4}$/)) {
  // utcOffset
  toLocalTZ = m => m.utcOffset(config.tz);
  fromLocalTZ = s => moment(s).utcOffset(config.tz, true);
} else {
  // tz
  if (!moment.tz.zone(config.tz)) {
    console.log(`WebCDR failed to start: incorrect timezone ${config.tz}`);
    process.exit(1);
  }

  toLocalTZ = m => m.tz(config.tz);
  fromLocalTZ = s => moment.tz(s, config.tz);
}

var router = require('express').Router();

router.get('/cdrs', function (req, res) {
  console.log('query', req.query);
  var page = parseInt(req.query.page, 10);
  var perPage = parseInt(req.query.per_page, 10);

  var filter = function () {
    this.where(function () {
      if (req.query.number) {
        var like = ['%', req.query.number, '%'].join('');
        this.where('src', 'like', like)
          .orWhere('dst', 'like', like);
      } else {
        this.whereRaw('1=1');
      }
    }).andWhere(function () {
      if (req.query.status) {
        this.whereIn('disposition', req.query.status);
      } else {
        this.whereRaw('1=1');
      }
    }).andWhere(function () {
      var df = 'YYYY-MM-DD HH:mm:ss'; // mysql format

      var start = req.query.start
        ? toLocalTZ(moment(req.query.start))
        : toLocalTZ(moment()).startOf('day');

      var end = req.query.end
        ? toLocalTZ(moment(req.query.end))
        : toLocalTZ(moment()).endOf('day');

      this.whereBetween('calldate', [start.format(df), end.format(df)]);
    }).andWhere(function () {
      if (req.query.direction) {
        var sqlDirection = req.query.direction.map(function (d) {
          switch (d) {
            case 'in': return '(LENGTH(src) > 5 AND LENGTH(dst) <= 5 )';
            case 'out': return '(LENGTH(src) <= 5 AND LENGTH(dst) > 5 )';
            case 'int': return '(LENGTH(src) <= 5 AND LENGTH(dst) <= 5 )';
          }
        }).join(' OR ');
        this.whereRaw(sqlDirection);
      } else {
        this.whereRaw('1=1');
      }
    }).andWhere(function () {
      if (req.user.acl) {
        this.where(function () {
          this.whereIn('src', req.user.acl);
        }).orWhere(function () {
          this.whereIn('dst', req.user.acl);
        });
        if (req.user.acl_in) {
          // allow to see inbound calls in addition to ACLed numbers
          this.orWhere(function () {
            this.whereRaw('(LENGTH(src) > 5 AND LENGTH(dst) <= 5 )');
          });
        }
      } else {
        this.whereRaw('1=1');
      }
    });
  };

  var countPromise = Bookshelf.knex(config.cdr.table).select(Bookshelf.knex.raw('COUNT(*) AS cnt'));
  filter.call(countPromise);

  var dataPromise = CDR.collection()
    .query(filter)
    .query(function (qb) {
      if (page && perPage) {
        qb.offset((page - 1) * perPage);
        qb.limit(perPage);
{'fraction_non_alphanumeric': 0.19209726443768996, 'fraction_numerical': 0.00851063829787234, 'mean_word_length': 2.5477088948787063, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, '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': '2705918', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2211, 'n_words': 429}