e5ec79ae0b8779ba3d13f8c0870732bec637631e
[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 _0xb6fb=['zip-dir','fast-json-patch','request-promise','moment','mustache','util','path','sox','ejs','lodash','squel','jsforce','to-csv','querystring','papaparse','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','offset','undefined','limit','count','set','Content-Range','json','apply','reject','save','then','destroy','error','send','index','map','Automation','type','key','model','name','query','keys','filters','intersection','fields','attributes','hasOwnProperty','sort','pick','filter','where','merge','VIRTUAL','options','findAll','rows','catch','show','rawAttributes','include','find','create','update','body','params','addConditions','Condition','ids','getConditions','findOne','length','order','nolimit','addActions','Action','omit','getActions','eml-format','rimraf'];(function(_0x138adb,_0x193fee){var _0x1b27f2=function(_0x5e97a2){while(--_0x5e97a2){_0x138adb['push'](_0x138adb['shift']());}};_0x1b27f2(++_0x193fee);}(_0xb6fb,0x1b6));var _0xbb6f=function(_0x1543d,_0x13d9eb){_0x1543d=_0x1543d-0x0;var _0x4dd719=_0xb6fb[_0x1543d];return _0x4dd719;};'use strict';var emlformat=require(_0xbb6f('0x0'));var rimraf=require(_0xbb6f('0x1'));var zipdir=require(_0xbb6f('0x2'));var jsonpatch=require(_0xbb6f('0x3'));var rp=require(_0xbb6f('0x4'));var moment=require(_0xbb6f('0x5'));var BPromise=require('bluebird');var Mustache=require(_0xbb6f('0x6'));var util=require(_0xbb6f('0x7'));var path=require(_0xbb6f('0x8'));var sox=require(_0xbb6f('0x9'));var csv=require('to-csv');var ejs=require(_0xbb6f('0xa'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require(_0xbb6f('0xb'));var squel=require(_0xbb6f('0xc'));var crypto=require('crypto');var jsforce=require(_0xbb6f('0xd'));var deskjs=require('desk.js');var toCsv=require(_0xbb6f('0xe'));var querystring=require(_0xbb6f('0xf'));var Papa=require(_0xbb6f('0x10'));var Redis=require(_0xbb6f('0x11'));var authService=require('../../components/auth/service');var qs=require(_0xbb6f('0x12'));var as=require(_0xbb6f('0x13'));var hardwareService=require(_0xbb6f('0x14'));var logger=require(_0xbb6f('0x15'))(_0xbb6f('0x16'));var utils=require(_0xbb6f('0x17'));var config=require(_0xbb6f('0x18'));var licenseUtil=require(_0xbb6f('0x19'));var db=require(_0xbb6f('0x1a'))['db'];function respondWithStatusCode(_0x215091,_0x13eede){_0x13eede=_0x13eede||0xcc;return function(_0x292753){if(_0x292753){return _0x215091[_0xbb6f('0x1b')](_0x13eede);}return _0x215091[_0xbb6f('0x1c')](_0x13eede)[_0xbb6f('0x1d')]();};}function respondWithResult(_0x58032d,_0x447b1b){_0x447b1b=_0x447b1b||0xc8;return function(_0x31946c){if(_0x31946c){return _0x58032d[_0xbb6f('0x1c')](_0x447b1b)['json'](_0x31946c);}};}function respondWithFilteredResult(_0x4d9c84,_0x214823){return function(_0x25d354){if(_0x25d354){var _0x15c647=typeof _0x214823[_0xbb6f('0x1e')]===_0xbb6f('0x1f')&&typeof _0x214823[_0xbb6f('0x20')]==='undefined';var _0x5eeb93=_0x25d354[_0xbb6f('0x21')];var _0x4cf242=_0x15c647?0x0:_0x214823[_0xbb6f('0x1e')];var _0x3da0b6=_0x15c647?_0x25d354['count']:_0x214823[_0xbb6f('0x1e')]+_0x214823[_0xbb6f('0x20')];var _0x449793;if(_0x3da0b6>=_0x5eeb93){_0x3da0b6=_0x5eeb93;_0x449793=0xc8;}else{_0x449793=0xce;}_0x4d9c84[_0xbb6f('0x1c')](_0x449793);return _0x4d9c84[_0xbb6f('0x22')](_0xbb6f('0x23'),_0x4cf242+'-'+_0x3da0b6+'/'+_0x5eeb93)[_0xbb6f('0x24')](_0x25d354);}return null;};}function patchUpdates(_0x345ffc){return function(_0x25f48e){try{jsonpatch[_0xbb6f('0x25')](_0x25f48e,_0x345ffc,!![]);}catch(_0x95a236){return BPromise[_0xbb6f('0x26')](_0x95a236);}return _0x25f48e[_0xbb6f('0x27')]();};}function saveUpdates(_0x550013,_0x51f891){return function(_0x2e9481){if(_0x2e9481){return _0x2e9481['update'](_0x550013)[_0xbb6f('0x28')](function(_0x3d09cb){return _0x3d09cb;});}return null;};}function removeEntity(_0x26d66d,_0xe49c6e){return function(_0xe63aed){if(_0xe63aed){return _0xe63aed[_0xbb6f('0x29')]()[_0xbb6f('0x28')](function(){_0x26d66d['status'](0xcc)[_0xbb6f('0x1d')]();});}};}function handleEntityNotFound(_0x4751c3,_0x2996cf){return function(_0x3723f5){if(!_0x3723f5){_0x4751c3[_0xbb6f('0x1b')](0x194);}return _0x3723f5;};}function handleError(_0x2d42d8,_0x44a31b){_0x44a31b=_0x44a31b||0x1f4;return function(_0x156b1d){logger[_0xbb6f('0x2a')](_0x156b1d['stack']);if(_0x156b1d['name']){delete _0x156b1d['name'];}_0x2d42d8['status'](_0x44a31b)[_0xbb6f('0x2b')](_0x156b1d);};}exports[_0xbb6f('0x2c')]=function(_0x435d35,_0x35575f){var _0x2a8e18={},_0x4fdac0={},_0x4aa37a={'count':0x0,'rows':[]};var _0x574ef8=_[_0xbb6f('0x2d')](db[_0xbb6f('0x2e')]['rawAttributes'],function(_0x4e81cd){return{'name':_0x4e81cd['fieldName'],'type':_0x4e81cd[_0xbb6f('0x2f')][_0xbb6f('0x30')]};});_0x4fdac0[_0xbb6f('0x31')]=_[_0xbb6f('0x2d')](_0x574ef8,_0xbb6f('0x32'));_0x4fdac0[_0xbb6f('0x33')]=_[_0xbb6f('0x34')](_0x435d35[_0xbb6f('0x33')]);_0x4fdac0[_0xbb6f('0x35')]=_[_0xbb6f('0x36')](_0x4fdac0[_0xbb6f('0x31')],_0x4fdac0[_0xbb6f('0x33')]);_0x2a8e18['attributes']=_['intersection'](_0x4fdac0['model'],qs[_0xbb6f('0x37')](_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x37')]));_0x2a8e18[_0xbb6f('0x38')]=_0x2a8e18[_0xbb6f('0x38')]['length']?_0x2a8e18[_0xbb6f('0x38')]:_0x4fdac0[_0xbb6f('0x31')];if(!_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x39')]('nolimit')){_0x2a8e18[_0xbb6f('0x20')]=qs[_0xbb6f('0x20')](_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x20')]);_0x2a8e18['offset']=qs[_0xbb6f('0x1e')](_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x1e')]);}_0x2a8e18['order']=qs['sort'](_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x3a')]);_0x2a8e18['where']=qs[_0xbb6f('0x35')](_[_0xbb6f('0x3b')](_0x435d35[_0xbb6f('0x33')],_0x4fdac0['filters']),_0x574ef8);if(_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x3c')]){_0x2a8e18[_0xbb6f('0x3d')]=_[_0xbb6f('0x3e')](_0x2a8e18['where'],{'$or':_['map'](_0x574ef8,function(_0x57fbab){if(_0x57fbab['type']!==_0xbb6f('0x3f')){var _0x3c0b17={};_0x3c0b17[_0x57fbab[_0xbb6f('0x32')]]={'$like':'%'+_0x435d35[_0xbb6f('0x33')][_0xbb6f('0x3c')]+'%'};return _0x3c0b17;}})});}_0x2a8e18=_[_0xbb6f('0x3e')]({},_0x2a8e18,_0x435d35[_0xbb6f('0x40')]);var _0x5605dd={'where':_0x2a8e18[_0xbb6f('0x3d')]};return db['Automation'][_0xbb6f('0x21')](_0x5605dd)['then'](function(_0x200f46){_0x4aa37a['count']=_0x200f46;if(_0x435d35['query']['includeAll']){_0x2a8e18['include']=[{'all':!![]}];}return db[_0xbb6f('0x2e')][_0xbb6f('0x41')](_0x2a8e18);})[_0xbb6f('0x28')](function(_0x352115){_0x4aa37a[_0xbb6f('0x42')]=_0x352115;return _0x4aa37a;})[_0xbb6f('0x28')](respondWithFilteredResult(_0x35575f,_0x2a8e18))[_0xbb6f('0x43')](handleError(_0x35575f,null));};exports[_0xbb6f('0x44')]=function(_0x124563,_0x2e449b){var _0x3a60b7={'raw':![],'where':{'id':_0x124563['params']['id']}},_0x2333d9={};_0x2333d9[_0xbb6f('0x31')]=_[_0xbb6f('0x34')](db[_0xbb6f('0x2e')][_0xbb6f('0x45')]);_0x2333d9[_0xbb6f('0x33')]=_[_0xbb6f('0x34')](_0x124563[_0xbb6f('0x33')]);_0x2333d9['filters']=_[_0xbb6f('0x36')](_0x2333d9[_0xbb6f('0x31')],_0x2333d9['query']);_0x3a60b7[_0xbb6f('0x38')]=_[_0xbb6f('0x36')](_0x2333d9[_0xbb6f('0x31')],qs[_0xbb6f('0x37')](_0x124563[_0xbb6f('0x33')][_0xbb6f('0x37')]));_0x3a60b7[_0xbb6f('0x38')]=_0x3a60b7[_0xbb6f('0x38')]['length']?_0x3a60b7[_0xbb6f('0x38')]:_0x2333d9[_0xbb6f('0x31')];if(_0x124563[_0xbb6f('0x33')]['includeAll']){_0x3a60b7[_0xbb6f('0x46')]=[{'all':!![]}];}_0x3a60b7=_[_0xbb6f('0x3e')]({},_0x3a60b7,_0x124563[_0xbb6f('0x40')]);return db[_0xbb6f('0x2e')][_0xbb6f('0x47')](_0x3a60b7)['then'](handleEntityNotFound(_0x2e449b,null))[_0xbb6f('0x28')](respondWithResult(_0x2e449b,null))[_0xbb6f('0x43')](handleError(_0x2e449b,null));};exports[_0xbb6f('0x48')]=function(_0x5e21f6,_0x2e8d06){return db[_0xbb6f('0x2e')][_0xbb6f('0x48')](_0x5e21f6['body'],{})[_0xbb6f('0x28')](respondWithResult(_0x2e8d06,0xc9))[_0xbb6f('0x43')](handleError(_0x2e8d06,null));};exports[_0xbb6f('0x49')]=function(_0x26d3a1,_0x59e5f7){if(_0x26d3a1[_0xbb6f('0x4a')]['id']){delete _0x26d3a1[_0xbb6f('0x4a')]['id'];}return db[_0xbb6f('0x2e')][_0xbb6f('0x47')]({'where':{'id':_0x26d3a1['params']['id']}})['then'](handleEntityNotFound(_0x59e5f7,null))[_0xbb6f('0x28')](saveUpdates(_0x26d3a1[_0xbb6f('0x4a')],null))[_0xbb6f('0x28')](respondWithResult(_0x59e5f7,null))['catch'](handleError(_0x59e5f7,null));};exports[_0xbb6f('0x29')]=function(_0x57abc5,_0x4356bd){return db[_0xbb6f('0x2e')][_0xbb6f('0x47')]({'where':{'id':_0x57abc5[_0xbb6f('0x4b')]['id']}})['then'](handleEntityNotFound(_0x4356bd,null))[_0xbb6f('0x28')](removeEntity(_0x4356bd,null))[_0xbb6f('0x43')](handleError(_0x4356bd,null));};exports[_0xbb6f('0x4c')]=function(_0x43ab30,_0x598a5a,_0x2e8690){return db[_0xbb6f('0x4d')][_0xbb6f('0x47')]({'where':{'id':_0x43ab30[_0xbb6f('0x4b')]['id']}})['then'](handleEntityNotFound(_0x598a5a,null))[_0xbb6f('0x28')](function(_0x5b6da9){if(_0x5b6da9){return _0x5b6da9[_0xbb6f('0x4c')](_0x43ab30[_0xbb6f('0x4a')][_0xbb6f('0x4e')],_['omit'](_0x43ab30[_0xbb6f('0x4a')],[_0xbb6f('0x4e'),'id'])||{});}})[_0xbb6f('0x28')](respondWithResult(_0x598a5a,null))['catch'](handleError(_0x598a5a,null));};exports[_0xbb6f('0x4f')]=function(_0x3a1db5,_0x20b08c,_0x5675b5){var _0x5362a3={};var _0x143dae={};var _0x51400f;var _0x2f4d89;return db['Automation'][_0xbb6f('0x50')]({'where':{'id':_0x3a1db5[_0xbb6f('0x4b')]['id']}})[_0xbb6f('0x28')](handleEntityNotFound(_0x20b08c,null))[_0xbb6f('0x28')](function(_0x274960){if(_0x274960){_0x51400f=_0x274960;_0x143dae['model']=_[_0xbb6f('0x34')](db[_0xbb6f('0x4d')][_0xbb6f('0x45')]);_0x143dae[_0xbb6f('0x33')]=_[_0xbb6f('0x34')](_0x3a1db5[_0xbb6f('0x33')]);_0x143dae['filters']=_['intersection'](_0x143dae['model'],_0x143dae[_0xbb6f('0x33')]);_0x5362a3['attributes']=_['intersection'](_0x143dae[_0xbb6f('0x31')],qs[_0xbb6f('0x37')](_0x3a1db5[_0xbb6f('0x33')][_0xbb6f('0x37')]));_0x5362a3[_0xbb6f('0x38')]=_0x5362a3['attributes'][_0xbb6f('0x51')]?_0x5362a3[_0xbb6f('0x38')]:_0x143dae[_0xbb6f('0x31')];_0x5362a3[_0xbb6f('0x52')]=qs[_0xbb6f('0x3a')](_0x3a1db5['query'][_0xbb6f('0x3a')]);_0x5362a3['where']=qs[_0xbb6f('0x35')](_[_0xbb6f('0x3b')](_0x3a1db5[_0xbb6f('0x33')],_0x143dae[_0xbb6f('0x35')]));if(_0x3a1db5[_0xbb6f('0x33')][_0xbb6f('0x3c')]){_0x5362a3['where']=_[_0xbb6f('0x3e')](_0x5362a3[_0xbb6f('0x3d')],{'$or':_[_0xbb6f('0x2d')](_0x5362a3[_0xbb6f('0x38')],function(_0x167542){var _0x386aab={};_0x386aab[_0x167542]={'$like':'%'+_0x3a1db5['query'][_0xbb6f('0x3c')]+'%'};return _0x386aab;})});}_0x5362a3=_[_0xbb6f('0x3e')]({},_0x5362a3,_0x3a1db5[_0xbb6f('0x40')]);return _0x51400f['getConditions'](_0x5362a3);}})[_0xbb6f('0x28')](function(_0x21ffc1){if(_0x21ffc1){_0x2f4d89=_0x21ffc1[_0xbb6f('0x51')];if(!_0x3a1db5['query'][_0xbb6f('0x39')](_0xbb6f('0x53'))){_0x5362a3[_0xbb6f('0x20')]=qs[_0xbb6f('0x20')](_0x3a1db5[_0xbb6f('0x33')][_0xbb6f('0x20')]);_0x5362a3['offset']=qs['offset'](_0x3a1db5['query'][_0xbb6f('0x1e')]);}return _0x51400f['getConditions'](_0x5362a3);}})[_0xbb6f('0x28')](function(_0x4d0e75){if(_0x4d0e75){return _0x4d0e75?{'count':_0x2f4d89,'rows':_0x4d0e75}:null;}})[_0xbb6f('0x28')](respondWithResult(_0x20b08c,null))[_0xbb6f('0x43')](handleError(_0x20b08c,null));};exports[_0xbb6f('0x54')]=function(_0x5e5b80,_0x160723,_0x1671f4){return db[_0xbb6f('0x55')][_0xbb6f('0x47')]({'where':{'id':_0x5e5b80[_0xbb6f('0x4b')]['id']}})[_0xbb6f('0x28')](handleEntityNotFound(_0x160723,null))['then'](function(_0x48ba05){if(_0x48ba05){return _0x48ba05[_0xbb6f('0x54')](_0x5e5b80[_0xbb6f('0x4a')][_0xbb6f('0x4e')],_[_0xbb6f('0x56')](_0x5e5b80[_0xbb6f('0x4a')],['ids','id'])||{});}})[_0xbb6f('0x28')](respondWithResult(_0x160723,null))[_0xbb6f('0x43')](handleError(_0x160723,null));};exports[_0xbb6f('0x57')]=function(_0x39fc2b,_0x5e5842,_0xca2905){var _0x62716={};var _0x10c8bd={};var _0x597220;var _0x206047;return db[_0xbb6f('0x2e')]['findOne']({'where':{'id':_0x39fc2b[_0xbb6f('0x4b')]['id']}})[_0xbb6f('0x28')](handleEntityNotFound(_0x5e5842,null))['then'](function(_0x49cc26){if(_0x49cc26){_0x597220=_0x49cc26;_0x10c8bd['model']=_[_0xbb6f('0x34')](db[_0xbb6f('0x55')][_0xbb6f('0x45')]);_0x10c8bd[_0xbb6f('0x33')]=_[_0xbb6f('0x34')](_0x39fc2b[_0xbb6f('0x33')]);_0x10c8bd[_0xbb6f('0x35')]=_[_0xbb6f('0x36')](_0x10c8bd[_0xbb6f('0x31')],_0x10c8bd[_0xbb6f('0x33')]);_0x62716[_0xbb6f('0x38')]=_[_0xbb6f('0x36')](_0x10c8bd['model'],qs['fields'](_0x39fc2b[_0xbb6f('0x33')]['fields']));_0x62716[_0xbb6f('0x38')]=_0x62716['attributes'][_0xbb6f('0x51')]?_0x62716[_0xbb6f('0x38')]:_0x10c8bd[_0xbb6f('0x31')];_0x62716[_0xbb6f('0x52')]=qs[_0xbb6f('0x3a')](_0x39fc2b[_0xbb6f('0x33')][_0xbb6f('0x3a')]);_0x62716[_0xbb6f('0x3d')]=qs['filters'](_[_0xbb6f('0x3b')](_0x39fc2b[_0xbb6f('0x33')],_0x10c8bd['filters']));if(_0x39fc2b[_0xbb6f('0x33')][_0xbb6f('0x3c')]){_0x62716[_0xbb6f('0x3d')]=_[_0xbb6f('0x3e')](_0x62716[_0xbb6f('0x3d')],{'$or':_[_0xbb6f('0x2d')](_0x62716[_0xbb6f('0x38')],function(_0x25661a){var _0x6a0869={};_0x6a0869[_0x25661a]={'$like':'%'+_0x39fc2b['query']['filter']+'%'};return _0x6a0869;})});}_0x62716=_[_0xbb6f('0x3e')]({},_0x62716,_0x39fc2b[_0xbb6f('0x40')]);return _0x597220['getActions'](_0x62716);}})[_0xbb6f('0x28')](function(_0x3d643d){if(_0x3d643d){_0x206047=_0x3d643d[_0xbb6f('0x51')];if(!_0x39fc2b[_0xbb6f('0x33')]['hasOwnProperty'](_0xbb6f('0x53'))){_0x62716['limit']=qs[_0xbb6f('0x20')](_0x39fc2b[_0xbb6f('0x33')][_0xbb6f('0x20')]);_0x62716[_0xbb6f('0x1e')]=qs['offset'](_0x39fc2b[_0xbb6f('0x33')]['offset']);}return _0x597220[_0xbb6f('0x57')](_0x62716);}})[_0xbb6f('0x28')](function(_0x48fe67){if(_0x48fe67){return _0x48fe67?{'count':_0x206047,'rows':_0x48fe67}:null;}})[_0xbb6f('0x28')](respondWithResult(_0x5e5842,null))[_0xbb6f('0x43')](handleError(_0x5e5842,null));};