Built motion from commit 167bccf5.|2.6.7
[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 _0x3577=['apply','reject','save','update','then','destroy','error','stack','name','send','index','rawAttributes','fieldName','key','model','map','keys','query','filters','intersection','attributes','fields','hasOwnProperty','sort','where','pick','filter','merge','options','Automation','includeAll','include','findAll','catch','show','params','length','find','create','body','addConditions','Condition','ids','getConditions','findOne','order','nolimit','addActions','Action','omit','getActions','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','ejs','fs-extra','lodash','squel','crypto','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','limit','undefined','count','set','Content-Range'];(function(_0x1c2a52,_0x4bfd87){var _0x71fd1a=function(_0x13bb38){while(--_0x13bb38){_0x1c2a52['push'](_0x1c2a52['shift']());}};_0x71fd1a(++_0x4bfd87);}(_0x3577,0x193));var _0x7357=function(_0x123c8b,_0x671f61){_0x123c8b=_0x123c8b-0x0;var _0x290ed2=_0x3577[_0x123c8b];return _0x290ed2;};'use strict';var emlformat=require(_0x7357('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x7357('0x1'));var jsonpatch=require(_0x7357('0x2'));var rp=require(_0x7357('0x3'));var moment=require(_0x7357('0x4'));var BPromise=require(_0x7357('0x5'));var Mustache=require(_0x7357('0x6'));var util=require(_0x7357('0x7'));var path=require('path');var sox=require(_0x7357('0x8'));var csv=require('to-csv');var ejs=require(_0x7357('0x9'));var fs=require('fs');var fs_extra=require(_0x7357('0xa'));var _=require(_0x7357('0xb'));var squel=require(_0x7357('0xc'));var crypto=require(_0x7357('0xd'));var jsforce=require('jsforce');var deskjs=require(_0x7357('0xe'));var toCsv=require(_0x7357('0xf'));var querystring=require(_0x7357('0x10'));var Papa=require(_0x7357('0x11'));var Redis=require(_0x7357('0x12'));var authService=require(_0x7357('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x7357('0x14'));var hardwareService=require(_0x7357('0x15'));var logger=require('../../config/logger')(_0x7357('0x16'));var utils=require(_0x7357('0x17'));var config=require(_0x7357('0x18'));var licenseUtil=require(_0x7357('0x19'));var db=require(_0x7357('0x1a'))['db'];function respondWithStatusCode(_0x13d261,_0x57e625){_0x57e625=_0x57e625||0xcc;return function(_0x137d6c){if(_0x137d6c){return _0x13d261[_0x7357('0x1b')](_0x57e625);}return _0x13d261[_0x7357('0x1c')](_0x57e625)[_0x7357('0x1d')]();};}function respondWithResult(_0x296b74,_0xf92139){_0xf92139=_0xf92139||0xc8;return function(_0x1e2a51){if(_0x1e2a51){return _0x296b74['status'](_0xf92139)[_0x7357('0x1e')](_0x1e2a51);}};}function respondWithFilteredResult(_0x4848d1,_0x4898bc){return function(_0x282ca6){if(_0x282ca6){var _0x33e0ab=typeof _0x4898bc[_0x7357('0x1f')]==='undefined'&&typeof _0x4898bc[_0x7357('0x20')]===_0x7357('0x21');var _0x50b9c2=_0x282ca6[_0x7357('0x22')];var _0x511c25=_0x33e0ab?0x0:_0x4898bc[_0x7357('0x1f')];var _0x385dc7=_0x33e0ab?_0x282ca6[_0x7357('0x22')]:_0x4898bc[_0x7357('0x1f')]+_0x4898bc[_0x7357('0x20')];var _0x125f0b;if(_0x385dc7>=_0x50b9c2){_0x385dc7=_0x50b9c2;_0x125f0b=0xc8;}else{_0x125f0b=0xce;}_0x4848d1[_0x7357('0x1c')](_0x125f0b);return _0x4848d1[_0x7357('0x23')](_0x7357('0x24'),_0x511c25+'-'+_0x385dc7+'/'+_0x50b9c2)[_0x7357('0x1e')](_0x282ca6);}return null;};}function patchUpdates(_0x4d8309){return function(_0xd607c6){try{jsonpatch[_0x7357('0x25')](_0xd607c6,_0x4d8309,!![]);}catch(_0x35d848){return BPromise[_0x7357('0x26')](_0x35d848);}return _0xd607c6[_0x7357('0x27')]();};}function saveUpdates(_0x1b4868,_0x1fcd78){return function(_0x2cafe3){if(_0x2cafe3){return _0x2cafe3[_0x7357('0x28')](_0x1b4868)[_0x7357('0x29')](function(_0x289928){return _0x289928;});}return null;};}function removeEntity(_0x4ccb53,_0x395ae5){return function(_0x3f7132){if(_0x3f7132){return _0x3f7132[_0x7357('0x2a')]()[_0x7357('0x29')](function(){_0x4ccb53['status'](0xcc)[_0x7357('0x1d')]();});}};}function handleEntityNotFound(_0x2ba9ed,_0x24944b){return function(_0x400732){if(!_0x400732){_0x2ba9ed[_0x7357('0x1b')](0x194);}return _0x400732;};}function handleError(_0x420ad2,_0x1861fd){_0x1861fd=_0x1861fd||0x1f4;return function(_0x131d6e){logger[_0x7357('0x2b')](_0x131d6e[_0x7357('0x2c')]);if(_0x131d6e[_0x7357('0x2d')]){delete _0x131d6e[_0x7357('0x2d')];}_0x420ad2[_0x7357('0x1c')](_0x1861fd)[_0x7357('0x2e')](_0x131d6e);};}exports[_0x7357('0x2f')]=function(_0xf3fce4,_0x34302f){var _0x5f5b89={},_0x59aa6e={},_0x143fec={'count':0x0,'rows':[]};var _0x34b6f4=_['map'](db['Automation'][_0x7357('0x30')],function(_0x17ab57){return{'name':_0x17ab57[_0x7357('0x31')],'type':_0x17ab57['type'][_0x7357('0x32')]};});_0x59aa6e[_0x7357('0x33')]=_[_0x7357('0x34')](_0x34b6f4,'name');_0x59aa6e['query']=_[_0x7357('0x35')](_0xf3fce4[_0x7357('0x36')]);_0x59aa6e[_0x7357('0x37')]=_[_0x7357('0x38')](_0x59aa6e[_0x7357('0x33')],_0x59aa6e[_0x7357('0x36')]);_0x5f5b89[_0x7357('0x39')]=_[_0x7357('0x38')](_0x59aa6e['model'],qs[_0x7357('0x3a')](_0xf3fce4[_0x7357('0x36')][_0x7357('0x3a')]));_0x5f5b89[_0x7357('0x39')]=_0x5f5b89[_0x7357('0x39')]['length']?_0x5f5b89[_0x7357('0x39')]:_0x59aa6e[_0x7357('0x33')];if(!_0xf3fce4['query'][_0x7357('0x3b')]('nolimit')){_0x5f5b89[_0x7357('0x20')]=qs['limit'](_0xf3fce4[_0x7357('0x36')]['limit']);_0x5f5b89[_0x7357('0x1f')]=qs[_0x7357('0x1f')](_0xf3fce4[_0x7357('0x36')][_0x7357('0x1f')]);}_0x5f5b89['order']=qs[_0x7357('0x3c')](_0xf3fce4[_0x7357('0x36')]['sort']);_0x5f5b89[_0x7357('0x3d')]=qs[_0x7357('0x37')](_[_0x7357('0x3e')](_0xf3fce4['query'],_0x59aa6e[_0x7357('0x37')]),_0x34b6f4);if(_0xf3fce4[_0x7357('0x36')][_0x7357('0x3f')]){_0x5f5b89[_0x7357('0x3d')]=_[_0x7357('0x40')](_0x5f5b89[_0x7357('0x3d')],{'$or':_[_0x7357('0x34')](_0x34b6f4,function(_0x363c24){if(_0x363c24['type']!=='VIRTUAL'){var _0x110fd2={};_0x110fd2[_0x363c24[_0x7357('0x2d')]]={'$like':'%'+_0xf3fce4['query'][_0x7357('0x3f')]+'%'};return _0x110fd2;}})});}_0x5f5b89=_[_0x7357('0x40')]({},_0x5f5b89,_0xf3fce4[_0x7357('0x41')]);var _0x2820a2={'where':_0x5f5b89[_0x7357('0x3d')]};return db[_0x7357('0x42')][_0x7357('0x22')](_0x2820a2)[_0x7357('0x29')](function(_0x4f345a){_0x143fec['count']=_0x4f345a;if(_0xf3fce4[_0x7357('0x36')][_0x7357('0x43')]){_0x5f5b89[_0x7357('0x44')]=[{'all':!![]}];}return db[_0x7357('0x42')][_0x7357('0x45')](_0x5f5b89);})['then'](function(_0x423c86){_0x143fec['rows']=_0x423c86;return _0x143fec;})[_0x7357('0x29')](respondWithFilteredResult(_0x34302f,_0x5f5b89))[_0x7357('0x46')](handleError(_0x34302f,null));};exports[_0x7357('0x47')]=function(_0x1afc77,_0x10981c){var _0x353b2d={'raw':![],'where':{'id':_0x1afc77[_0x7357('0x48')]['id']}},_0x1bec34={};_0x1bec34[_0x7357('0x33')]=_[_0x7357('0x35')](db['Automation'][_0x7357('0x30')]);_0x1bec34[_0x7357('0x36')]=_[_0x7357('0x35')](_0x1afc77['query']);_0x1bec34[_0x7357('0x37')]=_[_0x7357('0x38')](_0x1bec34['model'],_0x1bec34[_0x7357('0x36')]);_0x353b2d[_0x7357('0x39')]=_[_0x7357('0x38')](_0x1bec34['model'],qs[_0x7357('0x3a')](_0x1afc77[_0x7357('0x36')][_0x7357('0x3a')]));_0x353b2d[_0x7357('0x39')]=_0x353b2d[_0x7357('0x39')][_0x7357('0x49')]?_0x353b2d[_0x7357('0x39')]:_0x1bec34['model'];if(_0x1afc77[_0x7357('0x36')][_0x7357('0x43')]){_0x353b2d[_0x7357('0x44')]=[{'all':!![]}];}_0x353b2d=_['merge']({},_0x353b2d,_0x1afc77[_0x7357('0x41')]);return db[_0x7357('0x42')][_0x7357('0x4a')](_0x353b2d)[_0x7357('0x29')](handleEntityNotFound(_0x10981c,null))[_0x7357('0x29')](respondWithResult(_0x10981c,null))[_0x7357('0x46')](handleError(_0x10981c,null));};exports[_0x7357('0x4b')]=function(_0x2003e9,_0x115e98){return db['Automation'][_0x7357('0x4b')](_0x2003e9['body'],{})['then'](respondWithResult(_0x115e98,0xc9))['catch'](handleError(_0x115e98,null));};exports['update']=function(_0x1e5e7d,_0x427b1a){if(_0x1e5e7d[_0x7357('0x4c')]['id']){delete _0x1e5e7d['body']['id'];}return db['Automation'][_0x7357('0x4a')]({'where':{'id':_0x1e5e7d[_0x7357('0x48')]['id']}})['then'](handleEntityNotFound(_0x427b1a,null))[_0x7357('0x29')](saveUpdates(_0x1e5e7d[_0x7357('0x4c')],null))[_0x7357('0x29')](respondWithResult(_0x427b1a,null))[_0x7357('0x46')](handleError(_0x427b1a,null));};exports['destroy']=function(_0xc0b042,_0x3c1b1a){return db['Automation'][_0x7357('0x4a')]({'where':{'id':_0xc0b042['params']['id']}})['then'](handleEntityNotFound(_0x3c1b1a,null))[_0x7357('0x29')](removeEntity(_0x3c1b1a,null))[_0x7357('0x46')](handleError(_0x3c1b1a,null));};exports[_0x7357('0x4d')]=function(_0x53da2b,_0xe7b808,_0x357d3b){return db[_0x7357('0x4e')][_0x7357('0x4a')]({'where':{'id':_0x53da2b['params']['id']}})[_0x7357('0x29')](handleEntityNotFound(_0xe7b808,null))[_0x7357('0x29')](function(_0x5c7175){if(_0x5c7175){return _0x5c7175['addConditions'](_0x53da2b[_0x7357('0x4c')][_0x7357('0x4f')],_['omit'](_0x53da2b[_0x7357('0x4c')],['ids','id'])||{});}})['then'](respondWithResult(_0xe7b808,null))[_0x7357('0x46')](handleError(_0xe7b808,null));};exports[_0x7357('0x50')]=function(_0x268644,_0x532599,_0x15d3f0){var _0x2bfef5={};var _0x5186a1={};var _0x57c731;var _0x246432;return db['Automation'][_0x7357('0x51')]({'where':{'id':_0x268644[_0x7357('0x48')]['id']}})[_0x7357('0x29')](handleEntityNotFound(_0x532599,null))[_0x7357('0x29')](function(_0x42f518){if(_0x42f518){_0x57c731=_0x42f518;_0x5186a1[_0x7357('0x33')]=_['keys'](db[_0x7357('0x4e')][_0x7357('0x30')]);_0x5186a1[_0x7357('0x36')]=_[_0x7357('0x35')](_0x268644[_0x7357('0x36')]);_0x5186a1['filters']=_[_0x7357('0x38')](_0x5186a1[_0x7357('0x33')],_0x5186a1[_0x7357('0x36')]);_0x2bfef5[_0x7357('0x39')]=_['intersection'](_0x5186a1[_0x7357('0x33')],qs[_0x7357('0x3a')](_0x268644[_0x7357('0x36')][_0x7357('0x3a')]));_0x2bfef5[_0x7357('0x39')]=_0x2bfef5[_0x7357('0x39')][_0x7357('0x49')]?_0x2bfef5[_0x7357('0x39')]:_0x5186a1[_0x7357('0x33')];_0x2bfef5[_0x7357('0x52')]=qs[_0x7357('0x3c')](_0x268644[_0x7357('0x36')][_0x7357('0x3c')]);_0x2bfef5[_0x7357('0x3d')]=qs['filters'](_['pick'](_0x268644[_0x7357('0x36')],_0x5186a1[_0x7357('0x37')]));if(_0x268644[_0x7357('0x36')][_0x7357('0x3f')]){_0x2bfef5[_0x7357('0x3d')]=_[_0x7357('0x40')](_0x2bfef5[_0x7357('0x3d')],{'$or':_[_0x7357('0x34')](_0x2bfef5[_0x7357('0x39')],function(_0x220f8f){var _0x3cb11f={};_0x3cb11f[_0x220f8f]={'$like':'%'+_0x268644[_0x7357('0x36')]['filter']+'%'};return _0x3cb11f;})});}_0x2bfef5=_[_0x7357('0x40')]({},_0x2bfef5,_0x268644[_0x7357('0x41')]);return _0x57c731[_0x7357('0x50')](_0x2bfef5);}})[_0x7357('0x29')](function(_0x13d5ed){if(_0x13d5ed){_0x246432=_0x13d5ed[_0x7357('0x49')];if(!_0x268644['query'][_0x7357('0x3b')](_0x7357('0x53'))){_0x2bfef5['limit']=qs[_0x7357('0x20')](_0x268644[_0x7357('0x36')][_0x7357('0x20')]);_0x2bfef5[_0x7357('0x1f')]=qs[_0x7357('0x1f')](_0x268644['query']['offset']);}return _0x57c731[_0x7357('0x50')](_0x2bfef5);}})[_0x7357('0x29')](function(_0x569044){if(_0x569044){return _0x569044?{'count':_0x246432,'rows':_0x569044}:null;}})[_0x7357('0x29')](respondWithResult(_0x532599,null))[_0x7357('0x46')](handleError(_0x532599,null));};exports[_0x7357('0x54')]=function(_0x10431f,_0x22b496,_0x497a32){return db[_0x7357('0x55')][_0x7357('0x4a')]({'where':{'id':_0x10431f['params']['id']}})[_0x7357('0x29')](handleEntityNotFound(_0x22b496,null))[_0x7357('0x29')](function(_0x5e9063){if(_0x5e9063){return _0x5e9063[_0x7357('0x54')](_0x10431f['body']['ids'],_[_0x7357('0x56')](_0x10431f[_0x7357('0x4c')],[_0x7357('0x4f'),'id'])||{});}})[_0x7357('0x29')](respondWithResult(_0x22b496,null))[_0x7357('0x46')](handleError(_0x22b496,null));};exports['getActions']=function(_0x403b72,_0x5e9009,_0x1b33da){var _0x56946d={};var _0x501e0e={};var _0x30bcee;var _0x45fc5f;return db[_0x7357('0x42')]['findOne']({'where':{'id':_0x403b72['params']['id']}})[_0x7357('0x29')](handleEntityNotFound(_0x5e9009,null))['then'](function(_0x50c175){if(_0x50c175){_0x30bcee=_0x50c175;_0x501e0e[_0x7357('0x33')]=_[_0x7357('0x35')](db['Action'][_0x7357('0x30')]);_0x501e0e[_0x7357('0x36')]=_[_0x7357('0x35')](_0x403b72[_0x7357('0x36')]);_0x501e0e['filters']=_[_0x7357('0x38')](_0x501e0e[_0x7357('0x33')],_0x501e0e['query']);_0x56946d['attributes']=_['intersection'](_0x501e0e[_0x7357('0x33')],qs[_0x7357('0x3a')](_0x403b72[_0x7357('0x36')]['fields']));_0x56946d['attributes']=_0x56946d[_0x7357('0x39')][_0x7357('0x49')]?_0x56946d[_0x7357('0x39')]:_0x501e0e[_0x7357('0x33')];_0x56946d[_0x7357('0x52')]=qs[_0x7357('0x3c')](_0x403b72['query'][_0x7357('0x3c')]);_0x56946d[_0x7357('0x3d')]=qs[_0x7357('0x37')](_[_0x7357('0x3e')](_0x403b72[_0x7357('0x36')],_0x501e0e[_0x7357('0x37')]));if(_0x403b72[_0x7357('0x36')][_0x7357('0x3f')]){_0x56946d[_0x7357('0x3d')]=_[_0x7357('0x40')](_0x56946d[_0x7357('0x3d')],{'$or':_['map'](_0x56946d[_0x7357('0x39')],function(_0x31f223){var _0x31d7d2={};_0x31d7d2[_0x31f223]={'$like':'%'+_0x403b72[_0x7357('0x36')]['filter']+'%'};return _0x31d7d2;})});}_0x56946d=_[_0x7357('0x40')]({},_0x56946d,_0x403b72[_0x7357('0x41')]);return _0x30bcee[_0x7357('0x57')](_0x56946d);}})[_0x7357('0x29')](function(_0x3ed7aa){if(_0x3ed7aa){_0x45fc5f=_0x3ed7aa[_0x7357('0x49')];if(!_0x403b72[_0x7357('0x36')][_0x7357('0x3b')](_0x7357('0x53'))){_0x56946d[_0x7357('0x20')]=qs[_0x7357('0x20')](_0x403b72[_0x7357('0x36')][_0x7357('0x20')]);_0x56946d[_0x7357('0x1f')]=qs[_0x7357('0x1f')](_0x403b72['query']['offset']);}return _0x30bcee[_0x7357('0x57')](_0x56946d);}})['then'](function(_0x58f57a){if(_0x58f57a){return _0x58f57a?{'count':_0x45fc5f,'rows':_0x58f57a}:null;}})[_0x7357('0x29')](respondWithResult(_0x5e9009,null))[_0x7357('0x46')](handleError(_0x5e9009,null));};