e324b8c77af665d804b0220baabe065037365e25
[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 _0x20f7=['offset','undefined','count','limit','set','Content-Range','json','apply','reject','save','update','destroy','then','stack','name','send','index','map','Automation','rawAttributes','fieldName','type','key','query','filters','intersection','model','fields','length','attributes','hasOwnProperty','nolimit','order','sort','where','pick','merge','VIRTUAL','filter','options','includeAll','include','findAll','rows','catch','keys','create','body','params','find','Condition','addConditions','ids','omit','getConditions','findOne','addActions','Action','getActions','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','crypto','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status'];(function(_0x8e7bd2,_0x3870d7){var _0xa4e60f=function(_0x151499){while(--_0x151499){_0x8e7bd2['push'](_0x8e7bd2['shift']());}};_0xa4e60f(++_0x3870d7);}(_0x20f7,0xef));var _0x720f=function(_0x489b4d,_0x4ddcbe){_0x489b4d=_0x489b4d-0x0;var _0x17f1ba=_0x20f7[_0x489b4d];return _0x17f1ba;};'use strict';var emlformat=require(_0x720f('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x720f('0x1'));var jsonpatch=require(_0x720f('0x2'));var rp=require(_0x720f('0x3'));var moment=require(_0x720f('0x4'));var BPromise=require(_0x720f('0x5'));var Mustache=require(_0x720f('0x6'));var util=require(_0x720f('0x7'));var path=require(_0x720f('0x8'));var sox=require(_0x720f('0x9'));var csv=require('to-csv');var ejs=require(_0x720f('0xa'));var fs=require('fs');var fs_extra=require(_0x720f('0xb'));var _=require(_0x720f('0xc'));var squel=require(_0x720f('0xd'));var crypto=require(_0x720f('0xe'));var jsforce=require('jsforce');var deskjs=require(_0x720f('0xf'));var toCsv=require(_0x720f('0x10'));var querystring=require(_0x720f('0x11'));var Papa=require(_0x720f('0x12'));var Redis=require(_0x720f('0x13'));var authService=require(_0x720f('0x14'));var qs=require(_0x720f('0x15'));var as=require(_0x720f('0x16'));var hardwareService=require(_0x720f('0x17'));var logger=require(_0x720f('0x18'))(_0x720f('0x19'));var utils=require('../../config/utils');var config=require(_0x720f('0x1a'));var licenseUtil=require(_0x720f('0x1b'));var db=require(_0x720f('0x1c'))['db'];function respondWithStatusCode(_0x431d2b,_0x1770e6){_0x1770e6=_0x1770e6||0xcc;return function(_0x24192f){if(_0x24192f){return _0x431d2b[_0x720f('0x1d')](_0x1770e6);}return _0x431d2b[_0x720f('0x1e')](_0x1770e6)['end']();};}function respondWithResult(_0x574824,_0x410e86){_0x410e86=_0x410e86||0xc8;return function(_0xe90c13){if(_0xe90c13){return _0x574824['status'](_0x410e86)['json'](_0xe90c13);}};}function respondWithFilteredResult(_0x1e2907,_0x19c34c){return function(_0xfe470){if(_0xfe470){var _0x490e68=typeof _0x19c34c[_0x720f('0x1f')]===_0x720f('0x20')&&typeof _0x19c34c['limit']===_0x720f('0x20');var _0x31f3c6=_0xfe470[_0x720f('0x21')];var _0x1818bb=_0x490e68?0x0:_0x19c34c[_0x720f('0x1f')];var _0x7c897d=_0x490e68?_0xfe470['count']:_0x19c34c[_0x720f('0x1f')]+_0x19c34c[_0x720f('0x22')];var _0x85440e;if(_0x7c897d>=_0x31f3c6){_0x7c897d=_0x31f3c6;_0x85440e=0xc8;}else{_0x85440e=0xce;}_0x1e2907[_0x720f('0x1e')](_0x85440e);return _0x1e2907[_0x720f('0x23')](_0x720f('0x24'),_0x1818bb+'-'+_0x7c897d+'/'+_0x31f3c6)[_0x720f('0x25')](_0xfe470);}return null;};}function patchUpdates(_0x320246){return function(_0x34aeb5){try{jsonpatch[_0x720f('0x26')](_0x34aeb5,_0x320246,!![]);}catch(_0x1eef02){return BPromise[_0x720f('0x27')](_0x1eef02);}return _0x34aeb5[_0x720f('0x28')]();};}function saveUpdates(_0x514cec,_0x3ec8da){return function(_0x5a80ad){if(_0x5a80ad){return _0x5a80ad[_0x720f('0x29')](_0x514cec)['then'](function(_0x1c3d10){return _0x1c3d10;});}return null;};}function removeEntity(_0x5a8006,_0x57c461){return function(_0x32f6cf){if(_0x32f6cf){return _0x32f6cf[_0x720f('0x2a')]()[_0x720f('0x2b')](function(){_0x5a8006[_0x720f('0x1e')](0xcc)['end']();});}};}function handleEntityNotFound(_0x48db4e,_0x228611){return function(_0xa4d630){if(!_0xa4d630){_0x48db4e['sendStatus'](0x194);}return _0xa4d630;};}function handleError(_0x1c615d,_0x33ddfd){_0x33ddfd=_0x33ddfd||0x1f4;return function(_0x44d64e){logger['error'](_0x44d64e[_0x720f('0x2c')]);if(_0x44d64e['name']){delete _0x44d64e[_0x720f('0x2d')];}_0x1c615d['status'](_0x33ddfd)[_0x720f('0x2e')](_0x44d64e);};}exports[_0x720f('0x2f')]=function(_0x763793,_0x4ae0d3){var _0x37cba7={},_0xb05d93={},_0x3c371d={'count':0x0,'rows':[]};var _0x232e39=_[_0x720f('0x30')](db[_0x720f('0x31')][_0x720f('0x32')],function(_0x274fed){return{'name':_0x274fed[_0x720f('0x33')],'type':_0x274fed[_0x720f('0x34')][_0x720f('0x35')]};});_0xb05d93['model']=_[_0x720f('0x30')](_0x232e39,'name');_0xb05d93[_0x720f('0x36')]=_['keys'](_0x763793[_0x720f('0x36')]);_0xb05d93[_0x720f('0x37')]=_[_0x720f('0x38')](_0xb05d93[_0x720f('0x39')],_0xb05d93[_0x720f('0x36')]);_0x37cba7['attributes']=_[_0x720f('0x38')](_0xb05d93[_0x720f('0x39')],qs['fields'](_0x763793[_0x720f('0x36')][_0x720f('0x3a')]));_0x37cba7['attributes']=_0x37cba7['attributes'][_0x720f('0x3b')]?_0x37cba7[_0x720f('0x3c')]:_0xb05d93[_0x720f('0x39')];if(!_0x763793[_0x720f('0x36')][_0x720f('0x3d')](_0x720f('0x3e'))){_0x37cba7[_0x720f('0x22')]=qs[_0x720f('0x22')](_0x763793[_0x720f('0x36')]['limit']);_0x37cba7[_0x720f('0x1f')]=qs[_0x720f('0x1f')](_0x763793[_0x720f('0x36')][_0x720f('0x1f')]);}_0x37cba7[_0x720f('0x3f')]=qs['sort'](_0x763793['query'][_0x720f('0x40')]);_0x37cba7[_0x720f('0x41')]=qs[_0x720f('0x37')](_[_0x720f('0x42')](_0x763793['query'],_0xb05d93[_0x720f('0x37')]),_0x232e39);if(_0x763793[_0x720f('0x36')]['filter']){_0x37cba7['where']=_[_0x720f('0x43')](_0x37cba7[_0x720f('0x41')],{'$or':_[_0x720f('0x30')](_0x232e39,function(_0x42b154){if(_0x42b154['type']!==_0x720f('0x44')){var _0x3c6629={};_0x3c6629[_0x42b154[_0x720f('0x2d')]]={'$like':'%'+_0x763793[_0x720f('0x36')][_0x720f('0x45')]+'%'};return _0x3c6629;}})});}_0x37cba7=_[_0x720f('0x43')]({},_0x37cba7,_0x763793[_0x720f('0x46')]);var _0x4e7d72={'where':_0x37cba7[_0x720f('0x41')]};return db['Automation'][_0x720f('0x21')](_0x4e7d72)[_0x720f('0x2b')](function(_0x123b8a){_0x3c371d['count']=_0x123b8a;if(_0x763793['query'][_0x720f('0x47')]){_0x37cba7[_0x720f('0x48')]=[{'all':!![]}];}return db[_0x720f('0x31')][_0x720f('0x49')](_0x37cba7);})[_0x720f('0x2b')](function(_0x1c265e){_0x3c371d[_0x720f('0x4a')]=_0x1c265e;return _0x3c371d;})['then'](respondWithFilteredResult(_0x4ae0d3,_0x37cba7))[_0x720f('0x4b')](handleError(_0x4ae0d3,null));};exports['show']=function(_0x10d6ff,_0x39fb25){var _0x510a60={'raw':![],'where':{'id':_0x10d6ff['params']['id']}},_0x5190c6={};_0x5190c6[_0x720f('0x39')]=_['keys'](db[_0x720f('0x31')]['rawAttributes']);_0x5190c6[_0x720f('0x36')]=_[_0x720f('0x4c')](_0x10d6ff[_0x720f('0x36')]);_0x5190c6[_0x720f('0x37')]=_[_0x720f('0x38')](_0x5190c6[_0x720f('0x39')],_0x5190c6['query']);_0x510a60[_0x720f('0x3c')]=_[_0x720f('0x38')](_0x5190c6[_0x720f('0x39')],qs['fields'](_0x10d6ff[_0x720f('0x36')]['fields']));_0x510a60[_0x720f('0x3c')]=_0x510a60[_0x720f('0x3c')][_0x720f('0x3b')]?_0x510a60[_0x720f('0x3c')]:_0x5190c6[_0x720f('0x39')];if(_0x10d6ff[_0x720f('0x36')][_0x720f('0x47')]){_0x510a60[_0x720f('0x48')]=[{'all':!![]}];}_0x510a60=_[_0x720f('0x43')]({},_0x510a60,_0x10d6ff[_0x720f('0x46')]);return db[_0x720f('0x31')]['find'](_0x510a60)[_0x720f('0x2b')](handleEntityNotFound(_0x39fb25,null))[_0x720f('0x2b')](respondWithResult(_0x39fb25,null))[_0x720f('0x4b')](handleError(_0x39fb25,null));};exports[_0x720f('0x4d')]=function(_0x23dca5,_0x63a649){return db['Automation'][_0x720f('0x4d')](_0x23dca5[_0x720f('0x4e')],{})[_0x720f('0x2b')](respondWithResult(_0x63a649,0xc9))[_0x720f('0x4b')](handleError(_0x63a649,null));};exports[_0x720f('0x29')]=function(_0x486416,_0x1a6293){if(_0x486416[_0x720f('0x4e')]['id']){delete _0x486416['body']['id'];}return db['Automation']['find']({'where':{'id':_0x486416[_0x720f('0x4f')]['id']}})[_0x720f('0x2b')](handleEntityNotFound(_0x1a6293,null))[_0x720f('0x2b')](saveUpdates(_0x486416['body'],null))[_0x720f('0x2b')](respondWithResult(_0x1a6293,null))[_0x720f('0x4b')](handleError(_0x1a6293,null));};exports['destroy']=function(_0x365c98,_0x151651){return db[_0x720f('0x31')][_0x720f('0x50')]({'where':{'id':_0x365c98['params']['id']}})['then'](handleEntityNotFound(_0x151651,null))[_0x720f('0x2b')](removeEntity(_0x151651,null))['catch'](handleError(_0x151651,null));};exports['addConditions']=function(_0x254183,_0x503103,_0xcdd107){return db[_0x720f('0x51')][_0x720f('0x50')]({'where':{'id':_0x254183[_0x720f('0x4f')]['id']}})[_0x720f('0x2b')](handleEntityNotFound(_0x503103,null))[_0x720f('0x2b')](function(_0x4e56b3){if(_0x4e56b3){return _0x4e56b3[_0x720f('0x52')](_0x254183[_0x720f('0x4e')][_0x720f('0x53')],_[_0x720f('0x54')](_0x254183['body'],['ids','id'])||{});}})[_0x720f('0x2b')](respondWithResult(_0x503103,null))[_0x720f('0x4b')](handleError(_0x503103,null));};exports[_0x720f('0x55')]=function(_0xf61a6c,_0x3667ba,_0x123da6){var _0x11dc35={};var _0x2717f2={};var _0x38a0fc;var _0x199210;return db['Automation'][_0x720f('0x56')]({'where':{'id':_0xf61a6c[_0x720f('0x4f')]['id']}})[_0x720f('0x2b')](handleEntityNotFound(_0x3667ba,null))[_0x720f('0x2b')](function(_0x435337){if(_0x435337){_0x38a0fc=_0x435337;_0x2717f2[_0x720f('0x39')]=_[_0x720f('0x4c')](db[_0x720f('0x51')][_0x720f('0x32')]);_0x2717f2[_0x720f('0x36')]=_[_0x720f('0x4c')](_0xf61a6c['query']);_0x2717f2['filters']=_['intersection'](_0x2717f2[_0x720f('0x39')],_0x2717f2[_0x720f('0x36')]);_0x11dc35[_0x720f('0x3c')]=_[_0x720f('0x38')](_0x2717f2[_0x720f('0x39')],qs[_0x720f('0x3a')](_0xf61a6c[_0x720f('0x36')][_0x720f('0x3a')]));_0x11dc35[_0x720f('0x3c')]=_0x11dc35[_0x720f('0x3c')]['length']?_0x11dc35[_0x720f('0x3c')]:_0x2717f2['model'];_0x11dc35[_0x720f('0x3f')]=qs[_0x720f('0x40')](_0xf61a6c['query'][_0x720f('0x40')]);_0x11dc35[_0x720f('0x41')]=qs[_0x720f('0x37')](_[_0x720f('0x42')](_0xf61a6c['query'],_0x2717f2['filters']));if(_0xf61a6c[_0x720f('0x36')][_0x720f('0x45')]){_0x11dc35[_0x720f('0x41')]=_[_0x720f('0x43')](_0x11dc35['where'],{'$or':_[_0x720f('0x30')](_0x11dc35[_0x720f('0x3c')],function(_0x2fa57b){var _0x5b03a6={};_0x5b03a6[_0x2fa57b]={'$like':'%'+_0xf61a6c['query'][_0x720f('0x45')]+'%'};return _0x5b03a6;})});}_0x11dc35=_[_0x720f('0x43')]({},_0x11dc35,_0xf61a6c[_0x720f('0x46')]);return _0x38a0fc[_0x720f('0x55')](_0x11dc35);}})['then'](function(_0x40cbd3){if(_0x40cbd3){_0x199210=_0x40cbd3[_0x720f('0x3b')];if(!_0xf61a6c['query']['hasOwnProperty'](_0x720f('0x3e'))){_0x11dc35[_0x720f('0x22')]=qs[_0x720f('0x22')](_0xf61a6c[_0x720f('0x36')][_0x720f('0x22')]);_0x11dc35['offset']=qs['offset'](_0xf61a6c[_0x720f('0x36')]['offset']);}return _0x38a0fc[_0x720f('0x55')](_0x11dc35);}})[_0x720f('0x2b')](function(_0x5a0bc9){if(_0x5a0bc9){return _0x5a0bc9?{'count':_0x199210,'rows':_0x5a0bc9}:null;}})[_0x720f('0x2b')](respondWithResult(_0x3667ba,null))[_0x720f('0x4b')](handleError(_0x3667ba,null));};exports[_0x720f('0x57')]=function(_0x4057ae,_0xa08799,_0x171c2f){return db[_0x720f('0x58')][_0x720f('0x50')]({'where':{'id':_0x4057ae[_0x720f('0x4f')]['id']}})[_0x720f('0x2b')](handleEntityNotFound(_0xa08799,null))[_0x720f('0x2b')](function(_0x55449e){if(_0x55449e){return _0x55449e[_0x720f('0x57')](_0x4057ae['body'][_0x720f('0x53')],_['omit'](_0x4057ae[_0x720f('0x4e')],['ids','id'])||{});}})[_0x720f('0x2b')](respondWithResult(_0xa08799,null))[_0x720f('0x4b')](handleError(_0xa08799,null));};exports[_0x720f('0x59')]=function(_0x43e656,_0x468148,_0x5d9ee6){var _0x10f810={};var _0x53d9f4={};var _0x143bb7;var _0x546312;return db[_0x720f('0x31')][_0x720f('0x56')]({'where':{'id':_0x43e656['params']['id']}})['then'](handleEntityNotFound(_0x468148,null))['then'](function(_0x5bd676){if(_0x5bd676){_0x143bb7=_0x5bd676;_0x53d9f4[_0x720f('0x39')]=_[_0x720f('0x4c')](db[_0x720f('0x58')][_0x720f('0x32')]);_0x53d9f4[_0x720f('0x36')]=_['keys'](_0x43e656[_0x720f('0x36')]);_0x53d9f4[_0x720f('0x37')]=_[_0x720f('0x38')](_0x53d9f4[_0x720f('0x39')],_0x53d9f4['query']);_0x10f810[_0x720f('0x3c')]=_[_0x720f('0x38')](_0x53d9f4['model'],qs[_0x720f('0x3a')](_0x43e656['query']['fields']));_0x10f810[_0x720f('0x3c')]=_0x10f810[_0x720f('0x3c')][_0x720f('0x3b')]?_0x10f810[_0x720f('0x3c')]:_0x53d9f4[_0x720f('0x39')];_0x10f810[_0x720f('0x3f')]=qs['sort'](_0x43e656['query']['sort']);_0x10f810[_0x720f('0x41')]=qs[_0x720f('0x37')](_['pick'](_0x43e656[_0x720f('0x36')],_0x53d9f4[_0x720f('0x37')]));if(_0x43e656[_0x720f('0x36')][_0x720f('0x45')]){_0x10f810[_0x720f('0x41')]=_['merge'](_0x10f810[_0x720f('0x41')],{'$or':_[_0x720f('0x30')](_0x10f810[_0x720f('0x3c')],function(_0x390f34){var _0x2adc8f={};_0x2adc8f[_0x390f34]={'$like':'%'+_0x43e656[_0x720f('0x36')][_0x720f('0x45')]+'%'};return _0x2adc8f;})});}_0x10f810=_['merge']({},_0x10f810,_0x43e656[_0x720f('0x46')]);return _0x143bb7[_0x720f('0x59')](_0x10f810);}})[_0x720f('0x2b')](function(_0xb850cc){if(_0xb850cc){_0x546312=_0xb850cc[_0x720f('0x3b')];if(!_0x43e656['query'][_0x720f('0x3d')](_0x720f('0x3e'))){_0x10f810['limit']=qs[_0x720f('0x22')](_0x43e656['query'][_0x720f('0x22')]);_0x10f810['offset']=qs[_0x720f('0x1f')](_0x43e656[_0x720f('0x36')]['offset']);}return _0x143bb7[_0x720f('0x59')](_0x10f810);}})[_0x720f('0x2b')](function(_0x13a578){if(_0x13a578){return _0x13a578?{'count':_0x546312,'rows':_0x13a578}:null;}})[_0x720f('0x2b')](respondWithResult(_0x468148,null))[_0x720f('0x4b')](handleError(_0x468148,null));};