Built motion from commit 19a84241.|2.5.34
[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 _0x01a0=['CurrentCapacity','values','chatQueues','available','loggedIn','name','paused','mailQueues','openchannelQueues','waiting','smsQueues','faxQueues','whatsappQueues','chatQueuesWaitingInteractions','mailQueuesWaitingInteractions','openchannelQueuesWaitingInteractions','smsQueuesWaitingInteractions','faxQueuesWaitingInteractions','whatsappQueuesWaitingInteractions','server','http','127.0.0.1','log','Routing\x20RPC\x20server\x20listening\x20on\x20127.0.0.1:%d!','RPC\x20listening\x20on\x20port:','util','path','jayson/promise','uuid','../../../config/environment','../../../config/logger','../rpc/openchannelQueueReport','../rpc/chatQueueReport','../rpc/smsQueueReport','../lib/rule-engine','%s.%s.%s','isNil','IntervalId','Intervals','map','interval','listen','rules','message','interaction','isArray','applications','length','existsSync','join','.js','app','appdata','split','mail','push','slice','dialogflow','dialogflowv2','tag','TagId','format','%s:%s:%s','condition','intervals','consequence','priority','info','on\x20interval','Please\x20create\x20a\x20new\x20file\x20under','Applications\x20executed','stringify','matchPath','emit','complete','No\x20applications\x20to\x20execute','error','routing\x20is\x20empty','agent','agents','busy','end','channel','QueueReport','update','error\x20update\x20queue\x20report','stack','message\x20is\x20empty','busyQueue','hasOwnProperty','capacity'];(function(_0x19270b,_0x5d0683){var _0x30c3b5=function(_0x5f08fe){while(--_0x5f08fe){_0x19270b['push'](_0x19270b['shift']());}};_0x30c3b5(++_0x5d0683);}(_0x01a0,0x1e0));var _0x001a=function(_0x3ea83d,_0x415c18){_0x3ea83d=_0x3ea83d-0x0;var _0x48931e=_0x01a0[_0x3ea83d];return _0x48931e;};'use strict';var _=require('lodash');var util=require(_0x001a('0x0'));var path=require(_0x001a('0x1'));var fs=require('fs');var jayson=require(_0x001a('0x2'));var BPromise=require('bluebird');var uuid=require(_0x001a('0x3'));var config=require(_0x001a('0x4'));var logger=require(_0x001a('0x5'))('routing');var rpc={'openchannelQueueReport':require(_0x001a('0x6')),'mailQueueReport':require('../rpc/mailQueueReport'),'chatQueueReport':require(_0x001a('0x7')),'smsQueueReport':require(_0x001a('0x8')),'faxQueueReport':require('../rpc/faxQueueReport'),'whatsappQueueReport':require('../rpc/whatsappQueueReport')};var RuleEngine=require(_0x001a('0x9'));function getId(_0x117449,_0x5b300a,_0x55a850){return util['format'](_0x001a('0xa'),_0x117449,_0x5b300a||0x0,_0x55a850||0x0);}function getIntervals(_0x48904e,_0x45afd0){var _0x43b74b=[];if(!_[_0x001a('0xb')](_0x45afd0[_0x001a('0xc')])&&!_[_0x001a('0xb')](_0x48904e[_0x45afd0[_0x001a('0xc')]])&&!_[_0x001a('0xb')](_0x48904e[_0x45afd0[_0x001a('0xc')]][_0x001a('0xd')])){_0x43b74b=_[_0x001a('0xe')](_0x48904e[_0x45afd0[_0x001a('0xc')]][_0x001a('0xd')],_0x001a('0xf'));}else if(!_[_0x001a('0xb')](_0x45afd0[_0x001a('0xf')])){_0x43b74b=[_0x45afd0[_0x001a('0xf')]];}return _0x43b74b;}exports[_0x001a('0x10')]=function(_0x2e44c7){_0x2e44c7[_0x001a('0x11')]={};return new BPromise(function(_0x3f4039,_0x5457ed){var _0x51a1bc={'Start':function(_0x2646db){var _0x29d0fe=this;return new Promise(function(_0x122d3a,_0x1d0bad){if(_0x2646db){try{var _0x338006=[];var _0x3288f5=getId(_0x2646db['channel'],_0x2646db[_0x001a('0x12')]?_0x2646db[_0x001a('0x12')]['id']:0x0,_0x2646db[_0x001a('0x13')]?_0x2646db[_0x001a('0x13')]['id']:0x0);if(_0x2646db['applications']&&_[_0x001a('0x14')](_0x2646db[_0x001a('0x15')])&&_0x2646db[_0x001a('0x15')]['length']){for(var _0x24d5fa=0x0;_0x24d5fa<_0x2646db[_0x001a('0x15')][_0x001a('0x16')];_0x24d5fa+=0x1){var _0x3446b6=_0x2646db['applications'][_0x24d5fa];if(fs[_0x001a('0x17')](path[_0x001a('0x18')](__dirname,_0x001a('0x15'),_0x3446b6['app']+_0x001a('0x19')))){var _0x36361d=[];switch(_0x3446b6[_0x001a('0x1a')]['toLowerCase']()){case'autoreply':var _0x184d2d=_0x3446b6[_0x001a('0x1b')][_0x001a('0x1c')](',');if(_0x2646db['channel']===_0x001a('0x1d')){_0x36361d[_0x001a('0x1e')](_0x184d2d[0x0]);_0x36361d[_0x001a('0x1e')](_0x184d2d[0x1]);_0x36361d['push'](_0x184d2d[_0x001a('0x1f')](0x2,_0x184d2d['length'])['join'](','));}else{_0x36361d[_0x001a('0x1e')](_0x184d2d[0x0]);_0x36361d[_0x001a('0x1e')](_0x184d2d[_0x001a('0x1f')](0x1,_0x184d2d[_0x001a('0x16')])[_0x001a('0x18')](','));}break;case _0x001a('0x20'):var _0x48dfc9=_0x3446b6[_0x001a('0x1b')][_0x001a('0x1c')](',');_0x36361d[_0x001a('0x1e')](_0x48dfc9[0x0]);_0x36361d[_0x001a('0x1e')](_0x48dfc9[0x1]);_0x36361d[_0x001a('0x1e')](_0x48dfc9[_0x001a('0x1f')](0x2,_0x48dfc9[_0x001a('0x16')])['join'](','));break;case _0x001a('0x21'):var _0x1e29aa=_0x3446b6[_0x001a('0x1b')][_0x001a('0x1c')](',');_0x36361d[_0x001a('0x1e')](_0x1e29aa[0x0]);_0x36361d[_0x001a('0x1e')](_0x1e29aa[0x1]);_0x36361d['push'](_0x1e29aa[0x2]);_0x36361d['push'](_0x1e29aa[0x3]);_0x36361d[_0x001a('0x1e')](_0x1e29aa[_0x001a('0x1f')](0x4,_0x1e29aa[_0x001a('0x16')])['join'](','));break;case'amazonlex':var _0x562021=_0x3446b6[_0x001a('0x1b')]['split'](',');_0x36361d[_0x001a('0x1e')](_0x562021[0x0]);_0x36361d[_0x001a('0x1e')](_0x562021[0x1]);_0x36361d[_0x001a('0x1e')](_0x562021[0x2]);_0x36361d[_0x001a('0x1e')](_0x562021[0x3]);_0x36361d[_0x001a('0x1e')](_0x562021['slice'](0x4,_0x562021[_0x001a('0x16')])[_0x001a('0x18')](','));break;case _0x001a('0x12'):_0x36361d[_0x001a('0x1e')](_0x3446b6[_0x001a('0x1b')]);break;case _0x001a('0x22'):_0x36361d[_0x001a('0x1e')](_0x3446b6[_0x001a('0x23')]);_0x36361d[_0x001a('0x1e')](_0x3446b6[_0x001a('0x1b')]);break;default:_0x36361d=_[_0x001a('0xb')](_0x3446b6[_0x001a('0x1b')])?[]:_0x3446b6[_0x001a('0x1b')][_0x001a('0x1c')](',');}var _0x307a94={'id':util[_0x001a('0x24')](_0x001a('0x25'),_0x3288f5,_0x3446b6['id'],_0x3446b6[_0x001a('0x1a')]),'condition':require('./interval')[_0x001a('0x26')](_0x3288f5,getIntervals(_0x2e44c7[_0x001a('0x27')],_0x3446b6),_0x2e44c7),'consequence':require('./applications/'+_0x3446b6[_0x001a('0x1a')])[_0x001a('0x28')](_0x3288f5,_0x36361d,_0x2e44c7,_0x2646db),'p':_0x3446b6[_0x001a('0x29')]||0x0};logger[_0x001a('0x2a')]('Push\x20new\x20rule',_0x307a94['id'],_0x001a('0x2b'),getIntervals(_0x2e44c7['intervals'],_0x3446b6)[_0x001a('0x18')]('\x20|\x20'));_0x338006[_0x001a('0x1e')](_0x307a94);}else{logger['error'](_0x001a('0x2c'),path['join'](__dirname,_0x001a('0x15'),_0x3446b6[_0x001a('0x1a')]+_0x001a('0x19')));}}_0x2e44c7['rules'][_0x3288f5]=new RuleEngine(_0x338006);_0x2e44c7[_0x001a('0x11')][_0x3288f5]['execute'](_0x2646db,function(_0x133f2d){logger[_0x001a('0x2a')](_0x001a('0x2d'),JSON[_0x001a('0x2e')](_0x133f2d[_0x001a('0x2f')]));if(_0x2e44c7[_0x001a('0x11')][_0x3288f5]){_0x2e44c7[_0x001a('0x11')][_0x3288f5][_0x001a('0x30')](_0x001a('0x31'));}});return _0x122d3a({'code':0xc8,'message':_0x2646db});}else{logger['info'](_0x001a('0x32'));return _0x122d3a({'code':0xc8,'message':_0x2646db});}}catch(_0xb2b0f4){logger['error'](_0xb2b0f4['stack']);return _0x1d0bad(_0x29d0fe[_0x001a('0x33')](0x1f4,_0xb2b0f4));}}return _0x1d0bad(_0x001a('0x34'));});},'AcceptMessage':function(_0x297c15){var _0x25ddfd=this;return new Promise(function(_0x29d6cc,_0x360531){if(_0x297c15[_0x001a('0x35')]&&_0x297c15[_0x001a('0x35')]['id']){if(_0x2e44c7['agents'][_0x297c15['agent']['id']]){_0x2e44c7[_0x001a('0x36')][_0x297c15[_0x001a('0x35')]['id']][_0x001a('0x37')]=![];_0x2e44c7['agents'][_0x297c15[_0x001a('0x35')]['id']]['busyQueue']=undefined;}}if(_0x297c15['message']&&_0x297c15[_0x001a('0x12')]['id']){try{var _0xe03384=getId(_0x297c15['channel'],_0x297c15['message']?_0x297c15[_0x001a('0x12')]['id']:0x0,_0x297c15[_0x001a('0x13')]?_0x297c15[_0x001a('0x13')]['id']:0x0);if(_0x2e44c7[_0x001a('0x11')][_0xe03384]){_0x2e44c7['rules'][_0xe03384]['emit'](_0x001a('0x38'));if(rpc[_0x297c15[_0x001a('0x39')]+_0x001a('0x3a')]){rpc[_0x297c15[_0x001a('0x39')]+_0x001a('0x3a')][_0x001a('0x3b')](_0xe03384,{'UserId':_0x297c15['message']['UserId']})['catch'](function(_0x52918d){logger[_0x001a('0x33')](_0x001a('0x3c'));});}}return _0x29d6cc({'code':0xc8,'message':_0x297c15});}catch(_0x3fcc74){logger[_0x001a('0x33')](_0x3fcc74[_0x001a('0x3d')]);return _0x360531(_0x25ddfd[_0x001a('0x33')](0x1f4,_0x3fcc74));}}logger[_0x001a('0x33')](_0x001a('0x3e'));return _0x360531(_0x25ddfd[_0x001a('0x33')](0x1f4,_0x001a('0x3e')));});},'RejectMessage':function(_0x508389){var _0x3f1855=this;return new Promise(function(_0x7716da,_0x384136){if(_0x508389[_0x001a('0x35')]&&_0x508389[_0x001a('0x35')]['id']){try{if(_0x2e44c7['agents'][_0x508389[_0x001a('0x35')]['id']]){_0x2e44c7['agents'][_0x508389[_0x001a('0x35')]['id']][_0x001a('0x37')]=![];_0x2e44c7[_0x001a('0x36')][_0x508389[_0x001a('0x35')]['id']][_0x001a('0x3f')]=undefined;}return _0x7716da({'code':0xc8,'message':_0x508389});}catch(_0x2c15c1){logger['error'](_0x2c15c1[_0x001a('0x3d')]);return _0x384136(_0x3f1855[_0x001a('0x33')](0x1f4,_0x2c15c1));}}logger[_0x001a('0x33')](_0x001a('0x3e'));return _0x384136(_0x3f1855[_0x001a('0x33')](0x1f4,_0x001a('0x3e')));});},'AbandonInteraction':function(_0x4d5b6c){var _0x4b566d=this;return new Promise(function(_0x3ad54c,_0x5d183b){if(_0x4d5b6c['channel']&&_0x4d5b6c[_0x001a('0x13')]&&_0x4d5b6c[_0x001a('0x13')]['id']){try{var _0x4b5c11=getId(_0x4d5b6c[_0x001a('0x39')],_0x4d5b6c[_0x001a('0x12')]?_0x4d5b6c[_0x001a('0x12')]['id']:0x0,_0x4d5b6c['interaction']?_0x4d5b6c[_0x001a('0x13')]['id']:0x0);if(_0x2e44c7['rules'][_0x4b5c11]){_0x2e44c7[_0x001a('0x11')][_0x4b5c11][_0x001a('0x30')](_0x001a('0x38'));}return _0x3ad54c({'code':0xc8,'message':_0x4d5b6c});}catch(_0xacf539){logger['error'](_0xacf539[_0x001a('0x3d')]);return _0x5d183b(_0x4b566d[_0x001a('0x33')](0x1f4,_0xacf539));}}logger[_0x001a('0x33')](_0x001a('0x3e'));return _0x5d183b(_0x4b566d[_0x001a('0x33')](0x1f4,_0x001a('0x3e')));});},'AgentCapacity':function(_0x4b5803){var _0xacd680=this;return new Promise(function(_0x11f9c6,_0x55625){if(_0x2e44c7[_0x001a('0x36')][_0x001a('0x40')](_0x4b5803['id'])){for(var _0x5d4028 in _0x4b5803['capacity']){if(_0x4b5803[_0x001a('0x41')][_0x001a('0x40')](_0x5d4028)){if(_0x2e44c7['agents'][_0x4b5803['id']][_0x001a('0x40')](_0x5d4028+_0x001a('0x42'))){_0x2e44c7[_0x001a('0x36')][_0x4b5803['id']][_0x5d4028+_0x001a('0x42')]=parseInt(_0x4b5803[_0x001a('0x41')][_0x5d4028],0xa);}}}_0x2e44c7[_0x001a('0x36')][_0x4b5803['id']]['updateRealtime']();return _0x11f9c6({'code':0xc8,'message':_0x2e44c7[_0x001a('0x36')][_0x4b5803['id']]});}else{return _0x55625(_0xacd680[_0x001a('0x33')](0x1f4,'agent\x20not\x20found'));}});},'Agents':function(_0x31e794){return new Promise(function(_0x11a3cd,_0x3a2f3c){var _0xfc97f7=_[_0x001a('0x43')](_0x2e44c7[_0x001a('0x36')]);_0x11a3cd({'count':_0xfc97f7[_0x001a('0x16')],'rows':_0xfc97f7});});},'ChatQueues':function(_0x56d131){return new Promise(function(_0xc1285e,_0xbac6a1){var _0x39a8aa=[];for(var _0x3457ab in _0x2e44c7[_0x001a('0x44')]){if(_0x2e44c7['chatQueues'][_0x001a('0x40')](_0x3457ab)){var _0x37dc60=_0x2e44c7[_0x001a('0x44')][_0x3457ab];var _0x54474c={'id':_0x37dc60['id'],'available':_0x37dc60[_0x001a('0x45')],'loggedIn':_0x37dc60[_0x001a('0x46')],'name':_0x37dc60[_0x001a('0x47')],'paused':_0x37dc60[_0x001a('0x48')],'waiting':_0x37dc60['waiting']};_0x39a8aa[_0x001a('0x1e')](_0x54474c);}}_0xc1285e({'count':_0x39a8aa[_0x001a('0x16')],'rows':_0x39a8aa});});},'MailQueues':function(_0x1f4d58){return new Promise(function(_0x28ba72,_0x20b09d){var _0x41c675=[];for(var _0x25a33b in _0x2e44c7['mailQueues']){if(_0x2e44c7[_0x001a('0x49')][_0x001a('0x40')](_0x25a33b)){var _0x11d704=_0x2e44c7[_0x001a('0x49')][_0x25a33b];var _0x23144b={'id':_0x11d704['id'],'available':_0x11d704[_0x001a('0x45')],'loggedIn':_0x11d704[_0x001a('0x46')],'name':_0x11d704[_0x001a('0x47')],'paused':_0x11d704[_0x001a('0x48')],'waiting':_0x11d704['waiting']};_0x41c675['push'](_0x23144b);}}_0x28ba72({'count':_0x41c675[_0x001a('0x16')],'rows':_0x41c675});});},'OpenchannelQueues':function(_0x1cbdb7){return new Promise(function(_0x3d1ed9,_0x1a8c6f){var _0x4f8574=[];for(var _0x3ce563 in _0x2e44c7[_0x001a('0x4a')]){if(_0x2e44c7[_0x001a('0x4a')]['hasOwnProperty'](_0x3ce563)){var _0x32f1e2=_0x2e44c7[_0x001a('0x4a')][_0x3ce563];var _0x8cff9a={'id':_0x32f1e2['id'],'available':_0x32f1e2[_0x001a('0x45')],'loggedIn':_0x32f1e2[_0x001a('0x46')],'name':_0x32f1e2[_0x001a('0x47')],'paused':_0x32f1e2['paused'],'waiting':_0x32f1e2[_0x001a('0x4b')]};_0x4f8574['push'](_0x8cff9a);}}_0x3d1ed9({'count':_0x4f8574[_0x001a('0x16')],'rows':_0x4f8574});});},'SmsQueues':function(_0x2b5e16){return new Promise(function(_0x302e2e,_0x4a3549){var _0x58880e=[];for(var _0x1a99d2 in _0x2e44c7['smsQueues']){if(_0x2e44c7[_0x001a('0x4c')][_0x001a('0x40')](_0x1a99d2)){var _0x164ef2=_0x2e44c7['smsQueues'][_0x1a99d2];var _0x24f2e3={'id':_0x164ef2['id'],'available':_0x164ef2[_0x001a('0x45')],'loggedIn':_0x164ef2[_0x001a('0x46')],'name':_0x164ef2[_0x001a('0x47')],'paused':_0x164ef2[_0x001a('0x48')],'waiting':_0x164ef2[_0x001a('0x4b')]};_0x58880e[_0x001a('0x1e')](_0x24f2e3);}}_0x302e2e({'count':_0x58880e['length'],'rows':_0x58880e});});},'FaxQueues':function(_0x11826b){return new Promise(function(_0x33f3a8,_0x133d38){var _0x2455a5=[];for(var _0x4f5839 in _0x2e44c7['faxQueues']){if(_0x2e44c7[_0x001a('0x4d')]['hasOwnProperty'](_0x4f5839)){var _0x263482=_0x2e44c7[_0x001a('0x4d')][_0x4f5839];var _0x59f602={'id':_0x263482['id'],'available':_0x263482[_0x001a('0x45')],'loggedIn':_0x263482[_0x001a('0x46')],'name':_0x263482[_0x001a('0x47')],'paused':_0x263482[_0x001a('0x48')],'waiting':_0x263482[_0x001a('0x4b')]};_0x2455a5[_0x001a('0x1e')](_0x59f602);}}_0x33f3a8({'count':_0x2455a5[_0x001a('0x16')],'rows':_0x2455a5});});},'WhatsappQueues':function(_0x5dc81a){return new Promise(function(_0x335b67,_0x5db308){var _0xa0899e=[];for(var _0xb29eb6 in _0x2e44c7[_0x001a('0x4e')]){if(_0x2e44c7[_0x001a('0x4e')][_0x001a('0x40')](_0xb29eb6)){var _0x57e492=_0x2e44c7[_0x001a('0x4e')][_0xb29eb6];var _0x4353c6={'id':_0x57e492['id'],'available':_0x57e492[_0x001a('0x45')],'loggedIn':_0x57e492[_0x001a('0x46')],'name':_0x57e492[_0x001a('0x47')],'paused':_0x57e492[_0x001a('0x48')],'waiting':_0x57e492['waiting']};_0xa0899e[_0x001a('0x1e')](_0x4353c6);}}_0x335b67({'count':_0xa0899e[_0x001a('0x16')],'rows':_0xa0899e});});},'ChatQueuesWaitingInteractions':function(_0x43f28c){return new Promise(function(_0x367527,_0x230768){var _0x3474c4=_['values'](_0x2e44c7[_0x001a('0x4f')]);_0x367527({'count':_0x3474c4[_0x001a('0x16')],'rows':_0x3474c4});});},'MailQueuesWaitingInteractions':function(_0x2b48b9){return new Promise(function(_0x1f9a97,_0x41eed2){var _0x12a315=_[_0x001a('0x43')](_0x2e44c7[_0x001a('0x50')]);_0x1f9a97({'count':_0x12a315[_0x001a('0x16')],'rows':_0x12a315});});},'OpenchannelQueuesWaitingInteractions':function(_0x17356e){return new Promise(function(_0x1e6580,_0x12f158){var _0x3e6711=_[_0x001a('0x43')](_0x2e44c7[_0x001a('0x51')]);_0x1e6580({'count':_0x3e6711['length'],'rows':_0x3e6711});});},'SmsQueuesWaitingInteractions':function(_0x3ee937){return new Promise(function(_0x1d3922,_0x3669bc){var _0x9bd046=_[_0x001a('0x43')](_0x2e44c7[_0x001a('0x52')]);_0x1d3922({'count':_0x9bd046[_0x001a('0x16')],'rows':_0x9bd046});});},'FaxQueuesWaitingInteractions':function(_0x1fbfef){return new Promise(function(_0x25c759,_0x18a759){var _0x3675e6=_['values'](_0x2e44c7[_0x001a('0x53')]);_0x25c759({'count':_0x3675e6[_0x001a('0x16')],'rows':_0x3675e6});});},'WhatsappQueuesWaitingInteractions':function(_0x4e18c6){return new Promise(function(_0x56c3a0,_0x15e97f){var _0x5e7ef7=_[_0x001a('0x43')](_0x2e44c7[_0x001a('0x54')]);_0x56c3a0({'count':_0x5e7ef7[_0x001a('0x16')],'rows':_0x5e7ef7});});}};var _0x901765=jayson[_0x001a('0x55')](_0x51a1bc)[_0x001a('0x56')]();_0x901765['on'](_0x001a('0x33'),function(_0x172258){logger[_0x001a('0x33')](JSON[_0x001a('0x2e')](_0x172258));return _0x5457ed(_0x172258);});_0x901765[_0x001a('0x10')](0x232c,_0x001a('0x57'),function(){console[_0x001a('0x58')](_0x001a('0x59'),0x232c);return _0x3f4039(_0x001a('0x5a')+0x232c);});});};