Built motion from commit (unavailable).|2.4.21
[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 _0x8bae=['map','type','key','model','query','keys','filters','fields','attributes','hasOwnProperty','nolimit','limit','offset','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','Automation','rows','catch','show','params','rawAttributes','intersection','include','create','find','addConditions','Condition','body','ids','omit','getConditions','findOne','length','addActions','Action','order','getActions','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','squel','crypto','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../mysqldb','sendStatus','status','end','json','count','set','reject','save','update','then','destroy','error','stack','name','send','index'];(function(_0x2beba9,_0x304957){var _0xfb6f7e=function(_0x331689){while(--_0x331689){_0x2beba9['push'](_0x2beba9['shift']());}};_0xfb6f7e(++_0x304957);}(_0x8bae,0x121));var _0xe8ba=function(_0x19950b,_0x53d0d7){_0x19950b=_0x19950b-0x0;var _0x3c6b41=_0x8bae[_0x19950b];return _0x3c6b41;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xe8ba('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xe8ba('0x1'));var jsonpatch=require(_0xe8ba('0x2'));var rp=require(_0xe8ba('0x3'));var moment=require(_0xe8ba('0x4'));var BPromise=require(_0xe8ba('0x5'));var Mustache=require(_0xe8ba('0x6'));var util=require(_0xe8ba('0x7'));var path=require(_0xe8ba('0x8'));var sox=require(_0xe8ba('0x9'));var csv=require(_0xe8ba('0xa'));var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var squel=require(_0xe8ba('0xb'));var crypto=require(_0xe8ba('0xc'));var jsforce=require('jsforce');var deskjs=require(_0xe8ba('0xd'));var toCsv=require(_0xe8ba('0xa'));var querystring=require('querystring');var Papa=require(_0xe8ba('0xe'));var Redis=require(_0xe8ba('0xf'));var authService=require(_0xe8ba('0x10'));var qs=require(_0xe8ba('0x11'));var as=require(_0xe8ba('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xe8ba('0x13'))(_0xe8ba('0x14'));var utils=require(_0xe8ba('0x15'));var config=require('../../config/environment');var db=require(_0xe8ba('0x16'))['db'];function respondWithStatusCode(_0x34453d,_0xa020b9){_0xa020b9=_0xa020b9||0xcc;return function(_0x17e2c3){if(_0x17e2c3){return _0x34453d[_0xe8ba('0x17')](_0xa020b9);}return _0x34453d[_0xe8ba('0x18')](_0xa020b9)[_0xe8ba('0x19')]();};}function respondWithResult(_0x543823,_0xc14151){_0xc14151=_0xc14151||0xc8;return function(_0x57df0e){if(_0x57df0e){return _0x543823[_0xe8ba('0x18')](_0xc14151)[_0xe8ba('0x1a')](_0x57df0e);}};}function respondWithFilteredResult(_0xbd9071,_0x21564d){return function(_0x4b083e){if(_0x4b083e){var _0x3a8be9=_0x4b083e[_0xe8ba('0x1b')],_0x214a59=_0x21564d['offset'],_0x3b7698=_0x21564d['offset']+_0x21564d['limit'],_0x3bd4a0;if(_0x3b7698>=_0x3a8be9){_0x3b7698=_0x3a8be9;_0x3bd4a0=0xc8;}else{_0x3bd4a0=0xce;}_0xbd9071[_0xe8ba('0x18')](_0x3bd4a0);return _0xbd9071[_0xe8ba('0x1c')]('Content-Range',_0x214a59+'-'+_0x3b7698+'/'+_0x3a8be9)[_0xe8ba('0x1a')](_0x4b083e);}return null;};}function patchUpdates(_0x2c6511){return function(_0x3e25a6){try{jsonpatch['apply'](_0x3e25a6,_0x2c6511,!![]);}catch(_0x585df8){return BPromise[_0xe8ba('0x1d')](_0x585df8);}return _0x3e25a6[_0xe8ba('0x1e')]();};}function saveUpdates(_0xa4c83d,_0x4c00c1){return function(_0x355a4d){if(_0x355a4d){return _0x355a4d[_0xe8ba('0x1f')](_0xa4c83d)[_0xe8ba('0x20')](function(_0x4859e4){return _0x4859e4;});}return null;};}function removeEntity(_0x2f3b83,_0x3fa8b9){return function(_0x2cc132){if(_0x2cc132){return _0x2cc132[_0xe8ba('0x21')]()['then'](function(){_0x2f3b83[_0xe8ba('0x18')](0xcc)[_0xe8ba('0x19')]();});}};}function handleEntityNotFound(_0x146c73,_0x1826c5){return function(_0x329302){if(!_0x329302){_0x146c73[_0xe8ba('0x17')](0x194);}return _0x329302;};}function handleError(_0x3de460,_0x3d5197){_0x3d5197=_0x3d5197||0x1f4;return function(_0x32042b){logger[_0xe8ba('0x22')](_0x32042b[_0xe8ba('0x23')]);if(_0x32042b[_0xe8ba('0x24')]){delete _0x32042b[_0xe8ba('0x24')];}_0x3de460[_0xe8ba('0x18')](_0x3d5197)[_0xe8ba('0x25')](_0x32042b);};}exports[_0xe8ba('0x26')]=function(_0x3be00e,_0x146a0b){var _0x163d2d={},_0x2acab7={},_0x5a310a={'count':0x0,'rows':[]};var _0x4ca18b=_[_0xe8ba('0x27')](db['Automation']['rawAttributes'],function(_0x1b24c4){return{'name':_0x1b24c4['fieldName'],'type':_0x1b24c4[_0xe8ba('0x28')][_0xe8ba('0x29')]};});_0x2acab7[_0xe8ba('0x2a')]=_['map'](_0x4ca18b,_0xe8ba('0x24'));_0x2acab7[_0xe8ba('0x2b')]=_[_0xe8ba('0x2c')](_0x3be00e[_0xe8ba('0x2b')]);_0x2acab7[_0xe8ba('0x2d')]=_['intersection'](_0x2acab7[_0xe8ba('0x2a')],_0x2acab7[_0xe8ba('0x2b')]);_0x163d2d['attributes']=_['intersection'](_0x2acab7[_0xe8ba('0x2a')],qs[_0xe8ba('0x2e')](_0x3be00e[_0xe8ba('0x2b')]['fields']));_0x163d2d[_0xe8ba('0x2f')]=_0x163d2d[_0xe8ba('0x2f')]['length']?_0x163d2d[_0xe8ba('0x2f')]:_0x2acab7['model'];if(!_0x3be00e[_0xe8ba('0x2b')][_0xe8ba('0x30')](_0xe8ba('0x31'))){_0x163d2d[_0xe8ba('0x32')]=qs[_0xe8ba('0x32')](_0x3be00e[_0xe8ba('0x2b')]['limit']);_0x163d2d[_0xe8ba('0x33')]=qs['offset'](_0x3be00e[_0xe8ba('0x2b')]['offset']);}_0x163d2d['order']=qs[_0xe8ba('0x34')](_0x3be00e[_0xe8ba('0x2b')][_0xe8ba('0x34')]);_0x163d2d[_0xe8ba('0x35')]=qs[_0xe8ba('0x2d')](_[_0xe8ba('0x36')](_0x3be00e['query'],_0x2acab7[_0xe8ba('0x2d')]));if(_0x3be00e[_0xe8ba('0x2b')][_0xe8ba('0x37')]){_0x163d2d[_0xe8ba('0x35')]=_[_0xe8ba('0x38')](_0x163d2d[_0xe8ba('0x35')],{'$or':_[_0xe8ba('0x27')](_0x4ca18b,function(_0x4ccf48){if(_0x4ccf48['type']!==_0xe8ba('0x39')){var _0x4d605a={};_0x4d605a[_0x4ccf48[_0xe8ba('0x24')]]={'$like':'%'+_0x3be00e['query'][_0xe8ba('0x37')]+'%'};return _0x4d605a;}})});}_0x163d2d=_[_0xe8ba('0x38')]({},_0x163d2d,_0x3be00e[_0xe8ba('0x3a')]);var _0x8c9bf7={'where':_0x163d2d[_0xe8ba('0x35')]};return db['Automation']['count'](_0x8c9bf7)[_0xe8ba('0x20')](function(_0x93bc53){_0x5a310a[_0xe8ba('0x1b')]=_0x93bc53;if(_0x3be00e[_0xe8ba('0x2b')][_0xe8ba('0x3b')]){_0x163d2d['include']=[{'all':!![]}];}return db[_0xe8ba('0x3c')]['findAll'](_0x163d2d);})[_0xe8ba('0x20')](function(_0x1ff858){_0x5a310a[_0xe8ba('0x3d')]=_0x1ff858;return _0x5a310a;})['then'](respondWithFilteredResult(_0x146a0b,_0x163d2d))[_0xe8ba('0x3e')](handleError(_0x146a0b,null));};exports[_0xe8ba('0x3f')]=function(_0x406548,_0xbd77d1){var _0x351db9={'raw':![],'where':{'id':_0x406548[_0xe8ba('0x40')]['id']}},_0x447b7f={};_0x447b7f[_0xe8ba('0x2a')]=_[_0xe8ba('0x2c')](db[_0xe8ba('0x3c')][_0xe8ba('0x41')]);_0x447b7f['query']=_[_0xe8ba('0x2c')](_0x406548[_0xe8ba('0x2b')]);_0x447b7f['filters']=_[_0xe8ba('0x42')](_0x447b7f[_0xe8ba('0x2a')],_0x447b7f[_0xe8ba('0x2b')]);_0x351db9[_0xe8ba('0x2f')]=_[_0xe8ba('0x42')](_0x447b7f[_0xe8ba('0x2a')],qs[_0xe8ba('0x2e')](_0x406548[_0xe8ba('0x2b')][_0xe8ba('0x2e')]));_0x351db9[_0xe8ba('0x2f')]=_0x351db9[_0xe8ba('0x2f')]['length']?_0x351db9[_0xe8ba('0x2f')]:_0x447b7f[_0xe8ba('0x2a')];if(_0x406548['query']['includeAll']){_0x351db9[_0xe8ba('0x43')]=[{'all':!![]}];}_0x351db9=_[_0xe8ba('0x38')]({},_0x351db9,_0x406548[_0xe8ba('0x3a')]);return db[_0xe8ba('0x3c')]['find'](_0x351db9)[_0xe8ba('0x20')](handleEntityNotFound(_0xbd77d1,null))[_0xe8ba('0x20')](respondWithResult(_0xbd77d1,null))[_0xe8ba('0x3e')](handleError(_0xbd77d1,null));};exports[_0xe8ba('0x44')]=function(_0x51b655,_0x582857){return db[_0xe8ba('0x3c')][_0xe8ba('0x44')](_0x51b655['body'],{})[_0xe8ba('0x20')](respondWithResult(_0x582857,0xc9))[_0xe8ba('0x3e')](handleError(_0x582857,null));};exports[_0xe8ba('0x1f')]=function(_0x5b25d8,_0x1bc7fe){if(_0x5b25d8['body']['id']){delete _0x5b25d8['body']['id'];}return db[_0xe8ba('0x3c')][_0xe8ba('0x45')]({'where':{'id':_0x5b25d8[_0xe8ba('0x40')]['id']}})['then'](handleEntityNotFound(_0x1bc7fe,null))[_0xe8ba('0x20')](saveUpdates(_0x5b25d8['body'],null))[_0xe8ba('0x20')](respondWithResult(_0x1bc7fe,null))[_0xe8ba('0x3e')](handleError(_0x1bc7fe,null));};exports[_0xe8ba('0x21')]=function(_0x232b24,_0x4e7e8e){return db[_0xe8ba('0x3c')]['find']({'where':{'id':_0x232b24[_0xe8ba('0x40')]['id']}})[_0xe8ba('0x20')](handleEntityNotFound(_0x4e7e8e,null))[_0xe8ba('0x20')](removeEntity(_0x4e7e8e,null))[_0xe8ba('0x3e')](handleError(_0x4e7e8e,null));};exports[_0xe8ba('0x46')]=function(_0x25662c,_0x490f13,_0x9b5e54){return db[_0xe8ba('0x47')][_0xe8ba('0x45')]({'where':{'id':_0x25662c['params']['id']}})[_0xe8ba('0x20')](handleEntityNotFound(_0x490f13,null))[_0xe8ba('0x20')](function(_0x2a81f9){if(_0x2a81f9){return _0x2a81f9[_0xe8ba('0x46')](_0x25662c[_0xe8ba('0x48')][_0xe8ba('0x49')],_[_0xe8ba('0x4a')](_0x25662c['body'],[_0xe8ba('0x49'),'id'])||{});}})[_0xe8ba('0x20')](respondWithResult(_0x490f13,null))[_0xe8ba('0x3e')](handleError(_0x490f13,null));};exports[_0xe8ba('0x4b')]=function(_0x5cca71,_0x3026e2,_0x27e9e6){var _0x2a3ea9={};var _0x1a6fb2={};var _0x5cc73b;var _0x4d7f6d;return db[_0xe8ba('0x3c')][_0xe8ba('0x4c')]({'where':{'id':_0x5cca71['params']['id']}})[_0xe8ba('0x20')](handleEntityNotFound(_0x3026e2,null))[_0xe8ba('0x20')](function(_0x432a84){if(_0x432a84){_0x5cc73b=_0x432a84;_0x1a6fb2[_0xe8ba('0x2a')]=_[_0xe8ba('0x2c')](db[_0xe8ba('0x47')][_0xe8ba('0x41')]);_0x1a6fb2[_0xe8ba('0x2b')]=_[_0xe8ba('0x2c')](_0x5cca71[_0xe8ba('0x2b')]);_0x1a6fb2[_0xe8ba('0x2d')]=_[_0xe8ba('0x42')](_0x1a6fb2['model'],_0x1a6fb2[_0xe8ba('0x2b')]);_0x2a3ea9[_0xe8ba('0x2f')]=_[_0xe8ba('0x42')](_0x1a6fb2[_0xe8ba('0x2a')],qs[_0xe8ba('0x2e')](_0x5cca71[_0xe8ba('0x2b')][_0xe8ba('0x2e')]));_0x2a3ea9[_0xe8ba('0x2f')]=_0x2a3ea9[_0xe8ba('0x2f')][_0xe8ba('0x4d')]?_0x2a3ea9[_0xe8ba('0x2f')]:_0x1a6fb2[_0xe8ba('0x2a')];_0x2a3ea9['order']=qs[_0xe8ba('0x34')](_0x5cca71['query']['sort']);_0x2a3ea9[_0xe8ba('0x35')]=qs['filters'](_[_0xe8ba('0x36')](_0x5cca71[_0xe8ba('0x2b')],_0x1a6fb2[_0xe8ba('0x2d')]));if(_0x5cca71[_0xe8ba('0x2b')][_0xe8ba('0x37')]){_0x2a3ea9[_0xe8ba('0x35')]=_[_0xe8ba('0x38')](_0x2a3ea9[_0xe8ba('0x35')],{'$or':_[_0xe8ba('0x27')](_0x2a3ea9[_0xe8ba('0x2f')],function(_0x27f98f){var _0x431e23={};_0x431e23[_0x27f98f]={'$like':'%'+_0x5cca71[_0xe8ba('0x2b')][_0xe8ba('0x37')]+'%'};return _0x431e23;})});}_0x2a3ea9=_['merge']({},_0x2a3ea9,_0x5cca71[_0xe8ba('0x3a')]);return _0x5cc73b[_0xe8ba('0x4b')](_0x2a3ea9);}})[_0xe8ba('0x20')](function(_0x376244){if(_0x376244){_0x4d7f6d=_0x376244[_0xe8ba('0x4d')];if(!_0x5cca71['query']['hasOwnProperty'](_0xe8ba('0x31'))){_0x2a3ea9['limit']=qs[_0xe8ba('0x32')](_0x5cca71['query'][_0xe8ba('0x32')]);_0x2a3ea9[_0xe8ba('0x33')]=qs[_0xe8ba('0x33')](_0x5cca71['query'][_0xe8ba('0x33')]);}return _0x5cc73b[_0xe8ba('0x4b')](_0x2a3ea9);}})[_0xe8ba('0x20')](function(_0x57efda){if(_0x57efda){return _0x57efda?{'count':_0x4d7f6d,'rows':_0x57efda}:null;}})['then'](respondWithResult(_0x3026e2,null))[_0xe8ba('0x3e')](handleError(_0x3026e2,null));};exports['addActions']=function(_0x51c230,_0xf13b89,_0xe8223f){return db['Action'][_0xe8ba('0x45')]({'where':{'id':_0x51c230[_0xe8ba('0x40')]['id']}})[_0xe8ba('0x20')](handleEntityNotFound(_0xf13b89,null))['then'](function(_0x4cc143){if(_0x4cc143){return _0x4cc143[_0xe8ba('0x4e')](_0x51c230[_0xe8ba('0x48')][_0xe8ba('0x49')],_[_0xe8ba('0x4a')](_0x51c230[_0xe8ba('0x48')],['ids','id'])||{});}})['then'](respondWithResult(_0xf13b89,null))[_0xe8ba('0x3e')](handleError(_0xf13b89,null));};exports['getActions']=function(_0x4ff480,_0x4427b7,_0x17af50){var _0x288b06={};var _0x41bb6e={};var _0x4e5c5a;var _0x2fd54b;return db[_0xe8ba('0x3c')][_0xe8ba('0x4c')]({'where':{'id':_0x4ff480[_0xe8ba('0x40')]['id']}})[_0xe8ba('0x20')](handleEntityNotFound(_0x4427b7,null))[_0xe8ba('0x20')](function(_0x354c96){if(_0x354c96){_0x4e5c5a=_0x354c96;_0x41bb6e['model']=_['keys'](db[_0xe8ba('0x4f')]['rawAttributes']);_0x41bb6e['query']=_[_0xe8ba('0x2c')](_0x4ff480[_0xe8ba('0x2b')]);_0x41bb6e['filters']=_[_0xe8ba('0x42')](_0x41bb6e[_0xe8ba('0x2a')],_0x41bb6e['query']);_0x288b06[_0xe8ba('0x2f')]=_[_0xe8ba('0x42')](_0x41bb6e['model'],qs[_0xe8ba('0x2e')](_0x4ff480[_0xe8ba('0x2b')][_0xe8ba('0x2e')]));_0x288b06['attributes']=_0x288b06['attributes'][_0xe8ba('0x4d')]?_0x288b06[_0xe8ba('0x2f')]:_0x41bb6e[_0xe8ba('0x2a')];_0x288b06[_0xe8ba('0x50')]=qs[_0xe8ba('0x34')](_0x4ff480[_0xe8ba('0x2b')][_0xe8ba('0x34')]);_0x288b06[_0xe8ba('0x35')]=qs[_0xe8ba('0x2d')](_['pick'](_0x4ff480[_0xe8ba('0x2b')],_0x41bb6e['filters']));if(_0x4ff480[_0xe8ba('0x2b')]['filter']){_0x288b06[_0xe8ba('0x35')]=_[_0xe8ba('0x38')](_0x288b06['where'],{'$or':_[_0xe8ba('0x27')](_0x288b06[_0xe8ba('0x2f')],function(_0x1835d8){var _0x3970ba={};_0x3970ba[_0x1835d8]={'$like':'%'+_0x4ff480[_0xe8ba('0x2b')][_0xe8ba('0x37')]+'%'};return _0x3970ba;})});}_0x288b06=_['merge']({},_0x288b06,_0x4ff480[_0xe8ba('0x3a')]);return _0x4e5c5a[_0xe8ba('0x51')](_0x288b06);}})[_0xe8ba('0x20')](function(_0x1e6ab2){if(_0x1e6ab2){_0x2fd54b=_0x1e6ab2[_0xe8ba('0x4d')];if(!_0x4ff480[_0xe8ba('0x2b')]['hasOwnProperty']('nolimit')){_0x288b06[_0xe8ba('0x32')]=qs[_0xe8ba('0x32')](_0x4ff480[_0xe8ba('0x2b')][_0xe8ba('0x32')]);_0x288b06[_0xe8ba('0x33')]=qs['offset'](_0x4ff480[_0xe8ba('0x2b')][_0xe8ba('0x33')]);}return _0x4e5c5a[_0xe8ba('0x51')](_0x288b06);}})[_0xe8ba('0x20')](function(_0x3cdbfd){if(_0x3cdbfd){return _0x3cdbfd?{'count':_0x2fd54b,'rows':_0x3cdbfd}:null;}})[_0xe8ba('0x20')](respondWithResult(_0x4427b7,null))[_0xe8ba('0x3e')](handleError(_0x4427b7,null));};