Built motion from commit (unavailable).|2.5.8
[motion2.git] / server / services / routing / server / index.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 _0x4c2d=['split','push','dialogflow','slice','dialogflowv2','amazonlex','tag','TagId','./interval','condition','intervals','consequence','priority','info','\x20|\x20','error','rules','execute','Applications\x20executed','matchPath','emit','complete','No\x20applications\x20to\x20execute','stack','routing\x20is\x20empty','end','QueueReport','update','catch','error\x20update\x20queue\x20report','message\x20is\x20empty','agent','agents','busy','busyQueue','hasOwnProperty','capacity','agent\x20not\x20found','values','chatQueues','available','loggedIn','name','waiting','mailQueues','paused','openchannelQueues','smsQueues','faxQueues','whatsappQueues','chatQueuesWaitingInteractions','openchannelQueuesWaitingInteractions','smsQueuesWaitingInteractions','faxQueuesWaitingInteractions','whatsappQueuesWaitingInteractions','http','stringify','127.0.0.1','Routing\x20RPC\x20server\x20listening\x20on\x20127.0.0.1:%d!','RPC\x20listening\x20on\x20port:','lodash','util','jayson/promise','bluebird','uuid','../../../config/environment','../../../config/logger','../rpc/mailQueueReport','../rpc/chatQueueReport','../rpc/faxQueueReport','format','%s.%s.%s','IntervalId','isNil','Intervals','map','interval','listen','channel','message','interaction','isArray','applications','length','existsSync','join','app','.js','toLowerCase','autoreply','appdata'];(function(_0x11fc41,_0x2ae351){var _0x1edc14=function(_0x3e5443){while(--_0x3e5443){_0x11fc41['push'](_0x11fc41['shift']());}};_0x1edc14(++_0x2ae351);}(_0x4c2d,0x14d));var _0xd4c2=function(_0x2a2196,_0x257313){_0x2a2196=_0x2a2196-0x0;var _0xeb5a80=_0x4c2d[_0x2a2196];return _0xeb5a80;};'use strict';var _=require(_0xd4c2('0x0'));var util=require(_0xd4c2('0x1'));var path=require('path');var fs=require('fs');var jayson=require(_0xd4c2('0x2'));var BPromise=require(_0xd4c2('0x3'));var uuid=require(_0xd4c2('0x4'));var config=require(_0xd4c2('0x5'));var logger=require(_0xd4c2('0x6'))('routing');var rpc={'openchannelQueueReport':require('../rpc/openchannelQueueReport'),'mailQueueReport':require(_0xd4c2('0x7')),'chatQueueReport':require(_0xd4c2('0x8')),'smsQueueReport':require('../rpc/smsQueueReport'),'faxQueueReport':require(_0xd4c2('0x9')),'whatsappQueueReport':require('../rpc/whatsappQueueReport')};var RuleEngine=require('../lib/rule-engine');function getId(_0x2e1f09,_0x4111ae,_0x55880d){return util[_0xd4c2('0xa')](_0xd4c2('0xb'),_0x2e1f09,_0x4111ae||0x0,_0x55880d||0x0);}function getIntervals(_0x5b1805,_0x2e34b7){var _0x4899b2=[];if(!_['isNil'](_0x2e34b7[_0xd4c2('0xc')])&&!_['isNil'](_0x5b1805[_0x2e34b7[_0xd4c2('0xc')]])&&!_[_0xd4c2('0xd')](_0x5b1805[_0x2e34b7[_0xd4c2('0xc')]][_0xd4c2('0xe')])){_0x4899b2=_[_0xd4c2('0xf')](_0x5b1805[_0x2e34b7[_0xd4c2('0xc')]][_0xd4c2('0xe')],'interval');}else if(!_[_0xd4c2('0xd')](_0x2e34b7['interval'])){_0x4899b2=[_0x2e34b7[_0xd4c2('0x10')]];}return _0x4899b2;}exports[_0xd4c2('0x11')]=function(_0x43b616){_0x43b616['rules']={};return new BPromise(function(_0x36df4e,_0x38ea7d){var _0x2bad75={'Start':function(_0x43d365){var _0x213feb=this;return new Promise(function(_0x417757,_0x204e85){if(_0x43d365){try{var _0x4e1d03=[];var _0x5c1e42=getId(_0x43d365[_0xd4c2('0x12')],_0x43d365[_0xd4c2('0x13')]?_0x43d365[_0xd4c2('0x13')]['id']:0x0,_0x43d365['interaction']?_0x43d365[_0xd4c2('0x14')]['id']:0x0);if(_0x43d365['applications']&&_[_0xd4c2('0x15')](_0x43d365[_0xd4c2('0x16')])&&_0x43d365[_0xd4c2('0x16')][_0xd4c2('0x17')]){for(var _0x134dda=0x0;_0x134dda<_0x43d365[_0xd4c2('0x16')][_0xd4c2('0x17')];_0x134dda+=0x1){var _0x1993b5=_0x43d365[_0xd4c2('0x16')][_0x134dda];if(fs[_0xd4c2('0x18')](path[_0xd4c2('0x19')](__dirname,_0xd4c2('0x16'),_0x1993b5[_0xd4c2('0x1a')]+_0xd4c2('0x1b')))){var _0x4f3e8c=[];switch(_0x1993b5[_0xd4c2('0x1a')][_0xd4c2('0x1c')]()){case _0xd4c2('0x1d'):var _0x261d8a=_0x1993b5[_0xd4c2('0x1e')][_0xd4c2('0x1f')](',');_0x4f3e8c['push'](_0x261d8a[0x0]);_0x4f3e8c[_0xd4c2('0x20')](_0x261d8a['slice'](0x1,_0x261d8a['length'])[_0xd4c2('0x19')](','));break;case _0xd4c2('0x21'):var _0x558e57=_0x1993b5[_0xd4c2('0x1e')][_0xd4c2('0x1f')](',');_0x4f3e8c['push'](_0x558e57[0x0]);_0x4f3e8c['push'](_0x558e57[0x1]);_0x4f3e8c[_0xd4c2('0x20')](_0x558e57[_0xd4c2('0x22')](0x2,_0x558e57[_0xd4c2('0x17')])[_0xd4c2('0x19')](','));break;case _0xd4c2('0x23'):var _0x5d6a2e=_0x1993b5[_0xd4c2('0x1e')][_0xd4c2('0x1f')](',');_0x4f3e8c[_0xd4c2('0x20')](_0x5d6a2e[0x0]);_0x4f3e8c['push'](_0x5d6a2e[0x1]);_0x4f3e8c[_0xd4c2('0x20')](_0x5d6a2e[0x2]);_0x4f3e8c[_0xd4c2('0x20')](_0x5d6a2e[0x3]);_0x4f3e8c[_0xd4c2('0x20')](_0x5d6a2e[_0xd4c2('0x22')](0x4,_0x5d6a2e[_0xd4c2('0x17')])['join'](','));break;case _0xd4c2('0x24'):var _0x484203=_0x1993b5[_0xd4c2('0x1e')][_0xd4c2('0x1f')](',');_0x4f3e8c[_0xd4c2('0x20')](_0x484203[0x0]);_0x4f3e8c[_0xd4c2('0x20')](_0x484203[0x1]);_0x4f3e8c[_0xd4c2('0x20')](_0x484203[0x2]);_0x4f3e8c[_0xd4c2('0x20')](_0x484203[0x3]);_0x4f3e8c[_0xd4c2('0x20')](_0x484203[_0xd4c2('0x22')](0x4,_0x484203[_0xd4c2('0x17')])[_0xd4c2('0x19')](','));break;case'message':_0x4f3e8c['push'](_0x1993b5[_0xd4c2('0x1e')]);break;case _0xd4c2('0x25'):_0x4f3e8c[_0xd4c2('0x20')](_0x1993b5[_0xd4c2('0x26')]);_0x4f3e8c[_0xd4c2('0x20')](_0x1993b5[_0xd4c2('0x1e')]);break;default:_0x4f3e8c=_['isNil'](_0x1993b5['appdata'])?[]:_0x1993b5[_0xd4c2('0x1e')]['split'](',');}var _0x44b6db={'id':util['format']('%s:%s:%s',_0x5c1e42,_0x1993b5['id'],_0x1993b5[_0xd4c2('0x1a')]),'condition':require(_0xd4c2('0x27'))[_0xd4c2('0x28')](_0x5c1e42,getIntervals(_0x43b616[_0xd4c2('0x29')],_0x1993b5),_0x43b616),'consequence':require('./applications/'+_0x1993b5[_0xd4c2('0x1a')])[_0xd4c2('0x2a')](_0x5c1e42,_0x4f3e8c,_0x43b616,_0x43d365),'p':_0x1993b5[_0xd4c2('0x2b')]||0x0};logger[_0xd4c2('0x2c')]('Push\x20new\x20rule',_0x44b6db['id'],'on\x20interval',getIntervals(_0x43b616['intervals'],_0x1993b5)[_0xd4c2('0x19')](_0xd4c2('0x2d')));_0x4e1d03[_0xd4c2('0x20')](_0x44b6db);}else{logger[_0xd4c2('0x2e')]('Please\x20create\x20a\x20new\x20file\x20under',path[_0xd4c2('0x19')](__dirname,_0xd4c2('0x16'),_0x1993b5['app']+_0xd4c2('0x1b')));}}_0x43b616[_0xd4c2('0x2f')][_0x5c1e42]=new RuleEngine(_0x4e1d03);_0x43b616[_0xd4c2('0x2f')][_0x5c1e42][_0xd4c2('0x30')](_0x43d365,function(_0x60e7a2){logger[_0xd4c2('0x2c')](_0xd4c2('0x31'),JSON['stringify'](_0x60e7a2[_0xd4c2('0x32')]));if(_0x43b616[_0xd4c2('0x2f')][_0x5c1e42]){_0x43b616[_0xd4c2('0x2f')][_0x5c1e42][_0xd4c2('0x33')](_0xd4c2('0x34'));}});return _0x417757({'code':0xc8,'message':_0x43d365});}else{logger[_0xd4c2('0x2c')](_0xd4c2('0x35'));return _0x417757({'code':0xc8,'message':_0x43d365});}}catch(_0x4ae298){logger['error'](_0x4ae298[_0xd4c2('0x36')]);return _0x204e85(_0x213feb['error'](0x1f4,_0x4ae298));}}return _0x204e85(_0xd4c2('0x37'));});},'AcceptMessage':function(_0x204118){var _0x25a20a=this;return new Promise(function(_0x40c2c8,_0x1d0c2a){if(_0x204118[_0xd4c2('0x13')]&&_0x204118[_0xd4c2('0x13')]['id']){try{var _0x25004e=getId(_0x204118[_0xd4c2('0x12')],_0x204118[_0xd4c2('0x13')]?_0x204118[_0xd4c2('0x13')]['id']:0x0,_0x204118[_0xd4c2('0x14')]?_0x204118[_0xd4c2('0x14')]['id']:0x0);if(_0x43b616[_0xd4c2('0x2f')][_0x25004e]){_0x43b616[_0xd4c2('0x2f')][_0x25004e][_0xd4c2('0x33')](_0xd4c2('0x38'));if(rpc[_0x204118[_0xd4c2('0x12')]+_0xd4c2('0x39')]){rpc[_0x204118[_0xd4c2('0x12')]+_0xd4c2('0x39')][_0xd4c2('0x3a')](_0x25004e,{'UserId':_0x204118[_0xd4c2('0x13')]['UserId']})[_0xd4c2('0x3b')](function(_0x5a21eb){logger[_0xd4c2('0x2e')](_0xd4c2('0x3c'));});}}return _0x40c2c8({'code':0xc8,'message':_0x204118});}catch(_0x3bde7a){logger['error'](_0x3bde7a[_0xd4c2('0x36')]);return _0x1d0c2a(_0x25a20a[_0xd4c2('0x2e')](0x1f4,_0x3bde7a));}}logger['error'](_0xd4c2('0x3d'));return _0x1d0c2a(_0x25a20a[_0xd4c2('0x2e')](0x1f4,'message\x20is\x20empty'));});},'RejectMessage':function(_0x36ffcb){var _0x3507c5=this;return new Promise(function(_0x5a541d,_0x4d5acb){if(_0x36ffcb[_0xd4c2('0x3e')]&&_0x36ffcb[_0xd4c2('0x3e')]['id']){try{if(_0x43b616['agents'][_0x36ffcb[_0xd4c2('0x3e')]['id']]){_0x43b616[_0xd4c2('0x3f')][_0x36ffcb[_0xd4c2('0x3e')]['id']][_0xd4c2('0x40')]=![];_0x43b616[_0xd4c2('0x3f')][_0x36ffcb[_0xd4c2('0x3e')]['id']][_0xd4c2('0x41')]=undefined;}return _0x5a541d({'code':0xc8,'message':_0x36ffcb});}catch(_0x16abcb){logger[_0xd4c2('0x2e')](_0x16abcb[_0xd4c2('0x36')]);return _0x4d5acb(_0x3507c5['error'](0x1f4,_0x16abcb));}}logger[_0xd4c2('0x2e')]('message\x20is\x20empty');return _0x4d5acb(_0x3507c5[_0xd4c2('0x2e')](0x1f4,_0xd4c2('0x3d')));});},'AbandonInteraction':function(_0x342194){var _0x576651=this;return new Promise(function(_0x323650,_0xa04291){if(_0x342194[_0xd4c2('0x12')]&&_0x342194['interaction']&&_0x342194['interaction']['id']){try{var _0x3c6280=getId(_0x342194[_0xd4c2('0x12')],_0x342194[_0xd4c2('0x13')]?_0x342194[_0xd4c2('0x13')]['id']:0x0,_0x342194['interaction']?_0x342194[_0xd4c2('0x14')]['id']:0x0);if(_0x43b616[_0xd4c2('0x2f')][_0x3c6280]){_0x43b616[_0xd4c2('0x2f')][_0x3c6280][_0xd4c2('0x33')]('end');}return _0x323650({'code':0xc8,'message':_0x342194});}catch(_0x12310a){logger[_0xd4c2('0x2e')](_0x12310a[_0xd4c2('0x36')]);return _0xa04291(_0x576651[_0xd4c2('0x2e')](0x1f4,_0x12310a));}}logger[_0xd4c2('0x2e')](_0xd4c2('0x3d'));return _0xa04291(_0x576651[_0xd4c2('0x2e')](0x1f4,'message\x20is\x20empty'));});},'AgentCapacity':function(_0x1520e6){var _0x47a878=this;return new Promise(function(_0x355436,_0x3fe4d2){if(_0x43b616[_0xd4c2('0x3f')][_0xd4c2('0x42')](_0x1520e6['id'])){for(var _0x553f96 in _0x1520e6[_0xd4c2('0x43')]){if(_0x1520e6[_0xd4c2('0x43')]['hasOwnProperty'](_0x553f96)){if(_0x43b616[_0xd4c2('0x3f')][_0x1520e6['id']][_0xd4c2('0x42')](_0x553f96+'CurrentCapacity')){_0x43b616[_0xd4c2('0x3f')][_0x1520e6['id']][_0x553f96+'CurrentCapacity']=parseInt(_0x1520e6[_0xd4c2('0x43')][_0x553f96],0xa);}}}_0x43b616['agents'][_0x1520e6['id']]['updateRealtime']();return _0x355436({'code':0xc8,'message':_0x43b616[_0xd4c2('0x3f')][_0x1520e6['id']]});}else{return _0x3fe4d2(_0x47a878['error'](0x1f4,_0xd4c2('0x44')));}});},'Agents':function(_0x293cd6){return new Promise(function(_0x18628d,_0x3cbb95){var _0x5e3c85=_[_0xd4c2('0x45')](_0x43b616['agents']);_0x18628d({'count':_0x5e3c85['length'],'rows':_0x5e3c85});});},'ChatQueues':function(_0x419f7a){return new Promise(function(_0x4071d1,_0x295c13){var _0x180a8e=[];for(var _0x5ba22d in _0x43b616[_0xd4c2('0x46')]){if(_0x43b616[_0xd4c2('0x46')][_0xd4c2('0x42')](_0x5ba22d)){var _0x3b4c2=_0x43b616[_0xd4c2('0x46')][_0x5ba22d];var _0x5c4c86={'id':_0x3b4c2['id'],'available':_0x3b4c2[_0xd4c2('0x47')],'loggedIn':_0x3b4c2[_0xd4c2('0x48')],'name':_0x3b4c2[_0xd4c2('0x49')],'paused':_0x3b4c2['paused'],'waiting':_0x3b4c2[_0xd4c2('0x4a')]};_0x180a8e[_0xd4c2('0x20')](_0x5c4c86);}}_0x4071d1({'count':_0x180a8e[_0xd4c2('0x17')],'rows':_0x180a8e});});},'MailQueues':function(_0x188530){return new Promise(function(_0x1c0130,_0x30d733){var _0x40288b=[];for(var _0xef0121 in _0x43b616[_0xd4c2('0x4b')]){if(_0x43b616[_0xd4c2('0x4b')]['hasOwnProperty'](_0xef0121)){var _0x4a07d6=_0x43b616[_0xd4c2('0x4b')][_0xef0121];var _0x561c39={'id':_0x4a07d6['id'],'available':_0x4a07d6[_0xd4c2('0x47')],'loggedIn':_0x4a07d6[_0xd4c2('0x48')],'name':_0x4a07d6['name'],'paused':_0x4a07d6[_0xd4c2('0x4c')],'waiting':_0x4a07d6['waiting']};_0x40288b[_0xd4c2('0x20')](_0x561c39);}}_0x1c0130({'count':_0x40288b[_0xd4c2('0x17')],'rows':_0x40288b});});},'OpenchannelQueues':function(_0x1e2323){return new Promise(function(_0xa2ac35,_0x2fa6b9){var _0x141e81=[];for(var _0x1ca442 in _0x43b616[_0xd4c2('0x4d')]){if(_0x43b616['openchannelQueues'][_0xd4c2('0x42')](_0x1ca442)){var _0x1dccc6=_0x43b616[_0xd4c2('0x4d')][_0x1ca442];var _0x147a03={'id':_0x1dccc6['id'],'available':_0x1dccc6[_0xd4c2('0x47')],'loggedIn':_0x1dccc6[_0xd4c2('0x48')],'name':_0x1dccc6['name'],'paused':_0x1dccc6[_0xd4c2('0x4c')],'waiting':_0x1dccc6[_0xd4c2('0x4a')]};_0x141e81[_0xd4c2('0x20')](_0x147a03);}}_0xa2ac35({'count':_0x141e81['length'],'rows':_0x141e81});});},'SmsQueues':function(_0x4e1df0){return new Promise(function(_0x5ae9e1,_0x3a8f5e){var _0x92f6e=[];for(var _0x5ccfeb in _0x43b616[_0xd4c2('0x4e')]){if(_0x43b616[_0xd4c2('0x4e')]['hasOwnProperty'](_0x5ccfeb)){var _0x38017c=_0x43b616['smsQueues'][_0x5ccfeb];var _0x20f12e={'id':_0x38017c['id'],'available':_0x38017c[_0xd4c2('0x47')],'loggedIn':_0x38017c[_0xd4c2('0x48')],'name':_0x38017c['name'],'paused':_0x38017c[_0xd4c2('0x4c')],'waiting':_0x38017c[_0xd4c2('0x4a')]};_0x92f6e[_0xd4c2('0x20')](_0x20f12e);}}_0x5ae9e1({'count':_0x92f6e[_0xd4c2('0x17')],'rows':_0x92f6e});});},'FaxQueues':function(_0x2be634){return new Promise(function(_0x321c15,_0x5e345f){var _0x7b96d4=[];for(var _0x222626 in _0x43b616[_0xd4c2('0x4f')]){if(_0x43b616[_0xd4c2('0x4f')]['hasOwnProperty'](_0x222626)){var _0x38c9a9=_0x43b616['faxQueues'][_0x222626];var _0x4f1c5e={'id':_0x38c9a9['id'],'available':_0x38c9a9[_0xd4c2('0x47')],'loggedIn':_0x38c9a9[_0xd4c2('0x48')],'name':_0x38c9a9[_0xd4c2('0x49')],'paused':_0x38c9a9[_0xd4c2('0x4c')],'waiting':_0x38c9a9[_0xd4c2('0x4a')]};_0x7b96d4[_0xd4c2('0x20')](_0x4f1c5e);}}_0x321c15({'count':_0x7b96d4[_0xd4c2('0x17')],'rows':_0x7b96d4});});},'WhatsappQueues':function(_0x4b8763){return new Promise(function(_0x15905d,_0x1cb40b){var _0x538022=[];for(var _0x448577 in _0x43b616['whatsappQueues']){if(_0x43b616[_0xd4c2('0x50')][_0xd4c2('0x42')](_0x448577)){var _0x589e30=_0x43b616['whatsappQueues'][_0x448577];var _0x58a2b1={'id':_0x589e30['id'],'available':_0x589e30['available'],'loggedIn':_0x589e30[_0xd4c2('0x48')],'name':_0x589e30[_0xd4c2('0x49')],'paused':_0x589e30['paused'],'waiting':_0x589e30[_0xd4c2('0x4a')]};_0x538022[_0xd4c2('0x20')](_0x58a2b1);}}_0x15905d({'count':_0x538022[_0xd4c2('0x17')],'rows':_0x538022});});},'ChatQueuesWaitingInteractions':function(_0x4f2c1e){return new Promise(function(_0x384468,_0x22df5f){var _0x1a31a3=_[_0xd4c2('0x45')](_0x43b616[_0xd4c2('0x51')]);_0x384468({'count':_0x1a31a3[_0xd4c2('0x17')],'rows':_0x1a31a3});});},'MailQueuesWaitingInteractions':function(_0x32b5aa){return new Promise(function(_0x199520,_0x150e51){var _0x2b837c=_[_0xd4c2('0x45')](_0x43b616['mailQueuesWaitingInteractions']);_0x199520({'count':_0x2b837c[_0xd4c2('0x17')],'rows':_0x2b837c});});},'OpenchannelQueuesWaitingInteractions':function(_0x4c6b99){return new Promise(function(_0x15d777,_0x1853ae){var _0x2d9c47=_[_0xd4c2('0x45')](_0x43b616[_0xd4c2('0x52')]);_0x15d777({'count':_0x2d9c47[_0xd4c2('0x17')],'rows':_0x2d9c47});});},'SmsQueuesWaitingInteractions':function(_0x2926d0){return new Promise(function(_0x5c135a,_0x56a83b){var _0x195d64=_[_0xd4c2('0x45')](_0x43b616[_0xd4c2('0x53')]);_0x5c135a({'count':_0x195d64[_0xd4c2('0x17')],'rows':_0x195d64});});},'FaxQueuesWaitingInteractions':function(_0x2c110c){return new Promise(function(_0x4808b1,_0x237553){var _0x1f1135=_[_0xd4c2('0x45')](_0x43b616[_0xd4c2('0x54')]);_0x4808b1({'count':_0x1f1135['length'],'rows':_0x1f1135});});},'WhatsappQueuesWaitingInteractions':function(_0xe5614){return new Promise(function(_0x214960,_0x2f8dbb){var _0x1e6d84=_[_0xd4c2('0x45')](_0x43b616[_0xd4c2('0x55')]);_0x214960({'count':_0x1e6d84[_0xd4c2('0x17')],'rows':_0x1e6d84});});}};var _0x2e07ef=jayson['server'](_0x2bad75)[_0xd4c2('0x56')]();_0x2e07ef['on'](_0xd4c2('0x2e'),function(_0x28db50){logger[_0xd4c2('0x2e')](JSON[_0xd4c2('0x57')](_0x28db50));return _0x38ea7d(_0x28db50);});_0x2e07ef[_0xd4c2('0x11')](0x232c,_0xd4c2('0x58'),function(){console['log'](_0xd4c2('0x59'),0x232c);return _0x36df4e(_0xd4c2('0x5a')+0x232c);});});};