0f663c2c008f071865287cacd173b2ea3a84fdd2
[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 _0xf725=['busyQueue','end','QueueReport','update','UserId','message\x20is\x20empty','busy','stack','hasOwnProperty','capacity','CurrentCapacity','values','chatQueues','available','loggedIn','name','paused','mailQueues','waiting','openchannelQueues','smsQueues','faxQueues','whatsappQueues','mailQueuesWaitingInteractions','openchannelQueuesWaitingInteractions','smsQueuesWaitingInteractions','faxQueuesWaitingInteractions','http','127.0.0.1','log','Routing\x20RPC\x20server\x20listening\x20on\x20127.0.0.1:%d!','RPC\x20listening\x20on\x20port:','lodash','util','path','jayson/promise','bluebird','uuid','../../../config/environment','../../../config/logger','routing','../rpc/openchannelQueueReport','../rpc/chatQueueReport','../rpc/smsQueueReport','../rpc/faxQueueReport','../rpc/whatsappQueueReport','../lib/rule-engine','%s.%s.%s','isNil','IntervalId','Intervals','map','interval','listen','rules','channel','message','interaction','applications','length','join','app','.js','toLowerCase','autoreply','appdata','split','mail','push','slice','amazonlex','tag','format','%s:%s:%s','./interval','condition','intervals','./applications/','consequence','priority','Push\x20new\x20rule','\x20|\x20','error','Please\x20create\x20a\x20new\x20file\x20under','execute','info','Applications\x20executed','stringify','matchPath','emit','complete','routing\x20is\x20empty','agent','agents'];(function(_0x51426b,_0x56069c){var _0xb53cc3=function(_0x24363c){while(--_0x24363c){_0x51426b['push'](_0x51426b['shift']());}};_0xb53cc3(++_0x56069c);}(_0xf725,0x7e));var _0x5f72=function(_0x1b536b,_0x12a816){_0x1b536b=_0x1b536b-0x0;var _0x1aca27=_0xf725[_0x1b536b];return _0x1aca27;};'use strict';var _=require(_0x5f72('0x0'));var util=require(_0x5f72('0x1'));var path=require(_0x5f72('0x2'));var fs=require('fs');var jayson=require(_0x5f72('0x3'));var BPromise=require(_0x5f72('0x4'));var uuid=require(_0x5f72('0x5'));var config=require(_0x5f72('0x6'));var logger=require(_0x5f72('0x7'))(_0x5f72('0x8'));var rpc={'openchannelQueueReport':require(_0x5f72('0x9')),'mailQueueReport':require('../rpc/mailQueueReport'),'chatQueueReport':require(_0x5f72('0xa')),'smsQueueReport':require(_0x5f72('0xb')),'faxQueueReport':require(_0x5f72('0xc')),'whatsappQueueReport':require(_0x5f72('0xd'))};var RuleEngine=require(_0x5f72('0xe'));function getId(_0x4961d1,_0x158aa3,_0x523365){return util['format'](_0x5f72('0xf'),_0x4961d1,_0x158aa3||0x0,_0x523365||0x0);}function getIntervals(_0x3f189d,_0x5b74cf){var _0x994d2e=[];if(!_[_0x5f72('0x10')](_0x5b74cf['IntervalId'])&&!_[_0x5f72('0x10')](_0x3f189d[_0x5b74cf[_0x5f72('0x11')]])&&!_[_0x5f72('0x10')](_0x3f189d[_0x5b74cf[_0x5f72('0x11')]][_0x5f72('0x12')])){_0x994d2e=_[_0x5f72('0x13')](_0x3f189d[_0x5b74cf[_0x5f72('0x11')]]['Intervals'],_0x5f72('0x14'));}else if(!_[_0x5f72('0x10')](_0x5b74cf[_0x5f72('0x14')])){_0x994d2e=[_0x5b74cf[_0x5f72('0x14')]];}return _0x994d2e;}exports[_0x5f72('0x15')]=function(_0x57007a){_0x57007a[_0x5f72('0x16')]={};return new BPromise(function(_0x4f81e9,_0x50f8c8){var _0x1665be={'Start':function(_0x5a036c){var _0x3267a=this;return new Promise(function(_0x4adc76,_0x317eed){if(_0x5a036c){try{var _0x4e0c32=[];var _0x44893b=getId(_0x5a036c[_0x5f72('0x17')],_0x5a036c['message']?_0x5a036c[_0x5f72('0x18')]['id']:0x0,_0x5a036c[_0x5f72('0x19')]?_0x5a036c[_0x5f72('0x19')]['id']:0x0);if(_0x5a036c['applications']&&_['isArray'](_0x5a036c['applications'])&&_0x5a036c[_0x5f72('0x1a')][_0x5f72('0x1b')]){for(var _0x269dae=0x0;_0x269dae<_0x5a036c[_0x5f72('0x1a')][_0x5f72('0x1b')];_0x269dae+=0x1){var _0x16b13e=_0x5a036c[_0x5f72('0x1a')][_0x269dae];if(fs['existsSync'](path[_0x5f72('0x1c')](__dirname,_0x5f72('0x1a'),_0x16b13e[_0x5f72('0x1d')]+_0x5f72('0x1e')))){var _0x471a9c=[];switch(_0x16b13e[_0x5f72('0x1d')][_0x5f72('0x1f')]()){case _0x5f72('0x20'):var _0x27b1f6=_0x16b13e[_0x5f72('0x21')][_0x5f72('0x22')](',');if(_0x5a036c['channel']===_0x5f72('0x23')){_0x471a9c[_0x5f72('0x24')](_0x27b1f6[0x0]);_0x471a9c[_0x5f72('0x24')](_0x27b1f6[0x1]);_0x471a9c[_0x5f72('0x24')](_0x27b1f6['slice'](0x2,_0x27b1f6[_0x5f72('0x1b')])[_0x5f72('0x1c')](','));}else{_0x471a9c['push'](_0x27b1f6[0x0]);_0x471a9c[_0x5f72('0x24')](_0x27b1f6[_0x5f72('0x25')](0x1,_0x27b1f6[_0x5f72('0x1b')])['join'](','));}break;case'dialogflow':var _0x3d4cfe=_0x16b13e[_0x5f72('0x21')][_0x5f72('0x22')](',');_0x471a9c[_0x5f72('0x24')](_0x3d4cfe[0x0]);_0x471a9c[_0x5f72('0x24')](_0x3d4cfe[0x1]);_0x471a9c[_0x5f72('0x24')](_0x3d4cfe[_0x5f72('0x25')](0x2,_0x3d4cfe['length'])[_0x5f72('0x1c')](','));break;case'dialogflowv2':var _0xc8ca5a=_0x16b13e[_0x5f72('0x21')][_0x5f72('0x22')](',');_0x471a9c[_0x5f72('0x24')](_0xc8ca5a[0x0]);_0x471a9c[_0x5f72('0x24')](_0xc8ca5a[0x1]);_0x471a9c['push'](_0xc8ca5a[0x2]);_0x471a9c['push'](_0xc8ca5a[0x3]);_0x471a9c[_0x5f72('0x24')](_0xc8ca5a[_0x5f72('0x25')](0x4,_0xc8ca5a[_0x5f72('0x1b')])[_0x5f72('0x1c')](','));break;case _0x5f72('0x26'):var _0x7ed167=_0x16b13e[_0x5f72('0x21')]['split'](',');_0x471a9c[_0x5f72('0x24')](_0x7ed167[0x0]);_0x471a9c[_0x5f72('0x24')](_0x7ed167[0x1]);_0x471a9c[_0x5f72('0x24')](_0x7ed167[0x2]);_0x471a9c[_0x5f72('0x24')](_0x7ed167[0x3]);_0x471a9c[_0x5f72('0x24')](_0x7ed167['slice'](0x4,_0x7ed167[_0x5f72('0x1b')])[_0x5f72('0x1c')](','));break;case _0x5f72('0x18'):_0x471a9c[_0x5f72('0x24')](_0x16b13e[_0x5f72('0x21')]);break;case _0x5f72('0x27'):_0x471a9c['push'](_0x16b13e['TagId']);_0x471a9c[_0x5f72('0x24')](_0x16b13e[_0x5f72('0x21')]);break;default:_0x471a9c=_['isNil'](_0x16b13e[_0x5f72('0x21')])?[]:_0x16b13e[_0x5f72('0x21')][_0x5f72('0x22')](',');}var _0xf15ac8={'id':util[_0x5f72('0x28')](_0x5f72('0x29'),_0x44893b,_0x16b13e['id'],_0x16b13e[_0x5f72('0x1d')]),'condition':require(_0x5f72('0x2a'))[_0x5f72('0x2b')](_0x44893b,getIntervals(_0x57007a[_0x5f72('0x2c')],_0x16b13e),_0x57007a),'consequence':require(_0x5f72('0x2d')+_0x16b13e[_0x5f72('0x1d')])[_0x5f72('0x2e')](_0x44893b,_0x471a9c,_0x57007a,_0x5a036c),'p':_0x16b13e[_0x5f72('0x2f')]||0x0};logger['info'](_0x5f72('0x30'),_0xf15ac8['id'],'on\x20interval',getIntervals(_0x57007a['intervals'],_0x16b13e)[_0x5f72('0x1c')](_0x5f72('0x31')));_0x4e0c32[_0x5f72('0x24')](_0xf15ac8);}else{logger[_0x5f72('0x32')](_0x5f72('0x33'),path[_0x5f72('0x1c')](__dirname,'applications',_0x16b13e[_0x5f72('0x1d')]+_0x5f72('0x1e')));}}_0x57007a[_0x5f72('0x16')][_0x44893b]=new RuleEngine(_0x4e0c32);_0x57007a[_0x5f72('0x16')][_0x44893b][_0x5f72('0x34')](_0x5a036c,function(_0xccfc5d){logger[_0x5f72('0x35')](_0x5f72('0x36'),JSON[_0x5f72('0x37')](_0xccfc5d[_0x5f72('0x38')]));if(_0x57007a[_0x5f72('0x16')][_0x44893b]){_0x57007a[_0x5f72('0x16')][_0x44893b][_0x5f72('0x39')](_0x5f72('0x3a'));}});return _0x4adc76({'code':0xc8,'message':_0x5a036c});}else{logger[_0x5f72('0x35')]('No\x20applications\x20to\x20execute');return _0x4adc76({'code':0xc8,'message':_0x5a036c});}}catch(_0x594bb4){logger[_0x5f72('0x32')](_0x594bb4['stack']);return _0x317eed(_0x3267a['error'](0x1f4,_0x594bb4));}}return _0x317eed(_0x5f72('0x3b'));});},'AcceptMessage':function(_0x71e8e7){var _0x57d320=this;return new Promise(function(_0x3f0f8d,_0x42298c){if(_0x71e8e7['agent']&&_0x71e8e7[_0x5f72('0x3c')]['id']){if(_0x57007a[_0x5f72('0x3d')][_0x71e8e7[_0x5f72('0x3c')]['id']]){_0x57007a[_0x5f72('0x3d')][_0x71e8e7['agent']['id']]['busy']=![];_0x57007a[_0x5f72('0x3d')][_0x71e8e7[_0x5f72('0x3c')]['id']][_0x5f72('0x3e')]=undefined;}}if(_0x71e8e7[_0x5f72('0x18')]&&_0x71e8e7[_0x5f72('0x18')]['id']){try{var _0x6be017=getId(_0x71e8e7[_0x5f72('0x17')],_0x71e8e7['message']?_0x71e8e7[_0x5f72('0x18')]['id']:0x0,_0x71e8e7[_0x5f72('0x19')]?_0x71e8e7[_0x5f72('0x19')]['id']:0x0);if(_0x57007a[_0x5f72('0x16')][_0x6be017]){_0x57007a['rules'][_0x6be017][_0x5f72('0x39')](_0x5f72('0x3f'));if(rpc[_0x71e8e7[_0x5f72('0x17')]+_0x5f72('0x40')]){rpc[_0x71e8e7['channel']+_0x5f72('0x40')][_0x5f72('0x41')](_0x6be017,{'UserId':_0x71e8e7[_0x5f72('0x18')][_0x5f72('0x42')]})['catch'](function(_0x942c55){logger[_0x5f72('0x32')]('error\x20update\x20queue\x20report');});}}return _0x3f0f8d({'code':0xc8,'message':_0x71e8e7});}catch(_0x1911b0){logger[_0x5f72('0x32')](_0x1911b0['stack']);return _0x42298c(_0x57d320[_0x5f72('0x32')](0x1f4,_0x1911b0));}}logger[_0x5f72('0x32')](_0x5f72('0x43'));return _0x42298c(_0x57d320['error'](0x1f4,_0x5f72('0x43')));});},'RejectMessage':function(_0x5efa48){var _0x30a9b6=this;return new Promise(function(_0x355e23,_0x53e8d0){if(_0x5efa48[_0x5f72('0x3c')]&&_0x5efa48[_0x5f72('0x3c')]['id']){try{if(_0x57007a[_0x5f72('0x3d')][_0x5efa48[_0x5f72('0x3c')]['id']]){_0x57007a['agents'][_0x5efa48[_0x5f72('0x3c')]['id']][_0x5f72('0x44')]=![];_0x57007a[_0x5f72('0x3d')][_0x5efa48[_0x5f72('0x3c')]['id']]['busyQueue']=undefined;}return _0x355e23({'code':0xc8,'message':_0x5efa48});}catch(_0x16a452){logger['error'](_0x16a452[_0x5f72('0x45')]);return _0x53e8d0(_0x30a9b6[_0x5f72('0x32')](0x1f4,_0x16a452));}}logger[_0x5f72('0x32')](_0x5f72('0x43'));return _0x53e8d0(_0x30a9b6[_0x5f72('0x32')](0x1f4,_0x5f72('0x43')));});},'AbandonInteraction':function(_0x28008c){var _0x35116c=this;return new Promise(function(_0xfab233,_0x1d4a2f){if(_0x28008c['channel']&&_0x28008c[_0x5f72('0x19')]&&_0x28008c[_0x5f72('0x19')]['id']){try{var _0x5c5d94=getId(_0x28008c[_0x5f72('0x17')],_0x28008c[_0x5f72('0x18')]?_0x28008c['message']['id']:0x0,_0x28008c[_0x5f72('0x19')]?_0x28008c[_0x5f72('0x19')]['id']:0x0);if(_0x57007a[_0x5f72('0x16')][_0x5c5d94]){_0x57007a[_0x5f72('0x16')][_0x5c5d94][_0x5f72('0x39')]('end');}return _0xfab233({'code':0xc8,'message':_0x28008c});}catch(_0x540144){logger[_0x5f72('0x32')](_0x540144[_0x5f72('0x45')]);return _0x1d4a2f(_0x35116c[_0x5f72('0x32')](0x1f4,_0x540144));}}logger['error'](_0x5f72('0x43'));return _0x1d4a2f(_0x35116c[_0x5f72('0x32')](0x1f4,_0x5f72('0x43')));});},'AgentCapacity':function(_0x587edd){var _0x397b2a=this;return new Promise(function(_0x241231,_0x5e0fd9){if(_0x57007a['agents'][_0x5f72('0x46')](_0x587edd['id'])){for(var _0x57655b in _0x587edd['capacity']){if(_0x587edd[_0x5f72('0x47')][_0x5f72('0x46')](_0x57655b)){if(_0x57007a[_0x5f72('0x3d')][_0x587edd['id']][_0x5f72('0x46')](_0x57655b+_0x5f72('0x48'))){_0x57007a[_0x5f72('0x3d')][_0x587edd['id']][_0x57655b+_0x5f72('0x48')]=parseInt(_0x587edd['capacity'][_0x57655b],0xa);}}}_0x57007a[_0x5f72('0x3d')][_0x587edd['id']]['updateRealtime']();return _0x241231({'code':0xc8,'message':_0x57007a[_0x5f72('0x3d')][_0x587edd['id']]});}else{return _0x5e0fd9(_0x397b2a[_0x5f72('0x32')](0x1f4,'agent\x20not\x20found'));}});},'Agents':function(_0x4c3930){return new Promise(function(_0x2995dc,_0x269f23){var _0x39036d=_[_0x5f72('0x49')](_0x57007a['agents']);_0x2995dc({'count':_0x39036d[_0x5f72('0x1b')],'rows':_0x39036d});});},'ChatQueues':function(_0x1db15b){return new Promise(function(_0x14e20b,_0x48cae9){var _0x12a464=[];for(var _0x29113a in _0x57007a[_0x5f72('0x4a')]){if(_0x57007a[_0x5f72('0x4a')][_0x5f72('0x46')](_0x29113a)){var _0x36774e=_0x57007a[_0x5f72('0x4a')][_0x29113a];var _0x15e9a5={'id':_0x36774e['id'],'available':_0x36774e[_0x5f72('0x4b')],'loggedIn':_0x36774e[_0x5f72('0x4c')],'name':_0x36774e[_0x5f72('0x4d')],'paused':_0x36774e[_0x5f72('0x4e')],'waiting':_0x36774e['waiting']};_0x12a464[_0x5f72('0x24')](_0x15e9a5);}}_0x14e20b({'count':_0x12a464[_0x5f72('0x1b')],'rows':_0x12a464});});},'MailQueues':function(_0x27b9d7){return new Promise(function(_0x339a8d,_0xf1d356){var _0x39bc0e=[];for(var _0x55daf6 in _0x57007a[_0x5f72('0x4f')]){if(_0x57007a[_0x5f72('0x4f')][_0x5f72('0x46')](_0x55daf6)){var _0x419279=_0x57007a['mailQueues'][_0x55daf6];var _0x306c75={'id':_0x419279['id'],'available':_0x419279['available'],'loggedIn':_0x419279[_0x5f72('0x4c')],'name':_0x419279[_0x5f72('0x4d')],'paused':_0x419279[_0x5f72('0x4e')],'waiting':_0x419279[_0x5f72('0x50')]};_0x39bc0e['push'](_0x306c75);}}_0x339a8d({'count':_0x39bc0e[_0x5f72('0x1b')],'rows':_0x39bc0e});});},'OpenchannelQueues':function(_0x86b9a){return new Promise(function(_0x23d8c2,_0xb07770){var _0x59af10=[];for(var _0x50bd1a in _0x57007a[_0x5f72('0x51')]){if(_0x57007a['openchannelQueues']['hasOwnProperty'](_0x50bd1a)){var _0x130e4b=_0x57007a[_0x5f72('0x51')][_0x50bd1a];var _0x2a5dcb={'id':_0x130e4b['id'],'available':_0x130e4b['available'],'loggedIn':_0x130e4b[_0x5f72('0x4c')],'name':_0x130e4b[_0x5f72('0x4d')],'paused':_0x130e4b[_0x5f72('0x4e')],'waiting':_0x130e4b[_0x5f72('0x50')]};_0x59af10[_0x5f72('0x24')](_0x2a5dcb);}}_0x23d8c2({'count':_0x59af10[_0x5f72('0x1b')],'rows':_0x59af10});});},'SmsQueues':function(_0x342a31){return new Promise(function(_0x2475e9,_0x2369ec){var _0x24aa1e=[];for(var _0x196482 in _0x57007a[_0x5f72('0x52')]){if(_0x57007a[_0x5f72('0x52')]['hasOwnProperty'](_0x196482)){var _0x1c0ba1=_0x57007a[_0x5f72('0x52')][_0x196482];var _0x3753b5={'id':_0x1c0ba1['id'],'available':_0x1c0ba1['available'],'loggedIn':_0x1c0ba1[_0x5f72('0x4c')],'name':_0x1c0ba1['name'],'paused':_0x1c0ba1['paused'],'waiting':_0x1c0ba1['waiting']};_0x24aa1e['push'](_0x3753b5);}}_0x2475e9({'count':_0x24aa1e['length'],'rows':_0x24aa1e});});},'FaxQueues':function(_0x1cd273){return new Promise(function(_0x4cc70c,_0x551736){var _0x2a846b=[];for(var _0x2404fd in _0x57007a[_0x5f72('0x53')]){if(_0x57007a[_0x5f72('0x53')][_0x5f72('0x46')](_0x2404fd)){var _0x3352c5=_0x57007a[_0x5f72('0x53')][_0x2404fd];var _0x23ebb4={'id':_0x3352c5['id'],'available':_0x3352c5['available'],'loggedIn':_0x3352c5[_0x5f72('0x4c')],'name':_0x3352c5[_0x5f72('0x4d')],'paused':_0x3352c5[_0x5f72('0x4e')],'waiting':_0x3352c5[_0x5f72('0x50')]};_0x2a846b[_0x5f72('0x24')](_0x23ebb4);}}_0x4cc70c({'count':_0x2a846b[_0x5f72('0x1b')],'rows':_0x2a846b});});},'WhatsappQueues':function(_0x1da7de){return new Promise(function(_0x3317d9,_0x251883){var _0x14b72b=[];for(var _0x18e110 in _0x57007a[_0x5f72('0x54')]){if(_0x57007a[_0x5f72('0x54')][_0x5f72('0x46')](_0x18e110)){var _0x1d5d04=_0x57007a['whatsappQueues'][_0x18e110];var _0x164fba={'id':_0x1d5d04['id'],'available':_0x1d5d04[_0x5f72('0x4b')],'loggedIn':_0x1d5d04[_0x5f72('0x4c')],'name':_0x1d5d04['name'],'paused':_0x1d5d04[_0x5f72('0x4e')],'waiting':_0x1d5d04['waiting']};_0x14b72b[_0x5f72('0x24')](_0x164fba);}}_0x3317d9({'count':_0x14b72b[_0x5f72('0x1b')],'rows':_0x14b72b});});},'ChatQueuesWaitingInteractions':function(_0x43cfc0){return new Promise(function(_0x4281b7,_0x4ee2f6){var _0x8886b4=_[_0x5f72('0x49')](_0x57007a['chatQueuesWaitingInteractions']);_0x4281b7({'count':_0x8886b4[_0x5f72('0x1b')],'rows':_0x8886b4});});},'MailQueuesWaitingInteractions':function(_0x2ee75d){return new Promise(function(_0x3fb18b,_0x2cc457){var _0x2500d1=_[_0x5f72('0x49')](_0x57007a[_0x5f72('0x55')]);_0x3fb18b({'count':_0x2500d1[_0x5f72('0x1b')],'rows':_0x2500d1});});},'OpenchannelQueuesWaitingInteractions':function(_0x5ba635){return new Promise(function(_0x71d3c6,_0x653bd0){var _0xaaae35=_[_0x5f72('0x49')](_0x57007a[_0x5f72('0x56')]);_0x71d3c6({'count':_0xaaae35[_0x5f72('0x1b')],'rows':_0xaaae35});});},'SmsQueuesWaitingInteractions':function(_0x139175){return new Promise(function(_0x28e4e8,_0x50d2ae){var _0x2f1346=_[_0x5f72('0x49')](_0x57007a[_0x5f72('0x57')]);_0x28e4e8({'count':_0x2f1346['length'],'rows':_0x2f1346});});},'FaxQueuesWaitingInteractions':function(_0x3b2cff){return new Promise(function(_0xb078a3,_0x2aff96){var _0x54b9cd=_[_0x5f72('0x49')](_0x57007a[_0x5f72('0x58')]);_0xb078a3({'count':_0x54b9cd[_0x5f72('0x1b')],'rows':_0x54b9cd});});},'WhatsappQueuesWaitingInteractions':function(_0x3afc6a){return new Promise(function(_0xc929da,_0x310930){var _0x43064f=_['values'](_0x57007a['whatsappQueuesWaitingInteractions']);_0xc929da({'count':_0x43064f['length'],'rows':_0x43064f});});}};var _0x4d2539=jayson['server'](_0x1665be)[_0x5f72('0x59')]();_0x4d2539['on']('error',function(_0xa0db47){logger[_0x5f72('0x32')](JSON[_0x5f72('0x37')](_0xa0db47));return _0x50f8c8(_0xa0db47);});_0x4d2539[_0x5f72('0x15')](0x232c,_0x5f72('0x5a'),function(){console[_0x5f72('0x5b')](_0x5f72('0x5c'),0x232c);return _0x4f81e9(_0x5f72('0x5d')+0x232c);});});};