42d7325afe04feaee1b746be0252a6b36e7b382c
[motion2.git] / server / api / cdr / cdr.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x285f=['rows','show','keys','find','catch','create','body','update','params','describe','html-pdf','eml-format','rimraf','zip-dir','fast-json-patch','bluebird','mustache','util','sox','lodash','squel','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../mysqldb','sendStatus','status','end','json','count','offset','limit','set','Content-Range','apply','reject','then','destroy','error','stack','send','index','map','rawAttributes','fieldName','type','key','model','name','query','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','merge','VIRTUAL','options','Cdr','includeAll','include','findAll'];(function(_0x1318b2,_0x5e12be){var _0x51d93e=function(_0x45abbe){while(--_0x45abbe){_0x1318b2['push'](_0x1318b2['shift']());}};_0x51d93e(++_0x5e12be);}(_0x285f,0x142));var _0xf285=function(_0x4f4a34,_0x15cdf0){_0x4f4a34=_0x4f4a34-0x0;var _0x297e28=_0x285f[_0x4f4a34];return _0x297e28;};'use strict';var pdf=require(_0xf285('0x0'));var emlformat=require(_0xf285('0x1'));var rimraf=require(_0xf285('0x2'));var zipdir=require(_0xf285('0x3'));var jsonpatch=require(_0xf285('0x4'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xf285('0x5'));var Mustache=require(_0xf285('0x6'));var util=require(_0xf285('0x7'));var path=require('path');var sox=require(_0xf285('0x8'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var _=require(_0xf285('0x9'));var squel=require(_0xf285('0xa'));var crypto=require('crypto');var jsforce=require(_0xf285('0xb'));var deskjs=require(_0xf285('0xc'));var toCsv=require(_0xf285('0xd'));var querystring=require(_0xf285('0xe'));var Papa=require(_0xf285('0xf'));var Redis=require(_0xf285('0x10'));var authService=require(_0xf285('0x11'));var qs=require(_0xf285('0x12'));var as=require(_0xf285('0x13'));var hardwareService=require(_0xf285('0x14'));var logger=require('../../config/logger')(_0xf285('0x15'));var utils=require(_0xf285('0x16'));var config=require('../../config/environment');var db=require(_0xf285('0x17'))['db'];function respondWithStatusCode(_0x48ee38,_0x1c990d){_0x1c990d=_0x1c990d||0xcc;return function(_0x330083){if(_0x330083){return _0x48ee38[_0xf285('0x18')](_0x1c990d);}return _0x48ee38[_0xf285('0x19')](_0x1c990d)[_0xf285('0x1a')]();};}function respondWithResult(_0x403c2d,_0x532fd7){_0x532fd7=_0x532fd7||0xc8;return function(_0x3fc3dc){if(_0x3fc3dc){return _0x403c2d['status'](_0x532fd7)[_0xf285('0x1b')](_0x3fc3dc);}};}function respondWithFilteredResult(_0xe507fc,_0x4b6d7e){return function(_0x14997d){if(_0x14997d){var _0x3a913e=_0x14997d[_0xf285('0x1c')],_0x47dbfa=_0x4b6d7e[_0xf285('0x1d')],_0x174b76=_0x4b6d7e[_0xf285('0x1d')]+_0x4b6d7e[_0xf285('0x1e')],_0x1abe25;if(_0x174b76>=_0x3a913e){_0x174b76=_0x3a913e;_0x1abe25=0xc8;}else{_0x1abe25=0xce;}_0xe507fc['status'](_0x1abe25);return _0xe507fc[_0xf285('0x1f')](_0xf285('0x20'),_0x47dbfa+'-'+_0x174b76+'/'+_0x3a913e)['json'](_0x14997d);}return null;};}function patchUpdates(_0x2e08bb){return function(_0x486758){try{jsonpatch[_0xf285('0x21')](_0x486758,_0x2e08bb,!![]);}catch(_0x31ea8d){return BPromise[_0xf285('0x22')](_0x31ea8d);}return _0x486758['save']();};}function saveUpdates(_0x1c3a90,_0x39a693){return function(_0x5a169c){if(_0x5a169c){return _0x5a169c['update'](_0x1c3a90)[_0xf285('0x23')](function(_0x9e1ddc){return _0x9e1ddc;});}return null;};}function removeEntity(_0x802833,_0x3e4a14){return function(_0x4ca76c){if(_0x4ca76c){return _0x4ca76c[_0xf285('0x24')]()[_0xf285('0x23')](function(){_0x802833[_0xf285('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0x59e964,_0x44c6eb){return function(_0x46aeb5){if(!_0x46aeb5){_0x59e964['sendStatus'](0x194);}return _0x46aeb5;};}function handleError(_0x1b3e76,_0xc97436){_0xc97436=_0xc97436||0x1f4;return function(_0x58b6e2){logger[_0xf285('0x25')](_0x58b6e2[_0xf285('0x26')]);if(_0x58b6e2['name']){delete _0x58b6e2['name'];}_0x1b3e76[_0xf285('0x19')](_0xc97436)[_0xf285('0x27')](_0x58b6e2);};}exports[_0xf285('0x28')]=function(_0x3e77d0,_0x2b345b){var _0xbd24c8={},_0x322b06={},_0x4e7e31={'count':0x0,'rows':[]};var _0x31b8aa=_[_0xf285('0x29')](db['Cdr'][_0xf285('0x2a')],function(_0x3340ed){return{'name':_0x3340ed[_0xf285('0x2b')],'type':_0x3340ed[_0xf285('0x2c')][_0xf285('0x2d')]};});_0x322b06[_0xf285('0x2e')]=_[_0xf285('0x29')](_0x31b8aa,_0xf285('0x2f'));_0x322b06['query']=_['keys'](_0x3e77d0[_0xf285('0x30')]);_0x322b06['filters']=_[_0xf285('0x31')](_0x322b06[_0xf285('0x2e')],_0x322b06[_0xf285('0x30')]);_0xbd24c8['attributes']=_[_0xf285('0x31')](_0x322b06[_0xf285('0x2e')],qs['fields'](_0x3e77d0[_0xf285('0x30')][_0xf285('0x32')]));_0xbd24c8[_0xf285('0x33')]=_0xbd24c8[_0xf285('0x33')][_0xf285('0x34')]?_0xbd24c8[_0xf285('0x33')]:_0x322b06['model'];if(!_0x3e77d0['query'][_0xf285('0x35')](_0xf285('0x36'))){_0xbd24c8[_0xf285('0x1e')]=qs[_0xf285('0x1e')](_0x3e77d0[_0xf285('0x30')][_0xf285('0x1e')]);_0xbd24c8[_0xf285('0x1d')]=qs[_0xf285('0x1d')](_0x3e77d0[_0xf285('0x30')][_0xf285('0x1d')]);}_0xbd24c8[_0xf285('0x37')]=qs['sort'](_0x3e77d0[_0xf285('0x30')][_0xf285('0x38')]);_0xbd24c8['where']=qs[_0xf285('0x39')](_[_0xf285('0x3a')](_0x3e77d0[_0xf285('0x30')],_0x322b06[_0xf285('0x39')]),_0x31b8aa);if(_0x3e77d0[_0xf285('0x30')][_0xf285('0x3b')]){_0xbd24c8[_0xf285('0x3c')]=_[_0xf285('0x3d')](_0xbd24c8[_0xf285('0x3c')],{'$or':_[_0xf285('0x29')](_0x31b8aa,function(_0xe082ec){if(_0xe082ec[_0xf285('0x2c')]!==_0xf285('0x3e')){var _0x5250df={};_0x5250df[_0xe082ec[_0xf285('0x2f')]]={'$like':'%'+_0x3e77d0['query'][_0xf285('0x3b')]+'%'};return _0x5250df;}})});}_0xbd24c8=_[_0xf285('0x3d')]({},_0xbd24c8,_0x3e77d0[_0xf285('0x3f')]);var _0xc14c6={'where':_0xbd24c8[_0xf285('0x3c')]};return db[_0xf285('0x40')][_0xf285('0x1c')](_0xc14c6)[_0xf285('0x23')](function(_0x97029c){_0x4e7e31[_0xf285('0x1c')]=_0x97029c;if(_0x3e77d0[_0xf285('0x30')][_0xf285('0x41')]){_0xbd24c8[_0xf285('0x42')]=[{'all':!![]}];}return db[_0xf285('0x40')][_0xf285('0x43')](_0xbd24c8);})[_0xf285('0x23')](function(_0x2bb3b5){_0x4e7e31[_0xf285('0x44')]=_0x2bb3b5;return _0x4e7e31;})[_0xf285('0x23')](respondWithFilteredResult(_0x2b345b,_0xbd24c8))['catch'](handleError(_0x2b345b,null));};exports[_0xf285('0x45')]=function(_0x2a1bed,_0x562899){var _0x9091f4={'raw':!![],'where':{'id':_0x2a1bed['params']['id']}},_0x33b583={};_0x33b583['model']=_[_0xf285('0x46')](db[_0xf285('0x40')][_0xf285('0x2a')]);_0x33b583['query']=_[_0xf285('0x46')](_0x2a1bed[_0xf285('0x30')]);_0x33b583[_0xf285('0x39')]=_[_0xf285('0x31')](_0x33b583[_0xf285('0x2e')],_0x33b583[_0xf285('0x30')]);_0x9091f4[_0xf285('0x33')]=_[_0xf285('0x31')](_0x33b583[_0xf285('0x2e')],qs[_0xf285('0x32')](_0x2a1bed[_0xf285('0x30')][_0xf285('0x32')]));_0x9091f4['attributes']=_0x9091f4[_0xf285('0x33')][_0xf285('0x34')]?_0x9091f4[_0xf285('0x33')]:_0x33b583[_0xf285('0x2e')];if(_0x2a1bed[_0xf285('0x30')][_0xf285('0x41')]){_0x9091f4[_0xf285('0x42')]=[{'all':!![]}];}_0x9091f4=_[_0xf285('0x3d')]({},_0x9091f4,_0x2a1bed[_0xf285('0x3f')]);return db[_0xf285('0x40')][_0xf285('0x47')](_0x9091f4)[_0xf285('0x23')](handleEntityNotFound(_0x562899,null))[_0xf285('0x23')](respondWithResult(_0x562899,null))[_0xf285('0x48')](handleError(_0x562899,null));};exports[_0xf285('0x49')]=function(_0x91d3e2,_0x206d25){return db[_0xf285('0x40')]['create'](_0x91d3e2[_0xf285('0x4a')],{})[_0xf285('0x23')](respondWithResult(_0x206d25,0xc9))[_0xf285('0x48')](handleError(_0x206d25,null));};exports[_0xf285('0x4b')]=function(_0x886db4,_0x2c9cc0){if(_0x886db4[_0xf285('0x4a')]['id']){delete _0x886db4[_0xf285('0x4a')]['id'];}return db['Cdr'][_0xf285('0x47')]({'where':{'id':_0x886db4['params']['id']}})[_0xf285('0x23')](handleEntityNotFound(_0x2c9cc0,null))[_0xf285('0x23')](saveUpdates(_0x886db4[_0xf285('0x4a')],null))['then'](respondWithResult(_0x2c9cc0,null))[_0xf285('0x48')](handleError(_0x2c9cc0,null));};exports[_0xf285('0x24')]=function(_0x55db58,_0x419c52){return db[_0xf285('0x40')][_0xf285('0x47')]({'where':{'id':_0x55db58[_0xf285('0x4c')]['id']}})[_0xf285('0x23')](handleEntityNotFound(_0x419c52,null))[_0xf285('0x23')](removeEntity(_0x419c52,null))[_0xf285('0x48')](handleError(_0x419c52,null));};exports[_0xf285('0x4d')]=function(_0x217853,_0x4c4a97){return db['Cdr'][_0xf285('0x4d')]()[_0xf285('0x23')](respondWithResult(_0x4c4a97,null))[_0xf285('0x48')](handleError(_0x4c4a97,null));};