8817f8048000e68585bd736741fd92f8c11c44ef
[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 _0xb08f=['../../config/license/util','status','end','json','undefined','limit','count','offset','set','reject','save','update','then','sendStatus','name','send','index','map','Automation','rawAttributes','type','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','sort','where','pick','filter','merge','VIRTUAL','include','findAll','rows','catch','show','params','options','find','create','body','destroy','addConditions','Condition','ids','findOne','order','getConditions','addActions','omit','Action','getActions','eml-format','fast-json-patch','request-promise','moment','mustache','util','sox','to-csv','ejs','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment'];(function(_0x138836,_0x229129){var _0x322688=function(_0x54b101){while(--_0x54b101){_0x138836['push'](_0x138836['shift']());}};_0x322688(++_0x229129);}(_0xb08f,0x131));var _0xfb08=function(_0x54e8c0,_0x531cca){_0x54e8c0=_0x54e8c0-0x0;var _0xe45340=_0xb08f[_0x54e8c0];return _0xe45340;};'use strict';var emlformat=require(_0xfb08('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0xfb08('0x1'));var rp=require(_0xfb08('0x2'));var moment=require(_0xfb08('0x3'));var BPromise=require('bluebird');var Mustache=require(_0xfb08('0x4'));var util=require(_0xfb08('0x5'));var path=require('path');var sox=require(_0xfb08('0x6'));var csv=require(_0xfb08('0x7'));var ejs=require(_0xfb08('0x8'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xfb08('0x9'));var squel=require(_0xfb08('0xa'));var crypto=require(_0xfb08('0xb'));var jsforce=require(_0xfb08('0xc'));var deskjs=require(_0xfb08('0xd'));var toCsv=require(_0xfb08('0x7'));var querystring=require('querystring');var Papa=require(_0xfb08('0xe'));var Redis=require(_0xfb08('0xf'));var authService=require(_0xfb08('0x10'));var qs=require('../../components/parsers/qs');var as=require(_0xfb08('0x11'));var hardwareService=require(_0xfb08('0x12'));var logger=require(_0xfb08('0x13'))(_0xfb08('0x14'));var utils=require(_0xfb08('0x15'));var config=require(_0xfb08('0x16'));var licenseUtil=require(_0xfb08('0x17'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x7570ea,_0x1c135e){_0x1c135e=_0x1c135e||0xcc;return function(_0x5dcce0){if(_0x5dcce0){return _0x7570ea['sendStatus'](_0x1c135e);}return _0x7570ea[_0xfb08('0x18')](_0x1c135e)[_0xfb08('0x19')]();};}function respondWithResult(_0x26710e,_0x5e6fa6){_0x5e6fa6=_0x5e6fa6||0xc8;return function(_0x54b737){if(_0x54b737){return _0x26710e[_0xfb08('0x18')](_0x5e6fa6)[_0xfb08('0x1a')](_0x54b737);}};}function respondWithFilteredResult(_0x380d1a,_0x459ce5){return function(_0x4a5417){if(_0x4a5417){var _0x4d19f8=typeof _0x459ce5['offset']===_0xfb08('0x1b')&&typeof _0x459ce5[_0xfb08('0x1c')]===_0xfb08('0x1b');var _0x5b5831=_0x4a5417[_0xfb08('0x1d')];var _0x56f792=_0x4d19f8?0x0:_0x459ce5[_0xfb08('0x1e')];var _0x205158=_0x4d19f8?_0x4a5417[_0xfb08('0x1d')]:_0x459ce5[_0xfb08('0x1e')]+_0x459ce5[_0xfb08('0x1c')];var _0x17d416;if(_0x205158>=_0x5b5831){_0x205158=_0x5b5831;_0x17d416=0xc8;}else{_0x17d416=0xce;}_0x380d1a[_0xfb08('0x18')](_0x17d416);return _0x380d1a[_0xfb08('0x1f')]('Content-Range',_0x56f792+'-'+_0x205158+'/'+_0x5b5831)[_0xfb08('0x1a')](_0x4a5417);}return null;};}function patchUpdates(_0x104c08){return function(_0x5b0923){try{jsonpatch['apply'](_0x5b0923,_0x104c08,!![]);}catch(_0xe3c6f0){return BPromise[_0xfb08('0x20')](_0xe3c6f0);}return _0x5b0923[_0xfb08('0x21')]();};}function saveUpdates(_0x9af45d,_0x2b9b90){return function(_0x2203f2){if(_0x2203f2){return _0x2203f2[_0xfb08('0x22')](_0x9af45d)[_0xfb08('0x23')](function(_0x4452e3){return _0x4452e3;});}return null;};}function removeEntity(_0x46c26d,_0x5f55a1){return function(_0x47b78e){if(_0x47b78e){return _0x47b78e['destroy']()[_0xfb08('0x23')](function(){_0x46c26d[_0xfb08('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x34c0fb,_0x14d25e){return function(_0x6e9ac9){if(!_0x6e9ac9){_0x34c0fb[_0xfb08('0x24')](0x194);}return _0x6e9ac9;};}function handleError(_0x22f071,_0x2a295c){_0x2a295c=_0x2a295c||0x1f4;return function(_0x2f69f0){logger['error'](_0x2f69f0['stack']);if(_0x2f69f0[_0xfb08('0x25')]){delete _0x2f69f0[_0xfb08('0x25')];}_0x22f071['status'](_0x2a295c)[_0xfb08('0x26')](_0x2f69f0);};}exports[_0xfb08('0x27')]=function(_0x445dce,_0x1d9225){var _0x3de9a2={},_0x444e9a={},_0x463f60={'count':0x0,'rows':[]};var _0x190e41=_[_0xfb08('0x28')](db[_0xfb08('0x29')][_0xfb08('0x2a')],function(_0x1e1f49){return{'name':_0x1e1f49['fieldName'],'type':_0x1e1f49[_0xfb08('0x2b')][_0xfb08('0x2c')]};});_0x444e9a[_0xfb08('0x2d')]=_['map'](_0x190e41,_0xfb08('0x25'));_0x444e9a[_0xfb08('0x2e')]=_[_0xfb08('0x2f')](_0x445dce[_0xfb08('0x2e')]);_0x444e9a[_0xfb08('0x30')]=_[_0xfb08('0x31')](_0x444e9a['model'],_0x444e9a[_0xfb08('0x2e')]);_0x3de9a2['attributes']=_[_0xfb08('0x31')](_0x444e9a['model'],qs[_0xfb08('0x32')](_0x445dce[_0xfb08('0x2e')][_0xfb08('0x32')]));_0x3de9a2[_0xfb08('0x33')]=_0x3de9a2['attributes'][_0xfb08('0x34')]?_0x3de9a2[_0xfb08('0x33')]:_0x444e9a['model'];if(!_0x445dce[_0xfb08('0x2e')][_0xfb08('0x35')](_0xfb08('0x36'))){_0x3de9a2[_0xfb08('0x1c')]=qs[_0xfb08('0x1c')](_0x445dce[_0xfb08('0x2e')][_0xfb08('0x1c')]);_0x3de9a2[_0xfb08('0x1e')]=qs[_0xfb08('0x1e')](_0x445dce[_0xfb08('0x2e')][_0xfb08('0x1e')]);}_0x3de9a2['order']=qs[_0xfb08('0x37')](_0x445dce['query'][_0xfb08('0x37')]);_0x3de9a2[_0xfb08('0x38')]=qs['filters'](_[_0xfb08('0x39')](_0x445dce['query'],_0x444e9a[_0xfb08('0x30')]),_0x190e41);if(_0x445dce['query'][_0xfb08('0x3a')]){_0x3de9a2['where']=_[_0xfb08('0x3b')](_0x3de9a2[_0xfb08('0x38')],{'$or':_[_0xfb08('0x28')](_0x190e41,function(_0x95276a){if(_0x95276a[_0xfb08('0x2b')]!==_0xfb08('0x3c')){var _0x525192={};_0x525192[_0x95276a[_0xfb08('0x25')]]={'$like':'%'+_0x445dce[_0xfb08('0x2e')][_0xfb08('0x3a')]+'%'};return _0x525192;}})});}_0x3de9a2=_[_0xfb08('0x3b')]({},_0x3de9a2,_0x445dce['options']);var _0x54870c={'where':_0x3de9a2[_0xfb08('0x38')]};return db[_0xfb08('0x29')][_0xfb08('0x1d')](_0x54870c)[_0xfb08('0x23')](function(_0x44478e){_0x463f60[_0xfb08('0x1d')]=_0x44478e;if(_0x445dce[_0xfb08('0x2e')]['includeAll']){_0x3de9a2[_0xfb08('0x3d')]=[{'all':!![]}];}return db[_0xfb08('0x29')][_0xfb08('0x3e')](_0x3de9a2);})[_0xfb08('0x23')](function(_0x3b9e33){_0x463f60[_0xfb08('0x3f')]=_0x3b9e33;return _0x463f60;})[_0xfb08('0x23')](respondWithFilteredResult(_0x1d9225,_0x3de9a2))[_0xfb08('0x40')](handleError(_0x1d9225,null));};exports[_0xfb08('0x41')]=function(_0x227512,_0xd6ec7c){var _0x201abb={'raw':![],'where':{'id':_0x227512[_0xfb08('0x42')]['id']}},_0x796320={};_0x796320['model']=_['keys'](db[_0xfb08('0x29')][_0xfb08('0x2a')]);_0x796320['query']=_['keys'](_0x227512['query']);_0x796320[_0xfb08('0x30')]=_[_0xfb08('0x31')](_0x796320[_0xfb08('0x2d')],_0x796320[_0xfb08('0x2e')]);_0x201abb[_0xfb08('0x33')]=_[_0xfb08('0x31')](_0x796320[_0xfb08('0x2d')],qs[_0xfb08('0x32')](_0x227512['query']['fields']));_0x201abb['attributes']=_0x201abb['attributes'][_0xfb08('0x34')]?_0x201abb[_0xfb08('0x33')]:_0x796320[_0xfb08('0x2d')];if(_0x227512[_0xfb08('0x2e')]['includeAll']){_0x201abb[_0xfb08('0x3d')]=[{'all':!![]}];}_0x201abb=_['merge']({},_0x201abb,_0x227512[_0xfb08('0x43')]);return db[_0xfb08('0x29')][_0xfb08('0x44')](_0x201abb)[_0xfb08('0x23')](handleEntityNotFound(_0xd6ec7c,null))[_0xfb08('0x23')](respondWithResult(_0xd6ec7c,null))[_0xfb08('0x40')](handleError(_0xd6ec7c,null));};exports[_0xfb08('0x45')]=function(_0x208df3,_0x348f99){return db[_0xfb08('0x29')][_0xfb08('0x45')](_0x208df3[_0xfb08('0x46')],{})['then'](respondWithResult(_0x348f99,0xc9))[_0xfb08('0x40')](handleError(_0x348f99,null));};exports[_0xfb08('0x22')]=function(_0xe4546f,_0x3cef1d){if(_0xe4546f['body']['id']){delete _0xe4546f[_0xfb08('0x46')]['id'];}return db[_0xfb08('0x29')][_0xfb08('0x44')]({'where':{'id':_0xe4546f[_0xfb08('0x42')]['id']}})[_0xfb08('0x23')](handleEntityNotFound(_0x3cef1d,null))[_0xfb08('0x23')](saveUpdates(_0xe4546f[_0xfb08('0x46')],null))['then'](respondWithResult(_0x3cef1d,null))[_0xfb08('0x40')](handleError(_0x3cef1d,null));};exports[_0xfb08('0x47')]=function(_0x11b413,_0x203b3d){return db['Automation'][_0xfb08('0x44')]({'where':{'id':_0x11b413['params']['id']}})['then'](handleEntityNotFound(_0x203b3d,null))[_0xfb08('0x23')](removeEntity(_0x203b3d,null))[_0xfb08('0x40')](handleError(_0x203b3d,null));};exports[_0xfb08('0x48')]=function(_0x2154d4,_0x3b8fa0,_0x2cccc2){return db[_0xfb08('0x49')][_0xfb08('0x44')]({'where':{'id':_0x2154d4[_0xfb08('0x42')]['id']}})[_0xfb08('0x23')](handleEntityNotFound(_0x3b8fa0,null))['then'](function(_0x14e9d5){if(_0x14e9d5){return _0x14e9d5[_0xfb08('0x48')](_0x2154d4[_0xfb08('0x46')][_0xfb08('0x4a')],_['omit'](_0x2154d4[_0xfb08('0x46')],[_0xfb08('0x4a'),'id'])||{});}})['then'](respondWithResult(_0x3b8fa0,null))[_0xfb08('0x40')](handleError(_0x3b8fa0,null));};exports['getConditions']=function(_0x1a4276,_0x5bea7e,_0x168151){var _0x4df57b={};var _0xe490da={};var _0x2c5153;var _0x384c9d;return db[_0xfb08('0x29')][_0xfb08('0x4b')]({'where':{'id':_0x1a4276[_0xfb08('0x42')]['id']}})[_0xfb08('0x23')](handleEntityNotFound(_0x5bea7e,null))[_0xfb08('0x23')](function(_0x39b05e){if(_0x39b05e){_0x2c5153=_0x39b05e;_0xe490da[_0xfb08('0x2d')]=_['keys'](db[_0xfb08('0x49')][_0xfb08('0x2a')]);_0xe490da[_0xfb08('0x2e')]=_['keys'](_0x1a4276[_0xfb08('0x2e')]);_0xe490da[_0xfb08('0x30')]=_[_0xfb08('0x31')](_0xe490da[_0xfb08('0x2d')],_0xe490da[_0xfb08('0x2e')]);_0x4df57b[_0xfb08('0x33')]=_['intersection'](_0xe490da[_0xfb08('0x2d')],qs['fields'](_0x1a4276[_0xfb08('0x2e')]['fields']));_0x4df57b[_0xfb08('0x33')]=_0x4df57b['attributes'][_0xfb08('0x34')]?_0x4df57b['attributes']:_0xe490da[_0xfb08('0x2d')];_0x4df57b[_0xfb08('0x4c')]=qs['sort'](_0x1a4276[_0xfb08('0x2e')][_0xfb08('0x37')]);_0x4df57b[_0xfb08('0x38')]=qs['filters'](_['pick'](_0x1a4276['query'],_0xe490da['filters']));if(_0x1a4276[_0xfb08('0x2e')][_0xfb08('0x3a')]){_0x4df57b[_0xfb08('0x38')]=_[_0xfb08('0x3b')](_0x4df57b[_0xfb08('0x38')],{'$or':_[_0xfb08('0x28')](_0x4df57b[_0xfb08('0x33')],function(_0x534708){var _0xb32708={};_0xb32708[_0x534708]={'$like':'%'+_0x1a4276['query']['filter']+'%'};return _0xb32708;})});}_0x4df57b=_[_0xfb08('0x3b')]({},_0x4df57b,_0x1a4276['options']);return _0x2c5153[_0xfb08('0x4d')](_0x4df57b);}})[_0xfb08('0x23')](function(_0x20acb3){if(_0x20acb3){_0x384c9d=_0x20acb3[_0xfb08('0x34')];if(!_0x1a4276[_0xfb08('0x2e')][_0xfb08('0x35')](_0xfb08('0x36'))){_0x4df57b[_0xfb08('0x1c')]=qs[_0xfb08('0x1c')](_0x1a4276['query'][_0xfb08('0x1c')]);_0x4df57b['offset']=qs['offset'](_0x1a4276['query'][_0xfb08('0x1e')]);}return _0x2c5153[_0xfb08('0x4d')](_0x4df57b);}})[_0xfb08('0x23')](function(_0x5da64e){if(_0x5da64e){return _0x5da64e?{'count':_0x384c9d,'rows':_0x5da64e}:null;}})['then'](respondWithResult(_0x5bea7e,null))[_0xfb08('0x40')](handleError(_0x5bea7e,null));};exports[_0xfb08('0x4e')]=function(_0x259326,_0x654148,_0x2d86bf){return db['Action'][_0xfb08('0x44')]({'where':{'id':_0x259326['params']['id']}})[_0xfb08('0x23')](handleEntityNotFound(_0x654148,null))['then'](function(_0x56a8e4){if(_0x56a8e4){return _0x56a8e4[_0xfb08('0x4e')](_0x259326[_0xfb08('0x46')][_0xfb08('0x4a')],_[_0xfb08('0x4f')](_0x259326[_0xfb08('0x46')],[_0xfb08('0x4a'),'id'])||{});}})[_0xfb08('0x23')](respondWithResult(_0x654148,null))[_0xfb08('0x40')](handleError(_0x654148,null));};exports['getActions']=function(_0x3fa150,_0x147762,_0x542189){var _0x3ed7ba={};var _0xc7f51={};var _0x17158c;var _0x13730a;return db['Automation'][_0xfb08('0x4b')]({'where':{'id':_0x3fa150['params']['id']}})[_0xfb08('0x23')](handleEntityNotFound(_0x147762,null))[_0xfb08('0x23')](function(_0x4c9090){if(_0x4c9090){_0x17158c=_0x4c9090;_0xc7f51['model']=_['keys'](db[_0xfb08('0x50')][_0xfb08('0x2a')]);_0xc7f51[_0xfb08('0x2e')]=_[_0xfb08('0x2f')](_0x3fa150[_0xfb08('0x2e')]);_0xc7f51[_0xfb08('0x30')]=_['intersection'](_0xc7f51[_0xfb08('0x2d')],_0xc7f51['query']);_0x3ed7ba['attributes']=_[_0xfb08('0x31')](_0xc7f51[_0xfb08('0x2d')],qs[_0xfb08('0x32')](_0x3fa150[_0xfb08('0x2e')][_0xfb08('0x32')]));_0x3ed7ba[_0xfb08('0x33')]=_0x3ed7ba[_0xfb08('0x33')]['length']?_0x3ed7ba['attributes']:_0xc7f51[_0xfb08('0x2d')];_0x3ed7ba[_0xfb08('0x4c')]=qs[_0xfb08('0x37')](_0x3fa150['query'][_0xfb08('0x37')]);_0x3ed7ba['where']=qs[_0xfb08('0x30')](_['pick'](_0x3fa150[_0xfb08('0x2e')],_0xc7f51[_0xfb08('0x30')]));if(_0x3fa150[_0xfb08('0x2e')]['filter']){_0x3ed7ba['where']=_['merge'](_0x3ed7ba[_0xfb08('0x38')],{'$or':_[_0xfb08('0x28')](_0x3ed7ba[_0xfb08('0x33')],function(_0x2994c2){var _0x4253d1={};_0x4253d1[_0x2994c2]={'$like':'%'+_0x3fa150[_0xfb08('0x2e')][_0xfb08('0x3a')]+'%'};return _0x4253d1;})});}_0x3ed7ba=_['merge']({},_0x3ed7ba,_0x3fa150[_0xfb08('0x43')]);return _0x17158c['getActions'](_0x3ed7ba);}})[_0xfb08('0x23')](function(_0x26e405){if(_0x26e405){_0x13730a=_0x26e405[_0xfb08('0x34')];if(!_0x3fa150[_0xfb08('0x2e')][_0xfb08('0x35')](_0xfb08('0x36'))){_0x3ed7ba[_0xfb08('0x1c')]=qs['limit'](_0x3fa150[_0xfb08('0x2e')]['limit']);_0x3ed7ba[_0xfb08('0x1e')]=qs[_0xfb08('0x1e')](_0x3fa150['query'][_0xfb08('0x1e')]);}return _0x17158c[_0xfb08('0x51')](_0x3ed7ba);}})[_0xfb08('0x23')](function(_0x42fd02){if(_0x42fd02){return _0x42fd02?{'count':_0x13730a,'rows':_0x42fd02}:null;}})[_0xfb08('0x23')](respondWithResult(_0x147762,null))[_0xfb08('0x40')](handleError(_0x147762,null));};