4d02dfac2fe3545cc879ccdaa5b1c73674150fe0
[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 _0xb336=['catch','show','params','keys','include','options','find','create','body','describe','html-pdf','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','util','sox','ejs','lodash','squel','crypto','jsforce','to-csv','../../components/auth/service','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','end','status','json','offset','set','Content-Range','reject','save','update','then','destroy','stack','name','index','map','Cdr','rawAttributes','fieldName','type','key','model','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','order','where','pick','filter','merge','VIRTUAL','count','includeAll'];(function(_0x4a3b99,_0x1c33e7){var _0x42db20=function(_0x30e72b){while(--_0x30e72b){_0x4a3b99['push'](_0x4a3b99['shift']());}};_0x42db20(++_0x1c33e7);}(_0xb336,0x168));var _0x6b33=function(_0x16e5d1,_0x224246){_0x16e5d1=_0x16e5d1-0x0;var _0xd837fe=_0xb336[_0x16e5d1];return _0xd837fe;};'use strict';var pdf=require(_0x6b33('0x0'));var emlformat=require(_0x6b33('0x1'));var rimraf=require(_0x6b33('0x2'));var zipdir=require('zip-dir');var jsonpatch=require(_0x6b33('0x3'));var rp=require(_0x6b33('0x4'));var moment=require(_0x6b33('0x5'));var BPromise=require(_0x6b33('0x6'));var Mustache=require('mustache');var util=require(_0x6b33('0x7'));var path=require('path');var sox=require(_0x6b33('0x8'));var csv=require('to-csv');var ejs=require(_0x6b33('0x9'));var fs=require('fs');var _=require(_0x6b33('0xa'));var squel=require(_0x6b33('0xb'));var crypto=require(_0x6b33('0xc'));var jsforce=require(_0x6b33('0xd'));var deskjs=require('desk.js');var toCsv=require(_0x6b33('0xe'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x6b33('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x6b33('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x6b33('0x11'))('api');var utils=require(_0x6b33('0x12'));var config=require('../../config/environment');var licenseUtil=require(_0x6b33('0x13'));var db=require(_0x6b33('0x14'))['db'];function respondWithStatusCode(_0x223fd4,_0x270bd1){_0x270bd1=_0x270bd1||0xcc;return function(_0x5914e9){if(_0x5914e9){return _0x223fd4[_0x6b33('0x15')](_0x270bd1);}return _0x223fd4['status'](_0x270bd1)[_0x6b33('0x16')]();};}function respondWithResult(_0x457458,_0x15d11f){_0x15d11f=_0x15d11f||0xc8;return function(_0x2997ed){if(_0x2997ed){return _0x457458[_0x6b33('0x17')](_0x15d11f)[_0x6b33('0x18')](_0x2997ed);}};}function respondWithFilteredResult(_0x2ca2c3,_0x2be916){return function(_0x473778){if(_0x473778){var _0xbc0be0=_0x473778['count'],_0x65ce4a=_0x2be916[_0x6b33('0x19')],_0x146188=_0x2be916[_0x6b33('0x19')]+_0x2be916['limit'],_0x10b9cb;if(_0x146188>=_0xbc0be0){_0x146188=_0xbc0be0;_0x10b9cb=0xc8;}else{_0x10b9cb=0xce;}_0x2ca2c3['status'](_0x10b9cb);return _0x2ca2c3[_0x6b33('0x1a')](_0x6b33('0x1b'),_0x65ce4a+'-'+_0x146188+'/'+_0xbc0be0)[_0x6b33('0x18')](_0x473778);}return null;};}function patchUpdates(_0x34faee){return function(_0x4deacd){try{jsonpatch['apply'](_0x4deacd,_0x34faee,!![]);}catch(_0x3a0570){return BPromise[_0x6b33('0x1c')](_0x3a0570);}return _0x4deacd[_0x6b33('0x1d')]();};}function saveUpdates(_0x4cdf30,_0x1b9cee){return function(_0x326f10){if(_0x326f10){return _0x326f10[_0x6b33('0x1e')](_0x4cdf30)[_0x6b33('0x1f')](function(_0x4d91bc){return _0x4d91bc;});}return null;};}function removeEntity(_0x3ef5be,_0xd024ee){return function(_0x233051){if(_0x233051){return _0x233051[_0x6b33('0x20')]()[_0x6b33('0x1f')](function(){_0x3ef5be['status'](0xcc)[_0x6b33('0x16')]();});}};}function handleEntityNotFound(_0x5f3c46,_0x396422){return function(_0x5f5ba4){if(!_0x5f5ba4){_0x5f3c46[_0x6b33('0x15')](0x194);}return _0x5f5ba4;};}function handleError(_0x255fc4,_0x15a4e4){_0x15a4e4=_0x15a4e4||0x1f4;return function(_0x26b7d9){logger['error'](_0x26b7d9[_0x6b33('0x21')]);if(_0x26b7d9['name']){delete _0x26b7d9[_0x6b33('0x22')];}_0x255fc4[_0x6b33('0x17')](_0x15a4e4)['send'](_0x26b7d9);};}exports[_0x6b33('0x23')]=function(_0x59330a,_0x58ad16){var _0x4e8659={},_0x2c8ac8={},_0xb8007e={'count':0x0,'rows':[]};var _0x55170e=_[_0x6b33('0x24')](db[_0x6b33('0x25')][_0x6b33('0x26')],function(_0x30cb5f){return{'name':_0x30cb5f[_0x6b33('0x27')],'type':_0x30cb5f[_0x6b33('0x28')][_0x6b33('0x29')]};});_0x2c8ac8[_0x6b33('0x2a')]=_['map'](_0x55170e,_0x6b33('0x22'));_0x2c8ac8[_0x6b33('0x2b')]=_['keys'](_0x59330a[_0x6b33('0x2b')]);_0x2c8ac8[_0x6b33('0x2c')]=_[_0x6b33('0x2d')](_0x2c8ac8[_0x6b33('0x2a')],_0x2c8ac8[_0x6b33('0x2b')]);_0x4e8659['attributes']=_[_0x6b33('0x2d')](_0x2c8ac8[_0x6b33('0x2a')],qs[_0x6b33('0x2e')](_0x59330a[_0x6b33('0x2b')]['fields']));_0x4e8659[_0x6b33('0x2f')]=_0x4e8659[_0x6b33('0x2f')][_0x6b33('0x30')]?_0x4e8659['attributes']:_0x2c8ac8[_0x6b33('0x2a')];if(!_0x59330a['query'][_0x6b33('0x31')](_0x6b33('0x32'))){_0x4e8659[_0x6b33('0x33')]=qs[_0x6b33('0x33')](_0x59330a[_0x6b33('0x2b')][_0x6b33('0x33')]);_0x4e8659[_0x6b33('0x19')]=qs[_0x6b33('0x19')](_0x59330a[_0x6b33('0x2b')]['offset']);}_0x4e8659[_0x6b33('0x34')]=qs['sort'](_0x59330a[_0x6b33('0x2b')]['sort']);_0x4e8659[_0x6b33('0x35')]=qs['filters'](_[_0x6b33('0x36')](_0x59330a['query'],_0x2c8ac8[_0x6b33('0x2c')]),_0x55170e);if(_0x59330a[_0x6b33('0x2b')][_0x6b33('0x37')]){_0x4e8659[_0x6b33('0x35')]=_[_0x6b33('0x38')](_0x4e8659[_0x6b33('0x35')],{'$or':_[_0x6b33('0x24')](_0x55170e,function(_0x2531d5){if(_0x2531d5[_0x6b33('0x28')]!==_0x6b33('0x39')){var _0x1f45b4={};_0x1f45b4[_0x2531d5[_0x6b33('0x22')]]={'$like':'%'+_0x59330a[_0x6b33('0x2b')][_0x6b33('0x37')]+'%'};return _0x1f45b4;}})});}_0x4e8659=_['merge']({},_0x4e8659,_0x59330a['options']);var _0x265c8c={'where':_0x4e8659[_0x6b33('0x35')]};return db[_0x6b33('0x25')][_0x6b33('0x3a')](_0x265c8c)[_0x6b33('0x1f')](function(_0x61635c){_0xb8007e['count']=_0x61635c;if(_0x59330a[_0x6b33('0x2b')][_0x6b33('0x3b')]){_0x4e8659['include']=[{'all':!![]}];}return db[_0x6b33('0x25')]['findAll'](_0x4e8659);})[_0x6b33('0x1f')](function(_0x56548f){_0xb8007e['rows']=_0x56548f;return _0xb8007e;})['then'](respondWithFilteredResult(_0x58ad16,_0x4e8659))[_0x6b33('0x3c')](handleError(_0x58ad16,null));};exports[_0x6b33('0x3d')]=function(_0xb67d82,_0x7d1c56){var _0x7e6d8b={'raw':!![],'where':{'id':_0xb67d82[_0x6b33('0x3e')]['id']}},_0x4283c6={};_0x4283c6['model']=_[_0x6b33('0x3f')](db['Cdr']['rawAttributes']);_0x4283c6['query']=_['keys'](_0xb67d82[_0x6b33('0x2b')]);_0x4283c6[_0x6b33('0x2c')]=_['intersection'](_0x4283c6[_0x6b33('0x2a')],_0x4283c6['query']);_0x7e6d8b[_0x6b33('0x2f')]=_['intersection'](_0x4283c6['model'],qs[_0x6b33('0x2e')](_0xb67d82['query'][_0x6b33('0x2e')]));_0x7e6d8b['attributes']=_0x7e6d8b[_0x6b33('0x2f')]['length']?_0x7e6d8b[_0x6b33('0x2f')]:_0x4283c6[_0x6b33('0x2a')];if(_0xb67d82[_0x6b33('0x2b')][_0x6b33('0x3b')]){_0x7e6d8b[_0x6b33('0x40')]=[{'all':!![]}];}_0x7e6d8b=_['merge']({},_0x7e6d8b,_0xb67d82[_0x6b33('0x41')]);return db[_0x6b33('0x25')][_0x6b33('0x42')](_0x7e6d8b)[_0x6b33('0x1f')](handleEntityNotFound(_0x7d1c56,null))[_0x6b33('0x1f')](respondWithResult(_0x7d1c56,null))[_0x6b33('0x3c')](handleError(_0x7d1c56,null));};exports[_0x6b33('0x43')]=function(_0x4377d9,_0x3b0465){return db['Cdr'][_0x6b33('0x43')](_0x4377d9[_0x6b33('0x44')],{})[_0x6b33('0x1f')](respondWithResult(_0x3b0465,0xc9))[_0x6b33('0x3c')](handleError(_0x3b0465,null));};exports[_0x6b33('0x1e')]=function(_0x55cc5c,_0x13b4c3){if(_0x55cc5c[_0x6b33('0x44')]['id']){delete _0x55cc5c[_0x6b33('0x44')]['id'];}return db[_0x6b33('0x25')]['find']({'where':{'id':_0x55cc5c[_0x6b33('0x3e')]['id']}})[_0x6b33('0x1f')](handleEntityNotFound(_0x13b4c3,null))['then'](saveUpdates(_0x55cc5c[_0x6b33('0x44')],null))[_0x6b33('0x1f')](respondWithResult(_0x13b4c3,null))[_0x6b33('0x3c')](handleError(_0x13b4c3,null));};exports[_0x6b33('0x20')]=function(_0x4c9ec2,_0x23eb9d){return db[_0x6b33('0x25')][_0x6b33('0x42')]({'where':{'id':_0x4c9ec2[_0x6b33('0x3e')]['id']}})[_0x6b33('0x1f')](handleEntityNotFound(_0x23eb9d,null))['then'](removeEntity(_0x23eb9d,null))['catch'](handleError(_0x23eb9d,null));};exports[_0x6b33('0x45')]=function(_0x196fc1,_0x22449e){return db[_0x6b33('0x25')][_0x6b33('0x45')]()['then'](respondWithResult(_0x22449e,null))[_0x6b33('0x3c')](handleError(_0x22449e,null));};