01a300d3bed6487dab74cd54d6eeb4304db105b1
[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 _0xa020=['available','openchannelQueues','loggedIn','smsQueues','faxQueues','whatsappQueues','chatQueuesWaitingInteractions','mailQueuesWaitingInteractions','openchannelQueuesWaitingInteractions','smsQueuesWaitingInteractions','faxQueuesWaitingInteractions','whatsappQueuesWaitingInteractions','server','http','stringify','Routing\x20RPC\x20server\x20listening\x20on\x20127.0.0.1:%d!','util','path','bluebird','uuid','../../../config/environment','../../../config/logger','routing','../rpc/chatQueueReport','../rpc/smsQueueReport','../rpc/faxQueueReport','../rpc/whatsappQueueReport','../lib/rule-engine','format','%s.%s.%s','isNil','IntervalId','Intervals','map','interval','listen','rules','channel','message','interaction','applications','length','existsSync','join','.js','app','autoreply','appdata','split','push','slice','amazonlex','tag','TagId','%s:%s:%s','./interval','intervals','./applications/','consequence','priority','info','on\x20interval','\x20|\x20','error','Please\x20create\x20a\x20new\x20file\x20under','execute','Applications\x20executed','matchPath','emit','No\x20applications\x20to\x20execute','stack','end','QueueReport','update','UserId','error\x20update\x20queue\x20report','message\x20is\x20empty','agent','agents','busy','busyQueue','hasOwnProperty','capacity','CurrentCapacity','updateRealtime','agent\x20not\x20found','values','chatQueues','name','paused','waiting','mailQueues'];(function(_0xc5c57d,_0x5bccbd){var _0x3e6506=function(_0x11c184){while(--_0x11c184){_0xc5c57d['push'](_0xc5c57d['shift']());}};_0x3e6506(++_0x5bccbd);}(_0xa020,0x6c));var _0x0a02=function(_0xbbf64c,_0x363ad9){_0xbbf64c=_0xbbf64c-0x0;var _0x3c83c1=_0xa020[_0xbbf64c];return _0x3c83c1;};'use strict';var _=require('lodash');var util=require(_0x0a02('0x0'));var path=require(_0x0a02('0x1'));var fs=require('fs');var jayson=require('jayson/promise');var BPromise=require(_0x0a02('0x2'));var uuid=require(_0x0a02('0x3'));var config=require(_0x0a02('0x4'));var logger=require(_0x0a02('0x5'))(_0x0a02('0x6'));var rpc={'openchannelQueueReport':require('../rpc/openchannelQueueReport'),'mailQueueReport':require('../rpc/mailQueueReport'),'chatQueueReport':require(_0x0a02('0x7')),'smsQueueReport':require(_0x0a02('0x8')),'faxQueueReport':require(_0x0a02('0x9')),'whatsappQueueReport':require(_0x0a02('0xa'))};var RuleEngine=require(_0x0a02('0xb'));function getId(_0x41893b,_0x21aeff,_0x3e561d){return util[_0x0a02('0xc')](_0x0a02('0xd'),_0x41893b,_0x21aeff||0x0,_0x3e561d||0x0);}function getIntervals(_0xe2e5da,_0xdd339f){var _0xf6001=[];if(!_[_0x0a02('0xe')](_0xdd339f['IntervalId'])&&!_[_0x0a02('0xe')](_0xe2e5da[_0xdd339f['IntervalId']])&&!_['isNil'](_0xe2e5da[_0xdd339f[_0x0a02('0xf')]][_0x0a02('0x10')])){_0xf6001=_[_0x0a02('0x11')](_0xe2e5da[_0xdd339f['IntervalId']]['Intervals'],_0x0a02('0x12'));}else if(!_['isNil'](_0xdd339f[_0x0a02('0x12')])){_0xf6001=[_0xdd339f[_0x0a02('0x12')]];}return _0xf6001;}exports[_0x0a02('0x13')]=function(_0xee8114){_0xee8114[_0x0a02('0x14')]={};return new BPromise(function(_0x115c77,_0x7fb60e){var _0x33f8f5={'Start':function(_0x13c94f){var _0x1b4f68=this;return new Promise(function(_0x4bf5ac,_0x3a88d7){if(_0x13c94f){try{var _0x4c97de=[];var _0x39505c=getId(_0x13c94f[_0x0a02('0x15')],_0x13c94f[_0x0a02('0x16')]?_0x13c94f[_0x0a02('0x16')]['id']:0x0,_0x13c94f[_0x0a02('0x17')]?_0x13c94f[_0x0a02('0x17')]['id']:0x0);if(_0x13c94f[_0x0a02('0x18')]&&_['isArray'](_0x13c94f[_0x0a02('0x18')])&&_0x13c94f[_0x0a02('0x18')][_0x0a02('0x19')]){for(var _0x297a4f=0x0;_0x297a4f<_0x13c94f[_0x0a02('0x18')]['length'];_0x297a4f+=0x1){var _0x2871de=_0x13c94f[_0x0a02('0x18')][_0x297a4f];if(fs[_0x0a02('0x1a')](path[_0x0a02('0x1b')](__dirname,'applications',_0x2871de['app']+_0x0a02('0x1c')))){var _0x4ca892=[];switch(_0x2871de[_0x0a02('0x1d')]['toLowerCase']()){case _0x0a02('0x1e'):var _0x26a3bb=_0x2871de[_0x0a02('0x1f')][_0x0a02('0x20')](',');_0x4ca892[_0x0a02('0x21')](_0x26a3bb[0x0]);_0x4ca892[_0x0a02('0x21')](_0x26a3bb[_0x0a02('0x22')](0x1,_0x26a3bb[_0x0a02('0x19')])['join'](','));break;case'dialogflow':var _0x3ea942=_0x2871de[_0x0a02('0x1f')]['split'](',');_0x4ca892[_0x0a02('0x21')](_0x3ea942[0x0]);_0x4ca892['push'](_0x3ea942[0x1]);_0x4ca892[_0x0a02('0x21')](_0x3ea942[_0x0a02('0x22')](0x2,_0x3ea942['length'])[_0x0a02('0x1b')](','));break;case'dialogflowv2':var _0xe56f86=_0x2871de[_0x0a02('0x1f')]['split'](',');_0x4ca892[_0x0a02('0x21')](_0xe56f86[0x0]);_0x4ca892[_0x0a02('0x21')](_0xe56f86[0x1]);_0x4ca892['push'](_0xe56f86[0x2]);_0x4ca892[_0x0a02('0x21')](_0xe56f86[0x3]);_0x4ca892[_0x0a02('0x21')](_0xe56f86[_0x0a02('0x22')](0x4,_0xe56f86[_0x0a02('0x19')])[_0x0a02('0x1b')](','));break;case _0x0a02('0x23'):var _0x5141f5=_0x2871de[_0x0a02('0x1f')][_0x0a02('0x20')](',');_0x4ca892[_0x0a02('0x21')](_0x5141f5[0x0]);_0x4ca892['push'](_0x5141f5[0x1]);_0x4ca892[_0x0a02('0x21')](_0x5141f5[0x2]);_0x4ca892['push'](_0x5141f5[0x3]);_0x4ca892[_0x0a02('0x21')](_0x5141f5[_0x0a02('0x22')](0x4,_0x5141f5[_0x0a02('0x19')])[_0x0a02('0x1b')](','));break;case _0x0a02('0x16'):_0x4ca892[_0x0a02('0x21')](_0x2871de[_0x0a02('0x1f')]);break;case _0x0a02('0x24'):_0x4ca892['push'](_0x2871de[_0x0a02('0x25')]);_0x4ca892[_0x0a02('0x21')](_0x2871de['appdata']);break;default:_0x4ca892=_['isNil'](_0x2871de[_0x0a02('0x1f')])?[]:_0x2871de[_0x0a02('0x1f')][_0x0a02('0x20')](',');}var _0x4255fb={'id':util[_0x0a02('0xc')](_0x0a02('0x26'),_0x39505c,_0x2871de['id'],_0x2871de[_0x0a02('0x1d')]),'condition':require(_0x0a02('0x27'))['condition'](_0x39505c,getIntervals(_0xee8114[_0x0a02('0x28')],_0x2871de),_0xee8114),'consequence':require(_0x0a02('0x29')+_0x2871de[_0x0a02('0x1d')])[_0x0a02('0x2a')](_0x39505c,_0x4ca892,_0xee8114,_0x13c94f),'p':_0x2871de[_0x0a02('0x2b')]||0x0};logger[_0x0a02('0x2c')]('Push\x20new\x20rule',_0x4255fb['id'],_0x0a02('0x2d'),getIntervals(_0xee8114['intervals'],_0x2871de)[_0x0a02('0x1b')](_0x0a02('0x2e')));_0x4c97de[_0x0a02('0x21')](_0x4255fb);}else{logger[_0x0a02('0x2f')](_0x0a02('0x30'),path[_0x0a02('0x1b')](__dirname,_0x0a02('0x18'),_0x2871de[_0x0a02('0x1d')]+_0x0a02('0x1c')));}}_0xee8114[_0x0a02('0x14')][_0x39505c]=new RuleEngine(_0x4c97de);_0xee8114[_0x0a02('0x14')][_0x39505c][_0x0a02('0x31')](_0x13c94f,function(_0xcb5c43){logger['info'](_0x0a02('0x32'),JSON['stringify'](_0xcb5c43[_0x0a02('0x33')]));if(_0xee8114[_0x0a02('0x14')][_0x39505c]){_0xee8114[_0x0a02('0x14')][_0x39505c][_0x0a02('0x34')]('complete');}});return _0x4bf5ac({'code':0xc8,'message':_0x13c94f});}else{logger[_0x0a02('0x2c')](_0x0a02('0x35'));return _0x4bf5ac({'code':0xc8,'message':_0x13c94f});}}catch(_0x110442){logger[_0x0a02('0x2f')](_0x110442[_0x0a02('0x36')]);return _0x3a88d7(_0x1b4f68[_0x0a02('0x2f')](0x1f4,_0x110442));}}return _0x3a88d7('routing\x20is\x20empty');});},'AcceptMessage':function(_0x1837d2){var _0x112e16=this;return new Promise(function(_0x242d33,_0x441689){if(_0x1837d2[_0x0a02('0x16')]&&_0x1837d2[_0x0a02('0x16')]['id']){try{var _0x184a13=getId(_0x1837d2[_0x0a02('0x15')],_0x1837d2['message']?_0x1837d2[_0x0a02('0x16')]['id']:0x0,_0x1837d2[_0x0a02('0x17')]?_0x1837d2['interaction']['id']:0x0);if(_0xee8114[_0x0a02('0x14')][_0x184a13]){_0xee8114['rules'][_0x184a13][_0x0a02('0x34')](_0x0a02('0x37'));if(rpc[_0x1837d2[_0x0a02('0x15')]+_0x0a02('0x38')]){rpc[_0x1837d2[_0x0a02('0x15')]+_0x0a02('0x38')][_0x0a02('0x39')](_0x184a13,{'UserId':_0x1837d2[_0x0a02('0x16')][_0x0a02('0x3a')]})['catch'](function(_0x18df87){logger[_0x0a02('0x2f')](_0x0a02('0x3b'));});}}return _0x242d33({'code':0xc8,'message':_0x1837d2});}catch(_0x3046f4){logger[_0x0a02('0x2f')](_0x3046f4[_0x0a02('0x36')]);return _0x441689(_0x112e16[_0x0a02('0x2f')](0x1f4,_0x3046f4));}}logger[_0x0a02('0x2f')](_0x0a02('0x3c'));return _0x441689(_0x112e16['error'](0x1f4,_0x0a02('0x3c')));});},'RejectMessage':function(_0x14476f){var _0x3d8e01=this;return new Promise(function(_0x14a700,_0x1c25fb){if(_0x14476f[_0x0a02('0x3d')]&&_0x14476f[_0x0a02('0x3d')]['id']){try{if(_0xee8114[_0x0a02('0x3e')][_0x14476f[_0x0a02('0x3d')]['id']]){_0xee8114[_0x0a02('0x3e')][_0x14476f[_0x0a02('0x3d')]['id']][_0x0a02('0x3f')]=![];_0xee8114[_0x0a02('0x3e')][_0x14476f[_0x0a02('0x3d')]['id']][_0x0a02('0x40')]=undefined;}return _0x14a700({'code':0xc8,'message':_0x14476f});}catch(_0x41af97){logger[_0x0a02('0x2f')](_0x41af97[_0x0a02('0x36')]);return _0x1c25fb(_0x3d8e01['error'](0x1f4,_0x41af97));}}logger['error'](_0x0a02('0x3c'));return _0x1c25fb(_0x3d8e01['error'](0x1f4,'message\x20is\x20empty'));});},'AbandonInteraction':function(_0x3e2821){var _0x1ff236=this;return new Promise(function(_0x5a19f1,_0x574fdd){if(_0x3e2821['channel']&&_0x3e2821[_0x0a02('0x17')]&&_0x3e2821[_0x0a02('0x17')]['id']){try{var _0x1f3225=getId(_0x3e2821[_0x0a02('0x15')],_0x3e2821[_0x0a02('0x16')]?_0x3e2821['message']['id']:0x0,_0x3e2821['interaction']?_0x3e2821['interaction']['id']:0x0);if(_0xee8114[_0x0a02('0x14')][_0x1f3225]){_0xee8114[_0x0a02('0x14')][_0x1f3225][_0x0a02('0x34')]('end');}return _0x5a19f1({'code':0xc8,'message':_0x3e2821});}catch(_0x541dfc){logger[_0x0a02('0x2f')](_0x541dfc[_0x0a02('0x36')]);return _0x574fdd(_0x1ff236[_0x0a02('0x2f')](0x1f4,_0x541dfc));}}logger[_0x0a02('0x2f')](_0x0a02('0x3c'));return _0x574fdd(_0x1ff236[_0x0a02('0x2f')](0x1f4,'message\x20is\x20empty'));});},'AgentCapacity':function(_0x5667da){var _0x275175=this;return new Promise(function(_0x1543e8,_0x2e5ed1){if(_0xee8114[_0x0a02('0x3e')][_0x0a02('0x41')](_0x5667da['id'])){for(var _0x3d0943 in _0x5667da[_0x0a02('0x42')]){if(_0x5667da['capacity'][_0x0a02('0x41')](_0x3d0943)){if(_0xee8114[_0x0a02('0x3e')][_0x5667da['id']][_0x0a02('0x41')](_0x3d0943+_0x0a02('0x43'))){_0xee8114[_0x0a02('0x3e')][_0x5667da['id']][_0x3d0943+_0x0a02('0x43')]=parseInt(_0x5667da[_0x0a02('0x42')][_0x3d0943],0xa);}}}_0xee8114[_0x0a02('0x3e')][_0x5667da['id']][_0x0a02('0x44')]();return _0x1543e8({'code':0xc8,'message':_0xee8114['agents'][_0x5667da['id']]});}else{return _0x2e5ed1(_0x275175[_0x0a02('0x2f')](0x1f4,_0x0a02('0x45')));}});},'Agents':function(_0x5d9c25){return new Promise(function(_0x4a248c,_0x2f8fd2){var _0x24fc98=_[_0x0a02('0x46')](_0xee8114[_0x0a02('0x3e')]);_0x4a248c({'count':_0x24fc98[_0x0a02('0x19')],'rows':_0x24fc98});});},'ChatQueues':function(_0x41a9b0){return new Promise(function(_0x48acb6,_0x214f58){var _0x5bee69=[];for(var _0x35da31 in _0xee8114[_0x0a02('0x47')]){if(_0xee8114[_0x0a02('0x47')]['hasOwnProperty'](_0x35da31)){var _0x14bca5=_0xee8114[_0x0a02('0x47')][_0x35da31];var _0x3e7b30={'id':_0x14bca5['id'],'available':_0x14bca5['available'],'loggedIn':_0x14bca5['loggedIn'],'name':_0x14bca5[_0x0a02('0x48')],'paused':_0x14bca5[_0x0a02('0x49')],'waiting':_0x14bca5[_0x0a02('0x4a')]};_0x5bee69['push'](_0x3e7b30);}}_0x48acb6({'count':_0x5bee69['length'],'rows':_0x5bee69});});},'MailQueues':function(_0x44cde3){return new Promise(function(_0x4ceb37,_0x2521c4){var _0xa5e92d=[];for(var _0x328c08 in _0xee8114[_0x0a02('0x4b')]){if(_0xee8114[_0x0a02('0x4b')][_0x0a02('0x41')](_0x328c08)){var _0x54a585=_0xee8114[_0x0a02('0x4b')][_0x328c08];var _0x55b20f={'id':_0x54a585['id'],'available':_0x54a585[_0x0a02('0x4c')],'loggedIn':_0x54a585['loggedIn'],'name':_0x54a585[_0x0a02('0x48')],'paused':_0x54a585['paused'],'waiting':_0x54a585[_0x0a02('0x4a')]};_0xa5e92d[_0x0a02('0x21')](_0x55b20f);}}_0x4ceb37({'count':_0xa5e92d['length'],'rows':_0xa5e92d});});},'OpenchannelQueues':function(_0x417030){return new Promise(function(_0x232cb4,_0xf0ccfb){var _0x5ba4d9=[];for(var _0x38e61f in _0xee8114['openchannelQueues']){if(_0xee8114[_0x0a02('0x4d')][_0x0a02('0x41')](_0x38e61f)){var _0xf390eb=_0xee8114[_0x0a02('0x4d')][_0x38e61f];var _0x3a85c6={'id':_0xf390eb['id'],'available':_0xf390eb['available'],'loggedIn':_0xf390eb[_0x0a02('0x4e')],'name':_0xf390eb[_0x0a02('0x48')],'paused':_0xf390eb[_0x0a02('0x49')],'waiting':_0xf390eb[_0x0a02('0x4a')]};_0x5ba4d9[_0x0a02('0x21')](_0x3a85c6);}}_0x232cb4({'count':_0x5ba4d9[_0x0a02('0x19')],'rows':_0x5ba4d9});});},'SmsQueues':function(_0x3ea89a){return new Promise(function(_0x5c0fb3,_0x263fd5){var _0x116d82=[];for(var _0x848f0 in _0xee8114[_0x0a02('0x4f')]){if(_0xee8114[_0x0a02('0x4f')]['hasOwnProperty'](_0x848f0)){var _0x131f57=_0xee8114['smsQueues'][_0x848f0];var _0x3d1406={'id':_0x131f57['id'],'available':_0x131f57[_0x0a02('0x4c')],'loggedIn':_0x131f57[_0x0a02('0x4e')],'name':_0x131f57[_0x0a02('0x48')],'paused':_0x131f57[_0x0a02('0x49')],'waiting':_0x131f57['waiting']};_0x116d82[_0x0a02('0x21')](_0x3d1406);}}_0x5c0fb3({'count':_0x116d82[_0x0a02('0x19')],'rows':_0x116d82});});},'FaxQueues':function(_0x4f3d0e){return new Promise(function(_0x27bd3d,_0x54c2b8){var _0x3c6ba6=[];for(var _0x227ceb in _0xee8114[_0x0a02('0x50')]){if(_0xee8114[_0x0a02('0x50')][_0x0a02('0x41')](_0x227ceb)){var _0x5115b7=_0xee8114['faxQueues'][_0x227ceb];var _0x2f0bbb={'id':_0x5115b7['id'],'available':_0x5115b7[_0x0a02('0x4c')],'loggedIn':_0x5115b7['loggedIn'],'name':_0x5115b7[_0x0a02('0x48')],'paused':_0x5115b7[_0x0a02('0x49')],'waiting':_0x5115b7[_0x0a02('0x4a')]};_0x3c6ba6[_0x0a02('0x21')](_0x2f0bbb);}}_0x27bd3d({'count':_0x3c6ba6['length'],'rows':_0x3c6ba6});});},'WhatsappQueues':function(_0x43151a){return new Promise(function(_0x17834a,_0x4c15de){var _0x288522=[];for(var _0x492f77 in _0xee8114[_0x0a02('0x51')]){if(_0xee8114['whatsappQueues']['hasOwnProperty'](_0x492f77)){var _0x22d0d9=_0xee8114['whatsappQueues'][_0x492f77];var _0x30105b={'id':_0x22d0d9['id'],'available':_0x22d0d9[_0x0a02('0x4c')],'loggedIn':_0x22d0d9['loggedIn'],'name':_0x22d0d9['name'],'paused':_0x22d0d9['paused'],'waiting':_0x22d0d9[_0x0a02('0x4a')]};_0x288522[_0x0a02('0x21')](_0x30105b);}}_0x17834a({'count':_0x288522[_0x0a02('0x19')],'rows':_0x288522});});},'ChatQueuesWaitingInteractions':function(_0x665114){return new Promise(function(_0x314abb,_0x8e014d){var _0x5b5572=_[_0x0a02('0x46')](_0xee8114[_0x0a02('0x52')]);_0x314abb({'count':_0x5b5572[_0x0a02('0x19')],'rows':_0x5b5572});});},'MailQueuesWaitingInteractions':function(_0x42ea68){return new Promise(function(_0x1d4c2c,_0x167a4d){var _0x902ac4=_['values'](_0xee8114[_0x0a02('0x53')]);_0x1d4c2c({'count':_0x902ac4[_0x0a02('0x19')],'rows':_0x902ac4});});},'OpenchannelQueuesWaitingInteractions':function(_0x42dc36){return new Promise(function(_0x4eed09,_0x5898b4){var _0x25a63a=_['values'](_0xee8114[_0x0a02('0x54')]);_0x4eed09({'count':_0x25a63a['length'],'rows':_0x25a63a});});},'SmsQueuesWaitingInteractions':function(_0x133b9f){return new Promise(function(_0x37b007,_0x2b62d8){var _0x58dc60=_[_0x0a02('0x46')](_0xee8114[_0x0a02('0x55')]);_0x37b007({'count':_0x58dc60[_0x0a02('0x19')],'rows':_0x58dc60});});},'FaxQueuesWaitingInteractions':function(_0x429806){return new Promise(function(_0x1c755a,_0xc3a655){var _0x4f1131=_[_0x0a02('0x46')](_0xee8114[_0x0a02('0x56')]);_0x1c755a({'count':_0x4f1131[_0x0a02('0x19')],'rows':_0x4f1131});});},'WhatsappQueuesWaitingInteractions':function(_0x3f47d6){return new Promise(function(_0x2debce,_0x1ffb2e){var _0x1e4e79=_[_0x0a02('0x46')](_0xee8114[_0x0a02('0x57')]);_0x2debce({'count':_0x1e4e79['length'],'rows':_0x1e4e79});});}};var _0x536bda=jayson[_0x0a02('0x58')](_0x33f8f5)[_0x0a02('0x59')]();_0x536bda['on'](_0x0a02('0x2f'),function(_0x36db6c){logger[_0x0a02('0x2f')](JSON[_0x0a02('0x5a')](_0x36db6c));return _0x7fb60e(_0x36db6c);});_0x536bda['listen'](0x232c,'127.0.0.1',function(){console['log'](_0x0a02('0x5b'),0x232c);return _0x115c77('RPC\x20listening\x20on\x20port:'+0x232c);});});};