633913536f72267d91942d5770c493872507a366
[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 _0x317f=['end','QueueReport','update','UserId','catch','error','message\x20is\x20empty','agent','agents','busy','hasOwnProperty','capacity','CurrentCapacity','agent\x20not\x20found','values','chatQueues','available','waiting','mailQueues','loggedIn','name','paused','openchannelQueues','smsQueues','faxQueues','whatsappQueues','chatQueuesWaitingInteractions','mailQueuesWaitingInteractions','openchannelQueuesWaitingInteractions','whatsappQueuesWaitingInteractions','server','http','listen','log','Routing\x20RPC\x20server\x20listening\x20on\x20127.0.0.1:%d!','RPC\x20listening\x20on\x20port:','lodash','util','path','jayson/promise','bluebird','../../../config/logger','routing','../rpc/openchannelQueueReport','../rpc/mailQueueReport','../rpc/chatQueueReport','../rpc/whatsappQueueReport','../lib/rule-engine','format','%s.%s.%s','isNil','IntervalId','map','Intervals','interval','rules','message','interaction','applications','isArray','length','existsSync','app','.js','toLowerCase','autoreply','appdata','split','push','slice','join','dialogflow','amazonlex','tag','./interval','condition','intervals','./applications/','consequence','priority','info','Push\x20new\x20rule','on\x20interval','\x20|\x20','Please\x20create\x20a\x20new\x20file\x20under','execute','Applications\x20executed','stringify','matchPath','No\x20applications\x20to\x20execute','stack','routing\x20is\x20empty','channel','emit'];(function(_0x32d457,_0x1a39c9){var _0x338386=function(_0x199d7a){while(--_0x199d7a){_0x32d457['push'](_0x32d457['shift']());}};_0x338386(++_0x1a39c9);}(_0x317f,0x82));var _0xf317=function(_0x53dee9,_0x1dc404){_0x53dee9=_0x53dee9-0x0;var _0x4c5da0=_0x317f[_0x53dee9];return _0x4c5da0;};'use strict';var _=require(_0xf317('0x0'));var util=require(_0xf317('0x1'));var path=require(_0xf317('0x2'));var fs=require('fs');var jayson=require(_0xf317('0x3'));var BPromise=require(_0xf317('0x4'));var uuid=require('uuid');var config=require('../../../config/environment');var logger=require(_0xf317('0x5'))(_0xf317('0x6'));var rpc={'openchannelQueueReport':require(_0xf317('0x7')),'mailQueueReport':require(_0xf317('0x8')),'chatQueueReport':require(_0xf317('0x9')),'smsQueueReport':require('../rpc/smsQueueReport'),'faxQueueReport':require('../rpc/faxQueueReport'),'whatsappQueueReport':require(_0xf317('0xa'))};var RuleEngine=require(_0xf317('0xb'));function getId(_0x379c36,_0x33a9ab,_0x40c4c6){return util[_0xf317('0xc')](_0xf317('0xd'),_0x379c36,_0x33a9ab||0x0,_0x40c4c6||0x0);}function getIntervals(_0x376035,_0x543721){var _0x4a1385=[];if(!_[_0xf317('0xe')](_0x543721[_0xf317('0xf')])&&!_[_0xf317('0xe')](_0x376035[_0x543721[_0xf317('0xf')]])&&!_[_0xf317('0xe')](_0x376035[_0x543721['IntervalId']]['Intervals'])){_0x4a1385=_[_0xf317('0x10')](_0x376035[_0x543721[_0xf317('0xf')]][_0xf317('0x11')],_0xf317('0x12'));}else if(!_[_0xf317('0xe')](_0x543721[_0xf317('0x12')])){_0x4a1385=[_0x543721[_0xf317('0x12')]];}return _0x4a1385;}exports['listen']=function(_0x851b7c){_0x851b7c[_0xf317('0x13')]={};return new BPromise(function(_0x5be48f,_0x3df151){var _0x4165e9={'Start':function(_0x108be4){var _0x23765c=this;return new Promise(function(_0x43c91d,_0x28ff29){if(_0x108be4){try{var _0x442cba=[];var _0x357bbd=getId(_0x108be4['channel'],_0x108be4[_0xf317('0x14')]?_0x108be4[_0xf317('0x14')]['id']:0x0,_0x108be4[_0xf317('0x15')]?_0x108be4[_0xf317('0x15')]['id']:0x0);if(_0x108be4[_0xf317('0x16')]&&_[_0xf317('0x17')](_0x108be4[_0xf317('0x16')])&&_0x108be4[_0xf317('0x16')][_0xf317('0x18')]){for(var _0x317eaf=0x0;_0x317eaf<_0x108be4[_0xf317('0x16')]['length'];_0x317eaf+=0x1){var _0x2c6782=_0x108be4[_0xf317('0x16')][_0x317eaf];if(fs[_0xf317('0x19')](path['join'](__dirname,'applications',_0x2c6782[_0xf317('0x1a')]+_0xf317('0x1b')))){var _0x3521c4=[];switch(_0x2c6782[_0xf317('0x1a')][_0xf317('0x1c')]()){case _0xf317('0x1d'):var _0x135e38=_0x2c6782[_0xf317('0x1e')][_0xf317('0x1f')](',');_0x3521c4[_0xf317('0x20')](_0x135e38[0x0]);_0x3521c4[_0xf317('0x20')](_0x135e38[_0xf317('0x21')](0x1,_0x135e38['length'])[_0xf317('0x22')](','));break;case _0xf317('0x23'):var _0xccf4f8=_0x2c6782[_0xf317('0x1e')][_0xf317('0x1f')](',');_0x3521c4['push'](_0xccf4f8[0x0]);_0x3521c4['push'](_0xccf4f8[0x1]);_0x3521c4['push'](_0xccf4f8['slice'](0x2,_0xccf4f8[_0xf317('0x18')])['join'](','));break;case _0xf317('0x24'):var _0x1e8371=_0x2c6782[_0xf317('0x1e')]['split'](',');_0x3521c4['push'](_0x1e8371[0x0]);_0x3521c4[_0xf317('0x20')](_0x1e8371[0x1]);_0x3521c4[_0xf317('0x20')](_0x1e8371[0x2]);_0x3521c4['push'](_0x1e8371[0x3]);_0x3521c4[_0xf317('0x20')](_0x1e8371['slice'](0x4,_0x1e8371[_0xf317('0x18')])[_0xf317('0x22')](','));break;case'message':_0x3521c4[_0xf317('0x20')](_0x2c6782[_0xf317('0x1e')]);break;case _0xf317('0x25'):_0x3521c4[_0xf317('0x20')](_0x2c6782['TagId']);_0x3521c4[_0xf317('0x20')](_0x2c6782[_0xf317('0x1e')]);break;default:_0x3521c4=_[_0xf317('0xe')](_0x2c6782[_0xf317('0x1e')])?[]:_0x2c6782[_0xf317('0x1e')][_0xf317('0x1f')](',');}var _0x26e9b3={'id':util['format']('%s:%s:%s',_0x357bbd,_0x2c6782['id'],_0x2c6782[_0xf317('0x1a')]),'condition':require(_0xf317('0x26'))[_0xf317('0x27')](_0x357bbd,getIntervals(_0x851b7c[_0xf317('0x28')],_0x2c6782),_0x851b7c),'consequence':require(_0xf317('0x29')+_0x2c6782[_0xf317('0x1a')])[_0xf317('0x2a')](_0x357bbd,_0x3521c4,_0x851b7c,_0x108be4),'p':_0x2c6782[_0xf317('0x2b')]||0x0};logger[_0xf317('0x2c')](_0xf317('0x2d'),_0x26e9b3['id'],_0xf317('0x2e'),getIntervals(_0x851b7c['intervals'],_0x2c6782)[_0xf317('0x22')](_0xf317('0x2f')));_0x442cba['push'](_0x26e9b3);}else{logger['error'](_0xf317('0x30'),path[_0xf317('0x22')](__dirname,_0xf317('0x16'),_0x2c6782[_0xf317('0x1a')]+_0xf317('0x1b')));}}_0x851b7c[_0xf317('0x13')][_0x357bbd]=new RuleEngine(_0x442cba);_0x851b7c[_0xf317('0x13')][_0x357bbd][_0xf317('0x31')](_0x108be4,function(_0x1b7421){logger['info'](_0xf317('0x32'),JSON[_0xf317('0x33')](_0x1b7421[_0xf317('0x34')]));if(_0x851b7c[_0xf317('0x13')][_0x357bbd]){_0x851b7c[_0xf317('0x13')][_0x357bbd]['emit']('complete');}});return _0x43c91d({'code':0xc8,'message':_0x108be4});}else{logger['info'](_0xf317('0x35'));return _0x43c91d({'code':0xc8,'message':_0x108be4});}}catch(_0x413da9){logger['error'](_0x413da9[_0xf317('0x36')]);return _0x28ff29(_0x23765c['error'](0x1f4,_0x413da9));}}return _0x28ff29(_0xf317('0x37'));});},'AcceptMessage':function(_0x2861a7){var _0x243a9b=this;return new Promise(function(_0x12004d,_0x41ee0d){if(_0x2861a7[_0xf317('0x14')]&&_0x2861a7[_0xf317('0x14')]['id']){try{var _0x46f2e9=getId(_0x2861a7[_0xf317('0x38')],_0x2861a7['message']?_0x2861a7[_0xf317('0x14')]['id']:0x0,_0x2861a7[_0xf317('0x15')]?_0x2861a7['interaction']['id']:0x0);if(_0x851b7c[_0xf317('0x13')][_0x46f2e9]){_0x851b7c[_0xf317('0x13')][_0x46f2e9][_0xf317('0x39')](_0xf317('0x3a'));if(rpc[_0x2861a7[_0xf317('0x38')]+_0xf317('0x3b')]){rpc[_0x2861a7[_0xf317('0x38')]+_0xf317('0x3b')][_0xf317('0x3c')](_0x46f2e9,{'UserId':_0x2861a7['message'][_0xf317('0x3d')]})[_0xf317('0x3e')](function(_0x593de2){logger[_0xf317('0x3f')]('error\x20update\x20queue\x20report');});}}return _0x12004d({'code':0xc8,'message':_0x2861a7});}catch(_0x41a7fd){logger['error'](_0x41a7fd[_0xf317('0x36')]);return _0x41ee0d(_0x243a9b['error'](0x1f4,_0x41a7fd));}}logger[_0xf317('0x3f')](_0xf317('0x40'));return _0x41ee0d(_0x243a9b[_0xf317('0x3f')](0x1f4,_0xf317('0x40')));});},'RejectMessage':function(_0x241a1e){var _0x4556f4=this;return new Promise(function(_0x94766c,_0x4e717b){if(_0x241a1e[_0xf317('0x41')]&&_0x241a1e[_0xf317('0x41')]['id']){try{if(_0x851b7c[_0xf317('0x42')][_0x241a1e[_0xf317('0x41')]['id']]){_0x851b7c[_0xf317('0x42')][_0x241a1e['agent']['id']][_0xf317('0x43')]=![];_0x851b7c['agents'][_0x241a1e[_0xf317('0x41')]['id']]['busyQueue']=undefined;}return _0x94766c({'code':0xc8,'message':_0x241a1e});}catch(_0x976038){logger[_0xf317('0x3f')](_0x976038[_0xf317('0x36')]);return _0x4e717b(_0x4556f4[_0xf317('0x3f')](0x1f4,_0x976038));}}logger[_0xf317('0x3f')](_0xf317('0x40'));return _0x4e717b(_0x4556f4['error'](0x1f4,'message\x20is\x20empty'));});},'AbandonInteraction':function(_0x400dd8){var _0x406aac=this;return new Promise(function(_0x2daef4,_0x1443cd){if(_0x400dd8[_0xf317('0x38')]&&_0x400dd8['interaction']&&_0x400dd8[_0xf317('0x15')]['id']){try{var _0x3d5c87=getId(_0x400dd8[_0xf317('0x38')],_0x400dd8[_0xf317('0x14')]?_0x400dd8['message']['id']:0x0,_0x400dd8[_0xf317('0x15')]?_0x400dd8[_0xf317('0x15')]['id']:0x0);if(_0x851b7c[_0xf317('0x13')][_0x3d5c87]){_0x851b7c[_0xf317('0x13')][_0x3d5c87][_0xf317('0x39')](_0xf317('0x3a'));}return _0x2daef4({'code':0xc8,'message':_0x400dd8});}catch(_0xa0f73f){logger[_0xf317('0x3f')](_0xa0f73f[_0xf317('0x36')]);return _0x1443cd(_0x406aac[_0xf317('0x3f')](0x1f4,_0xa0f73f));}}logger['error']('message\x20is\x20empty');return _0x1443cd(_0x406aac[_0xf317('0x3f')](0x1f4,_0xf317('0x40')));});},'AgentCapacity':function(_0x3b3048){var _0xf8f7f3=this;return new Promise(function(_0x3f41f8,_0x859fe1){if(_0x851b7c['agents'][_0xf317('0x44')](_0x3b3048['id'])){for(var _0x2511ff in _0x3b3048[_0xf317('0x45')]){if(_0x3b3048['capacity'][_0xf317('0x44')](_0x2511ff)){if(_0x851b7c[_0xf317('0x42')][_0x3b3048['id']][_0xf317('0x44')](_0x2511ff+_0xf317('0x46'))){_0x851b7c['agents'][_0x3b3048['id']][_0x2511ff+'CurrentCapacity']=parseInt(_0x3b3048[_0xf317('0x45')][_0x2511ff],0xa);}}}_0x851b7c[_0xf317('0x42')][_0x3b3048['id']]['updateRealtime']();return _0x3f41f8({'code':0xc8,'message':_0x851b7c[_0xf317('0x42')][_0x3b3048['id']]});}else{return _0x859fe1(_0xf8f7f3[_0xf317('0x3f')](0x1f4,_0xf317('0x47')));}});},'Agents':function(_0x319862){return new Promise(function(_0x1c0b0a,_0x5494e4){var _0x55ae3e=_[_0xf317('0x48')](_0x851b7c[_0xf317('0x42')]);_0x1c0b0a({'count':_0x55ae3e[_0xf317('0x18')],'rows':_0x55ae3e});});},'ChatQueues':function(_0x123bc8){return new Promise(function(_0xd3609e,_0xf280cf){var _0x1dd353=[];for(var _0x593c7a in _0x851b7c['chatQueues']){if(_0x851b7c[_0xf317('0x49')][_0xf317('0x44')](_0x593c7a)){var _0x43a9b6=_0x851b7c[_0xf317('0x49')][_0x593c7a];var _0x4e06c6={'id':_0x43a9b6['id'],'available':_0x43a9b6[_0xf317('0x4a')],'loggedIn':_0x43a9b6['loggedIn'],'name':_0x43a9b6['name'],'paused':_0x43a9b6['paused'],'waiting':_0x43a9b6[_0xf317('0x4b')]};_0x1dd353[_0xf317('0x20')](_0x4e06c6);}}_0xd3609e({'count':_0x1dd353['length'],'rows':_0x1dd353});});},'MailQueues':function(_0x52fb9d){return new Promise(function(_0x5e5b17,_0x548200){var _0x281986=[];for(var _0x266b6b in _0x851b7c[_0xf317('0x4c')]){if(_0x851b7c[_0xf317('0x4c')][_0xf317('0x44')](_0x266b6b)){var _0x2fbc57=_0x851b7c[_0xf317('0x4c')][_0x266b6b];var _0x49badb={'id':_0x2fbc57['id'],'available':_0x2fbc57['available'],'loggedIn':_0x2fbc57[_0xf317('0x4d')],'name':_0x2fbc57[_0xf317('0x4e')],'paused':_0x2fbc57[_0xf317('0x4f')],'waiting':_0x2fbc57['waiting']};_0x281986[_0xf317('0x20')](_0x49badb);}}_0x5e5b17({'count':_0x281986[_0xf317('0x18')],'rows':_0x281986});});},'OpenchannelQueues':function(_0x4579e4){return new Promise(function(_0x17a99c,_0x1d0cd5){var _0x2d0830=[];for(var _0x1e9195 in _0x851b7c[_0xf317('0x50')]){if(_0x851b7c[_0xf317('0x50')]['hasOwnProperty'](_0x1e9195)){var _0x329177=_0x851b7c[_0xf317('0x50')][_0x1e9195];var _0x2c9161={'id':_0x329177['id'],'available':_0x329177['available'],'loggedIn':_0x329177[_0xf317('0x4d')],'name':_0x329177[_0xf317('0x4e')],'paused':_0x329177['paused'],'waiting':_0x329177['waiting']};_0x2d0830['push'](_0x2c9161);}}_0x17a99c({'count':_0x2d0830[_0xf317('0x18')],'rows':_0x2d0830});});},'SmsQueues':function(_0x3c7c3c){return new Promise(function(_0x5dfff5,_0x46049a){var _0x5d9401=[];for(var _0x169c2d in _0x851b7c[_0xf317('0x51')]){if(_0x851b7c[_0xf317('0x51')][_0xf317('0x44')](_0x169c2d)){var _0x2eaa17=_0x851b7c[_0xf317('0x51')][_0x169c2d];var _0x55b35b={'id':_0x2eaa17['id'],'available':_0x2eaa17[_0xf317('0x4a')],'loggedIn':_0x2eaa17[_0xf317('0x4d')],'name':_0x2eaa17['name'],'paused':_0x2eaa17[_0xf317('0x4f')],'waiting':_0x2eaa17[_0xf317('0x4b')]};_0x5d9401[_0xf317('0x20')](_0x55b35b);}}_0x5dfff5({'count':_0x5d9401[_0xf317('0x18')],'rows':_0x5d9401});});},'FaxQueues':function(_0x12d2c0){return new Promise(function(_0x165840,_0x239486){var _0x2f0a92=[];for(var _0xfebe6f in _0x851b7c[_0xf317('0x52')]){if(_0x851b7c[_0xf317('0x52')][_0xf317('0x44')](_0xfebe6f)){var _0x207794=_0x851b7c['faxQueues'][_0xfebe6f];var _0x595179={'id':_0x207794['id'],'available':_0x207794[_0xf317('0x4a')],'loggedIn':_0x207794[_0xf317('0x4d')],'name':_0x207794[_0xf317('0x4e')],'paused':_0x207794[_0xf317('0x4f')],'waiting':_0x207794[_0xf317('0x4b')]};_0x2f0a92[_0xf317('0x20')](_0x595179);}}_0x165840({'count':_0x2f0a92['length'],'rows':_0x2f0a92});});},'WhatsappQueues':function(_0x483524){return new Promise(function(_0x361100,_0x18c7e6){var _0x11191e=[];for(var _0x34ab77 in _0x851b7c[_0xf317('0x53')]){if(_0x851b7c[_0xf317('0x53')]['hasOwnProperty'](_0x34ab77)){var _0x4d0f14=_0x851b7c['whatsappQueues'][_0x34ab77];var _0x536e9e={'id':_0x4d0f14['id'],'available':_0x4d0f14[_0xf317('0x4a')],'loggedIn':_0x4d0f14[_0xf317('0x4d')],'name':_0x4d0f14[_0xf317('0x4e')],'paused':_0x4d0f14[_0xf317('0x4f')],'waiting':_0x4d0f14['waiting']};_0x11191e[_0xf317('0x20')](_0x536e9e);}}_0x361100({'count':_0x11191e[_0xf317('0x18')],'rows':_0x11191e});});},'ChatQueuesWaitingInteractions':function(_0x5ea0e9){return new Promise(function(_0x354774,_0x451372){var _0x59a09d=_[_0xf317('0x48')](_0x851b7c[_0xf317('0x54')]);_0x354774({'count':_0x59a09d['length'],'rows':_0x59a09d});});},'MailQueuesWaitingInteractions':function(_0x484503){return new Promise(function(_0x52a0c3,_0x286fe0){var _0x176058=_[_0xf317('0x48')](_0x851b7c[_0xf317('0x55')]);_0x52a0c3({'count':_0x176058[_0xf317('0x18')],'rows':_0x176058});});},'OpenchannelQueuesWaitingInteractions':function(_0x4ac586){return new Promise(function(_0x392280,_0x6f075a){var _0x2a9cf2=_[_0xf317('0x48')](_0x851b7c[_0xf317('0x56')]);_0x392280({'count':_0x2a9cf2[_0xf317('0x18')],'rows':_0x2a9cf2});});},'SmsQueuesWaitingInteractions':function(_0x45067e){return new Promise(function(_0x2a80e7,_0x3356d6){var _0x395148=_[_0xf317('0x48')](_0x851b7c['smsQueuesWaitingInteractions']);_0x2a80e7({'count':_0x395148[_0xf317('0x18')],'rows':_0x395148});});},'FaxQueuesWaitingInteractions':function(_0x102582){return new Promise(function(_0x4e0f04,_0xdfbb3){var _0x52aa9e=_[_0xf317('0x48')](_0x851b7c['faxQueuesWaitingInteractions']);_0x4e0f04({'count':_0x52aa9e['length'],'rows':_0x52aa9e});});},'WhatsappQueuesWaitingInteractions':function(_0x557d5a){return new Promise(function(_0x18a79c,_0x33f2b9){var _0x632655=_['values'](_0x851b7c[_0xf317('0x57')]);_0x18a79c({'count':_0x632655[_0xf317('0x18')],'rows':_0x632655});});}};var _0x3d6553=jayson[_0xf317('0x58')](_0x4165e9)[_0xf317('0x59')]();_0x3d6553['on'](_0xf317('0x3f'),function(_0x5b8889){logger['error'](JSON[_0xf317('0x33')](_0x5b8889));return _0x3df151(_0x5b8889);});_0x3d6553[_0xf317('0x5a')](0x232c,'127.0.0.1',function(){console[_0xf317('0x5b')](_0xf317('0x5c'),0x232c);return _0x5be48f(_0xf317('0x5d')+0x232c);});});};