Built motion from commit 3c2ce842.|2.6.31
[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 _0x50ff=['send','index','map','Automation','rawAttributes','fieldName','type','keys','query','filters','attributes','intersection','model','fields','length','hasOwnProperty','nolimit','order','sort','where','filter','VIRTUAL','merge','include','rows','catch','show','params','includeAll','find','create','body','addConditions','Condition','ids','omit','getConditions','findOne','pick','options','addActions','Action','getActions','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','path','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','limit','undefined','count','Content-Range','apply','reject','save','update','then','destroy','error','stack','name'];(function(_0x2b4724,_0x4b5a31){var _0x59e45d=function(_0x39d57a){while(--_0x39d57a){_0x2b4724['push'](_0x2b4724['shift']());}};_0x59e45d(++_0x4b5a31);}(_0x50ff,0x12d));var _0xf50f=function(_0x4dff13,_0x4b370c){_0x4dff13=_0x4dff13-0x0;var _0xebeadb=_0x50ff[_0x4dff13];return _0xebeadb;};'use strict';var emlformat=require(_0xf50f('0x0'));var rimraf=require(_0xf50f('0x1'));var zipdir=require(_0xf50f('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf50f('0x3'));var moment=require(_0xf50f('0x4'));var BPromise=require(_0xf50f('0x5'));var Mustache=require('mustache');var util=require('util');var path=require(_0xf50f('0x6'));var sox=require('sox');var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xf50f('0x7'));var _=require(_0xf50f('0x8'));var squel=require(_0xf50f('0x9'));var crypto=require(_0xf50f('0xa'));var jsforce=require(_0xf50f('0xb'));var deskjs=require(_0xf50f('0xc'));var toCsv=require(_0xf50f('0xd'));var querystring=require(_0xf50f('0xe'));var Papa=require('papaparse');var Redis=require(_0xf50f('0xf'));var authService=require(_0xf50f('0x10'));var qs=require(_0xf50f('0x11'));var as=require(_0xf50f('0x12'));var hardwareService=require(_0xf50f('0x13'));var logger=require(_0xf50f('0x14'))('api');var utils=require(_0xf50f('0x15'));var config=require(_0xf50f('0x16'));var licenseUtil=require(_0xf50f('0x17'));var db=require(_0xf50f('0x18'))['db'];function respondWithStatusCode(_0x57e86a,_0x5737d7){_0x5737d7=_0x5737d7||0xcc;return function(_0xfa07f2){if(_0xfa07f2){return _0x57e86a[_0xf50f('0x19')](_0x5737d7);}return _0x57e86a[_0xf50f('0x1a')](_0x5737d7)[_0xf50f('0x1b')]();};}function respondWithResult(_0x46a110,_0x34bf23){_0x34bf23=_0x34bf23||0xc8;return function(_0x2d2c06){if(_0x2d2c06){return _0x46a110[_0xf50f('0x1a')](_0x34bf23)[_0xf50f('0x1c')](_0x2d2c06);}};}function respondWithFilteredResult(_0x48471d,_0x365a65){return function(_0x2eb671){if(_0x2eb671){var _0xa95f00=typeof _0x365a65[_0xf50f('0x1d')]==='undefined'&&typeof _0x365a65[_0xf50f('0x1e')]===_0xf50f('0x1f');var _0x355e25=_0x2eb671['count'];var _0x566ed7=_0xa95f00?0x0:_0x365a65[_0xf50f('0x1d')];var _0x5e64c9=_0xa95f00?_0x2eb671[_0xf50f('0x20')]:_0x365a65[_0xf50f('0x1d')]+_0x365a65[_0xf50f('0x1e')];var _0x4fd987;if(_0x5e64c9>=_0x355e25){_0x5e64c9=_0x355e25;_0x4fd987=0xc8;}else{_0x4fd987=0xce;}_0x48471d[_0xf50f('0x1a')](_0x4fd987);return _0x48471d['set'](_0xf50f('0x21'),_0x566ed7+'-'+_0x5e64c9+'/'+_0x355e25)[_0xf50f('0x1c')](_0x2eb671);}return null;};}function patchUpdates(_0x578f19){return function(_0x25b1f7){try{jsonpatch[_0xf50f('0x22')](_0x25b1f7,_0x578f19,!![]);}catch(_0x5dd501){return BPromise[_0xf50f('0x23')](_0x5dd501);}return _0x25b1f7[_0xf50f('0x24')]();};}function saveUpdates(_0x4c7788,_0x432f93){return function(_0x343478){if(_0x343478){return _0x343478[_0xf50f('0x25')](_0x4c7788)[_0xf50f('0x26')](function(_0x5e3945){return _0x5e3945;});}return null;};}function removeEntity(_0x3d2e9b,_0x174d2b){return function(_0x233199){if(_0x233199){return _0x233199[_0xf50f('0x27')]()['then'](function(){_0x3d2e9b['status'](0xcc)[_0xf50f('0x1b')]();});}};}function handleEntityNotFound(_0x16ca13,_0x4f6181){return function(_0x497404){if(!_0x497404){_0x16ca13['sendStatus'](0x194);}return _0x497404;};}function handleError(_0x1135c3,_0xdee982){_0xdee982=_0xdee982||0x1f4;return function(_0x36c1c1){logger[_0xf50f('0x28')](_0x36c1c1[_0xf50f('0x29')]);if(_0x36c1c1[_0xf50f('0x2a')]){delete _0x36c1c1[_0xf50f('0x2a')];}_0x1135c3[_0xf50f('0x1a')](_0xdee982)[_0xf50f('0x2b')](_0x36c1c1);};}exports[_0xf50f('0x2c')]=function(_0x3378a6,_0x16c2cb){var _0x1409bb={},_0x2dca9c={},_0x2a87ce={'count':0x0,'rows':[]};var _0x356e61=_[_0xf50f('0x2d')](db[_0xf50f('0x2e')][_0xf50f('0x2f')],function(_0x3a7a14){return{'name':_0x3a7a14[_0xf50f('0x30')],'type':_0x3a7a14[_0xf50f('0x31')]['key']};});_0x2dca9c['model']=_['map'](_0x356e61,'name');_0x2dca9c['query']=_[_0xf50f('0x32')](_0x3378a6[_0xf50f('0x33')]);_0x2dca9c[_0xf50f('0x34')]=_['intersection'](_0x2dca9c['model'],_0x2dca9c[_0xf50f('0x33')]);_0x1409bb[_0xf50f('0x35')]=_[_0xf50f('0x36')](_0x2dca9c[_0xf50f('0x37')],qs[_0xf50f('0x38')](_0x3378a6['query'][_0xf50f('0x38')]));_0x1409bb[_0xf50f('0x35')]=_0x1409bb[_0xf50f('0x35')][_0xf50f('0x39')]?_0x1409bb['attributes']:_0x2dca9c['model'];if(!_0x3378a6[_0xf50f('0x33')][_0xf50f('0x3a')](_0xf50f('0x3b'))){_0x1409bb['limit']=qs[_0xf50f('0x1e')](_0x3378a6[_0xf50f('0x33')][_0xf50f('0x1e')]);_0x1409bb[_0xf50f('0x1d')]=qs['offset'](_0x3378a6['query'][_0xf50f('0x1d')]);}_0x1409bb[_0xf50f('0x3c')]=qs[_0xf50f('0x3d')](_0x3378a6['query'][_0xf50f('0x3d')]);_0x1409bb[_0xf50f('0x3e')]=qs[_0xf50f('0x34')](_['pick'](_0x3378a6[_0xf50f('0x33')],_0x2dca9c[_0xf50f('0x34')]),_0x356e61);if(_0x3378a6[_0xf50f('0x33')][_0xf50f('0x3f')]){_0x1409bb[_0xf50f('0x3e')]=_['merge'](_0x1409bb[_0xf50f('0x3e')],{'$or':_[_0xf50f('0x2d')](_0x356e61,function(_0x10e1ea){if(_0x10e1ea['type']!==_0xf50f('0x40')){var _0x1a8392={};_0x1a8392[_0x10e1ea[_0xf50f('0x2a')]]={'$like':'%'+_0x3378a6[_0xf50f('0x33')]['filter']+'%'};return _0x1a8392;}})});}_0x1409bb=_[_0xf50f('0x41')]({},_0x1409bb,_0x3378a6['options']);var _0x2c7e17={'where':_0x1409bb['where']};return db[_0xf50f('0x2e')][_0xf50f('0x20')](_0x2c7e17)['then'](function(_0x27573f){_0x2a87ce[_0xf50f('0x20')]=_0x27573f;if(_0x3378a6['query']['includeAll']){_0x1409bb[_0xf50f('0x42')]=[{'all':!![]}];}return db[_0xf50f('0x2e')]['findAll'](_0x1409bb);})['then'](function(_0x2240ac){_0x2a87ce[_0xf50f('0x43')]=_0x2240ac;return _0x2a87ce;})[_0xf50f('0x26')](respondWithFilteredResult(_0x16c2cb,_0x1409bb))[_0xf50f('0x44')](handleError(_0x16c2cb,null));};exports[_0xf50f('0x45')]=function(_0x3e67c,_0x358eac){var _0xa4e46a={'raw':![],'where':{'id':_0x3e67c[_0xf50f('0x46')]['id']}},_0x319bca={};_0x319bca['model']=_[_0xf50f('0x32')](db[_0xf50f('0x2e')]['rawAttributes']);_0x319bca[_0xf50f('0x33')]=_[_0xf50f('0x32')](_0x3e67c[_0xf50f('0x33')]);_0x319bca[_0xf50f('0x34')]=_[_0xf50f('0x36')](_0x319bca[_0xf50f('0x37')],_0x319bca[_0xf50f('0x33')]);_0xa4e46a[_0xf50f('0x35')]=_[_0xf50f('0x36')](_0x319bca[_0xf50f('0x37')],qs[_0xf50f('0x38')](_0x3e67c[_0xf50f('0x33')][_0xf50f('0x38')]));_0xa4e46a['attributes']=_0xa4e46a['attributes'][_0xf50f('0x39')]?_0xa4e46a[_0xf50f('0x35')]:_0x319bca['model'];if(_0x3e67c[_0xf50f('0x33')][_0xf50f('0x47')]){_0xa4e46a[_0xf50f('0x42')]=[{'all':!![]}];}_0xa4e46a=_[_0xf50f('0x41')]({},_0xa4e46a,_0x3e67c['options']);return db[_0xf50f('0x2e')][_0xf50f('0x48')](_0xa4e46a)['then'](handleEntityNotFound(_0x358eac,null))[_0xf50f('0x26')](respondWithResult(_0x358eac,null))['catch'](handleError(_0x358eac,null));};exports[_0xf50f('0x49')]=function(_0x165553,_0x1a9f9b){return db[_0xf50f('0x2e')][_0xf50f('0x49')](_0x165553[_0xf50f('0x4a')],{})[_0xf50f('0x26')](respondWithResult(_0x1a9f9b,0xc9))['catch'](handleError(_0x1a9f9b,null));};exports[_0xf50f('0x25')]=function(_0x4fcf31,_0x108d61){if(_0x4fcf31[_0xf50f('0x4a')]['id']){delete _0x4fcf31['body']['id'];}return db[_0xf50f('0x2e')]['find']({'where':{'id':_0x4fcf31[_0xf50f('0x46')]['id']}})[_0xf50f('0x26')](handleEntityNotFound(_0x108d61,null))['then'](saveUpdates(_0x4fcf31[_0xf50f('0x4a')],null))[_0xf50f('0x26')](respondWithResult(_0x108d61,null))['catch'](handleError(_0x108d61,null));};exports[_0xf50f('0x27')]=function(_0x5bc1e3,_0x47e048){return db['Automation'][_0xf50f('0x48')]({'where':{'id':_0x5bc1e3['params']['id']}})['then'](handleEntityNotFound(_0x47e048,null))[_0xf50f('0x26')](removeEntity(_0x47e048,null))[_0xf50f('0x44')](handleError(_0x47e048,null));};exports[_0xf50f('0x4b')]=function(_0x28c61b,_0x27ae2b,_0xc4172d){return db[_0xf50f('0x4c')][_0xf50f('0x48')]({'where':{'id':_0x28c61b[_0xf50f('0x46')]['id']}})[_0xf50f('0x26')](handleEntityNotFound(_0x27ae2b,null))[_0xf50f('0x26')](function(_0x5bbe4f){if(_0x5bbe4f){return _0x5bbe4f[_0xf50f('0x4b')](_0x28c61b[_0xf50f('0x4a')][_0xf50f('0x4d')],_[_0xf50f('0x4e')](_0x28c61b[_0xf50f('0x4a')],[_0xf50f('0x4d'),'id'])||{});}})[_0xf50f('0x26')](respondWithResult(_0x27ae2b,null))[_0xf50f('0x44')](handleError(_0x27ae2b,null));};exports[_0xf50f('0x4f')]=function(_0x14a5e9,_0x12e8ec,_0xba727e){var _0x2891cc={};var _0x217748={};var _0x108132;var _0x5280d4;return db['Automation'][_0xf50f('0x50')]({'where':{'id':_0x14a5e9[_0xf50f('0x46')]['id']}})[_0xf50f('0x26')](handleEntityNotFound(_0x12e8ec,null))[_0xf50f('0x26')](function(_0x32911f){if(_0x32911f){_0x108132=_0x32911f;_0x217748[_0xf50f('0x37')]=_[_0xf50f('0x32')](db[_0xf50f('0x4c')][_0xf50f('0x2f')]);_0x217748[_0xf50f('0x33')]=_[_0xf50f('0x32')](_0x14a5e9[_0xf50f('0x33')]);_0x217748[_0xf50f('0x34')]=_[_0xf50f('0x36')](_0x217748[_0xf50f('0x37')],_0x217748[_0xf50f('0x33')]);_0x2891cc[_0xf50f('0x35')]=_[_0xf50f('0x36')](_0x217748[_0xf50f('0x37')],qs[_0xf50f('0x38')](_0x14a5e9['query'][_0xf50f('0x38')]));_0x2891cc['attributes']=_0x2891cc[_0xf50f('0x35')]['length']?_0x2891cc[_0xf50f('0x35')]:_0x217748['model'];_0x2891cc[_0xf50f('0x3c')]=qs[_0xf50f('0x3d')](_0x14a5e9[_0xf50f('0x33')][_0xf50f('0x3d')]);_0x2891cc[_0xf50f('0x3e')]=qs[_0xf50f('0x34')](_[_0xf50f('0x51')](_0x14a5e9[_0xf50f('0x33')],_0x217748[_0xf50f('0x34')]));if(_0x14a5e9[_0xf50f('0x33')][_0xf50f('0x3f')]){_0x2891cc[_0xf50f('0x3e')]=_['merge'](_0x2891cc[_0xf50f('0x3e')],{'$or':_[_0xf50f('0x2d')](_0x2891cc[_0xf50f('0x35')],function(_0x250c46){var _0x45827d={};_0x45827d[_0x250c46]={'$like':'%'+_0x14a5e9[_0xf50f('0x33')][_0xf50f('0x3f')]+'%'};return _0x45827d;})});}_0x2891cc=_[_0xf50f('0x41')]({},_0x2891cc,_0x14a5e9[_0xf50f('0x52')]);return _0x108132[_0xf50f('0x4f')](_0x2891cc);}})[_0xf50f('0x26')](function(_0x2007d5){if(_0x2007d5){_0x5280d4=_0x2007d5['length'];if(!_0x14a5e9[_0xf50f('0x33')][_0xf50f('0x3a')]('nolimit')){_0x2891cc[_0xf50f('0x1e')]=qs[_0xf50f('0x1e')](_0x14a5e9[_0xf50f('0x33')]['limit']);_0x2891cc[_0xf50f('0x1d')]=qs[_0xf50f('0x1d')](_0x14a5e9[_0xf50f('0x33')]['offset']);}return _0x108132[_0xf50f('0x4f')](_0x2891cc);}})[_0xf50f('0x26')](function(_0x35b5b1){if(_0x35b5b1){return _0x35b5b1?{'count':_0x5280d4,'rows':_0x35b5b1}:null;}})['then'](respondWithResult(_0x12e8ec,null))[_0xf50f('0x44')](handleError(_0x12e8ec,null));};exports[_0xf50f('0x53')]=function(_0x4a55b2,_0x5c30d0,_0x173e54){return db[_0xf50f('0x54')][_0xf50f('0x48')]({'where':{'id':_0x4a55b2[_0xf50f('0x46')]['id']}})[_0xf50f('0x26')](handleEntityNotFound(_0x5c30d0,null))['then'](function(_0x1ab78e){if(_0x1ab78e){return _0x1ab78e[_0xf50f('0x53')](_0x4a55b2['body']['ids'],_[_0xf50f('0x4e')](_0x4a55b2['body'],[_0xf50f('0x4d'),'id'])||{});}})[_0xf50f('0x26')](respondWithResult(_0x5c30d0,null))[_0xf50f('0x44')](handleError(_0x5c30d0,null));};exports['getActions']=function(_0x239355,_0x11dd53,_0x17c8f8){var _0x51e58e={};var _0x50e330={};var _0xda6c78;var _0x2d5d31;return db[_0xf50f('0x2e')][_0xf50f('0x50')]({'where':{'id':_0x239355[_0xf50f('0x46')]['id']}})[_0xf50f('0x26')](handleEntityNotFound(_0x11dd53,null))[_0xf50f('0x26')](function(_0x465a54){if(_0x465a54){_0xda6c78=_0x465a54;_0x50e330[_0xf50f('0x37')]=_['keys'](db[_0xf50f('0x54')]['rawAttributes']);_0x50e330['query']=_['keys'](_0x239355[_0xf50f('0x33')]);_0x50e330['filters']=_[_0xf50f('0x36')](_0x50e330[_0xf50f('0x37')],_0x50e330[_0xf50f('0x33')]);_0x51e58e['attributes']=_[_0xf50f('0x36')](_0x50e330[_0xf50f('0x37')],qs['fields'](_0x239355[_0xf50f('0x33')][_0xf50f('0x38')]));_0x51e58e[_0xf50f('0x35')]=_0x51e58e[_0xf50f('0x35')][_0xf50f('0x39')]?_0x51e58e[_0xf50f('0x35')]:_0x50e330[_0xf50f('0x37')];_0x51e58e[_0xf50f('0x3c')]=qs[_0xf50f('0x3d')](_0x239355[_0xf50f('0x33')]['sort']);_0x51e58e[_0xf50f('0x3e')]=qs[_0xf50f('0x34')](_[_0xf50f('0x51')](_0x239355['query'],_0x50e330[_0xf50f('0x34')]));if(_0x239355[_0xf50f('0x33')][_0xf50f('0x3f')]){_0x51e58e['where']=_[_0xf50f('0x41')](_0x51e58e[_0xf50f('0x3e')],{'$or':_[_0xf50f('0x2d')](_0x51e58e[_0xf50f('0x35')],function(_0x3cddec){var _0x4710b7={};_0x4710b7[_0x3cddec]={'$like':'%'+_0x239355[_0xf50f('0x33')][_0xf50f('0x3f')]+'%'};return _0x4710b7;})});}_0x51e58e=_[_0xf50f('0x41')]({},_0x51e58e,_0x239355[_0xf50f('0x52')]);return _0xda6c78[_0xf50f('0x55')](_0x51e58e);}})[_0xf50f('0x26')](function(_0x11c315){if(_0x11c315){_0x2d5d31=_0x11c315[_0xf50f('0x39')];if(!_0x239355['query'][_0xf50f('0x3a')](_0xf50f('0x3b'))){_0x51e58e['limit']=qs[_0xf50f('0x1e')](_0x239355[_0xf50f('0x33')][_0xf50f('0x1e')]);_0x51e58e['offset']=qs['offset'](_0x239355[_0xf50f('0x33')][_0xf50f('0x1d')]);}return _0xda6c78[_0xf50f('0x55')](_0x51e58e);}})[_0xf50f('0x26')](function(_0x524b8b){if(_0x524b8b){return _0x524b8b?{'count':_0x2d5d31,'rows':_0x524b8b}:null;}})[_0xf50f('0x26')](respondWithResult(_0x11dd53,null))[_0xf50f('0x44')](handleError(_0x11dd53,null));};