c998e09dee765f9aac1a405a581bfad1bf3f3f2d
[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 _0xb2f3=['undefined','offset','count','limit','set','Content-Range','apply','reject','save','then','destroy','error','name','send','index','SquareRecording','type','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','filter','map','VIRTUAL','merge','options','include','findAll','rows','catch','keys','rawAttributes','find','create','body','update','params','download','uniqueid','savePath','filename','format','existsSync','decryptFile','secrets','recording','Sequelize','ValidationError','SquareRecording\x20not\x20found','unlink','Not\x20found','isNil','Entity\x20not\x20found','info','cancellazione\x20fisica\x20documento\x20%s','File:\x20\x22','\x22\x20trovato\x20eseguo\x20la\x20cancellazione','unlinkSync','.cypher','eml-format','zip-dir','moment','bluebird','mustache','util','path','to-csv','ejs','fs-extra','squel','jsforce','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','sendStatus','status','end'];(function(_0xcc24e8,_0x230fe5){var _0x279917=function(_0x5838ad){while(--_0x5838ad){_0xcc24e8['push'](_0xcc24e8['shift']());}};_0x279917(++_0x230fe5);}(_0xb2f3,0x101));var _0x3b2f=function(_0x46e363,_0x45d7e2){_0x46e363=_0x46e363-0x0;var _0x3fb059=_0xb2f3[_0x46e363];return _0x3fb059;};'use strict';var emlformat=require(_0x3b2f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x3b2f('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x3b2f('0x2'));var BPromise=require(_0x3b2f('0x3'));var Mustache=require(_0x3b2f('0x4'));var util=require(_0x3b2f('0x5'));var path=require(_0x3b2f('0x6'));var sox=require('sox');var csv=require(_0x3b2f('0x7'));var ejs=require(_0x3b2f('0x8'));var fs=require('fs');var fs_extra=require(_0x3b2f('0x9'));var _=require('lodash');var squel=require(_0x3b2f('0xa'));var crypto=require('crypto');var jsforce=require(_0x3b2f('0xb'));var deskjs=require(_0x3b2f('0xc'));var toCsv=require(_0x3b2f('0x7'));var querystring=require(_0x3b2f('0xd'));var Papa=require(_0x3b2f('0xe'));var Redis=require(_0x3b2f('0xf'));var authService=require(_0x3b2f('0x10'));var qs=require(_0x3b2f('0x11'));var as=require(_0x3b2f('0x12'));var hardwareService=require(_0x3b2f('0x13'));var logger=require(_0x3b2f('0x14'))(_0x3b2f('0x15'));var utils=require(_0x3b2f('0x16'));var config=require('../../config/environment');var licenseUtil=require(_0x3b2f('0x17'));var db=require(_0x3b2f('0x18'))['db'];function respondWithStatusCode(_0x3116ce,_0x35a48e){_0x35a48e=_0x35a48e||0xcc;return function(_0x1b7242){if(_0x1b7242){return _0x3116ce[_0x3b2f('0x19')](_0x35a48e);}return _0x3116ce[_0x3b2f('0x1a')](_0x35a48e)[_0x3b2f('0x1b')]();};}function respondWithResult(_0x5aa344,_0x6741d3){_0x6741d3=_0x6741d3||0xc8;return function(_0x30db5d){if(_0x30db5d){return _0x5aa344[_0x3b2f('0x1a')](_0x6741d3)['json'](_0x30db5d);}};}function respondWithFilteredResult(_0x5461ff,_0x77cd95){return function(_0x2f9bc1){if(_0x2f9bc1){var _0x453e68=typeof _0x77cd95['offset']==='undefined'&&typeof _0x77cd95['limit']===_0x3b2f('0x1c');var _0x46be6e=_0x2f9bc1['count'];var _0x4ed89a=_0x453e68?0x0:_0x77cd95[_0x3b2f('0x1d')];var _0x1e6655=_0x453e68?_0x2f9bc1[_0x3b2f('0x1e')]:_0x77cd95[_0x3b2f('0x1d')]+_0x77cd95[_0x3b2f('0x1f')];var _0x4150f2;if(_0x1e6655>=_0x46be6e){_0x1e6655=_0x46be6e;_0x4150f2=0xc8;}else{_0x4150f2=0xce;}_0x5461ff[_0x3b2f('0x1a')](_0x4150f2);return _0x5461ff[_0x3b2f('0x20')](_0x3b2f('0x21'),_0x4ed89a+'-'+_0x1e6655+'/'+_0x46be6e)['json'](_0x2f9bc1);}return null;};}function patchUpdates(_0x59c444){return function(_0x14a4ee){try{jsonpatch[_0x3b2f('0x22')](_0x14a4ee,_0x59c444,!![]);}catch(_0x5aba3c){return BPromise[_0x3b2f('0x23')](_0x5aba3c);}return _0x14a4ee[_0x3b2f('0x24')]();};}function saveUpdates(_0x31211b,_0x11dbee){return function(_0x48c61a){if(_0x48c61a){return _0x48c61a['update'](_0x31211b)[_0x3b2f('0x25')](function(_0x960754){return _0x960754;});}return null;};}function removeEntity(_0x111e11,_0x307046){return function(_0x1da579){if(_0x1da579){return _0x1da579[_0x3b2f('0x26')]()[_0x3b2f('0x25')](function(){_0x111e11[_0x3b2f('0x1a')](0xcc)[_0x3b2f('0x1b')]();});}};}function handleEntityNotFound(_0x26e7ab,_0x5118b1){return function(_0x29969d){if(!_0x29969d){_0x26e7ab['sendStatus'](0x194);}return _0x29969d;};}function handleError(_0x3f201d,_0x1b9ac0){_0x1b9ac0=_0x1b9ac0||0x1f4;return function(_0x4872e1){logger[_0x3b2f('0x27')](_0x4872e1['stack']);if(_0x4872e1[_0x3b2f('0x28')]){delete _0x4872e1[_0x3b2f('0x28')];}_0x3f201d[_0x3b2f('0x1a')](_0x1b9ac0)[_0x3b2f('0x29')](_0x4872e1);};}exports[_0x3b2f('0x2a')]=function(_0x56fee7,_0x9c2e78){var _0x546d0b={},_0x2fb3a4={},_0xe8ba55={'count':0x0,'rows':[]};var _0x2a7ca5=_['map'](db[_0x3b2f('0x2b')]['rawAttributes'],function(_0x5013d0){return{'name':_0x5013d0['fieldName'],'type':_0x5013d0[_0x3b2f('0x2c')]['key']};});_0x2fb3a4[_0x3b2f('0x2d')]=_['map'](_0x2a7ca5,_0x3b2f('0x28'));_0x2fb3a4['query']=_['keys'](_0x56fee7[_0x3b2f('0x2e')]);_0x2fb3a4[_0x3b2f('0x2f')]=_[_0x3b2f('0x30')](_0x2fb3a4['model'],_0x2fb3a4[_0x3b2f('0x2e')]);_0x546d0b[_0x3b2f('0x31')]=_['intersection'](_0x2fb3a4['model'],qs[_0x3b2f('0x32')](_0x56fee7['query'][_0x3b2f('0x32')]));_0x546d0b[_0x3b2f('0x31')]=_0x546d0b[_0x3b2f('0x31')][_0x3b2f('0x33')]?_0x546d0b['attributes']:_0x2fb3a4[_0x3b2f('0x2d')];if(!_0x56fee7[_0x3b2f('0x2e')][_0x3b2f('0x34')](_0x3b2f('0x35'))){_0x546d0b[_0x3b2f('0x1f')]=qs[_0x3b2f('0x1f')](_0x56fee7[_0x3b2f('0x2e')][_0x3b2f('0x1f')]);_0x546d0b[_0x3b2f('0x1d')]=qs[_0x3b2f('0x1d')](_0x56fee7[_0x3b2f('0x2e')][_0x3b2f('0x1d')]);}_0x546d0b['order']=qs[_0x3b2f('0x36')](_0x56fee7[_0x3b2f('0x2e')][_0x3b2f('0x36')]);_0x546d0b[_0x3b2f('0x37')]=qs['filters'](_[_0x3b2f('0x38')](_0x56fee7[_0x3b2f('0x2e')],_0x2fb3a4[_0x3b2f('0x2f')]),_0x2a7ca5);if(_0x56fee7[_0x3b2f('0x2e')][_0x3b2f('0x39')]){_0x546d0b[_0x3b2f('0x37')]=_['merge'](_0x546d0b[_0x3b2f('0x37')],{'$or':_[_0x3b2f('0x3a')](_0x2a7ca5,function(_0x4e90ff){if(_0x4e90ff['type']!==_0x3b2f('0x3b')){var _0x2c37a2={};_0x2c37a2[_0x4e90ff[_0x3b2f('0x28')]]={'$like':'%'+_0x56fee7[_0x3b2f('0x2e')][_0x3b2f('0x39')]+'%'};return _0x2c37a2;}})});}_0x546d0b=_[_0x3b2f('0x3c')]({},_0x546d0b,_0x56fee7[_0x3b2f('0x3d')]);var _0x13ec33={'where':_0x546d0b['where']};return db['SquareRecording']['count'](_0x13ec33)['then'](function(_0x3ffd92){_0xe8ba55[_0x3b2f('0x1e')]=_0x3ffd92;if(_0x56fee7[_0x3b2f('0x2e')]['includeAll']){_0x546d0b[_0x3b2f('0x3e')]=[{'all':!![]}];}return db[_0x3b2f('0x2b')][_0x3b2f('0x3f')](_0x546d0b);})[_0x3b2f('0x25')](function(_0x5b5614){_0xe8ba55[_0x3b2f('0x40')]=_0x5b5614;return _0xe8ba55;})[_0x3b2f('0x25')](respondWithFilteredResult(_0x9c2e78,_0x546d0b))[_0x3b2f('0x41')](handleError(_0x9c2e78,null));};exports['show']=function(_0x37bdc7,_0x25914d){var _0x223c44={'raw':!![],'where':{'id':_0x37bdc7['params']['id']}},_0x59ee4a={};_0x59ee4a[_0x3b2f('0x2d')]=_[_0x3b2f('0x42')](db['SquareRecording'][_0x3b2f('0x43')]);_0x59ee4a['query']=_[_0x3b2f('0x42')](_0x37bdc7[_0x3b2f('0x2e')]);_0x59ee4a[_0x3b2f('0x2f')]=_[_0x3b2f('0x30')](_0x59ee4a['model'],_0x59ee4a[_0x3b2f('0x2e')]);_0x223c44[_0x3b2f('0x31')]=_[_0x3b2f('0x30')](_0x59ee4a[_0x3b2f('0x2d')],qs[_0x3b2f('0x32')](_0x37bdc7[_0x3b2f('0x2e')][_0x3b2f('0x32')]));_0x223c44[_0x3b2f('0x31')]=_0x223c44[_0x3b2f('0x31')][_0x3b2f('0x33')]?_0x223c44['attributes']:_0x59ee4a[_0x3b2f('0x2d')];if(_0x37bdc7['query']['includeAll']){_0x223c44['include']=[{'all':!![]}];}_0x223c44=_[_0x3b2f('0x3c')]({},_0x223c44,_0x37bdc7[_0x3b2f('0x3d')]);return db[_0x3b2f('0x2b')][_0x3b2f('0x44')](_0x223c44)[_0x3b2f('0x25')](handleEntityNotFound(_0x25914d,null))[_0x3b2f('0x25')](respondWithResult(_0x25914d,null))['catch'](handleError(_0x25914d,null));};exports[_0x3b2f('0x45')]=function(_0x55c258,_0x2ebfcc){return db[_0x3b2f('0x2b')][_0x3b2f('0x45')](_0x55c258[_0x3b2f('0x46')],{})[_0x3b2f('0x25')](respondWithResult(_0x2ebfcc,0xc9))[_0x3b2f('0x41')](handleError(_0x2ebfcc,null));};exports[_0x3b2f('0x47')]=function(_0x39ade1,_0x1f3609){if(_0x39ade1[_0x3b2f('0x46')]['id']){delete _0x39ade1[_0x3b2f('0x46')]['id'];}return db['SquareRecording'][_0x3b2f('0x44')]({'where':{'id':_0x39ade1[_0x3b2f('0x48')]['id']}})[_0x3b2f('0x25')](handleEntityNotFound(_0x1f3609,null))[_0x3b2f('0x25')](saveUpdates(_0x39ade1['body'],null))[_0x3b2f('0x25')](respondWithResult(_0x1f3609,null))[_0x3b2f('0x41')](handleError(_0x1f3609,null));};exports[_0x3b2f('0x49')]=function(_0x18f20e,_0x5f096f,_0x77d8d5){var _0x5ad169=require('../../components/encryptor');var _0x54da15=![];var _0x154a55={};if(_0x18f20e[_0x3b2f('0x2e')][_0x3b2f('0x2c')]&&_0x18f20e['query'][_0x3b2f('0x2c')]===_0x3b2f('0x4a')){_0x154a55[_0x3b2f('0x4a')]=_0x18f20e[_0x3b2f('0x48')]['id'];}else{_0x154a55['id']=_0x18f20e['params']['id'];}return db[_0x3b2f('0x2b')][_0x3b2f('0x44')]({'where':_0x154a55,'attributes':['id',_0x3b2f('0x4b'),_0x3b2f('0x4c')],'raw':!![]})['then'](handleEntityNotFound(_0x5f096f,null))['then'](function(_0x652aaa){if(_0x652aaa){var _0x20ba45=_0x652aaa[_0x3b2f('0x4b')];var _0x1b3ac0=util[_0x3b2f('0x4d')]('%s.cypher',_0x20ba45);if(fs[_0x3b2f('0x4e')](_0x1b3ac0)){_0x54da15=!![];return _0x5ad169[_0x3b2f('0x4f')](_0x1b3ac0,_0x20ba45,config[_0x3b2f('0x50')][_0x3b2f('0x51')])[_0x3b2f('0x25')](function(){return _0x652aaa;});}return _0x652aaa;}})[_0x3b2f('0x25')](function(_0x1f026f){if(_0x1f026f){var _0xcefcc2=_0x1f026f['savePath'];if(!fs[_0x3b2f('0x4e')](_0xcefcc2)){throw new db[(_0x3b2f('0x52'))][(_0x3b2f('0x53'))](_0x3b2f('0x54'));}if(_0x1f026f[_0x3b2f('0x4c')]){return _0x5f096f['download'](_0xcefcc2,_0x1f026f['filename']+path['extname'](_0xcefcc2),function(){if(_0x54da15){fs[_0x3b2f('0x55')](_0xcefcc2);}});}else{return _0x5f096f['download'](_0xcefcc2,function(_0x34ba77){if(_0x54da15){fs['unlink'](_0xcefcc2);}});}}})[_0x3b2f('0x41')](function(_0x21cab6){if(_0x21cab6['message']==='SquareRecording\x20not\x20found'){_0x5f096f[_0x3b2f('0x1a')](0x194)['send'](_0x3b2f('0x56'));}else{return handleError(_0x5f096f,null);}});};exports['destroy']=function(_0x5202b7,_0x15ca7c){return db[_0x3b2f('0x2b')][_0x3b2f('0x44')]({'where':{'id':_0x5202b7[_0x3b2f('0x48')]['id']}})[_0x3b2f('0x25')](function(_0xa6d64c){if(_[_0x3b2f('0x57')](_0xa6d64c)){throw new ReferenceError(_0x3b2f('0x58'));}return _0xa6d64c;})[_0x3b2f('0x25')](function(_0x2028b5){var _0x30b2d2=_0x2028b5[_0x3b2f('0x4b')];logger[_0x3b2f('0x59')](_0x3b2f('0x5a'),_0x30b2d2);if(!_[_0x3b2f('0x57')](_0x2028b5[_0x3b2f('0x4b')])&&fs[_0x3b2f('0x4e')](_0x30b2d2)){logger[_0x3b2f('0x59')](_0x3b2f('0x5b')+_0x30b2d2+_0x3b2f('0x5c'));fs[_0x3b2f('0x5d')](_0x30b2d2);}_0x30b2d2=_0x30b2d2+_0x3b2f('0x5e');if(!_[_0x3b2f('0x57')](_0x2028b5[_0x3b2f('0x4b')])&&fs[_0x3b2f('0x4e')](_0x30b2d2)){logger[_0x3b2f('0x59')](_0x3b2f('0x5b')+_0x30b2d2+_0x3b2f('0x5c'));fs[_0x3b2f('0x5d')](_0x30b2d2);}return _0x2028b5;})[_0x3b2f('0x25')](function(_0x1a46bc){if(!_0x1a46bc)throw new InternalError();return _0x1a46bc[_0x3b2f('0x26')]();})['then'](function(){_0x15ca7c[_0x3b2f('0x1a')](0xcc)[_0x3b2f('0x1b')]();})['catch'](handleError(_0x15ca7c,null));};