Built motion from commit (unavailable).|2.2.0
[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 _0x7f71=['rules','message','interaction','applications','isArray','length','existsSync','join','app','toLowerCase','appdata','push','slice','amazonlex','tag','TagId','split','%s:%s:%s','condition','intervals','./applications/','consequence','priority','on\x20interval','\x20|\x20','Please\x20create\x20a\x20new\x20file\x20under','.js','execute','info','Applications\x20executed','matchPath','emit','end','No\x20applications\x20to\x20execute','error','channel','QueueReport','update','UserId','error\x20update\x20queue\x20report','stack','message\x20is\x20empty','agent','agents','busyQueue','capacity','hasOwnProperty','CurrentCapacity','updateRealtime','agent\x20not\x20found','values','chatQueues','loggedIn','name','waiting','mailQueues','available','paused','openchannelQueues','smsQueues','faxQueues','openchannelQueuesWaitingInteractions','smsQueuesWaitingInteractions','faxQueuesWaitingInteractions','server','http','stringify','127.0.0.1','log','Routing\x20RPC\x20server\x20listening\x20on\x20127.0.0.1:%d!','RPC\x20listening\x20on\x20port:','lodash','path','bluebird','../../../config/logger','../rpc/openchannelQueueReport','../rpc/mailQueueReport','../rpc/smsQueueReport','../rpc/faxQueueReport','../lib/rule-engine','format','%s.%s.%s','IntervalId','isNil','Intervals','map','interval','listen'];(function(_0x51cfa8,_0x577287){var _0x4e523f=function(_0x47a903){while(--_0x47a903){_0x51cfa8['push'](_0x51cfa8['shift']());}};_0x4e523f(++_0x577287);}(_0x7f71,0x9f));var _0x17f7=function(_0x487683,_0x58b9b8){_0x487683=_0x487683-0x0;var _0x178a4d=_0x7f71[_0x487683];return _0x178a4d;};'use strict';var _=require(_0x17f7('0x0'));var util=require('util');var path=require(_0x17f7('0x1'));var fs=require('fs');var jayson=require('jayson/promise');var BPromise=require(_0x17f7('0x2'));var uuid=require('uuid');var config=require('../../../config/environment');var logger=require(_0x17f7('0x3'))('routing');var rpc={'openchannelQueueReport':require(_0x17f7('0x4'))};var rpc={'openchannelQueueReport':require(_0x17f7('0x4')),'mailQueueReport':require(_0x17f7('0x5')),'chatQueueReport':require('../rpc/chatQueueReport'),'smsQueueReport':require(_0x17f7('0x6')),'faxQueueReport':require(_0x17f7('0x7'))};var RuleEngine=require(_0x17f7('0x8'));function getId(_0x3285ff,_0x139e5d,_0x23d703){return util[_0x17f7('0x9')](_0x17f7('0xa'),_0x3285ff,_0x139e5d||0x0,_0x23d703||0x0);}function getIntervals(_0x221587,_0x3a3269){var _0x313375=[];if(!_['isNil'](_0x3a3269[_0x17f7('0xb')])&&!_[_0x17f7('0xc')](_0x221587[_0x3a3269['IntervalId']])&&!_['isNil'](_0x221587[_0x3a3269[_0x17f7('0xb')]][_0x17f7('0xd')])){_0x313375=_[_0x17f7('0xe')](_0x221587[_0x3a3269['IntervalId']][_0x17f7('0xd')],'interval');}else if(!_['isNil'](_0x3a3269[_0x17f7('0xf')])){_0x313375=[_0x3a3269[_0x17f7('0xf')]];}return _0x313375;}exports[_0x17f7('0x10')]=function(_0x5d7e52){_0x5d7e52[_0x17f7('0x11')]={};return new BPromise(function(_0xff95ed,_0x3c5e1a){var _0x57aa3d={'Start':function(_0x314ecb){var _0x1a6f69=this;return new Promise(function(_0x1c0c14,_0x47f0e3){if(_0x314ecb){try{var _0x32cdef=[];var _0x29126a=getId(_0x314ecb['channel'],_0x314ecb['message']?_0x314ecb[_0x17f7('0x12')]['id']:0x0,_0x314ecb['interaction']?_0x314ecb[_0x17f7('0x13')]['id']:0x0);if(_0x314ecb[_0x17f7('0x14')]&&_[_0x17f7('0x15')](_0x314ecb[_0x17f7('0x14')])&&_0x314ecb['applications'][_0x17f7('0x16')]){for(var _0x435f2a=0x0;_0x435f2a<_0x314ecb[_0x17f7('0x14')][_0x17f7('0x16')];_0x435f2a+=0x1){var _0x12eefc=_0x314ecb[_0x17f7('0x14')][_0x435f2a];if(fs[_0x17f7('0x17')](path[_0x17f7('0x18')](__dirname,_0x17f7('0x14'),_0x12eefc[_0x17f7('0x19')]+'.js'))){var _0x318aaa=[];switch(_0x12eefc[_0x17f7('0x19')][_0x17f7('0x1a')]()){case'autoreply':var _0x30b81c=_0x12eefc[_0x17f7('0x1b')]['split'](',');_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x0]);_0x318aaa[_0x17f7('0x1c')](_0x30b81c[_0x17f7('0x1d')](0x1,_0x30b81c['length'])[_0x17f7('0x18')](','));break;case'dialogflow':var _0x30b81c=_0x12eefc[_0x17f7('0x1b')]['split'](',');_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x0]);_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x1]);_0x318aaa['push'](_0x30b81c[_0x17f7('0x1d')](0x2,_0x30b81c[_0x17f7('0x16')])[_0x17f7('0x18')](','));break;case _0x17f7('0x1e'):var _0x30b81c=_0x12eefc[_0x17f7('0x1b')]['split'](',');_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x0]);_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x1]);_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x2]);_0x318aaa[_0x17f7('0x1c')](_0x30b81c[0x3]);_0x318aaa[_0x17f7('0x1c')](_0x30b81c[_0x17f7('0x1d')](0x4,_0x30b81c['length'])[_0x17f7('0x18')](','));break;case _0x17f7('0x12'):_0x318aaa[_0x17f7('0x1c')](_0x12eefc[_0x17f7('0x1b')]);break;case _0x17f7('0x1f'):_0x318aaa[_0x17f7('0x1c')](_0x12eefc[_0x17f7('0x20')]);_0x318aaa['push'](_0x12eefc['appdata']);break;default:_0x318aaa=_[_0x17f7('0xc')](_0x12eefc['appdata'])?[]:_0x12eefc[_0x17f7('0x1b')][_0x17f7('0x21')](',');}var _0x43bc13={'id':util['format'](_0x17f7('0x22'),_0x29126a,_0x12eefc['id'],_0x12eefc[_0x17f7('0x19')]),'condition':require('./interval')[_0x17f7('0x23')](_0x29126a,getIntervals(_0x5d7e52[_0x17f7('0x24')],_0x12eefc),_0x5d7e52),'consequence':require(_0x17f7('0x25')+_0x12eefc[_0x17f7('0x19')])[_0x17f7('0x26')](_0x29126a,_0x318aaa,_0x5d7e52,_0x314ecb),'p':_0x12eefc[_0x17f7('0x27')]||0x0};logger['info']('Push\x20new\x20rule',_0x43bc13['id'],_0x17f7('0x28'),getIntervals(_0x5d7e52[_0x17f7('0x24')],_0x12eefc)[_0x17f7('0x18')](_0x17f7('0x29')));_0x32cdef[_0x17f7('0x1c')](_0x43bc13);}else{logger['error'](_0x17f7('0x2a'),path[_0x17f7('0x18')](__dirname,_0x17f7('0x14'),_0x12eefc['app']+_0x17f7('0x2b')));}}_0x5d7e52[_0x17f7('0x11')][_0x29126a]=new RuleEngine(_0x32cdef);_0x5d7e52['rules'][_0x29126a][_0x17f7('0x2c')](_0x314ecb,function(_0x420476){logger[_0x17f7('0x2d')](_0x17f7('0x2e'),JSON['stringify'](_0x420476[_0x17f7('0x2f')]));if(_0x5d7e52[_0x17f7('0x11')][_0x29126a]){_0x5d7e52[_0x17f7('0x11')][_0x29126a][_0x17f7('0x30')](_0x17f7('0x31'));}});return _0x1c0c14({'code':0xc8,'message':_0x314ecb});}else{logger[_0x17f7('0x2d')](_0x17f7('0x32'));return _0x1c0c14({'code':0xc8,'message':_0x314ecb});}}catch(_0x4e1dd2){logger[_0x17f7('0x33')](_0x4e1dd2['stack']);return _0x47f0e3(_0x1a6f69['error'](0x1f4,_0x4e1dd2));}}return _0x47f0e3('routing\x20is\x20empty');});},'AcceptMessage':function(_0x24b001){var _0x37009d=this;return new Promise(function(_0x463ab0,_0x4ec2e5){if(_0x24b001['message']&&_0x24b001[_0x17f7('0x12')]['id']){try{var _0x3d2f7f=getId(_0x24b001['channel'],_0x24b001[_0x17f7('0x12')]?_0x24b001[_0x17f7('0x12')]['id']:0x0,_0x24b001[_0x17f7('0x13')]?_0x24b001[_0x17f7('0x13')]['id']:0x0);if(_0x5d7e52[_0x17f7('0x11')][_0x3d2f7f]){_0x5d7e52[_0x17f7('0x11')][_0x3d2f7f][_0x17f7('0x30')](_0x17f7('0x31'));if(rpc[_0x24b001[_0x17f7('0x34')]+'QueueReport']){rpc[_0x24b001['channel']+_0x17f7('0x35')][_0x17f7('0x36')](_0x3d2f7f,{'UserId':_0x24b001[_0x17f7('0x12')][_0x17f7('0x37')]})['catch'](function(_0x3c3202){logger[_0x17f7('0x33')](_0x17f7('0x38'));});}}return _0x463ab0({'code':0xc8,'message':_0x24b001});}catch(_0x148bb9){logger[_0x17f7('0x33')](_0x148bb9[_0x17f7('0x39')]);return _0x4ec2e5(_0x37009d[_0x17f7('0x33')](0x1f4,_0x148bb9));}}logger[_0x17f7('0x33')](_0x17f7('0x3a'));return _0x4ec2e5(_0x37009d[_0x17f7('0x33')](0x1f4,_0x17f7('0x3a')));});},'RejectMessage':function(_0x156652){var _0x1af58e=this;return new Promise(function(_0x138f61,_0x5ae226){if(_0x156652[_0x17f7('0x3b')]&&_0x156652[_0x17f7('0x3b')]['id']){try{if(_0x5d7e52[_0x17f7('0x3c')][_0x156652['agent']['id']]){_0x5d7e52['agents'][_0x156652['agent']['id']]['busy']=![];_0x5d7e52['agents'][_0x156652[_0x17f7('0x3b')]['id']][_0x17f7('0x3d')]=undefined;}return _0x138f61({'code':0xc8,'message':_0x156652});}catch(_0x2b0b4f){logger[_0x17f7('0x33')](_0x2b0b4f[_0x17f7('0x39')]);return _0x5ae226(_0x1af58e[_0x17f7('0x33')](0x1f4,_0x2b0b4f));}}logger['error']('message\x20is\x20empty');return _0x5ae226(_0x1af58e[_0x17f7('0x33')](0x1f4,'message\x20is\x20empty'));});},'AbandonInteraction':function(_0xdca6a9){var _0x38b9e2=this;return new Promise(function(_0x27d407,_0x2fd78c){if(_0xdca6a9[_0x17f7('0x34')]&&_0xdca6a9[_0x17f7('0x13')]&&_0xdca6a9[_0x17f7('0x13')]['id']){try{var _0x214797=getId(_0xdca6a9[_0x17f7('0x34')],_0xdca6a9[_0x17f7('0x12')]?_0xdca6a9[_0x17f7('0x12')]['id']:0x0,_0xdca6a9['interaction']?_0xdca6a9[_0x17f7('0x13')]['id']:0x0);if(_0x5d7e52[_0x17f7('0x11')][_0x214797]){_0x5d7e52[_0x17f7('0x11')][_0x214797]['emit'](_0x17f7('0x31'));}return _0x27d407({'code':0xc8,'message':_0xdca6a9});}catch(_0x7fb12f){logger[_0x17f7('0x33')](_0x7fb12f['stack']);return _0x2fd78c(_0x38b9e2[_0x17f7('0x33')](0x1f4,_0x7fb12f));}}logger[_0x17f7('0x33')]('message\x20is\x20empty');return _0x2fd78c(_0x38b9e2[_0x17f7('0x33')](0x1f4,_0x17f7('0x3a')));});},'AgentCapacity':function(_0xeba9be){var _0x335793=this;return new Promise(function(_0x4f80c9,_0x5cd35a){if(_0x5d7e52[_0x17f7('0x3c')]['hasOwnProperty'](_0xeba9be['id'])){for(var _0x3f1c58 in _0xeba9be[_0x17f7('0x3e')]){if(_0xeba9be['capacity'][_0x17f7('0x3f')](_0x3f1c58)){if(_0x5d7e52[_0x17f7('0x3c')][_0xeba9be['id']][_0x17f7('0x3f')](_0x3f1c58+_0x17f7('0x40'))){_0x5d7e52[_0x17f7('0x3c')][_0xeba9be['id']][_0x3f1c58+_0x17f7('0x40')]=parseInt(_0xeba9be[_0x17f7('0x3e')][_0x3f1c58],0xa);}}}_0x5d7e52[_0x17f7('0x3c')][_0xeba9be['id']][_0x17f7('0x41')]();return _0x4f80c9({'code':0xc8,'message':_0x5d7e52[_0x17f7('0x3c')][_0xeba9be['id']]});}else{return _0x5cd35a(_0x335793[_0x17f7('0x33')](0x1f4,_0x17f7('0x42')));}});},'Agents':function(_0x5f162b){return new Promise(function(_0x13e5fb,_0x84a6b3){var _0xaf6bba=_[_0x17f7('0x43')](_0x5d7e52[_0x17f7('0x3c')]);_0x13e5fb({'count':_0xaf6bba['length'],'rows':_0xaf6bba});});},'ChatQueues':function(_0x22dbf6){return new Promise(function(_0x512224,_0x2d01d4){var _0x3f2248=[];for(var _0x50eb3d in _0x5d7e52[_0x17f7('0x44')]){if(_0x5d7e52[_0x17f7('0x44')]['hasOwnProperty'](_0x50eb3d)){var _0x1512cd=_0x5d7e52['chatQueues'][_0x50eb3d];var _0x2d346f={'id':_0x1512cd['id'],'available':_0x1512cd['available'],'loggedIn':_0x1512cd[_0x17f7('0x45')],'name':_0x1512cd[_0x17f7('0x46')],'paused':_0x1512cd['paused'],'waiting':_0x1512cd[_0x17f7('0x47')]};_0x3f2248[_0x17f7('0x1c')](_0x2d346f);}}_0x512224({'count':_0x3f2248[_0x17f7('0x16')],'rows':_0x3f2248});});},'MailQueues':function(_0x3cdc71){return new Promise(function(_0x42702a,_0x34d463){var _0x3459c0=[];for(var _0x20602d in _0x5d7e52[_0x17f7('0x48')]){if(_0x5d7e52[_0x17f7('0x48')][_0x17f7('0x3f')](_0x20602d)){var _0x182b11=_0x5d7e52[_0x17f7('0x48')][_0x20602d];var _0x40995e={'id':_0x182b11['id'],'available':_0x182b11[_0x17f7('0x49')],'loggedIn':_0x182b11[_0x17f7('0x45')],'name':_0x182b11[_0x17f7('0x46')],'paused':_0x182b11[_0x17f7('0x4a')],'waiting':_0x182b11[_0x17f7('0x47')]};_0x3459c0['push'](_0x40995e);}}_0x42702a({'count':_0x3459c0['length'],'rows':_0x3459c0});});},'OpenchannelQueues':function(_0x5dd1df){return new Promise(function(_0x3ab24d,_0x251ee0){var _0x30d80a=[];for(var _0x2c073b in _0x5d7e52[_0x17f7('0x4b')]){if(_0x5d7e52['openchannelQueues']['hasOwnProperty'](_0x2c073b)){var _0x1c9482=_0x5d7e52['openchannelQueues'][_0x2c073b];var _0x40bc03={'id':_0x1c9482['id'],'available':_0x1c9482[_0x17f7('0x49')],'loggedIn':_0x1c9482['loggedIn'],'name':_0x1c9482[_0x17f7('0x46')],'paused':_0x1c9482[_0x17f7('0x4a')],'waiting':_0x1c9482[_0x17f7('0x47')]};_0x30d80a[_0x17f7('0x1c')](_0x40bc03);}}_0x3ab24d({'count':_0x30d80a[_0x17f7('0x16')],'rows':_0x30d80a});});},'SmsQueues':function(_0x197ba2){return new Promise(function(_0x1bc92a,_0x5e38d7){var _0x529153=[];for(var _0x3bdbd8 in _0x5d7e52[_0x17f7('0x4c')]){if(_0x5d7e52[_0x17f7('0x4c')][_0x17f7('0x3f')](_0x3bdbd8)){var _0x2e2333=_0x5d7e52['smsQueues'][_0x3bdbd8];var _0x24922c={'id':_0x2e2333['id'],'available':_0x2e2333[_0x17f7('0x49')],'loggedIn':_0x2e2333['loggedIn'],'name':_0x2e2333['name'],'paused':_0x2e2333[_0x17f7('0x4a')],'waiting':_0x2e2333[_0x17f7('0x47')]};_0x529153[_0x17f7('0x1c')](_0x24922c);}}_0x1bc92a({'count':_0x529153[_0x17f7('0x16')],'rows':_0x529153});});},'FaxQueues':function(_0xe1cd07){return new Promise(function(_0x5b9eca,_0x33036f){var _0x42e452=[];for(var _0x1d0004 in _0x5d7e52[_0x17f7('0x4d')]){if(_0x5d7e52[_0x17f7('0x4d')][_0x17f7('0x3f')](_0x1d0004)){var _0x25834f=_0x5d7e52['faxQueues'][_0x1d0004];var _0x484f1d={'id':_0x25834f['id'],'available':_0x25834f[_0x17f7('0x49')],'loggedIn':_0x25834f[_0x17f7('0x45')],'name':_0x25834f[_0x17f7('0x46')],'paused':_0x25834f[_0x17f7('0x4a')],'waiting':_0x25834f['waiting']};_0x42e452[_0x17f7('0x1c')](_0x484f1d);}}_0x5b9eca({'count':_0x42e452[_0x17f7('0x16')],'rows':_0x42e452});});},'ChatQueuesWaitingInteractions':function(_0x15ffe7){return new Promise(function(_0x35b75b,_0x2d92df){var _0x268cdf=_[_0x17f7('0x43')](_0x5d7e52['chatQueuesWaitingInteractions']);_0x35b75b({'count':_0x268cdf[_0x17f7('0x16')],'rows':_0x268cdf});});},'MailQueuesWaitingInteractions':function(_0x3548f8){return new Promise(function(_0x2d4a53,_0x296949){var _0x36311c=_[_0x17f7('0x43')](_0x5d7e52['mailQueuesWaitingInteractions']);_0x2d4a53({'count':_0x36311c[_0x17f7('0x16')],'rows':_0x36311c});});},'OpenchannelQueuesWaitingInteractions':function(_0x4b68c0){return new Promise(function(_0x20dcb8,_0xbd9d6b){var _0x2ea0fa=_[_0x17f7('0x43')](_0x5d7e52[_0x17f7('0x4e')]);_0x20dcb8({'count':_0x2ea0fa['length'],'rows':_0x2ea0fa});});},'SmsQueuesWaitingInteractions':function(_0xfa6d86){return new Promise(function(_0x1faf1f,_0x3d8126){var _0x1fc990=_[_0x17f7('0x43')](_0x5d7e52[_0x17f7('0x4f')]);_0x1faf1f({'count':_0x1fc990['length'],'rows':_0x1fc990});});},'FaxQueuesWaitingInteractions':function(_0x15faf7){return new Promise(function(_0x21cbf2,_0x47a245){var _0xb7a64b=_['values'](_0x5d7e52[_0x17f7('0x50')]);_0x21cbf2({'count':_0xb7a64b[_0x17f7('0x16')],'rows':_0xb7a64b});});}};var _0x12f978=jayson[_0x17f7('0x51')](_0x57aa3d)[_0x17f7('0x52')]();_0x12f978['on'](_0x17f7('0x33'),function(_0x10f7b8){logger[_0x17f7('0x33')](JSON[_0x17f7('0x53')](_0x10f7b8));return _0x3c5e1a(_0x10f7b8);});_0x12f978[_0x17f7('0x10')](0x232c,_0x17f7('0x54'),function(){console[_0x17f7('0x55')](_0x17f7('0x56'),0x232c);return _0xff95ed(_0x17f7('0x57')+0x232c);});});};