4a6097166dbd8356da9f1a7df3df026bda861006
[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 _0x0bcf=['key','model','filters','intersection','fields','query','attributes','length','hasOwnProperty','nolimit','order','pick','filter','where','merge','VIRTUAL','rows','catch','show','params','keys','rawAttributes','includeAll','include','options','find','create','body','addConditions','Condition','ids','findOne','sort','getConditions','addActions','Action','omit','getActions','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','Content-Range','apply','reject','update','then','destroy','error','stack','name','send','index','map','Automation','fieldName','type'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x0bcf,0x18a));var _0xf0bc=function(_0x280d71,_0xe818ec){_0x280d71=_0x280d71-0x0;var _0x4eec09=_0x0bcf[_0x280d71];return _0x4eec09;};'use strict';var emlformat=require(_0xf0bc('0x0'));var rimraf=require(_0xf0bc('0x1'));var zipdir=require(_0xf0bc('0x2'));var jsonpatch=require(_0xf0bc('0x3'));var rp=require(_0xf0bc('0x4'));var moment=require(_0xf0bc('0x5'));var BPromise=require('bluebird');var Mustache=require(_0xf0bc('0x6'));var util=require('util');var path=require(_0xf0bc('0x7'));var sox=require(_0xf0bc('0x8'));var csv=require('to-csv');var ejs=require(_0xf0bc('0x9'));var fs=require('fs');var fs_extra=require(_0xf0bc('0xa'));var _=require(_0xf0bc('0xb'));var squel=require(_0xf0bc('0xc'));var crypto=require(_0xf0bc('0xd'));var jsforce=require(_0xf0bc('0xe'));var deskjs=require(_0xf0bc('0xf'));var toCsv=require(_0xf0bc('0x10'));var querystring=require(_0xf0bc('0x11'));var Papa=require(_0xf0bc('0x12'));var Redis=require(_0xf0bc('0x13'));var authService=require(_0xf0bc('0x14'));var qs=require(_0xf0bc('0x15'));var as=require(_0xf0bc('0x16'));var hardwareService=require(_0xf0bc('0x17'));var logger=require(_0xf0bc('0x18'))('api');var utils=require(_0xf0bc('0x19'));var config=require(_0xf0bc('0x1a'));var licenseUtil=require('../../config/license/util');var db=require(_0xf0bc('0x1b'))['db'];function respondWithStatusCode(_0x1dfb13,_0x5cfa67){_0x5cfa67=_0x5cfa67||0xcc;return function(_0x58fdb2){if(_0x58fdb2){return _0x1dfb13[_0xf0bc('0x1c')](_0x5cfa67);}return _0x1dfb13[_0xf0bc('0x1d')](_0x5cfa67)[_0xf0bc('0x1e')]();};}function respondWithResult(_0x42bef6,_0x64f5aa){_0x64f5aa=_0x64f5aa||0xc8;return function(_0x37e74a){if(_0x37e74a){return _0x42bef6[_0xf0bc('0x1d')](_0x64f5aa)[_0xf0bc('0x1f')](_0x37e74a);}};}function respondWithFilteredResult(_0x3ae80a,_0x33ba71){return function(_0x23b329){if(_0x23b329){var _0x40664f=typeof _0x33ba71[_0xf0bc('0x20')]===_0xf0bc('0x21')&&typeof _0x33ba71[_0xf0bc('0x22')]===_0xf0bc('0x21');var _0x240a0f=_0x23b329[_0xf0bc('0x23')];var _0x25c6ff=_0x40664f?0x0:_0x33ba71[_0xf0bc('0x20')];var _0xc26eb9=_0x40664f?_0x23b329[_0xf0bc('0x23')]:_0x33ba71['offset']+_0x33ba71[_0xf0bc('0x22')];var _0x2b3649;if(_0xc26eb9>=_0x240a0f){_0xc26eb9=_0x240a0f;_0x2b3649=0xc8;}else{_0x2b3649=0xce;}_0x3ae80a[_0xf0bc('0x1d')](_0x2b3649);return _0x3ae80a['set'](_0xf0bc('0x24'),_0x25c6ff+'-'+_0xc26eb9+'/'+_0x240a0f)[_0xf0bc('0x1f')](_0x23b329);}return null;};}function patchUpdates(_0x263c5a){return function(_0x175af2){try{jsonpatch[_0xf0bc('0x25')](_0x175af2,_0x263c5a,!![]);}catch(_0xa2ce13){return BPromise[_0xf0bc('0x26')](_0xa2ce13);}return _0x175af2['save']();};}function saveUpdates(_0x49adc9,_0x551d31){return function(_0x3ab72b){if(_0x3ab72b){return _0x3ab72b[_0xf0bc('0x27')](_0x49adc9)[_0xf0bc('0x28')](function(_0x202ee1){return _0x202ee1;});}return null;};}function removeEntity(_0x154577,_0x273e52){return function(_0x379f44){if(_0x379f44){return _0x379f44[_0xf0bc('0x29')]()['then'](function(){_0x154577[_0xf0bc('0x1d')](0xcc)[_0xf0bc('0x1e')]();});}};}function handleEntityNotFound(_0x3a04a,_0x58434f){return function(_0x3e2937){if(!_0x3e2937){_0x3a04a[_0xf0bc('0x1c')](0x194);}return _0x3e2937;};}function handleError(_0x136353,_0x4c139){_0x4c139=_0x4c139||0x1f4;return function(_0x1e7568){logger[_0xf0bc('0x2a')](_0x1e7568[_0xf0bc('0x2b')]);if(_0x1e7568['name']){delete _0x1e7568[_0xf0bc('0x2c')];}_0x136353['status'](_0x4c139)[_0xf0bc('0x2d')](_0x1e7568);};}exports[_0xf0bc('0x2e')]=function(_0x15ae6b,_0x3699c3){var _0x1ceaa7={},_0x1ca2e4={},_0x3c6cd9={'count':0x0,'rows':[]};var _0x55f02a=_[_0xf0bc('0x2f')](db[_0xf0bc('0x30')]['rawAttributes'],function(_0x3e6ab8){return{'name':_0x3e6ab8[_0xf0bc('0x31')],'type':_0x3e6ab8[_0xf0bc('0x32')][_0xf0bc('0x33')]};});_0x1ca2e4[_0xf0bc('0x34')]=_[_0xf0bc('0x2f')](_0x55f02a,_0xf0bc('0x2c'));_0x1ca2e4['query']=_['keys'](_0x15ae6b['query']);_0x1ca2e4[_0xf0bc('0x35')]=_['intersection'](_0x1ca2e4[_0xf0bc('0x34')],_0x1ca2e4['query']);_0x1ceaa7['attributes']=_[_0xf0bc('0x36')](_0x1ca2e4[_0xf0bc('0x34')],qs[_0xf0bc('0x37')](_0x15ae6b[_0xf0bc('0x38')]['fields']));_0x1ceaa7[_0xf0bc('0x39')]=_0x1ceaa7[_0xf0bc('0x39')][_0xf0bc('0x3a')]?_0x1ceaa7[_0xf0bc('0x39')]:_0x1ca2e4[_0xf0bc('0x34')];if(!_0x15ae6b['query'][_0xf0bc('0x3b')](_0xf0bc('0x3c'))){_0x1ceaa7['limit']=qs['limit'](_0x15ae6b[_0xf0bc('0x38')][_0xf0bc('0x22')]);_0x1ceaa7[_0xf0bc('0x20')]=qs[_0xf0bc('0x20')](_0x15ae6b[_0xf0bc('0x38')]['offset']);}_0x1ceaa7[_0xf0bc('0x3d')]=qs['sort'](_0x15ae6b[_0xf0bc('0x38')]['sort']);_0x1ceaa7['where']=qs[_0xf0bc('0x35')](_[_0xf0bc('0x3e')](_0x15ae6b[_0xf0bc('0x38')],_0x1ca2e4['filters']),_0x55f02a);if(_0x15ae6b[_0xf0bc('0x38')][_0xf0bc('0x3f')]){_0x1ceaa7[_0xf0bc('0x40')]=_[_0xf0bc('0x41')](_0x1ceaa7[_0xf0bc('0x40')],{'$or':_[_0xf0bc('0x2f')](_0x55f02a,function(_0x13887a){if(_0x13887a[_0xf0bc('0x32')]!==_0xf0bc('0x42')){var _0x4b6b0b={};_0x4b6b0b[_0x13887a[_0xf0bc('0x2c')]]={'$like':'%'+_0x15ae6b[_0xf0bc('0x38')][_0xf0bc('0x3f')]+'%'};return _0x4b6b0b;}})});}_0x1ceaa7=_['merge']({},_0x1ceaa7,_0x15ae6b['options']);var _0x5e76d0={'where':_0x1ceaa7[_0xf0bc('0x40')]};return db[_0xf0bc('0x30')][_0xf0bc('0x23')](_0x5e76d0)[_0xf0bc('0x28')](function(_0x4573b3){_0x3c6cd9[_0xf0bc('0x23')]=_0x4573b3;if(_0x15ae6b[_0xf0bc('0x38')]['includeAll']){_0x1ceaa7['include']=[{'all':!![]}];}return db[_0xf0bc('0x30')]['findAll'](_0x1ceaa7);})[_0xf0bc('0x28')](function(_0x579e36){_0x3c6cd9[_0xf0bc('0x43')]=_0x579e36;return _0x3c6cd9;})[_0xf0bc('0x28')](respondWithFilteredResult(_0x3699c3,_0x1ceaa7))[_0xf0bc('0x44')](handleError(_0x3699c3,null));};exports[_0xf0bc('0x45')]=function(_0x4c0d8c,_0x4e404e){var _0xfbec9={'raw':![],'where':{'id':_0x4c0d8c[_0xf0bc('0x46')]['id']}},_0x44b2d1={};_0x44b2d1[_0xf0bc('0x34')]=_[_0xf0bc('0x47')](db['Automation'][_0xf0bc('0x48')]);_0x44b2d1[_0xf0bc('0x38')]=_[_0xf0bc('0x47')](_0x4c0d8c[_0xf0bc('0x38')]);_0x44b2d1[_0xf0bc('0x35')]=_[_0xf0bc('0x36')](_0x44b2d1[_0xf0bc('0x34')],_0x44b2d1[_0xf0bc('0x38')]);_0xfbec9[_0xf0bc('0x39')]=_[_0xf0bc('0x36')](_0x44b2d1[_0xf0bc('0x34')],qs[_0xf0bc('0x37')](_0x4c0d8c[_0xf0bc('0x38')][_0xf0bc('0x37')]));_0xfbec9[_0xf0bc('0x39')]=_0xfbec9[_0xf0bc('0x39')]['length']?_0xfbec9['attributes']:_0x44b2d1['model'];if(_0x4c0d8c[_0xf0bc('0x38')][_0xf0bc('0x49')]){_0xfbec9[_0xf0bc('0x4a')]=[{'all':!![]}];}_0xfbec9=_[_0xf0bc('0x41')]({},_0xfbec9,_0x4c0d8c[_0xf0bc('0x4b')]);return db[_0xf0bc('0x30')][_0xf0bc('0x4c')](_0xfbec9)[_0xf0bc('0x28')](handleEntityNotFound(_0x4e404e,null))[_0xf0bc('0x28')](respondWithResult(_0x4e404e,null))[_0xf0bc('0x44')](handleError(_0x4e404e,null));};exports[_0xf0bc('0x4d')]=function(_0x584277,_0x26d9e1){return db[_0xf0bc('0x30')][_0xf0bc('0x4d')](_0x584277[_0xf0bc('0x4e')],{})[_0xf0bc('0x28')](respondWithResult(_0x26d9e1,0xc9))[_0xf0bc('0x44')](handleError(_0x26d9e1,null));};exports[_0xf0bc('0x27')]=function(_0x3dcc3e,_0x4b76b5){if(_0x3dcc3e[_0xf0bc('0x4e')]['id']){delete _0x3dcc3e[_0xf0bc('0x4e')]['id'];}return db[_0xf0bc('0x30')]['find']({'where':{'id':_0x3dcc3e[_0xf0bc('0x46')]['id']}})[_0xf0bc('0x28')](handleEntityNotFound(_0x4b76b5,null))[_0xf0bc('0x28')](saveUpdates(_0x3dcc3e[_0xf0bc('0x4e')],null))[_0xf0bc('0x28')](respondWithResult(_0x4b76b5,null))[_0xf0bc('0x44')](handleError(_0x4b76b5,null));};exports[_0xf0bc('0x29')]=function(_0x47f9cf,_0x23f03e){return db[_0xf0bc('0x30')]['find']({'where':{'id':_0x47f9cf[_0xf0bc('0x46')]['id']}})[_0xf0bc('0x28')](handleEntityNotFound(_0x23f03e,null))[_0xf0bc('0x28')](removeEntity(_0x23f03e,null))[_0xf0bc('0x44')](handleError(_0x23f03e,null));};exports[_0xf0bc('0x4f')]=function(_0x3a4d63,_0x331083,_0x1177b5){return db[_0xf0bc('0x50')][_0xf0bc('0x4c')]({'where':{'id':_0x3a4d63['params']['id']}})[_0xf0bc('0x28')](handleEntityNotFound(_0x331083,null))[_0xf0bc('0x28')](function(_0x42c6c0){if(_0x42c6c0){return _0x42c6c0[_0xf0bc('0x4f')](_0x3a4d63[_0xf0bc('0x4e')][_0xf0bc('0x51')],_['omit'](_0x3a4d63['body'],[_0xf0bc('0x51'),'id'])||{});}})[_0xf0bc('0x28')](respondWithResult(_0x331083,null))[_0xf0bc('0x44')](handleError(_0x331083,null));};exports['getConditions']=function(_0x2b0595,_0x490393,_0x30eefc){var _0x5c0bb5={};var _0x3478e5={};var _0xd65b35;var _0x13ecaf;return db['Automation'][_0xf0bc('0x52')]({'where':{'id':_0x2b0595[_0xf0bc('0x46')]['id']}})[_0xf0bc('0x28')](handleEntityNotFound(_0x490393,null))[_0xf0bc('0x28')](function(_0x1f8d1c){if(_0x1f8d1c){_0xd65b35=_0x1f8d1c;_0x3478e5[_0xf0bc('0x34')]=_['keys'](db[_0xf0bc('0x50')][_0xf0bc('0x48')]);_0x3478e5['query']=_[_0xf0bc('0x47')](_0x2b0595[_0xf0bc('0x38')]);_0x3478e5[_0xf0bc('0x35')]=_[_0xf0bc('0x36')](_0x3478e5[_0xf0bc('0x34')],_0x3478e5[_0xf0bc('0x38')]);_0x5c0bb5['attributes']=_[_0xf0bc('0x36')](_0x3478e5['model'],qs[_0xf0bc('0x37')](_0x2b0595['query'][_0xf0bc('0x37')]));_0x5c0bb5[_0xf0bc('0x39')]=_0x5c0bb5['attributes']['length']?_0x5c0bb5[_0xf0bc('0x39')]:_0x3478e5[_0xf0bc('0x34')];_0x5c0bb5[_0xf0bc('0x3d')]=qs[_0xf0bc('0x53')](_0x2b0595[_0xf0bc('0x38')][_0xf0bc('0x53')]);_0x5c0bb5[_0xf0bc('0x40')]=qs[_0xf0bc('0x35')](_[_0xf0bc('0x3e')](_0x2b0595[_0xf0bc('0x38')],_0x3478e5['filters']));if(_0x2b0595[_0xf0bc('0x38')][_0xf0bc('0x3f')]){_0x5c0bb5[_0xf0bc('0x40')]=_[_0xf0bc('0x41')](_0x5c0bb5[_0xf0bc('0x40')],{'$or':_[_0xf0bc('0x2f')](_0x5c0bb5['attributes'],function(_0x47cd49){var _0x32a78a={};_0x32a78a[_0x47cd49]={'$like':'%'+_0x2b0595[_0xf0bc('0x38')][_0xf0bc('0x3f')]+'%'};return _0x32a78a;})});}_0x5c0bb5=_[_0xf0bc('0x41')]({},_0x5c0bb5,_0x2b0595[_0xf0bc('0x4b')]);return _0xd65b35[_0xf0bc('0x54')](_0x5c0bb5);}})['then'](function(_0x1fb89e){if(_0x1fb89e){_0x13ecaf=_0x1fb89e[_0xf0bc('0x3a')];if(!_0x2b0595['query'][_0xf0bc('0x3b')]('nolimit')){_0x5c0bb5[_0xf0bc('0x22')]=qs[_0xf0bc('0x22')](_0x2b0595[_0xf0bc('0x38')][_0xf0bc('0x22')]);_0x5c0bb5[_0xf0bc('0x20')]=qs['offset'](_0x2b0595['query'][_0xf0bc('0x20')]);}return _0xd65b35['getConditions'](_0x5c0bb5);}})[_0xf0bc('0x28')](function(_0x1e9de7){if(_0x1e9de7){return _0x1e9de7?{'count':_0x13ecaf,'rows':_0x1e9de7}:null;}})[_0xf0bc('0x28')](respondWithResult(_0x490393,null))[_0xf0bc('0x44')](handleError(_0x490393,null));};exports[_0xf0bc('0x55')]=function(_0x3e2608,_0x17be54,_0xe6d1da){return db[_0xf0bc('0x56')][_0xf0bc('0x4c')]({'where':{'id':_0x3e2608[_0xf0bc('0x46')]['id']}})[_0xf0bc('0x28')](handleEntityNotFound(_0x17be54,null))[_0xf0bc('0x28')](function(_0x23ec21){if(_0x23ec21){return _0x23ec21[_0xf0bc('0x55')](_0x3e2608[_0xf0bc('0x4e')][_0xf0bc('0x51')],_[_0xf0bc('0x57')](_0x3e2608['body'],[_0xf0bc('0x51'),'id'])||{});}})[_0xf0bc('0x28')](respondWithResult(_0x17be54,null))['catch'](handleError(_0x17be54,null));};exports['getActions']=function(_0x125303,_0x3c44e9,_0x6d3257){var _0x86ad2c={};var _0x406343={};var _0x21b7f2;var _0x3cc1f2;return db[_0xf0bc('0x30')]['findOne']({'where':{'id':_0x125303['params']['id']}})[_0xf0bc('0x28')](handleEntityNotFound(_0x3c44e9,null))[_0xf0bc('0x28')](function(_0x3ffdc2){if(_0x3ffdc2){_0x21b7f2=_0x3ffdc2;_0x406343[_0xf0bc('0x34')]=_['keys'](db[_0xf0bc('0x56')][_0xf0bc('0x48')]);_0x406343[_0xf0bc('0x38')]=_['keys'](_0x125303[_0xf0bc('0x38')]);_0x406343[_0xf0bc('0x35')]=_[_0xf0bc('0x36')](_0x406343[_0xf0bc('0x34')],_0x406343[_0xf0bc('0x38')]);_0x86ad2c[_0xf0bc('0x39')]=_[_0xf0bc('0x36')](_0x406343['model'],qs[_0xf0bc('0x37')](_0x125303[_0xf0bc('0x38')][_0xf0bc('0x37')]));_0x86ad2c['attributes']=_0x86ad2c[_0xf0bc('0x39')]['length']?_0x86ad2c[_0xf0bc('0x39')]:_0x406343[_0xf0bc('0x34')];_0x86ad2c[_0xf0bc('0x3d')]=qs[_0xf0bc('0x53')](_0x125303[_0xf0bc('0x38')][_0xf0bc('0x53')]);_0x86ad2c['where']=qs['filters'](_['pick'](_0x125303['query'],_0x406343[_0xf0bc('0x35')]));if(_0x125303[_0xf0bc('0x38')][_0xf0bc('0x3f')]){_0x86ad2c['where']=_[_0xf0bc('0x41')](_0x86ad2c[_0xf0bc('0x40')],{'$or':_[_0xf0bc('0x2f')](_0x86ad2c[_0xf0bc('0x39')],function(_0xcb1ebf){var _0x625d1c={};_0x625d1c[_0xcb1ebf]={'$like':'%'+_0x125303['query'][_0xf0bc('0x3f')]+'%'};return _0x625d1c;})});}_0x86ad2c=_[_0xf0bc('0x41')]({},_0x86ad2c,_0x125303['options']);return _0x21b7f2[_0xf0bc('0x58')](_0x86ad2c);}})['then'](function(_0x153d88){if(_0x153d88){_0x3cc1f2=_0x153d88[_0xf0bc('0x3a')];if(!_0x125303[_0xf0bc('0x38')][_0xf0bc('0x3b')](_0xf0bc('0x3c'))){_0x86ad2c['limit']=qs[_0xf0bc('0x22')](_0x125303[_0xf0bc('0x38')]['limit']);_0x86ad2c['offset']=qs[_0xf0bc('0x20')](_0x125303[_0xf0bc('0x38')][_0xf0bc('0x20')]);}return _0x21b7f2['getActions'](_0x86ad2c);}})['then'](function(_0x5b2499){if(_0x5b2499){return _0x5b2499?{'count':_0x3cc1f2,'rows':_0x5b2499}:null;}})[_0xf0bc('0x28')](respondWithResult(_0x3c44e9,null))[_0xf0bc('0x44')](handleError(_0x3c44e9,null));};