ba4279cbdffcb1a60da3d5a7121acafbcc447ac6
[motion2.git] / server / api / automation / automation.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 _0x33f2=['merge','options','includeAll','Automation','findAll','show','params','include','catch','create','body','find','Condition','addConditions','ids','getConditions','findOne','pick','nolimit','addActions','Action','getActions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../config/license/hardware','api','../../config/environment','../../config/license/util','sendStatus','status','end','json','undefined','count','offset','limit','set','apply','reject','save','update','then','destroy','error','name','send','index','map','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','sort','where','filter'];(function(_0x12b038,_0x5a0c2a){var _0x4f851c=function(_0x38a6c3){while(--_0x38a6c3){_0x12b038['push'](_0x12b038['shift']());}};_0x4f851c(++_0x5a0c2a);}(_0x33f2,0x162));var _0x233f=function(_0x2d126e,_0x1e8257){_0x2d126e=_0x2d126e-0x0;var _0xcb04d3=_0x33f2[_0x2d126e];return _0xcb04d3;};'use strict';var emlformat=require(_0x233f('0x0'));var rimraf=require(_0x233f('0x1'));var zipdir=require(_0x233f('0x2'));var jsonpatch=require(_0x233f('0x3'));var rp=require(_0x233f('0x4'));var moment=require(_0x233f('0x5'));var BPromise=require(_0x233f('0x6'));var Mustache=require(_0x233f('0x7'));var util=require(_0x233f('0x8'));var path=require(_0x233f('0x9'));var sox=require('sox');var csv=require(_0x233f('0xa'));var ejs=require(_0x233f('0xb'));var fs=require('fs');var fs_extra=require(_0x233f('0xc'));var _=require(_0x233f('0xd'));var squel=require('squel');var crypto=require(_0x233f('0xe'));var jsforce=require(_0x233f('0xf'));var deskjs=require(_0x233f('0x10'));var toCsv=require('to-csv');var querystring=require(_0x233f('0x11'));var Papa=require(_0x233f('0x12'));var Redis=require(_0x233f('0x13'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x233f('0x14'));var logger=require('../../config/logger')(_0x233f('0x15'));var utils=require('../../config/utils');var config=require(_0x233f('0x16'));var licenseUtil=require(_0x233f('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x33b37c,_0x3f359e){_0x3f359e=_0x3f359e||0xcc;return function(_0x1f425f){if(_0x1f425f){return _0x33b37c[_0x233f('0x18')](_0x3f359e);}return _0x33b37c[_0x233f('0x19')](_0x3f359e)[_0x233f('0x1a')]();};}function respondWithResult(_0x5ee50c,_0x1005e4){_0x1005e4=_0x1005e4||0xc8;return function(_0x3022fb){if(_0x3022fb){return _0x5ee50c[_0x233f('0x19')](_0x1005e4)[_0x233f('0x1b')](_0x3022fb);}};}function respondWithFilteredResult(_0x11d60a,_0x1b41ce){return function(_0x25bb3b){if(_0x25bb3b){var _0x63d6d8=typeof _0x1b41ce['offset']===_0x233f('0x1c')&&typeof _0x1b41ce['limit']===_0x233f('0x1c');var _0x24ef36=_0x25bb3b[_0x233f('0x1d')];var _0x4b844d=_0x63d6d8?0x0:_0x1b41ce['offset'];var _0x5ad42d=_0x63d6d8?_0x25bb3b['count']:_0x1b41ce[_0x233f('0x1e')]+_0x1b41ce[_0x233f('0x1f')];var _0x42e3e0;if(_0x5ad42d>=_0x24ef36){_0x5ad42d=_0x24ef36;_0x42e3e0=0xc8;}else{_0x42e3e0=0xce;}_0x11d60a['status'](_0x42e3e0);return _0x11d60a[_0x233f('0x20')]('Content-Range',_0x4b844d+'-'+_0x5ad42d+'/'+_0x24ef36)[_0x233f('0x1b')](_0x25bb3b);}return null;};}function patchUpdates(_0x47ab8a){return function(_0x563a1b){try{jsonpatch[_0x233f('0x21')](_0x563a1b,_0x47ab8a,!![]);}catch(_0x1a9946){return BPromise[_0x233f('0x22')](_0x1a9946);}return _0x563a1b[_0x233f('0x23')]();};}function saveUpdates(_0x55665c,_0x86cbab){return function(_0x527eab){if(_0x527eab){return _0x527eab[_0x233f('0x24')](_0x55665c)[_0x233f('0x25')](function(_0x1f3d0e){return _0x1f3d0e;});}return null;};}function removeEntity(_0x2a6089,_0x3fad70){return function(_0x3cae8c){if(_0x3cae8c){return _0x3cae8c[_0x233f('0x26')]()['then'](function(){_0x2a6089[_0x233f('0x19')](0xcc)[_0x233f('0x1a')]();});}};}function handleEntityNotFound(_0x1de48d,_0x42687c){return function(_0x26beae){if(!_0x26beae){_0x1de48d['sendStatus'](0x194);}return _0x26beae;};}function handleError(_0x4f7cfb,_0x5d3abb){_0x5d3abb=_0x5d3abb||0x1f4;return function(_0x4a5b6b){logger[_0x233f('0x27')](_0x4a5b6b['stack']);if(_0x4a5b6b[_0x233f('0x28')]){delete _0x4a5b6b[_0x233f('0x28')];}_0x4f7cfb['status'](_0x5d3abb)[_0x233f('0x29')](_0x4a5b6b);};}exports[_0x233f('0x2a')]=function(_0x4530d9,_0x15f8fb){var _0x5ca356={},_0xa211c8={},_0x516250={'count':0x0,'rows':[]};var _0x39a5e9=_[_0x233f('0x2b')](db['Automation'][_0x233f('0x2c')],function(_0x2fdfd6){return{'name':_0x2fdfd6[_0x233f('0x2d')],'type':_0x2fdfd6[_0x233f('0x2e')][_0x233f('0x2f')]};});_0xa211c8[_0x233f('0x30')]=_[_0x233f('0x2b')](_0x39a5e9,_0x233f('0x28'));_0xa211c8[_0x233f('0x31')]=_[_0x233f('0x32')](_0x4530d9[_0x233f('0x31')]);_0xa211c8[_0x233f('0x33')]=_[_0x233f('0x34')](_0xa211c8[_0x233f('0x30')],_0xa211c8['query']);_0x5ca356[_0x233f('0x35')]=_[_0x233f('0x34')](_0xa211c8[_0x233f('0x30')],qs[_0x233f('0x36')](_0x4530d9[_0x233f('0x31')][_0x233f('0x36')]));_0x5ca356[_0x233f('0x35')]=_0x5ca356[_0x233f('0x35')][_0x233f('0x37')]?_0x5ca356[_0x233f('0x35')]:_0xa211c8[_0x233f('0x30')];if(!_0x4530d9[_0x233f('0x31')][_0x233f('0x38')]('nolimit')){_0x5ca356['limit']=qs['limit'](_0x4530d9['query'][_0x233f('0x1f')]);_0x5ca356[_0x233f('0x1e')]=qs['offset'](_0x4530d9[_0x233f('0x31')][_0x233f('0x1e')]);}_0x5ca356[_0x233f('0x39')]=qs['sort'](_0x4530d9['query'][_0x233f('0x3a')]);_0x5ca356[_0x233f('0x3b')]=qs['filters'](_['pick'](_0x4530d9[_0x233f('0x31')],_0xa211c8['filters']),_0x39a5e9);if(_0x4530d9[_0x233f('0x31')][_0x233f('0x3c')]){_0x5ca356[_0x233f('0x3b')]=_[_0x233f('0x3d')](_0x5ca356[_0x233f('0x3b')],{'$or':_[_0x233f('0x2b')](_0x39a5e9,function(_0x211d19){if(_0x211d19[_0x233f('0x2e')]!=='VIRTUAL'){var _0x8b3ccb={};_0x8b3ccb[_0x211d19['name']]={'$like':'%'+_0x4530d9[_0x233f('0x31')]['filter']+'%'};return _0x8b3ccb;}})});}_0x5ca356=_[_0x233f('0x3d')]({},_0x5ca356,_0x4530d9[_0x233f('0x3e')]);var _0x4eb836={'where':_0x5ca356[_0x233f('0x3b')]};return db['Automation'][_0x233f('0x1d')](_0x4eb836)[_0x233f('0x25')](function(_0x4d8137){_0x516250[_0x233f('0x1d')]=_0x4d8137;if(_0x4530d9['query'][_0x233f('0x3f')]){_0x5ca356['include']=[{'all':!![]}];}return db[_0x233f('0x40')][_0x233f('0x41')](_0x5ca356);})[_0x233f('0x25')](function(_0x296db7){_0x516250['rows']=_0x296db7;return _0x516250;})['then'](respondWithFilteredResult(_0x15f8fb,_0x5ca356))['catch'](handleError(_0x15f8fb,null));};exports[_0x233f('0x42')]=function(_0x14bd41,_0x54d99b){var _0x2f4c65={'raw':![],'where':{'id':_0x14bd41[_0x233f('0x43')]['id']}},_0x116a32={};_0x116a32['model']=_['keys'](db[_0x233f('0x40')][_0x233f('0x2c')]);_0x116a32[_0x233f('0x31')]=_[_0x233f('0x32')](_0x14bd41[_0x233f('0x31')]);_0x116a32[_0x233f('0x33')]=_[_0x233f('0x34')](_0x116a32[_0x233f('0x30')],_0x116a32[_0x233f('0x31')]);_0x2f4c65[_0x233f('0x35')]=_['intersection'](_0x116a32['model'],qs[_0x233f('0x36')](_0x14bd41['query'][_0x233f('0x36')]));_0x2f4c65[_0x233f('0x35')]=_0x2f4c65['attributes']['length']?_0x2f4c65['attributes']:_0x116a32['model'];if(_0x14bd41[_0x233f('0x31')][_0x233f('0x3f')]){_0x2f4c65[_0x233f('0x44')]=[{'all':!![]}];}_0x2f4c65=_[_0x233f('0x3d')]({},_0x2f4c65,_0x14bd41[_0x233f('0x3e')]);return db[_0x233f('0x40')]['find'](_0x2f4c65)[_0x233f('0x25')](handleEntityNotFound(_0x54d99b,null))['then'](respondWithResult(_0x54d99b,null))[_0x233f('0x45')](handleError(_0x54d99b,null));};exports[_0x233f('0x46')]=function(_0x2dbc41,_0x323120){return db[_0x233f('0x40')]['create'](_0x2dbc41[_0x233f('0x47')],{})[_0x233f('0x25')](respondWithResult(_0x323120,0xc9))[_0x233f('0x45')](handleError(_0x323120,null));};exports[_0x233f('0x24')]=function(_0x5d7dac,_0x1a4cc6){if(_0x5d7dac[_0x233f('0x47')]['id']){delete _0x5d7dac[_0x233f('0x47')]['id'];}return db[_0x233f('0x40')]['find']({'where':{'id':_0x5d7dac[_0x233f('0x43')]['id']}})[_0x233f('0x25')](handleEntityNotFound(_0x1a4cc6,null))[_0x233f('0x25')](saveUpdates(_0x5d7dac[_0x233f('0x47')],null))[_0x233f('0x25')](respondWithResult(_0x1a4cc6,null))[_0x233f('0x45')](handleError(_0x1a4cc6,null));};exports[_0x233f('0x26')]=function(_0x11e15d,_0x1f9472){return db[_0x233f('0x40')][_0x233f('0x48')]({'where':{'id':_0x11e15d[_0x233f('0x43')]['id']}})[_0x233f('0x25')](handleEntityNotFound(_0x1f9472,null))[_0x233f('0x25')](removeEntity(_0x1f9472,null))[_0x233f('0x45')](handleError(_0x1f9472,null));};exports['addConditions']=function(_0x4c3d19,_0x29cbb3,_0x1524ea){return db[_0x233f('0x49')][_0x233f('0x48')]({'where':{'id':_0x4c3d19[_0x233f('0x43')]['id']}})[_0x233f('0x25')](handleEntityNotFound(_0x29cbb3,null))[_0x233f('0x25')](function(_0x210c4e){if(_0x210c4e){return _0x210c4e[_0x233f('0x4a')](_0x4c3d19[_0x233f('0x47')][_0x233f('0x4b')],_['omit'](_0x4c3d19[_0x233f('0x47')],['ids','id'])||{});}})[_0x233f('0x25')](respondWithResult(_0x29cbb3,null))[_0x233f('0x45')](handleError(_0x29cbb3,null));};exports[_0x233f('0x4c')]=function(_0x547e6b,_0x150e4d,_0xde4a27){var _0x28cf39={};var _0x306039={};var _0x2f387f;var _0x469a5a;return db['Automation'][_0x233f('0x4d')]({'where':{'id':_0x547e6b['params']['id']}})[_0x233f('0x25')](handleEntityNotFound(_0x150e4d,null))[_0x233f('0x25')](function(_0x5f34b7){if(_0x5f34b7){_0x2f387f=_0x5f34b7;_0x306039[_0x233f('0x30')]=_[_0x233f('0x32')](db[_0x233f('0x49')][_0x233f('0x2c')]);_0x306039[_0x233f('0x31')]=_['keys'](_0x547e6b['query']);_0x306039[_0x233f('0x33')]=_['intersection'](_0x306039[_0x233f('0x30')],_0x306039[_0x233f('0x31')]);_0x28cf39['attributes']=_['intersection'](_0x306039[_0x233f('0x30')],qs[_0x233f('0x36')](_0x547e6b[_0x233f('0x31')][_0x233f('0x36')]));_0x28cf39[_0x233f('0x35')]=_0x28cf39[_0x233f('0x35')][_0x233f('0x37')]?_0x28cf39[_0x233f('0x35')]:_0x306039[_0x233f('0x30')];_0x28cf39[_0x233f('0x39')]=qs[_0x233f('0x3a')](_0x547e6b[_0x233f('0x31')][_0x233f('0x3a')]);_0x28cf39[_0x233f('0x3b')]=qs[_0x233f('0x33')](_[_0x233f('0x4e')](_0x547e6b[_0x233f('0x31')],_0x306039[_0x233f('0x33')]));if(_0x547e6b[_0x233f('0x31')][_0x233f('0x3c')]){_0x28cf39['where']=_[_0x233f('0x3d')](_0x28cf39[_0x233f('0x3b')],{'$or':_['map'](_0x28cf39[_0x233f('0x35')],function(_0x5a8a29){var _0x57aa9c={};_0x57aa9c[_0x5a8a29]={'$like':'%'+_0x547e6b[_0x233f('0x31')][_0x233f('0x3c')]+'%'};return _0x57aa9c;})});}_0x28cf39=_[_0x233f('0x3d')]({},_0x28cf39,_0x547e6b[_0x233f('0x3e')]);return _0x2f387f[_0x233f('0x4c')](_0x28cf39);}})[_0x233f('0x25')](function(_0x2826dd){if(_0x2826dd){_0x469a5a=_0x2826dd[_0x233f('0x37')];if(!_0x547e6b[_0x233f('0x31')]['hasOwnProperty'](_0x233f('0x4f'))){_0x28cf39[_0x233f('0x1f')]=qs['limit'](_0x547e6b['query']['limit']);_0x28cf39[_0x233f('0x1e')]=qs['offset'](_0x547e6b[_0x233f('0x31')][_0x233f('0x1e')]);}return _0x2f387f[_0x233f('0x4c')](_0x28cf39);}})['then'](function(_0x2c2a04){if(_0x2c2a04){return _0x2c2a04?{'count':_0x469a5a,'rows':_0x2c2a04}:null;}})[_0x233f('0x25')](respondWithResult(_0x150e4d,null))['catch'](handleError(_0x150e4d,null));};exports[_0x233f('0x50')]=function(_0x25a188,_0x2d3905,_0x52973b){return db[_0x233f('0x51')]['find']({'where':{'id':_0x25a188[_0x233f('0x43')]['id']}})[_0x233f('0x25')](handleEntityNotFound(_0x2d3905,null))['then'](function(_0x47c9d9){if(_0x47c9d9){return _0x47c9d9[_0x233f('0x50')](_0x25a188[_0x233f('0x47')][_0x233f('0x4b')],_['omit'](_0x25a188['body'],[_0x233f('0x4b'),'id'])||{});}})[_0x233f('0x25')](respondWithResult(_0x2d3905,null))[_0x233f('0x45')](handleError(_0x2d3905,null));};exports[_0x233f('0x52')]=function(_0x155561,_0x18830c,_0x1ecac4){var _0x5c0819={};var _0x2dca8d={};var _0x4de0d9;var _0x46a1a3;return db[_0x233f('0x40')][_0x233f('0x4d')]({'where':{'id':_0x155561[_0x233f('0x43')]['id']}})['then'](handleEntityNotFound(_0x18830c,null))['then'](function(_0x54b0a5){if(_0x54b0a5){_0x4de0d9=_0x54b0a5;_0x2dca8d[_0x233f('0x30')]=_[_0x233f('0x32')](db[_0x233f('0x51')][_0x233f('0x2c')]);_0x2dca8d[_0x233f('0x31')]=_[_0x233f('0x32')](_0x155561[_0x233f('0x31')]);_0x2dca8d['filters']=_[_0x233f('0x34')](_0x2dca8d['model'],_0x2dca8d[_0x233f('0x31')]);_0x5c0819[_0x233f('0x35')]=_[_0x233f('0x34')](_0x2dca8d[_0x233f('0x30')],qs['fields'](_0x155561[_0x233f('0x31')][_0x233f('0x36')]));_0x5c0819[_0x233f('0x35')]=_0x5c0819[_0x233f('0x35')]['length']?_0x5c0819[_0x233f('0x35')]:_0x2dca8d['model'];_0x5c0819['order']=qs[_0x233f('0x3a')](_0x155561[_0x233f('0x31')][_0x233f('0x3a')]);_0x5c0819[_0x233f('0x3b')]=qs[_0x233f('0x33')](_[_0x233f('0x4e')](_0x155561[_0x233f('0x31')],_0x2dca8d[_0x233f('0x33')]));if(_0x155561[_0x233f('0x31')][_0x233f('0x3c')]){_0x5c0819[_0x233f('0x3b')]=_[_0x233f('0x3d')](_0x5c0819[_0x233f('0x3b')],{'$or':_[_0x233f('0x2b')](_0x5c0819['attributes'],function(_0x540aa5){var _0x27c759={};_0x27c759[_0x540aa5]={'$like':'%'+_0x155561[_0x233f('0x31')][_0x233f('0x3c')]+'%'};return _0x27c759;})});}_0x5c0819=_[_0x233f('0x3d')]({},_0x5c0819,_0x155561[_0x233f('0x3e')]);return _0x4de0d9[_0x233f('0x52')](_0x5c0819);}})[_0x233f('0x25')](function(_0x3b0583){if(_0x3b0583){_0x46a1a3=_0x3b0583[_0x233f('0x37')];if(!_0x155561[_0x233f('0x31')][_0x233f('0x38')](_0x233f('0x4f'))){_0x5c0819[_0x233f('0x1f')]=qs[_0x233f('0x1f')](_0x155561['query']['limit']);_0x5c0819[_0x233f('0x1e')]=qs[_0x233f('0x1e')](_0x155561[_0x233f('0x31')][_0x233f('0x1e')]);}return _0x4de0d9[_0x233f('0x52')](_0x5c0819);}})[_0x233f('0x25')](function(_0x11218e){if(_0x11218e){return _0x11218e?{'count':_0x46a1a3,'rows':_0x11218e}:null;}})[_0x233f('0x25')](respondWithResult(_0x18830c,null))['catch'](handleError(_0x18830c,null));};