6a07074cc2e9f374b9912ca7362f22d595e5ab85
[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 _0x18f5=['ejs','fs-extra','lodash','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/environment','../../config/license/util','sendStatus','end','status','json','limit','undefined','count','offset','set','apply','update','then','destroy','stack','send','index','map','Automation','rawAttributes','fieldName','key','model','name','query','keys','attributes','intersection','fields','length','nolimit','order','sort','where','pick','filters','filter','merge','type','VIRTUAL','options','includeAll','include','findAll','rows','catch','params','find','create','body','addConditions','ids','findOne','Condition','hasOwnProperty','getConditions','addActions','omit','getActions','Action','eml-format','rimraf','request-promise','bluebird','mustache','util','path','to-csv'];(function(_0x14a862,_0x2b559d){var _0x245eb5=function(_0x6e014c){while(--_0x6e014c){_0x14a862['push'](_0x14a862['shift']());}};_0x245eb5(++_0x2b559d);}(_0x18f5,0x192));var _0x518f=function(_0x13b8f3,_0x442821){_0x13b8f3=_0x13b8f3-0x0;var _0x1a6d06=_0x18f5[_0x13b8f3];return _0x1a6d06;};'use strict';var emlformat=require(_0x518f('0x0'));var rimraf=require(_0x518f('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x518f('0x2'));var moment=require('moment');var BPromise=require(_0x518f('0x3'));var Mustache=require(_0x518f('0x4'));var util=require(_0x518f('0x5'));var path=require(_0x518f('0x6'));var sox=require('sox');var csv=require(_0x518f('0x7'));var ejs=require(_0x518f('0x8'));var fs=require('fs');var fs_extra=require(_0x518f('0x9'));var _=require(_0x518f('0xa'));var squel=require('squel');var crypto=require(_0x518f('0xb'));var jsforce=require('jsforce');var deskjs=require(_0x518f('0xc'));var toCsv=require('to-csv');var querystring=require(_0x518f('0xd'));var Papa=require(_0x518f('0xe'));var Redis=require(_0x518f('0xf'));var authService=require(_0x518f('0x10'));var qs=require(_0x518f('0x11'));var as=require(_0x518f('0x12'));var hardwareService=require(_0x518f('0x13'));var logger=require(_0x518f('0x14'))('api');var utils=require('../../config/utils');var config=require(_0x518f('0x15'));var licenseUtil=require(_0x518f('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x357b3a,_0x2c7893){_0x2c7893=_0x2c7893||0xcc;return function(_0x5801c1){if(_0x5801c1){return _0x357b3a[_0x518f('0x17')](_0x2c7893);}return _0x357b3a['status'](_0x2c7893)[_0x518f('0x18')]();};}function respondWithResult(_0x5bcbb4,_0x52e433){_0x52e433=_0x52e433||0xc8;return function(_0x4033ab){if(_0x4033ab){return _0x5bcbb4[_0x518f('0x19')](_0x52e433)[_0x518f('0x1a')](_0x4033ab);}};}function respondWithFilteredResult(_0x3c32d2,_0x149585){return function(_0x4fe053){if(_0x4fe053){var _0x1d9d02=typeof _0x149585['offset']==='undefined'&&typeof _0x149585[_0x518f('0x1b')]===_0x518f('0x1c');var _0x2fff9c=_0x4fe053[_0x518f('0x1d')];var _0x1a2f5d=_0x1d9d02?0x0:_0x149585[_0x518f('0x1e')];var _0x318952=_0x1d9d02?_0x4fe053[_0x518f('0x1d')]:_0x149585['offset']+_0x149585['limit'];var _0x3bf8be;if(_0x318952>=_0x2fff9c){_0x318952=_0x2fff9c;_0x3bf8be=0xc8;}else{_0x3bf8be=0xce;}_0x3c32d2[_0x518f('0x19')](_0x3bf8be);return _0x3c32d2[_0x518f('0x1f')]('Content-Range',_0x1a2f5d+'-'+_0x318952+'/'+_0x2fff9c)[_0x518f('0x1a')](_0x4fe053);}return null;};}function patchUpdates(_0x2e3556){return function(_0x15dace){try{jsonpatch[_0x518f('0x20')](_0x15dace,_0x2e3556,!![]);}catch(_0x4096cd){return BPromise['reject'](_0x4096cd);}return _0x15dace['save']();};}function saveUpdates(_0x1987c1,_0x37ca6f){return function(_0x5a2452){if(_0x5a2452){return _0x5a2452[_0x518f('0x21')](_0x1987c1)[_0x518f('0x22')](function(_0x1fe83e){return _0x1fe83e;});}return null;};}function removeEntity(_0x1ef0c6,_0x25a113){return function(_0x515f8e){if(_0x515f8e){return _0x515f8e[_0x518f('0x23')]()['then'](function(){_0x1ef0c6[_0x518f('0x19')](0xcc)[_0x518f('0x18')]();});}};}function handleEntityNotFound(_0x38bc8c,_0x297337){return function(_0xc50938){if(!_0xc50938){_0x38bc8c['sendStatus'](0x194);}return _0xc50938;};}function handleError(_0x4e2a54,_0x3258ce){_0x3258ce=_0x3258ce||0x1f4;return function(_0xb0db0d){logger['error'](_0xb0db0d[_0x518f('0x24')]);if(_0xb0db0d['name']){delete _0xb0db0d['name'];}_0x4e2a54[_0x518f('0x19')](_0x3258ce)[_0x518f('0x25')](_0xb0db0d);};}exports[_0x518f('0x26')]=function(_0x2472d1,_0x5760ac){var _0x31e60c={},_0x262e6a={},_0x4fc97b={'count':0x0,'rows':[]};var _0x29c138=_[_0x518f('0x27')](db[_0x518f('0x28')][_0x518f('0x29')],function(_0x27d657){return{'name':_0x27d657[_0x518f('0x2a')],'type':_0x27d657['type'][_0x518f('0x2b')]};});_0x262e6a[_0x518f('0x2c')]=_[_0x518f('0x27')](_0x29c138,_0x518f('0x2d'));_0x262e6a[_0x518f('0x2e')]=_[_0x518f('0x2f')](_0x2472d1[_0x518f('0x2e')]);_0x262e6a['filters']=_['intersection'](_0x262e6a['model'],_0x262e6a[_0x518f('0x2e')]);_0x31e60c[_0x518f('0x30')]=_[_0x518f('0x31')](_0x262e6a[_0x518f('0x2c')],qs[_0x518f('0x32')](_0x2472d1[_0x518f('0x2e')][_0x518f('0x32')]));_0x31e60c[_0x518f('0x30')]=_0x31e60c[_0x518f('0x30')][_0x518f('0x33')]?_0x31e60c['attributes']:_0x262e6a['model'];if(!_0x2472d1[_0x518f('0x2e')]['hasOwnProperty'](_0x518f('0x34'))){_0x31e60c[_0x518f('0x1b')]=qs[_0x518f('0x1b')](_0x2472d1['query'][_0x518f('0x1b')]);_0x31e60c[_0x518f('0x1e')]=qs[_0x518f('0x1e')](_0x2472d1[_0x518f('0x2e')][_0x518f('0x1e')]);}_0x31e60c[_0x518f('0x35')]=qs[_0x518f('0x36')](_0x2472d1['query'][_0x518f('0x36')]);_0x31e60c[_0x518f('0x37')]=qs['filters'](_[_0x518f('0x38')](_0x2472d1[_0x518f('0x2e')],_0x262e6a[_0x518f('0x39')]),_0x29c138);if(_0x2472d1['query'][_0x518f('0x3a')]){_0x31e60c[_0x518f('0x37')]=_[_0x518f('0x3b')](_0x31e60c[_0x518f('0x37')],{'$or':_[_0x518f('0x27')](_0x29c138,function(_0xfd16c){if(_0xfd16c[_0x518f('0x3c')]!==_0x518f('0x3d')){var _0x103136={};_0x103136[_0xfd16c['name']]={'$like':'%'+_0x2472d1[_0x518f('0x2e')]['filter']+'%'};return _0x103136;}})});}_0x31e60c=_[_0x518f('0x3b')]({},_0x31e60c,_0x2472d1[_0x518f('0x3e')]);var _0x299189={'where':_0x31e60c[_0x518f('0x37')]};return db[_0x518f('0x28')][_0x518f('0x1d')](_0x299189)[_0x518f('0x22')](function(_0x16a1fd){_0x4fc97b[_0x518f('0x1d')]=_0x16a1fd;if(_0x2472d1[_0x518f('0x2e')][_0x518f('0x3f')]){_0x31e60c[_0x518f('0x40')]=[{'all':!![]}];}return db[_0x518f('0x28')][_0x518f('0x41')](_0x31e60c);})['then'](function(_0x1fc793){_0x4fc97b[_0x518f('0x42')]=_0x1fc793;return _0x4fc97b;})[_0x518f('0x22')](respondWithFilteredResult(_0x5760ac,_0x31e60c))[_0x518f('0x43')](handleError(_0x5760ac,null));};exports['show']=function(_0x47dbc2,_0x147c9a){var _0x3d86e7={'raw':![],'where':{'id':_0x47dbc2[_0x518f('0x44')]['id']}},_0x3e981b={};_0x3e981b[_0x518f('0x2c')]=_[_0x518f('0x2f')](db[_0x518f('0x28')][_0x518f('0x29')]);_0x3e981b['query']=_[_0x518f('0x2f')](_0x47dbc2[_0x518f('0x2e')]);_0x3e981b[_0x518f('0x39')]=_[_0x518f('0x31')](_0x3e981b[_0x518f('0x2c')],_0x3e981b[_0x518f('0x2e')]);_0x3d86e7[_0x518f('0x30')]=_[_0x518f('0x31')](_0x3e981b[_0x518f('0x2c')],qs['fields'](_0x47dbc2[_0x518f('0x2e')]['fields']));_0x3d86e7[_0x518f('0x30')]=_0x3d86e7['attributes'][_0x518f('0x33')]?_0x3d86e7[_0x518f('0x30')]:_0x3e981b['model'];if(_0x47dbc2['query'][_0x518f('0x3f')]){_0x3d86e7[_0x518f('0x40')]=[{'all':!![]}];}_0x3d86e7=_[_0x518f('0x3b')]({},_0x3d86e7,_0x47dbc2[_0x518f('0x3e')]);return db[_0x518f('0x28')][_0x518f('0x45')](_0x3d86e7)[_0x518f('0x22')](handleEntityNotFound(_0x147c9a,null))[_0x518f('0x22')](respondWithResult(_0x147c9a,null))[_0x518f('0x43')](handleError(_0x147c9a,null));};exports[_0x518f('0x46')]=function(_0xdd266f,_0x1b362d){return db[_0x518f('0x28')]['create'](_0xdd266f['body'],{})[_0x518f('0x22')](respondWithResult(_0x1b362d,0xc9))['catch'](handleError(_0x1b362d,null));};exports[_0x518f('0x21')]=function(_0x1544fc,_0x2fddab){if(_0x1544fc['body']['id']){delete _0x1544fc[_0x518f('0x47')]['id'];}return db['Automation'][_0x518f('0x45')]({'where':{'id':_0x1544fc[_0x518f('0x44')]['id']}})[_0x518f('0x22')](handleEntityNotFound(_0x2fddab,null))[_0x518f('0x22')](saveUpdates(_0x1544fc[_0x518f('0x47')],null))['then'](respondWithResult(_0x2fddab,null))['catch'](handleError(_0x2fddab,null));};exports[_0x518f('0x23')]=function(_0x189f0b,_0x55209f){return db[_0x518f('0x28')][_0x518f('0x45')]({'where':{'id':_0x189f0b['params']['id']}})[_0x518f('0x22')](handleEntityNotFound(_0x55209f,null))['then'](removeEntity(_0x55209f,null))[_0x518f('0x43')](handleError(_0x55209f,null));};exports[_0x518f('0x48')]=function(_0x3e70e7,_0x224e6c,_0x171d27){return db['Condition'][_0x518f('0x45')]({'where':{'id':_0x3e70e7['params']['id']}})['then'](handleEntityNotFound(_0x224e6c,null))[_0x518f('0x22')](function(_0x2f0882){if(_0x2f0882){return _0x2f0882[_0x518f('0x48')](_0x3e70e7[_0x518f('0x47')][_0x518f('0x49')],_['omit'](_0x3e70e7[_0x518f('0x47')],[_0x518f('0x49'),'id'])||{});}})[_0x518f('0x22')](respondWithResult(_0x224e6c,null))['catch'](handleError(_0x224e6c,null));};exports['getConditions']=function(_0x33781d,_0x449e8d,_0x370b91){var _0x1eaa22={};var _0x3d19fc={};var _0x569ae6;var _0x16ed5f;return db[_0x518f('0x28')][_0x518f('0x4a')]({'where':{'id':_0x33781d['params']['id']}})[_0x518f('0x22')](handleEntityNotFound(_0x449e8d,null))[_0x518f('0x22')](function(_0x23e88b){if(_0x23e88b){_0x569ae6=_0x23e88b;_0x3d19fc[_0x518f('0x2c')]=_['keys'](db[_0x518f('0x4b')]['rawAttributes']);_0x3d19fc[_0x518f('0x2e')]=_[_0x518f('0x2f')](_0x33781d[_0x518f('0x2e')]);_0x3d19fc[_0x518f('0x39')]=_[_0x518f('0x31')](_0x3d19fc[_0x518f('0x2c')],_0x3d19fc[_0x518f('0x2e')]);_0x1eaa22[_0x518f('0x30')]=_['intersection'](_0x3d19fc[_0x518f('0x2c')],qs[_0x518f('0x32')](_0x33781d[_0x518f('0x2e')][_0x518f('0x32')]));_0x1eaa22[_0x518f('0x30')]=_0x1eaa22['attributes'][_0x518f('0x33')]?_0x1eaa22[_0x518f('0x30')]:_0x3d19fc['model'];_0x1eaa22['order']=qs['sort'](_0x33781d[_0x518f('0x2e')][_0x518f('0x36')]);_0x1eaa22[_0x518f('0x37')]=qs[_0x518f('0x39')](_[_0x518f('0x38')](_0x33781d[_0x518f('0x2e')],_0x3d19fc[_0x518f('0x39')]));if(_0x33781d['query'][_0x518f('0x3a')]){_0x1eaa22['where']=_[_0x518f('0x3b')](_0x1eaa22[_0x518f('0x37')],{'$or':_[_0x518f('0x27')](_0x1eaa22['attributes'],function(_0x45a9d1){var _0x3baa6e={};_0x3baa6e[_0x45a9d1]={'$like':'%'+_0x33781d[_0x518f('0x2e')][_0x518f('0x3a')]+'%'};return _0x3baa6e;})});}_0x1eaa22=_[_0x518f('0x3b')]({},_0x1eaa22,_0x33781d[_0x518f('0x3e')]);return _0x569ae6['getConditions'](_0x1eaa22);}})['then'](function(_0x52e5b7){if(_0x52e5b7){_0x16ed5f=_0x52e5b7['length'];if(!_0x33781d[_0x518f('0x2e')][_0x518f('0x4c')](_0x518f('0x34'))){_0x1eaa22[_0x518f('0x1b')]=qs[_0x518f('0x1b')](_0x33781d[_0x518f('0x2e')][_0x518f('0x1b')]);_0x1eaa22[_0x518f('0x1e')]=qs['offset'](_0x33781d[_0x518f('0x2e')][_0x518f('0x1e')]);}return _0x569ae6[_0x518f('0x4d')](_0x1eaa22);}})[_0x518f('0x22')](function(_0x4a47fc){if(_0x4a47fc){return _0x4a47fc?{'count':_0x16ed5f,'rows':_0x4a47fc}:null;}})['then'](respondWithResult(_0x449e8d,null))[_0x518f('0x43')](handleError(_0x449e8d,null));};exports[_0x518f('0x4e')]=function(_0x22e204,_0x3a1228,_0x3332b1){return db['Action'][_0x518f('0x45')]({'where':{'id':_0x22e204[_0x518f('0x44')]['id']}})['then'](handleEntityNotFound(_0x3a1228,null))[_0x518f('0x22')](function(_0xea2fb){if(_0xea2fb){return _0xea2fb['addActions'](_0x22e204[_0x518f('0x47')]['ids'],_[_0x518f('0x4f')](_0x22e204[_0x518f('0x47')],[_0x518f('0x49'),'id'])||{});}})['then'](respondWithResult(_0x3a1228,null))[_0x518f('0x43')](handleError(_0x3a1228,null));};exports[_0x518f('0x50')]=function(_0x165177,_0x540b30,_0x4667a6){var _0xd590a1={};var _0x121ff1={};var _0x57aea7;var _0x4f944b;return db[_0x518f('0x28')][_0x518f('0x4a')]({'where':{'id':_0x165177[_0x518f('0x44')]['id']}})[_0x518f('0x22')](handleEntityNotFound(_0x540b30,null))[_0x518f('0x22')](function(_0x4cefe5){if(_0x4cefe5){_0x57aea7=_0x4cefe5;_0x121ff1[_0x518f('0x2c')]=_[_0x518f('0x2f')](db[_0x518f('0x51')][_0x518f('0x29')]);_0x121ff1['query']=_['keys'](_0x165177[_0x518f('0x2e')]);_0x121ff1[_0x518f('0x39')]=_[_0x518f('0x31')](_0x121ff1['model'],_0x121ff1['query']);_0xd590a1[_0x518f('0x30')]=_['intersection'](_0x121ff1[_0x518f('0x2c')],qs[_0x518f('0x32')](_0x165177[_0x518f('0x2e')][_0x518f('0x32')]));_0xd590a1[_0x518f('0x30')]=_0xd590a1[_0x518f('0x30')][_0x518f('0x33')]?_0xd590a1[_0x518f('0x30')]:_0x121ff1['model'];_0xd590a1['order']=qs[_0x518f('0x36')](_0x165177[_0x518f('0x2e')]['sort']);_0xd590a1['where']=qs[_0x518f('0x39')](_[_0x518f('0x38')](_0x165177['query'],_0x121ff1[_0x518f('0x39')]));if(_0x165177[_0x518f('0x2e')]['filter']){_0xd590a1['where']=_[_0x518f('0x3b')](_0xd590a1[_0x518f('0x37')],{'$or':_['map'](_0xd590a1[_0x518f('0x30')],function(_0x554004){var _0x3fadda={};_0x3fadda[_0x554004]={'$like':'%'+_0x165177[_0x518f('0x2e')]['filter']+'%'};return _0x3fadda;})});}_0xd590a1=_[_0x518f('0x3b')]({},_0xd590a1,_0x165177[_0x518f('0x3e')]);return _0x57aea7[_0x518f('0x50')](_0xd590a1);}})[_0x518f('0x22')](function(_0x273b29){if(_0x273b29){_0x4f944b=_0x273b29[_0x518f('0x33')];if(!_0x165177[_0x518f('0x2e')][_0x518f('0x4c')](_0x518f('0x34'))){_0xd590a1['limit']=qs['limit'](_0x165177['query'][_0x518f('0x1b')]);_0xd590a1['offset']=qs['offset'](_0x165177[_0x518f('0x2e')][_0x518f('0x1e')]);}return _0x57aea7[_0x518f('0x50')](_0xd590a1);}})[_0x518f('0x22')](function(_0x2a90e9){if(_0x2a90e9){return _0x2a90e9?{'count':_0x4f944b,'rows':_0x2a90e9}:null;}})[_0x518f('0x22')](respondWithResult(_0x540b30,null))[_0x518f('0x43')](handleError(_0x540b30,null));};