Built motion from commit c116dc8b.|2.6.30
[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 _0xb21f=['limit','set','Content-Range','reject','update','destroy','then','stack','name','send','index','Automation','rawAttributes','fieldName','type','map','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','include','findAll','rows','params','find','catch','create','body','Condition','addConditions','ids','omit','getConditions','addActions','Action','getActions','findOne','eml-format','rimraf','zip-dir','mustache','util','path','sox','to-csv','ejs','fs-extra','crypto','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','count'];(function(_0x3ab9ee,_0x4a74b8){var _0x82ee88=function(_0x6ccc7e){while(--_0x6ccc7e){_0x3ab9ee['push'](_0x3ab9ee['shift']());}};_0x82ee88(++_0x4a74b8);}(_0xb21f,0x173));var _0xfb21=function(_0x22663b,_0xc8c6e3){_0x22663b=_0x22663b-0x0;var _0x310d0a=_0xb21f[_0x22663b];return _0x310d0a;};'use strict';var emlformat=require(_0xfb21('0x0'));var rimraf=require(_0xfb21('0x1'));var zipdir=require(_0xfb21('0x2'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0xfb21('0x3'));var util=require(_0xfb21('0x4'));var path=require(_0xfb21('0x5'));var sox=require(_0xfb21('0x6'));var csv=require(_0xfb21('0x7'));var ejs=require(_0xfb21('0x8'));var fs=require('fs');var fs_extra=require(_0xfb21('0x9'));var _=require('lodash');var squel=require('squel');var crypto=require(_0xfb21('0xa'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0xfb21('0x7'));var querystring=require(_0xfb21('0xb'));var Papa=require(_0xfb21('0xc'));var Redis=require('ioredis');var authService=require(_0xfb21('0xd'));var qs=require(_0xfb21('0xe'));var as=require(_0xfb21('0xf'));var hardwareService=require(_0xfb21('0x10'));var logger=require('../../config/logger')(_0xfb21('0x11'));var utils=require(_0xfb21('0x12'));var config=require(_0xfb21('0x13'));var licenseUtil=require(_0xfb21('0x14'));var db=require(_0xfb21('0x15'))['db'];function respondWithStatusCode(_0x1301de,_0x552b5c){_0x552b5c=_0x552b5c||0xcc;return function(_0x43ed72){if(_0x43ed72){return _0x1301de[_0xfb21('0x16')](_0x552b5c);}return _0x1301de[_0xfb21('0x17')](_0x552b5c)[_0xfb21('0x18')]();};}function respondWithResult(_0x24730d,_0x552adb){_0x552adb=_0x552adb||0xc8;return function(_0x57ad25){if(_0x57ad25){return _0x24730d['status'](_0x552adb)[_0xfb21('0x19')](_0x57ad25);}};}function respondWithFilteredResult(_0x3aba51,_0x54ea23){return function(_0xca028b){if(_0xca028b){var _0x50b418=typeof _0x54ea23[_0xfb21('0x1a')]===_0xfb21('0x1b')&&typeof _0x54ea23['limit']===_0xfb21('0x1b');var _0x4fa4f2=_0xca028b[_0xfb21('0x1c')];var _0x36c98b=_0x50b418?0x0:_0x54ea23[_0xfb21('0x1a')];var _0x19bd2e=_0x50b418?_0xca028b[_0xfb21('0x1c')]:_0x54ea23[_0xfb21('0x1a')]+_0x54ea23[_0xfb21('0x1d')];var _0x1e9ce5;if(_0x19bd2e>=_0x4fa4f2){_0x19bd2e=_0x4fa4f2;_0x1e9ce5=0xc8;}else{_0x1e9ce5=0xce;}_0x3aba51[_0xfb21('0x17')](_0x1e9ce5);return _0x3aba51[_0xfb21('0x1e')](_0xfb21('0x1f'),_0x36c98b+'-'+_0x19bd2e+'/'+_0x4fa4f2)[_0xfb21('0x19')](_0xca028b);}return null;};}function patchUpdates(_0x3eb78e){return function(_0x33eca4){try{jsonpatch['apply'](_0x33eca4,_0x3eb78e,!![]);}catch(_0x21b638){return BPromise[_0xfb21('0x20')](_0x21b638);}return _0x33eca4['save']();};}function saveUpdates(_0x3f25aa,_0x1fc0c9){return function(_0x46b103){if(_0x46b103){return _0x46b103[_0xfb21('0x21')](_0x3f25aa)['then'](function(_0x286bd6){return _0x286bd6;});}return null;};}function removeEntity(_0x301751,_0x2975bf){return function(_0x1718f0){if(_0x1718f0){return _0x1718f0[_0xfb21('0x22')]()[_0xfb21('0x23')](function(){_0x301751['status'](0xcc)[_0xfb21('0x18')]();});}};}function handleEntityNotFound(_0x47df02,_0x538f6f){return function(_0x316bd1){if(!_0x316bd1){_0x47df02[_0xfb21('0x16')](0x194);}return _0x316bd1;};}function handleError(_0x3ead1e,_0x357591){_0x357591=_0x357591||0x1f4;return function(_0x273331){logger['error'](_0x273331[_0xfb21('0x24')]);if(_0x273331[_0xfb21('0x25')]){delete _0x273331[_0xfb21('0x25')];}_0x3ead1e[_0xfb21('0x17')](_0x357591)[_0xfb21('0x26')](_0x273331);};}exports[_0xfb21('0x27')]=function(_0x20fa7e,_0x1a390c){var _0x4247b3={},_0x111d21={},_0x5445ed={'count':0x0,'rows':[]};var _0x1b2f9e=_['map'](db[_0xfb21('0x28')][_0xfb21('0x29')],function(_0x2482d4){return{'name':_0x2482d4[_0xfb21('0x2a')],'type':_0x2482d4[_0xfb21('0x2b')]['key']};});_0x111d21['model']=_[_0xfb21('0x2c')](_0x1b2f9e,_0xfb21('0x25'));_0x111d21[_0xfb21('0x2d')]=_[_0xfb21('0x2e')](_0x20fa7e['query']);_0x111d21[_0xfb21('0x2f')]=_[_0xfb21('0x30')](_0x111d21[_0xfb21('0x31')],_0x111d21[_0xfb21('0x2d')]);_0x4247b3[_0xfb21('0x32')]=_[_0xfb21('0x30')](_0x111d21[_0xfb21('0x31')],qs[_0xfb21('0x33')](_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x33')]));_0x4247b3[_0xfb21('0x32')]=_0x4247b3[_0xfb21('0x32')][_0xfb21('0x34')]?_0x4247b3[_0xfb21('0x32')]:_0x111d21[_0xfb21('0x31')];if(!_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x35')](_0xfb21('0x36'))){_0x4247b3['limit']=qs[_0xfb21('0x1d')](_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x1d')]);_0x4247b3[_0xfb21('0x1a')]=qs['offset'](_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x1a')]);}_0x4247b3[_0xfb21('0x37')]=qs[_0xfb21('0x38')](_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x38')]);_0x4247b3[_0xfb21('0x39')]=qs[_0xfb21('0x2f')](_[_0xfb21('0x3a')](_0x20fa7e[_0xfb21('0x2d')],_0x111d21[_0xfb21('0x2f')]),_0x1b2f9e);if(_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x3b')]){_0x4247b3[_0xfb21('0x39')]=_[_0xfb21('0x3c')](_0x4247b3[_0xfb21('0x39')],{'$or':_[_0xfb21('0x2c')](_0x1b2f9e,function(_0x207ef3){if(_0x207ef3[_0xfb21('0x2b')]!==_0xfb21('0x3d')){var _0x1c0610={};_0x1c0610[_0x207ef3['name']]={'$like':'%'+_0x20fa7e[_0xfb21('0x2d')][_0xfb21('0x3b')]+'%'};return _0x1c0610;}})});}_0x4247b3=_['merge']({},_0x4247b3,_0x20fa7e[_0xfb21('0x3e')]);var _0x21d214={'where':_0x4247b3[_0xfb21('0x39')]};return db[_0xfb21('0x28')]['count'](_0x21d214)['then'](function(_0x219615){_0x5445ed[_0xfb21('0x1c')]=_0x219615;if(_0x20fa7e[_0xfb21('0x2d')]['includeAll']){_0x4247b3[_0xfb21('0x3f')]=[{'all':!![]}];}return db[_0xfb21('0x28')][_0xfb21('0x40')](_0x4247b3);})['then'](function(_0x538769){_0x5445ed[_0xfb21('0x41')]=_0x538769;return _0x5445ed;})[_0xfb21('0x23')](respondWithFilteredResult(_0x1a390c,_0x4247b3))['catch'](handleError(_0x1a390c,null));};exports['show']=function(_0x7bd3bf,_0x128f22){var _0x22f2fe={'raw':![],'where':{'id':_0x7bd3bf[_0xfb21('0x42')]['id']}},_0x59627b={};_0x59627b['model']=_[_0xfb21('0x2e')](db[_0xfb21('0x28')][_0xfb21('0x29')]);_0x59627b[_0xfb21('0x2d')]=_['keys'](_0x7bd3bf['query']);_0x59627b[_0xfb21('0x2f')]=_[_0xfb21('0x30')](_0x59627b['model'],_0x59627b[_0xfb21('0x2d')]);_0x22f2fe[_0xfb21('0x32')]=_[_0xfb21('0x30')](_0x59627b['model'],qs[_0xfb21('0x33')](_0x7bd3bf['query'][_0xfb21('0x33')]));_0x22f2fe['attributes']=_0x22f2fe[_0xfb21('0x32')][_0xfb21('0x34')]?_0x22f2fe['attributes']:_0x59627b[_0xfb21('0x31')];if(_0x7bd3bf['query']['includeAll']){_0x22f2fe[_0xfb21('0x3f')]=[{'all':!![]}];}_0x22f2fe=_['merge']({},_0x22f2fe,_0x7bd3bf[_0xfb21('0x3e')]);return db['Automation'][_0xfb21('0x43')](_0x22f2fe)[_0xfb21('0x23')](handleEntityNotFound(_0x128f22,null))['then'](respondWithResult(_0x128f22,null))[_0xfb21('0x44')](handleError(_0x128f22,null));};exports[_0xfb21('0x45')]=function(_0x12527b,_0x17f60f){return db[_0xfb21('0x28')]['create'](_0x12527b['body'],{})[_0xfb21('0x23')](respondWithResult(_0x17f60f,0xc9))['catch'](handleError(_0x17f60f,null));};exports[_0xfb21('0x21')]=function(_0x92ca87,_0xae25af){if(_0x92ca87[_0xfb21('0x46')]['id']){delete _0x92ca87['body']['id'];}return db[_0xfb21('0x28')][_0xfb21('0x43')]({'where':{'id':_0x92ca87[_0xfb21('0x42')]['id']}})[_0xfb21('0x23')](handleEntityNotFound(_0xae25af,null))['then'](saveUpdates(_0x92ca87[_0xfb21('0x46')],null))[_0xfb21('0x23')](respondWithResult(_0xae25af,null))[_0xfb21('0x44')](handleError(_0xae25af,null));};exports[_0xfb21('0x22')]=function(_0x2f66a7,_0x3ba243){return db[_0xfb21('0x28')][_0xfb21('0x43')]({'where':{'id':_0x2f66a7[_0xfb21('0x42')]['id']}})[_0xfb21('0x23')](handleEntityNotFound(_0x3ba243,null))[_0xfb21('0x23')](removeEntity(_0x3ba243,null))[_0xfb21('0x44')](handleError(_0x3ba243,null));};exports['addConditions']=function(_0xbc78e9,_0x5039c5,_0x39fef9){return db[_0xfb21('0x47')][_0xfb21('0x43')]({'where':{'id':_0xbc78e9[_0xfb21('0x42')]['id']}})[_0xfb21('0x23')](handleEntityNotFound(_0x5039c5,null))[_0xfb21('0x23')](function(_0x1edd10){if(_0x1edd10){return _0x1edd10[_0xfb21('0x48')](_0xbc78e9[_0xfb21('0x46')][_0xfb21('0x49')],_[_0xfb21('0x4a')](_0xbc78e9['body'],[_0xfb21('0x49'),'id'])||{});}})[_0xfb21('0x23')](respondWithResult(_0x5039c5,null))[_0xfb21('0x44')](handleError(_0x5039c5,null));};exports[_0xfb21('0x4b')]=function(_0x5f1ffc,_0x54065f,_0x869e6d){var _0x1a5896={};var _0x2c8b27={};var _0x3b40b3;var _0x5a5f12;return db[_0xfb21('0x28')]['findOne']({'where':{'id':_0x5f1ffc[_0xfb21('0x42')]['id']}})['then'](handleEntityNotFound(_0x54065f,null))[_0xfb21('0x23')](function(_0x19fb5e){if(_0x19fb5e){_0x3b40b3=_0x19fb5e;_0x2c8b27['model']=_[_0xfb21('0x2e')](db[_0xfb21('0x47')][_0xfb21('0x29')]);_0x2c8b27[_0xfb21('0x2d')]=_[_0xfb21('0x2e')](_0x5f1ffc['query']);_0x2c8b27['filters']=_[_0xfb21('0x30')](_0x2c8b27[_0xfb21('0x31')],_0x2c8b27[_0xfb21('0x2d')]);_0x1a5896[_0xfb21('0x32')]=_[_0xfb21('0x30')](_0x2c8b27['model'],qs[_0xfb21('0x33')](_0x5f1ffc[_0xfb21('0x2d')][_0xfb21('0x33')]));_0x1a5896[_0xfb21('0x32')]=_0x1a5896[_0xfb21('0x32')][_0xfb21('0x34')]?_0x1a5896[_0xfb21('0x32')]:_0x2c8b27[_0xfb21('0x31')];_0x1a5896['order']=qs['sort'](_0x5f1ffc[_0xfb21('0x2d')]['sort']);_0x1a5896['where']=qs[_0xfb21('0x2f')](_['pick'](_0x5f1ffc[_0xfb21('0x2d')],_0x2c8b27[_0xfb21('0x2f')]));if(_0x5f1ffc[_0xfb21('0x2d')][_0xfb21('0x3b')]){_0x1a5896[_0xfb21('0x39')]=_[_0xfb21('0x3c')](_0x1a5896[_0xfb21('0x39')],{'$or':_['map'](_0x1a5896['attributes'],function(_0x28c943){var _0x334a0d={};_0x334a0d[_0x28c943]={'$like':'%'+_0x5f1ffc[_0xfb21('0x2d')][_0xfb21('0x3b')]+'%'};return _0x334a0d;})});}_0x1a5896=_[_0xfb21('0x3c')]({},_0x1a5896,_0x5f1ffc[_0xfb21('0x3e')]);return _0x3b40b3[_0xfb21('0x4b')](_0x1a5896);}})[_0xfb21('0x23')](function(_0x1f377c){if(_0x1f377c){_0x5a5f12=_0x1f377c['length'];if(!_0x5f1ffc['query']['hasOwnProperty'](_0xfb21('0x36'))){_0x1a5896[_0xfb21('0x1d')]=qs[_0xfb21('0x1d')](_0x5f1ffc[_0xfb21('0x2d')]['limit']);_0x1a5896[_0xfb21('0x1a')]=qs[_0xfb21('0x1a')](_0x5f1ffc['query'][_0xfb21('0x1a')]);}return _0x3b40b3[_0xfb21('0x4b')](_0x1a5896);}})[_0xfb21('0x23')](function(_0x3f928e){if(_0x3f928e){return _0x3f928e?{'count':_0x5a5f12,'rows':_0x3f928e}:null;}})[_0xfb21('0x23')](respondWithResult(_0x54065f,null))[_0xfb21('0x44')](handleError(_0x54065f,null));};exports[_0xfb21('0x4c')]=function(_0x17b001,_0x39b2e8,_0x319506){return db[_0xfb21('0x4d')]['find']({'where':{'id':_0x17b001[_0xfb21('0x42')]['id']}})['then'](handleEntityNotFound(_0x39b2e8,null))['then'](function(_0x14b377){if(_0x14b377){return _0x14b377[_0xfb21('0x4c')](_0x17b001[_0xfb21('0x46')][_0xfb21('0x49')],_[_0xfb21('0x4a')](_0x17b001['body'],[_0xfb21('0x49'),'id'])||{});}})[_0xfb21('0x23')](respondWithResult(_0x39b2e8,null))['catch'](handleError(_0x39b2e8,null));};exports[_0xfb21('0x4e')]=function(_0x96713b,_0x2da405,_0x526b08){var _0x3a455d={};var _0x49c4aa={};var _0x2d971c;var _0x3cf98e;return db[_0xfb21('0x28')][_0xfb21('0x4f')]({'where':{'id':_0x96713b[_0xfb21('0x42')]['id']}})[_0xfb21('0x23')](handleEntityNotFound(_0x2da405,null))['then'](function(_0x1a9765){if(_0x1a9765){_0x2d971c=_0x1a9765;_0x49c4aa[_0xfb21('0x31')]=_[_0xfb21('0x2e')](db[_0xfb21('0x4d')][_0xfb21('0x29')]);_0x49c4aa[_0xfb21('0x2d')]=_[_0xfb21('0x2e')](_0x96713b[_0xfb21('0x2d')]);_0x49c4aa[_0xfb21('0x2f')]=_['intersection'](_0x49c4aa['model'],_0x49c4aa[_0xfb21('0x2d')]);_0x3a455d['attributes']=_[_0xfb21('0x30')](_0x49c4aa[_0xfb21('0x31')],qs[_0xfb21('0x33')](_0x96713b[_0xfb21('0x2d')]['fields']));_0x3a455d['attributes']=_0x3a455d['attributes'][_0xfb21('0x34')]?_0x3a455d[_0xfb21('0x32')]:_0x49c4aa[_0xfb21('0x31')];_0x3a455d[_0xfb21('0x37')]=qs[_0xfb21('0x38')](_0x96713b[_0xfb21('0x2d')]['sort']);_0x3a455d['where']=qs['filters'](_[_0xfb21('0x3a')](_0x96713b[_0xfb21('0x2d')],_0x49c4aa[_0xfb21('0x2f')]));if(_0x96713b[_0xfb21('0x2d')][_0xfb21('0x3b')]){_0x3a455d[_0xfb21('0x39')]=_['merge'](_0x3a455d[_0xfb21('0x39')],{'$or':_[_0xfb21('0x2c')](_0x3a455d['attributes'],function(_0x2a8eba){var _0x5bc3be={};_0x5bc3be[_0x2a8eba]={'$like':'%'+_0x96713b['query'][_0xfb21('0x3b')]+'%'};return _0x5bc3be;})});}_0x3a455d=_[_0xfb21('0x3c')]({},_0x3a455d,_0x96713b[_0xfb21('0x3e')]);return _0x2d971c[_0xfb21('0x4e')](_0x3a455d);}})[_0xfb21('0x23')](function(_0xdf447a){if(_0xdf447a){_0x3cf98e=_0xdf447a[_0xfb21('0x34')];if(!_0x96713b['query'][_0xfb21('0x35')](_0xfb21('0x36'))){_0x3a455d[_0xfb21('0x1d')]=qs[_0xfb21('0x1d')](_0x96713b[_0xfb21('0x2d')][_0xfb21('0x1d')]);_0x3a455d['offset']=qs[_0xfb21('0x1a')](_0x96713b[_0xfb21('0x2d')][_0xfb21('0x1a')]);}return _0x2d971c[_0xfb21('0x4e')](_0x3a455d);}})[_0xfb21('0x23')](function(_0x2b5235){if(_0x2b5235){return _0x2b5235?{'count':_0x3cf98e,'rows':_0x2b5235}:null;}})[_0xfb21('0x23')](respondWithResult(_0x2da405,null))[_0xfb21('0x44')](handleError(_0x2da405,null));};