Built motion from commit 9d0783e6.|2.6.13
[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 _0x17cd=['Automation','rawAttributes','fieldName','key','model','query','filters','intersection','attributes','fields','length','nolimit','order','sort','where','pick','merge','type','VIRTUAL','filter','options','includeAll','include','catch','show','params','keys','find','create','body','addConditions','ids','getConditions','findOne','hasOwnProperty','addActions','omit','getActions','Action','eml-format','rimraf','request-promise','moment','bluebird','util','path','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','count','limit','set','apply','save','update','destroy','then','name','index','map'];(function(_0x478414,_0xcf142d){var _0x5eb6b9=function(_0x10e6fe){while(--_0x10e6fe){_0x478414['push'](_0x478414['shift']());}};_0x5eb6b9(++_0xcf142d);}(_0x17cd,0x16b));var _0xd17c=function(_0x56482c,_0x16118d){_0x56482c=_0x56482c-0x0;var _0x5aae7a=_0x17cd[_0x56482c];return _0x5aae7a;};'use strict';var emlformat=require(_0xd17c('0x0'));var rimraf=require(_0xd17c('0x1'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0xd17c('0x2'));var moment=require(_0xd17c('0x3'));var BPromise=require(_0xd17c('0x4'));var Mustache=require('mustache');var util=require(_0xd17c('0x5'));var path=require(_0xd17c('0x6'));var sox=require('sox');var csv=require(_0xd17c('0x7'));var ejs=require(_0xd17c('0x8'));var fs=require('fs');var fs_extra=require(_0xd17c('0x9'));var _=require(_0xd17c('0xa'));var squel=require(_0xd17c('0xb'));var crypto=require(_0xd17c('0xc'));var jsforce=require(_0xd17c('0xd'));var deskjs=require('desk.js');var toCsv=require(_0xd17c('0x7'));var querystring=require(_0xd17c('0xe'));var Papa=require(_0xd17c('0xf'));var Redis=require(_0xd17c('0x10'));var authService=require(_0xd17c('0x11'));var qs=require(_0xd17c('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xd17c('0x13'));var logger=require(_0xd17c('0x14'))('api');var utils=require(_0xd17c('0x15'));var config=require(_0xd17c('0x16'));var licenseUtil=require(_0xd17c('0x17'));var db=require(_0xd17c('0x18'))['db'];function respondWithStatusCode(_0xc2271b,_0x3316be){_0x3316be=_0x3316be||0xcc;return function(_0x15642b){if(_0x15642b){return _0xc2271b[_0xd17c('0x19')](_0x3316be);}return _0xc2271b[_0xd17c('0x1a')](_0x3316be)[_0xd17c('0x1b')]();};}function respondWithResult(_0xd64a7c,_0x4cd737){_0x4cd737=_0x4cd737||0xc8;return function(_0x5814de){if(_0x5814de){return _0xd64a7c[_0xd17c('0x1a')](_0x4cd737)[_0xd17c('0x1c')](_0x5814de);}};}function respondWithFilteredResult(_0x4bcf67,_0x146e74){return function(_0x32dc7e){if(_0x32dc7e){var _0x25698c=typeof _0x146e74[_0xd17c('0x1d')]===_0xd17c('0x1e')&&typeof _0x146e74['limit']===_0xd17c('0x1e');var _0x5e5880=_0x32dc7e[_0xd17c('0x1f')];var _0x8121e8=_0x25698c?0x0:_0x146e74[_0xd17c('0x1d')];var _0xfb0519=_0x25698c?_0x32dc7e[_0xd17c('0x1f')]:_0x146e74[_0xd17c('0x1d')]+_0x146e74[_0xd17c('0x20')];var _0x4306f9;if(_0xfb0519>=_0x5e5880){_0xfb0519=_0x5e5880;_0x4306f9=0xc8;}else{_0x4306f9=0xce;}_0x4bcf67['status'](_0x4306f9);return _0x4bcf67[_0xd17c('0x21')]('Content-Range',_0x8121e8+'-'+_0xfb0519+'/'+_0x5e5880)['json'](_0x32dc7e);}return null;};}function patchUpdates(_0x4ecd27){return function(_0x160f9c){try{jsonpatch[_0xd17c('0x22')](_0x160f9c,_0x4ecd27,!![]);}catch(_0x149640){return BPromise['reject'](_0x149640);}return _0x160f9c[_0xd17c('0x23')]();};}function saveUpdates(_0x162155,_0x62efc0){return function(_0x34f677){if(_0x34f677){return _0x34f677[_0xd17c('0x24')](_0x162155)['then'](function(_0x125926){return _0x125926;});}return null;};}function removeEntity(_0x5763d0,_0x2c66b4){return function(_0x392ae9){if(_0x392ae9){return _0x392ae9[_0xd17c('0x25')]()[_0xd17c('0x26')](function(){_0x5763d0[_0xd17c('0x1a')](0xcc)[_0xd17c('0x1b')]();});}};}function handleEntityNotFound(_0x3f0e3f,_0x7830c9){return function(_0x2d3734){if(!_0x2d3734){_0x3f0e3f['sendStatus'](0x194);}return _0x2d3734;};}function handleError(_0x504252,_0x28c532){_0x28c532=_0x28c532||0x1f4;return function(_0x2c3983){logger['error'](_0x2c3983['stack']);if(_0x2c3983[_0xd17c('0x27')]){delete _0x2c3983['name'];}_0x504252[_0xd17c('0x1a')](_0x28c532)['send'](_0x2c3983);};}exports[_0xd17c('0x28')]=function(_0x5b6047,_0x56083c){var _0x45605b={},_0x5e7e89={},_0x23c109={'count':0x0,'rows':[]};var _0x27e2b3=_[_0xd17c('0x29')](db[_0xd17c('0x2a')][_0xd17c('0x2b')],function(_0x4be1f8){return{'name':_0x4be1f8[_0xd17c('0x2c')],'type':_0x4be1f8['type'][_0xd17c('0x2d')]};});_0x5e7e89[_0xd17c('0x2e')]=_[_0xd17c('0x29')](_0x27e2b3,_0xd17c('0x27'));_0x5e7e89[_0xd17c('0x2f')]=_['keys'](_0x5b6047['query']);_0x5e7e89[_0xd17c('0x30')]=_[_0xd17c('0x31')](_0x5e7e89[_0xd17c('0x2e')],_0x5e7e89[_0xd17c('0x2f')]);_0x45605b[_0xd17c('0x32')]=_[_0xd17c('0x31')](_0x5e7e89['model'],qs['fields'](_0x5b6047[_0xd17c('0x2f')][_0xd17c('0x33')]));_0x45605b[_0xd17c('0x32')]=_0x45605b['attributes'][_0xd17c('0x34')]?_0x45605b[_0xd17c('0x32')]:_0x5e7e89[_0xd17c('0x2e')];if(!_0x5b6047[_0xd17c('0x2f')]['hasOwnProperty'](_0xd17c('0x35'))){_0x45605b['limit']=qs[_0xd17c('0x20')](_0x5b6047['query'][_0xd17c('0x20')]);_0x45605b['offset']=qs[_0xd17c('0x1d')](_0x5b6047[_0xd17c('0x2f')][_0xd17c('0x1d')]);}_0x45605b[_0xd17c('0x36')]=qs[_0xd17c('0x37')](_0x5b6047[_0xd17c('0x2f')]['sort']);_0x45605b[_0xd17c('0x38')]=qs['filters'](_[_0xd17c('0x39')](_0x5b6047[_0xd17c('0x2f')],_0x5e7e89[_0xd17c('0x30')]),_0x27e2b3);if(_0x5b6047['query']['filter']){_0x45605b[_0xd17c('0x38')]=_[_0xd17c('0x3a')](_0x45605b[_0xd17c('0x38')],{'$or':_[_0xd17c('0x29')](_0x27e2b3,function(_0x1f00da){if(_0x1f00da[_0xd17c('0x3b')]!==_0xd17c('0x3c')){var _0x27be42={};_0x27be42[_0x1f00da['name']]={'$like':'%'+_0x5b6047['query'][_0xd17c('0x3d')]+'%'};return _0x27be42;}})});}_0x45605b=_['merge']({},_0x45605b,_0x5b6047[_0xd17c('0x3e')]);var _0xe00c08={'where':_0x45605b[_0xd17c('0x38')]};return db[_0xd17c('0x2a')][_0xd17c('0x1f')](_0xe00c08)[_0xd17c('0x26')](function(_0x296177){_0x23c109[_0xd17c('0x1f')]=_0x296177;if(_0x5b6047[_0xd17c('0x2f')][_0xd17c('0x3f')]){_0x45605b[_0xd17c('0x40')]=[{'all':!![]}];}return db[_0xd17c('0x2a')]['findAll'](_0x45605b);})[_0xd17c('0x26')](function(_0x462734){_0x23c109['rows']=_0x462734;return _0x23c109;})['then'](respondWithFilteredResult(_0x56083c,_0x45605b))[_0xd17c('0x41')](handleError(_0x56083c,null));};exports[_0xd17c('0x42')]=function(_0x114a09,_0x589509){var _0x5126e1={'raw':![],'where':{'id':_0x114a09[_0xd17c('0x43')]['id']}},_0x2d13d4={};_0x2d13d4[_0xd17c('0x2e')]=_[_0xd17c('0x44')](db[_0xd17c('0x2a')]['rawAttributes']);_0x2d13d4[_0xd17c('0x2f')]=_[_0xd17c('0x44')](_0x114a09[_0xd17c('0x2f')]);_0x2d13d4[_0xd17c('0x30')]=_[_0xd17c('0x31')](_0x2d13d4[_0xd17c('0x2e')],_0x2d13d4[_0xd17c('0x2f')]);_0x5126e1[_0xd17c('0x32')]=_[_0xd17c('0x31')](_0x2d13d4[_0xd17c('0x2e')],qs['fields'](_0x114a09[_0xd17c('0x2f')][_0xd17c('0x33')]));_0x5126e1[_0xd17c('0x32')]=_0x5126e1[_0xd17c('0x32')]['length']?_0x5126e1[_0xd17c('0x32')]:_0x2d13d4[_0xd17c('0x2e')];if(_0x114a09[_0xd17c('0x2f')][_0xd17c('0x3f')]){_0x5126e1[_0xd17c('0x40')]=[{'all':!![]}];}_0x5126e1=_['merge']({},_0x5126e1,_0x114a09[_0xd17c('0x3e')]);return db[_0xd17c('0x2a')][_0xd17c('0x45')](_0x5126e1)['then'](handleEntityNotFound(_0x589509,null))['then'](respondWithResult(_0x589509,null))[_0xd17c('0x41')](handleError(_0x589509,null));};exports[_0xd17c('0x46')]=function(_0x2ea574,_0x3a2298){return db[_0xd17c('0x2a')][_0xd17c('0x46')](_0x2ea574[_0xd17c('0x47')],{})[_0xd17c('0x26')](respondWithResult(_0x3a2298,0xc9))[_0xd17c('0x41')](handleError(_0x3a2298,null));};exports[_0xd17c('0x24')]=function(_0x1e035b,_0x582cfc){if(_0x1e035b[_0xd17c('0x47')]['id']){delete _0x1e035b[_0xd17c('0x47')]['id'];}return db[_0xd17c('0x2a')][_0xd17c('0x45')]({'where':{'id':_0x1e035b['params']['id']}})[_0xd17c('0x26')](handleEntityNotFound(_0x582cfc,null))[_0xd17c('0x26')](saveUpdates(_0x1e035b[_0xd17c('0x47')],null))[_0xd17c('0x26')](respondWithResult(_0x582cfc,null))[_0xd17c('0x41')](handleError(_0x582cfc,null));};exports[_0xd17c('0x25')]=function(_0x5cddc5,_0x4e5a13){return db[_0xd17c('0x2a')]['find']({'where':{'id':_0x5cddc5[_0xd17c('0x43')]['id']}})['then'](handleEntityNotFound(_0x4e5a13,null))['then'](removeEntity(_0x4e5a13,null))[_0xd17c('0x41')](handleError(_0x4e5a13,null));};exports[_0xd17c('0x48')]=function(_0x48f093,_0x14a987,_0x23e0b1){return db['Condition'][_0xd17c('0x45')]({'where':{'id':_0x48f093[_0xd17c('0x43')]['id']}})[_0xd17c('0x26')](handleEntityNotFound(_0x14a987,null))['then'](function(_0x461f84){if(_0x461f84){return _0x461f84[_0xd17c('0x48')](_0x48f093[_0xd17c('0x47')][_0xd17c('0x49')],_['omit'](_0x48f093[_0xd17c('0x47')],[_0xd17c('0x49'),'id'])||{});}})[_0xd17c('0x26')](respondWithResult(_0x14a987,null))[_0xd17c('0x41')](handleError(_0x14a987,null));};exports[_0xd17c('0x4a')]=function(_0x2df753,_0x3a9f41,_0x5e21d3){var _0x42edc4={};var _0x18f3ca={};var _0x4d5cb2;var _0x2d8f38;return db[_0xd17c('0x2a')][_0xd17c('0x4b')]({'where':{'id':_0x2df753[_0xd17c('0x43')]['id']}})[_0xd17c('0x26')](handleEntityNotFound(_0x3a9f41,null))[_0xd17c('0x26')](function(_0x47200f){if(_0x47200f){_0x4d5cb2=_0x47200f;_0x18f3ca[_0xd17c('0x2e')]=_[_0xd17c('0x44')](db['Condition'][_0xd17c('0x2b')]);_0x18f3ca[_0xd17c('0x2f')]=_[_0xd17c('0x44')](_0x2df753[_0xd17c('0x2f')]);_0x18f3ca[_0xd17c('0x30')]=_['intersection'](_0x18f3ca[_0xd17c('0x2e')],_0x18f3ca[_0xd17c('0x2f')]);_0x42edc4[_0xd17c('0x32')]=_[_0xd17c('0x31')](_0x18f3ca['model'],qs[_0xd17c('0x33')](_0x2df753[_0xd17c('0x2f')][_0xd17c('0x33')]));_0x42edc4[_0xd17c('0x32')]=_0x42edc4[_0xd17c('0x32')][_0xd17c('0x34')]?_0x42edc4[_0xd17c('0x32')]:_0x18f3ca[_0xd17c('0x2e')];_0x42edc4[_0xd17c('0x36')]=qs['sort'](_0x2df753[_0xd17c('0x2f')][_0xd17c('0x37')]);_0x42edc4[_0xd17c('0x38')]=qs[_0xd17c('0x30')](_['pick'](_0x2df753[_0xd17c('0x2f')],_0x18f3ca['filters']));if(_0x2df753[_0xd17c('0x2f')][_0xd17c('0x3d')]){_0x42edc4[_0xd17c('0x38')]=_['merge'](_0x42edc4[_0xd17c('0x38')],{'$or':_[_0xd17c('0x29')](_0x42edc4[_0xd17c('0x32')],function(_0x211f77){var _0x3fdd1e={};_0x3fdd1e[_0x211f77]={'$like':'%'+_0x2df753['query'][_0xd17c('0x3d')]+'%'};return _0x3fdd1e;})});}_0x42edc4=_[_0xd17c('0x3a')]({},_0x42edc4,_0x2df753[_0xd17c('0x3e')]);return _0x4d5cb2[_0xd17c('0x4a')](_0x42edc4);}})[_0xd17c('0x26')](function(_0x201915){if(_0x201915){_0x2d8f38=_0x201915['length'];if(!_0x2df753['query'][_0xd17c('0x4c')](_0xd17c('0x35'))){_0x42edc4[_0xd17c('0x20')]=qs[_0xd17c('0x20')](_0x2df753[_0xd17c('0x2f')]['limit']);_0x42edc4[_0xd17c('0x1d')]=qs[_0xd17c('0x1d')](_0x2df753[_0xd17c('0x2f')][_0xd17c('0x1d')]);}return _0x4d5cb2[_0xd17c('0x4a')](_0x42edc4);}})[_0xd17c('0x26')](function(_0x33c1ae){if(_0x33c1ae){return _0x33c1ae?{'count':_0x2d8f38,'rows':_0x33c1ae}:null;}})[_0xd17c('0x26')](respondWithResult(_0x3a9f41,null))['catch'](handleError(_0x3a9f41,null));};exports[_0xd17c('0x4d')]=function(_0x55cc4c,_0x87b6b2,_0x5d99be){return db['Action'][_0xd17c('0x45')]({'where':{'id':_0x55cc4c[_0xd17c('0x43')]['id']}})[_0xd17c('0x26')](handleEntityNotFound(_0x87b6b2,null))['then'](function(_0x221676){if(_0x221676){return _0x221676[_0xd17c('0x4d')](_0x55cc4c['body'][_0xd17c('0x49')],_[_0xd17c('0x4e')](_0x55cc4c[_0xd17c('0x47')],['ids','id'])||{});}})['then'](respondWithResult(_0x87b6b2,null))[_0xd17c('0x41')](handleError(_0x87b6b2,null));};exports[_0xd17c('0x4f')]=function(_0x4dc3d4,_0xd7a66a,_0x57f535){var _0x22b8b2={};var _0x4c8678={};var _0x28e904;var _0x5d34e8;return db[_0xd17c('0x2a')]['findOne']({'where':{'id':_0x4dc3d4[_0xd17c('0x43')]['id']}})[_0xd17c('0x26')](handleEntityNotFound(_0xd7a66a,null))[_0xd17c('0x26')](function(_0x118436){if(_0x118436){_0x28e904=_0x118436;_0x4c8678[_0xd17c('0x2e')]=_['keys'](db[_0xd17c('0x50')][_0xd17c('0x2b')]);_0x4c8678['query']=_[_0xd17c('0x44')](_0x4dc3d4[_0xd17c('0x2f')]);_0x4c8678[_0xd17c('0x30')]=_[_0xd17c('0x31')](_0x4c8678[_0xd17c('0x2e')],_0x4c8678[_0xd17c('0x2f')]);_0x22b8b2['attributes']=_[_0xd17c('0x31')](_0x4c8678['model'],qs[_0xd17c('0x33')](_0x4dc3d4[_0xd17c('0x2f')][_0xd17c('0x33')]));_0x22b8b2[_0xd17c('0x32')]=_0x22b8b2['attributes']['length']?_0x22b8b2['attributes']:_0x4c8678['model'];_0x22b8b2[_0xd17c('0x36')]=qs[_0xd17c('0x37')](_0x4dc3d4['query'][_0xd17c('0x37')]);_0x22b8b2['where']=qs[_0xd17c('0x30')](_['pick'](_0x4dc3d4[_0xd17c('0x2f')],_0x4c8678['filters']));if(_0x4dc3d4[_0xd17c('0x2f')]['filter']){_0x22b8b2['where']=_[_0xd17c('0x3a')](_0x22b8b2[_0xd17c('0x38')],{'$or':_[_0xd17c('0x29')](_0x22b8b2[_0xd17c('0x32')],function(_0x8c700f){var _0x4451c4={};_0x4451c4[_0x8c700f]={'$like':'%'+_0x4dc3d4['query'][_0xd17c('0x3d')]+'%'};return _0x4451c4;})});}_0x22b8b2=_[_0xd17c('0x3a')]({},_0x22b8b2,_0x4dc3d4[_0xd17c('0x3e')]);return _0x28e904[_0xd17c('0x4f')](_0x22b8b2);}})[_0xd17c('0x26')](function(_0x4f8a2a){if(_0x4f8a2a){_0x5d34e8=_0x4f8a2a[_0xd17c('0x34')];if(!_0x4dc3d4[_0xd17c('0x2f')][_0xd17c('0x4c')](_0xd17c('0x35'))){_0x22b8b2[_0xd17c('0x20')]=qs['limit'](_0x4dc3d4[_0xd17c('0x2f')][_0xd17c('0x20')]);_0x22b8b2[_0xd17c('0x1d')]=qs['offset'](_0x4dc3d4[_0xd17c('0x2f')][_0xd17c('0x1d')]);}return _0x28e904[_0xd17c('0x4f')](_0x22b8b2);}})['then'](function(_0x360a62){if(_0x360a62){return _0x360a62?{'count':_0x5d34e8,'rows':_0x360a62}:null;}})[_0xd17c('0x26')](respondWithResult(_0xd7a66a,null))[_0xd17c('0x41')](handleError(_0xd7a66a,null));};