Built motion from commit a61a4a20.|2.5.46
[motion2.git] / server / api / squareRecording / squareRecording.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 _0xc7dd=['type','model','map','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','sort','where','pick','filter','merge','include','findAll','rows','catch','show','includeAll','options','find','create','body','update','download','../../components/encryptor','uniqueid','params','savePath','existsSync','ValidationError','SquareRecording\x20not\x20found','filename','extname','unlink','message','Not\x20found','isNil','Entity\x20not\x20found','info','cancellazione\x20fisica\x20documento\x20%s','File:\x20\x22','\x22\x20trovato\x20eseguo\x20la\x20cancellazione','unlinkSync','.cypher','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../mysqldb','sendStatus','status','end','offset','limit','count','Content-Range','json','apply','reject','save','then','destroy','error','stack','name','send','SquareRecording','rawAttributes','fieldName'];(function(_0x5f30b5,_0x44f528){var _0x37d206=function(_0x13c754){while(--_0x13c754){_0x5f30b5['push'](_0x5f30b5['shift']());}};_0x37d206(++_0x44f528);}(_0xc7dd,0x1a4));var _0xdc7d=function(_0x2c341e,_0x414d11){_0x2c341e=_0x2c341e-0x0;var _0x43c1e8=_0xc7dd[_0x2c341e];return _0x43c1e8;};'use strict';var emlformat=require(_0xdc7d('0x0'));var rimraf=require(_0xdc7d('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xdc7d('0x2'));var rp=require('request-promise');var moment=require(_0xdc7d('0x3'));var BPromise=require(_0xdc7d('0x4'));var Mustache=require(_0xdc7d('0x5'));var util=require(_0xdc7d('0x6'));var path=require(_0xdc7d('0x7'));var sox=require(_0xdc7d('0x8'));var csv=require('to-csv');var ejs=require(_0xdc7d('0x9'));var fs=require('fs');var fs_extra=require(_0xdc7d('0xa'));var _=require(_0xdc7d('0xb'));var squel=require('squel');var crypto=require(_0xdc7d('0xc'));var jsforce=require(_0xdc7d('0xd'));var deskjs=require(_0xdc7d('0xe'));var toCsv=require(_0xdc7d('0xf'));var querystring=require(_0xdc7d('0x10'));var Papa=require(_0xdc7d('0x11'));var Redis=require(_0xdc7d('0x12'));var authService=require('../../components/auth/service');var qs=require(_0xdc7d('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xdc7d('0x14'));var logger=require(_0xdc7d('0x15'))(_0xdc7d('0x16'));var utils=require(_0xdc7d('0x17'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0xdc7d('0x18'))['db'];function respondWithStatusCode(_0x3a1ca1,_0x2aed9e){_0x2aed9e=_0x2aed9e||0xcc;return function(_0x19cd76){if(_0x19cd76){return _0x3a1ca1[_0xdc7d('0x19')](_0x2aed9e);}return _0x3a1ca1[_0xdc7d('0x1a')](_0x2aed9e)[_0xdc7d('0x1b')]();};}function respondWithResult(_0x3778bf,_0x249867){_0x249867=_0x249867||0xc8;return function(_0x4594ff){if(_0x4594ff){return _0x3778bf[_0xdc7d('0x1a')](_0x249867)['json'](_0x4594ff);}};}function respondWithFilteredResult(_0x1a9e81,_0x7d7525){return function(_0xe45f40){if(_0xe45f40){var _0x266cfc=typeof _0x7d7525[_0xdc7d('0x1c')]==='undefined'&&typeof _0x7d7525[_0xdc7d('0x1d')]==='undefined';var _0x66064=_0xe45f40[_0xdc7d('0x1e')];var _0x4896f1=_0x266cfc?0x0:_0x7d7525[_0xdc7d('0x1c')];var _0x58e347=_0x266cfc?_0xe45f40['count']:_0x7d7525[_0xdc7d('0x1c')]+_0x7d7525['limit'];var _0x320ca3;if(_0x58e347>=_0x66064){_0x58e347=_0x66064;_0x320ca3=0xc8;}else{_0x320ca3=0xce;}_0x1a9e81['status'](_0x320ca3);return _0x1a9e81['set'](_0xdc7d('0x1f'),_0x4896f1+'-'+_0x58e347+'/'+_0x66064)[_0xdc7d('0x20')](_0xe45f40);}return null;};}function patchUpdates(_0x3f55d2){return function(_0x5bf66b){try{jsonpatch[_0xdc7d('0x21')](_0x5bf66b,_0x3f55d2,!![]);}catch(_0x2d268f){return BPromise[_0xdc7d('0x22')](_0x2d268f);}return _0x5bf66b[_0xdc7d('0x23')]();};}function saveUpdates(_0x1aeda1,_0x4a1d8c){return function(_0x977c9){if(_0x977c9){return _0x977c9['update'](_0x1aeda1)[_0xdc7d('0x24')](function(_0x2bea9c){return _0x2bea9c;});}return null;};}function removeEntity(_0x431e55,_0x34a141){return function(_0x342299){if(_0x342299){return _0x342299[_0xdc7d('0x25')]()[_0xdc7d('0x24')](function(){_0x431e55[_0xdc7d('0x1a')](0xcc)[_0xdc7d('0x1b')]();});}};}function handleEntityNotFound(_0x56d0b0,_0x4172f0){return function(_0x109d3e){if(!_0x109d3e){_0x56d0b0[_0xdc7d('0x19')](0x194);}return _0x109d3e;};}function handleError(_0x6308e7,_0x232923){_0x232923=_0x232923||0x1f4;return function(_0x5181c0){logger[_0xdc7d('0x26')](_0x5181c0[_0xdc7d('0x27')]);if(_0x5181c0[_0xdc7d('0x28')]){delete _0x5181c0[_0xdc7d('0x28')];}_0x6308e7[_0xdc7d('0x1a')](_0x232923)[_0xdc7d('0x29')](_0x5181c0);};}exports['index']=function(_0x120413,_0x121468){var _0x1566e4={},_0x180a13={},_0x5cdd63={'count':0x0,'rows':[]};var _0x495521=_['map'](db[_0xdc7d('0x2a')][_0xdc7d('0x2b')],function(_0x2896c8){return{'name':_0x2896c8[_0xdc7d('0x2c')],'type':_0x2896c8[_0xdc7d('0x2d')]['key']};});_0x180a13[_0xdc7d('0x2e')]=_[_0xdc7d('0x2f')](_0x495521,_0xdc7d('0x28'));_0x180a13[_0xdc7d('0x30')]=_[_0xdc7d('0x31')](_0x120413[_0xdc7d('0x30')]);_0x180a13[_0xdc7d('0x32')]=_[_0xdc7d('0x33')](_0x180a13[_0xdc7d('0x2e')],_0x180a13[_0xdc7d('0x30')]);_0x1566e4[_0xdc7d('0x34')]=_[_0xdc7d('0x33')](_0x180a13['model'],qs['fields'](_0x120413[_0xdc7d('0x30')][_0xdc7d('0x35')]));_0x1566e4['attributes']=_0x1566e4[_0xdc7d('0x34')][_0xdc7d('0x36')]?_0x1566e4[_0xdc7d('0x34')]:_0x180a13[_0xdc7d('0x2e')];if(!_0x120413[_0xdc7d('0x30')][_0xdc7d('0x37')]('nolimit')){_0x1566e4['limit']=qs[_0xdc7d('0x1d')](_0x120413['query'][_0xdc7d('0x1d')]);_0x1566e4[_0xdc7d('0x1c')]=qs[_0xdc7d('0x1c')](_0x120413['query']['offset']);}_0x1566e4['order']=qs[_0xdc7d('0x38')](_0x120413[_0xdc7d('0x30')][_0xdc7d('0x38')]);_0x1566e4[_0xdc7d('0x39')]=qs['filters'](_[_0xdc7d('0x3a')](_0x120413['query'],_0x180a13[_0xdc7d('0x32')]),_0x495521);if(_0x120413[_0xdc7d('0x30')][_0xdc7d('0x3b')]){_0x1566e4[_0xdc7d('0x39')]=_['merge'](_0x1566e4[_0xdc7d('0x39')],{'$or':_[_0xdc7d('0x2f')](_0x495521,function(_0xc7c916){if(_0xc7c916[_0xdc7d('0x2d')]!=='VIRTUAL'){var _0x29386d={};_0x29386d[_0xc7c916[_0xdc7d('0x28')]]={'$like':'%'+_0x120413[_0xdc7d('0x30')][_0xdc7d('0x3b')]+'%'};return _0x29386d;}})});}_0x1566e4=_[_0xdc7d('0x3c')]({},_0x1566e4,_0x120413['options']);var _0x27760a={'where':_0x1566e4['where']};return db[_0xdc7d('0x2a')][_0xdc7d('0x1e')](_0x27760a)[_0xdc7d('0x24')](function(_0xc9fa86){_0x5cdd63[_0xdc7d('0x1e')]=_0xc9fa86;if(_0x120413[_0xdc7d('0x30')]['includeAll']){_0x1566e4[_0xdc7d('0x3d')]=[{'all':!![]}];}return db[_0xdc7d('0x2a')][_0xdc7d('0x3e')](_0x1566e4);})['then'](function(_0x36445c){_0x5cdd63[_0xdc7d('0x3f')]=_0x36445c;return _0x5cdd63;})[_0xdc7d('0x24')](respondWithFilteredResult(_0x121468,_0x1566e4))[_0xdc7d('0x40')](handleError(_0x121468,null));};exports[_0xdc7d('0x41')]=function(_0x38f245,_0xf084d7){var _0x1191c6={'raw':!![],'where':{'id':_0x38f245['params']['id']}},_0x34d2d5={};_0x34d2d5[_0xdc7d('0x2e')]=_[_0xdc7d('0x31')](db[_0xdc7d('0x2a')][_0xdc7d('0x2b')]);_0x34d2d5[_0xdc7d('0x30')]=_[_0xdc7d('0x31')](_0x38f245[_0xdc7d('0x30')]);_0x34d2d5[_0xdc7d('0x32')]=_['intersection'](_0x34d2d5['model'],_0x34d2d5[_0xdc7d('0x30')]);_0x1191c6[_0xdc7d('0x34')]=_[_0xdc7d('0x33')](_0x34d2d5[_0xdc7d('0x2e')],qs[_0xdc7d('0x35')](_0x38f245[_0xdc7d('0x30')][_0xdc7d('0x35')]));_0x1191c6[_0xdc7d('0x34')]=_0x1191c6[_0xdc7d('0x34')][_0xdc7d('0x36')]?_0x1191c6['attributes']:_0x34d2d5[_0xdc7d('0x2e')];if(_0x38f245[_0xdc7d('0x30')][_0xdc7d('0x42')]){_0x1191c6[_0xdc7d('0x3d')]=[{'all':!![]}];}_0x1191c6=_[_0xdc7d('0x3c')]({},_0x1191c6,_0x38f245[_0xdc7d('0x43')]);return db['SquareRecording'][_0xdc7d('0x44')](_0x1191c6)[_0xdc7d('0x24')](handleEntityNotFound(_0xf084d7,null))[_0xdc7d('0x24')](respondWithResult(_0xf084d7,null))['catch'](handleError(_0xf084d7,null));};exports[_0xdc7d('0x45')]=function(_0x44efc2,_0x5a18fa){return db[_0xdc7d('0x2a')]['create'](_0x44efc2[_0xdc7d('0x46')],{})['then'](respondWithResult(_0x5a18fa,0xc9))['catch'](handleError(_0x5a18fa,null));};exports[_0xdc7d('0x47')]=function(_0x32f2c4,_0x174a03){if(_0x32f2c4['body']['id']){delete _0x32f2c4[_0xdc7d('0x46')]['id'];}return db[_0xdc7d('0x2a')]['find']({'where':{'id':_0x32f2c4['params']['id']}})[_0xdc7d('0x24')](handleEntityNotFound(_0x174a03,null))[_0xdc7d('0x24')](saveUpdates(_0x32f2c4[_0xdc7d('0x46')],null))['then'](respondWithResult(_0x174a03,null))[_0xdc7d('0x40')](handleError(_0x174a03,null));};exports[_0xdc7d('0x48')]=function(_0x45a1a4,_0x2b4c39,_0x589efc){var _0x3d9313=require(_0xdc7d('0x49'));var _0x24243a=![];var _0x1ed940={};if(_0x45a1a4[_0xdc7d('0x30')][_0xdc7d('0x2d')]&&_0x45a1a4[_0xdc7d('0x30')]['type']===_0xdc7d('0x4a')){_0x1ed940[_0xdc7d('0x4a')]=_0x45a1a4['params']['id'];}else{_0x1ed940['id']=_0x45a1a4[_0xdc7d('0x4b')]['id'];}return db[_0xdc7d('0x2a')]['find']({'where':_0x1ed940,'attributes':['id',_0xdc7d('0x4c'),'filename'],'raw':!![]})['then'](handleEntityNotFound(_0x2b4c39,null))[_0xdc7d('0x24')](function(_0x148851){if(_0x148851){var _0x13f23a=_0x148851[_0xdc7d('0x4c')];var _0xf0a146=util['format']('%s.cypher',_0x13f23a);if(fs[_0xdc7d('0x4d')](_0xf0a146)){_0x24243a=!![];return _0x3d9313['decryptFile'](_0xf0a146,_0x13f23a,config['secrets']['recording'])[_0xdc7d('0x24')](function(){return _0x148851;});}return _0x148851;}})[_0xdc7d('0x24')](function(_0x258ab2){if(_0x258ab2){var _0x56926b=_0x258ab2[_0xdc7d('0x4c')];if(!fs[_0xdc7d('0x4d')](_0x56926b)){throw new db['Sequelize'][(_0xdc7d('0x4e'))](_0xdc7d('0x4f'));}if(_0x258ab2[_0xdc7d('0x50')]){return _0x2b4c39[_0xdc7d('0x48')](_0x56926b,_0x258ab2[_0xdc7d('0x50')]+path[_0xdc7d('0x51')](_0x56926b),function(){if(_0x24243a){fs[_0xdc7d('0x52')](_0x56926b);}});}else{return _0x2b4c39[_0xdc7d('0x48')](_0x56926b,function(_0x73faae){if(_0x24243a){fs[_0xdc7d('0x52')](_0x56926b);}});}}})['catch'](function(_0xa69745){if(_0xa69745[_0xdc7d('0x53')]==='SquareRecording\x20not\x20found'){_0x2b4c39[_0xdc7d('0x1a')](0x194)[_0xdc7d('0x29')](_0xdc7d('0x54'));}else{return handleError(_0x2b4c39,null);}});};exports[_0xdc7d('0x25')]=function(_0x4b1dce,_0xac90ff){return db[_0xdc7d('0x2a')][_0xdc7d('0x44')]({'where':{'id':_0x4b1dce[_0xdc7d('0x4b')]['id']}})[_0xdc7d('0x24')](function(_0x4d1a2a){if(_[_0xdc7d('0x55')](_0x4d1a2a)){throw new ReferenceError(_0xdc7d('0x56'));}return _0x4d1a2a;})[_0xdc7d('0x24')](function(_0x3a1cac){var _0x29b9e6=_0x3a1cac[_0xdc7d('0x4c')];logger[_0xdc7d('0x57')](_0xdc7d('0x58'),_0x29b9e6);if(!_['isNil'](_0x3a1cac[_0xdc7d('0x4c')])&&fs[_0xdc7d('0x4d')](_0x29b9e6)){logger['info'](_0xdc7d('0x59')+_0x29b9e6+_0xdc7d('0x5a'));fs[_0xdc7d('0x5b')](_0x29b9e6);}_0x29b9e6=_0x29b9e6+_0xdc7d('0x5c');if(!_[_0xdc7d('0x55')](_0x3a1cac['savePath'])&&fs[_0xdc7d('0x4d')](_0x29b9e6)){logger['info'](_0xdc7d('0x59')+_0x29b9e6+_0xdc7d('0x5a'));fs[_0xdc7d('0x5b')](_0x29b9e6);}return _0x3a1cac;})['then'](function(_0x5db70c){if(!_0x5db70c)throw new InternalError();return _0x5db70c['destroy']();})[_0xdc7d('0x24')](function(){_0xac90ff[_0xdc7d('0x1a')](0xcc)[_0xdc7d('0x1b')]();})[_0xdc7d('0x40')](handleError(_0xac90ff,null));};