c35dbdf62d02cdddb597fd0e76e5d1e76d89df74
[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 _0x8ff6=['type','model','query','keys','filters','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','pick','filter','where','merge','options','includeAll','include','findAll','rows','catch','show','params','find','create','body','download','../../components/encryptor','uniqueid','savePath','filename','format','%s.cypher','existsSync','decryptFile','secrets','recording','Sequelize','SquareRecording\x20not\x20found','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','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','error','stack','name','send','index','map','SquareRecording','rawAttributes','fieldName'];(function(_0x143417,_0xaa9b96){var _0x156dab=function(_0x24a1bb){while(--_0x24a1bb){_0x143417['push'](_0x143417['shift']());}};_0x156dab(++_0xaa9b96);}(_0x8ff6,0x1d0));var _0x68ff=function(_0x160c73,_0x2ee678){_0x160c73=_0x160c73-0x0;var _0xd9302=_0x8ff6[_0x160c73];return _0xd9302;};'use strict';var emlformat=require(_0x68ff('0x0'));var rimraf=require(_0x68ff('0x1'));var zipdir=require(_0x68ff('0x2'));var jsonpatch=require(_0x68ff('0x3'));var rp=require(_0x68ff('0x4'));var moment=require(_0x68ff('0x5'));var BPromise=require(_0x68ff('0x6'));var Mustache=require('mustache');var util=require(_0x68ff('0x7'));var path=require(_0x68ff('0x8'));var sox=require(_0x68ff('0x9'));var csv=require(_0x68ff('0xa'));var ejs=require(_0x68ff('0xb'));var fs=require('fs');var fs_extra=require(_0x68ff('0xc'));var _=require(_0x68ff('0xd'));var squel=require('squel');var crypto=require(_0x68ff('0xe'));var jsforce=require(_0x68ff('0xf'));var deskjs=require(_0x68ff('0x10'));var toCsv=require(_0x68ff('0xa'));var querystring=require(_0x68ff('0x11'));var Papa=require(_0x68ff('0x12'));var Redis=require('ioredis');var authService=require(_0x68ff('0x13'));var qs=require(_0x68ff('0x14'));var as=require(_0x68ff('0x15'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x68ff('0x16'));var utils=require('../../config/utils');var config=require(_0x68ff('0x17'));var licenseUtil=require(_0x68ff('0x18'));var db=require(_0x68ff('0x19'))['db'];function respondWithStatusCode(_0x2dbcdf,_0x4a2ad6){_0x4a2ad6=_0x4a2ad6||0xcc;return function(_0x543ad4){if(_0x543ad4){return _0x2dbcdf[_0x68ff('0x1a')](_0x4a2ad6);}return _0x2dbcdf[_0x68ff('0x1b')](_0x4a2ad6)[_0x68ff('0x1c')]();};}function respondWithResult(_0x51bc11,_0x47056c){_0x47056c=_0x47056c||0xc8;return function(_0x53cff6){if(_0x53cff6){return _0x51bc11[_0x68ff('0x1b')](_0x47056c)[_0x68ff('0x1d')](_0x53cff6);}};}function respondWithFilteredResult(_0x4df40a,_0x4f0efc){return function(_0x1c9b64){if(_0x1c9b64){var _0x5129c1=typeof _0x4f0efc[_0x68ff('0x1e')]===_0x68ff('0x1f')&&typeof _0x4f0efc[_0x68ff('0x20')]===_0x68ff('0x1f');var _0x41008e=_0x1c9b64[_0x68ff('0x21')];var _0x8d1acf=_0x5129c1?0x0:_0x4f0efc[_0x68ff('0x1e')];var _0x4dfd81=_0x5129c1?_0x1c9b64[_0x68ff('0x21')]:_0x4f0efc[_0x68ff('0x1e')]+_0x4f0efc[_0x68ff('0x20')];var _0x5c4cf8;if(_0x4dfd81>=_0x41008e){_0x4dfd81=_0x41008e;_0x5c4cf8=0xc8;}else{_0x5c4cf8=0xce;}_0x4df40a[_0x68ff('0x1b')](_0x5c4cf8);return _0x4df40a[_0x68ff('0x22')](_0x68ff('0x23'),_0x8d1acf+'-'+_0x4dfd81+'/'+_0x41008e)[_0x68ff('0x1d')](_0x1c9b64);}return null;};}function patchUpdates(_0x45a607){return function(_0x576c52){try{jsonpatch[_0x68ff('0x24')](_0x576c52,_0x45a607,!![]);}catch(_0x517ec8){return BPromise[_0x68ff('0x25')](_0x517ec8);}return _0x576c52[_0x68ff('0x26')]();};}function saveUpdates(_0x450cd6,_0xebe6af){return function(_0x3f3de7){if(_0x3f3de7){return _0x3f3de7[_0x68ff('0x27')](_0x450cd6)[_0x68ff('0x28')](function(_0x45288e){return _0x45288e;});}return null;};}function removeEntity(_0x14c995,_0x4bd081){return function(_0x541b0c){if(_0x541b0c){return _0x541b0c[_0x68ff('0x29')]()[_0x68ff('0x28')](function(){_0x14c995[_0x68ff('0x1b')](0xcc)[_0x68ff('0x1c')]();});}};}function handleEntityNotFound(_0x8d70da,_0x300faa){return function(_0x2c6f7b){if(!_0x2c6f7b){_0x8d70da[_0x68ff('0x1a')](0x194);}return _0x2c6f7b;};}function handleError(_0x4193bf,_0x3b7709){_0x3b7709=_0x3b7709||0x1f4;return function(_0xe77416){logger[_0x68ff('0x2a')](_0xe77416[_0x68ff('0x2b')]);if(_0xe77416['name']){delete _0xe77416[_0x68ff('0x2c')];}_0x4193bf[_0x68ff('0x1b')](_0x3b7709)[_0x68ff('0x2d')](_0xe77416);};}exports[_0x68ff('0x2e')]=function(_0x5d2c7d,_0x13cece){var _0x5359c6={},_0x3ac4ae={},_0x3b576f={'count':0x0,'rows':[]};var _0x237829=_[_0x68ff('0x2f')](db[_0x68ff('0x30')][_0x68ff('0x31')],function(_0x416554){return{'name':_0x416554[_0x68ff('0x32')],'type':_0x416554[_0x68ff('0x33')]['key']};});_0x3ac4ae[_0x68ff('0x34')]=_[_0x68ff('0x2f')](_0x237829,_0x68ff('0x2c'));_0x3ac4ae[_0x68ff('0x35')]=_[_0x68ff('0x36')](_0x5d2c7d[_0x68ff('0x35')]);_0x3ac4ae[_0x68ff('0x37')]=_['intersection'](_0x3ac4ae[_0x68ff('0x34')],_0x3ac4ae['query']);_0x5359c6[_0x68ff('0x38')]=_[_0x68ff('0x39')](_0x3ac4ae[_0x68ff('0x34')],qs[_0x68ff('0x3a')](_0x5d2c7d[_0x68ff('0x35')][_0x68ff('0x3a')]));_0x5359c6['attributes']=_0x5359c6[_0x68ff('0x38')][_0x68ff('0x3b')]?_0x5359c6['attributes']:_0x3ac4ae[_0x68ff('0x34')];if(!_0x5d2c7d[_0x68ff('0x35')][_0x68ff('0x3c')](_0x68ff('0x3d'))){_0x5359c6['limit']=qs[_0x68ff('0x20')](_0x5d2c7d[_0x68ff('0x35')][_0x68ff('0x20')]);_0x5359c6['offset']=qs[_0x68ff('0x1e')](_0x5d2c7d['query'][_0x68ff('0x1e')]);}_0x5359c6[_0x68ff('0x3e')]=qs[_0x68ff('0x3f')](_0x5d2c7d[_0x68ff('0x35')]['sort']);_0x5359c6['where']=qs[_0x68ff('0x37')](_[_0x68ff('0x40')](_0x5d2c7d[_0x68ff('0x35')],_0x3ac4ae['filters']),_0x237829);if(_0x5d2c7d['query'][_0x68ff('0x41')]){_0x5359c6[_0x68ff('0x42')]=_[_0x68ff('0x43')](_0x5359c6['where'],{'$or':_['map'](_0x237829,function(_0x1f5da4){if(_0x1f5da4[_0x68ff('0x33')]!=='VIRTUAL'){var _0x1f92cd={};_0x1f92cd[_0x1f5da4[_0x68ff('0x2c')]]={'$like':'%'+_0x5d2c7d[_0x68ff('0x35')]['filter']+'%'};return _0x1f92cd;}})});}_0x5359c6=_['merge']({},_0x5359c6,_0x5d2c7d[_0x68ff('0x44')]);var _0x4f9999={'where':_0x5359c6[_0x68ff('0x42')]};return db[_0x68ff('0x30')][_0x68ff('0x21')](_0x4f9999)['then'](function(_0x4aec5d){_0x3b576f[_0x68ff('0x21')]=_0x4aec5d;if(_0x5d2c7d[_0x68ff('0x35')][_0x68ff('0x45')]){_0x5359c6[_0x68ff('0x46')]=[{'all':!![]}];}return db[_0x68ff('0x30')][_0x68ff('0x47')](_0x5359c6);})[_0x68ff('0x28')](function(_0x246417){_0x3b576f[_0x68ff('0x48')]=_0x246417;return _0x3b576f;})[_0x68ff('0x28')](respondWithFilteredResult(_0x13cece,_0x5359c6))[_0x68ff('0x49')](handleError(_0x13cece,null));};exports[_0x68ff('0x4a')]=function(_0x5a5f5c,_0x407eee){var _0x2bf2bc={'raw':!![],'where':{'id':_0x5a5f5c[_0x68ff('0x4b')]['id']}},_0x18957d={};_0x18957d[_0x68ff('0x34')]=_[_0x68ff('0x36')](db[_0x68ff('0x30')]['rawAttributes']);_0x18957d[_0x68ff('0x35')]=_['keys'](_0x5a5f5c[_0x68ff('0x35')]);_0x18957d['filters']=_[_0x68ff('0x39')](_0x18957d[_0x68ff('0x34')],_0x18957d[_0x68ff('0x35')]);_0x2bf2bc[_0x68ff('0x38')]=_[_0x68ff('0x39')](_0x18957d['model'],qs[_0x68ff('0x3a')](_0x5a5f5c[_0x68ff('0x35')]['fields']));_0x2bf2bc['attributes']=_0x2bf2bc[_0x68ff('0x38')][_0x68ff('0x3b')]?_0x2bf2bc['attributes']:_0x18957d['model'];if(_0x5a5f5c[_0x68ff('0x35')][_0x68ff('0x45')]){_0x2bf2bc[_0x68ff('0x46')]=[{'all':!![]}];}_0x2bf2bc=_[_0x68ff('0x43')]({},_0x2bf2bc,_0x5a5f5c['options']);return db[_0x68ff('0x30')][_0x68ff('0x4c')](_0x2bf2bc)['then'](handleEntityNotFound(_0x407eee,null))[_0x68ff('0x28')](respondWithResult(_0x407eee,null))[_0x68ff('0x49')](handleError(_0x407eee,null));};exports[_0x68ff('0x4d')]=function(_0x404cc1,_0x5706af){return db[_0x68ff('0x30')][_0x68ff('0x4d')](_0x404cc1[_0x68ff('0x4e')],{})['then'](respondWithResult(_0x5706af,0xc9))[_0x68ff('0x49')](handleError(_0x5706af,null));};exports[_0x68ff('0x27')]=function(_0x1ede60,_0x125de6){if(_0x1ede60['body']['id']){delete _0x1ede60[_0x68ff('0x4e')]['id'];}return db['SquareRecording'][_0x68ff('0x4c')]({'where':{'id':_0x1ede60[_0x68ff('0x4b')]['id']}})['then'](handleEntityNotFound(_0x125de6,null))[_0x68ff('0x28')](saveUpdates(_0x1ede60[_0x68ff('0x4e')],null))[_0x68ff('0x28')](respondWithResult(_0x125de6,null))[_0x68ff('0x49')](handleError(_0x125de6,null));};exports[_0x68ff('0x4f')]=function(_0x5ba243,_0x4546a8,_0x517823){var _0x7575b4=require(_0x68ff('0x50'));var _0x1d365f=![];var _0x1ac47a={};if(_0x5ba243[_0x68ff('0x35')][_0x68ff('0x33')]&&_0x5ba243[_0x68ff('0x35')][_0x68ff('0x33')]==='uniqueid'){_0x1ac47a[_0x68ff('0x51')]=_0x5ba243['params']['id'];}else{_0x1ac47a['id']=_0x5ba243[_0x68ff('0x4b')]['id'];}return db[_0x68ff('0x30')][_0x68ff('0x4c')]({'where':_0x1ac47a,'attributes':['id',_0x68ff('0x52'),_0x68ff('0x53')],'raw':!![]})[_0x68ff('0x28')](handleEntityNotFound(_0x4546a8,null))[_0x68ff('0x28')](function(_0x422241){if(_0x422241){var _0xdb72d6=_0x422241['savePath'];var _0x511834=util[_0x68ff('0x54')](_0x68ff('0x55'),_0xdb72d6);if(fs[_0x68ff('0x56')](_0x511834)){_0x1d365f=!![];return _0x7575b4[_0x68ff('0x57')](_0x511834,_0xdb72d6,config[_0x68ff('0x58')][_0x68ff('0x59')])[_0x68ff('0x28')](function(){return _0x422241;});}return _0x422241;}})['then'](function(_0x4819ea){if(_0x4819ea){var _0x57632a=_0x4819ea[_0x68ff('0x52')];if(!fs[_0x68ff('0x56')](_0x57632a)){throw new db[(_0x68ff('0x5a'))]['ValidationError'](_0x68ff('0x5b'));}if(_0x4819ea[_0x68ff('0x53')]){return _0x4546a8['download'](_0x57632a,_0x4819ea[_0x68ff('0x53')]+path['extname'](_0x57632a),function(){if(_0x1d365f){fs[_0x68ff('0x5c')](_0x57632a);}});}else{return _0x4546a8[_0x68ff('0x4f')](_0x57632a,function(_0x26e3c8){if(_0x1d365f){fs[_0x68ff('0x5c')](_0x57632a);}});}}})[_0x68ff('0x49')](function(_0x401d4f){if(_0x401d4f[_0x68ff('0x5d')]===_0x68ff('0x5b')){_0x4546a8['status'](0x194)[_0x68ff('0x2d')](_0x68ff('0x5e'));}else{return handleError(_0x4546a8,null);}});};exports[_0x68ff('0x29')]=function(_0x18664e,_0x2a5a82){return db[_0x68ff('0x30')][_0x68ff('0x4c')]({'where':{'id':_0x18664e[_0x68ff('0x4b')]['id']}})[_0x68ff('0x28')](function(_0x42a32f){if(_[_0x68ff('0x5f')](_0x42a32f)){throw new ReferenceError(_0x68ff('0x60'));}return _0x42a32f;})['then'](function(_0x51650b){var _0x4f0794=_0x51650b[_0x68ff('0x52')];logger[_0x68ff('0x61')](_0x68ff('0x62'),_0x4f0794);if(!_[_0x68ff('0x5f')](_0x51650b[_0x68ff('0x52')])&&fs[_0x68ff('0x56')](_0x4f0794)){logger[_0x68ff('0x61')](_0x68ff('0x63')+_0x4f0794+_0x68ff('0x64'));fs[_0x68ff('0x65')](_0x4f0794);}_0x4f0794=_0x4f0794+_0x68ff('0x66');if(!_[_0x68ff('0x5f')](_0x51650b[_0x68ff('0x52')])&&fs[_0x68ff('0x56')](_0x4f0794)){logger[_0x68ff('0x61')](_0x68ff('0x63')+_0x4f0794+_0x68ff('0x64'));fs['unlinkSync'](_0x4f0794);}return _0x51650b;})[_0x68ff('0x28')](function(_0x5064ae){if(!_0x5064ae)throw new InternalError();return _0x5064ae[_0x68ff('0x29')]();})[_0x68ff('0x28')](function(){_0x2a5a82[_0x68ff('0x1b')](0xcc)['end']();})[_0x68ff('0x49')](handleError(_0x2a5a82,null));};