Built motion from commit (unavailable).|2.5.27
[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 _0x8a28=['condition','intervals','./applications/','consequence','priority','Push\x20new\x20rule','on\x20interval','\x20|\x20','error','Please\x20create\x20a\x20new\x20file\x20under','execute','info','Applications\x20executed','stringify','matchPath','emit','complete','No\x20applications\x20to\x20execute','routing\x20is\x20empty','agent','agents','busy','busyQueue','end','QueueReport','update','UserId','catch','error\x20update\x20queue\x20report','stack','message\x20is\x20empty','capacity','hasOwnProperty','CurrentCapacity','updateRealtime','agent\x20not\x20found','values','chatQueues','available','paused','mailQueues','loggedIn','name','openchannelQueues','waiting','smsQueues','faxQueues','whatsappQueues','openchannelQueuesWaitingInteractions','faxQueuesWaitingInteractions','whatsappQueuesWaitingInteractions','server','http','listen','127.0.0.1','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','../rpc/openchannelQueueReport','../rpc/mailQueueReport','../rpc/chatQueueReport','../rpc/smsQueueReport','../rpc/faxQueueReport','../lib/rule-engine','format','%s.%s.%s','isNil','IntervalId','Intervals','map','interval','rules','channel','message','interaction','applications','isArray','length','join','app','.js','toLowerCase','autoreply','appdata','push','slice','dialogflow','amazonlex','split','TagId','./interval'];(function(_0x510783,_0x158f2f){var _0x3944f2=function(_0x390db9){while(--_0x390db9){_0x510783['push'](_0x510783['shift']());}};_0x3944f2(++_0x158f2f);}(_0x8a28,0xfd));var _0x88a2=function(_0x2b5eae,_0x55010d){_0x2b5eae=_0x2b5eae-0x0;var _0x5f3ac9=_0x8a28[_0x2b5eae];return _0x5f3ac9;};'use strict';var _=require(_0x88a2('0x0'));var util=require(_0x88a2('0x1'));var path=require(_0x88a2('0x2'));var fs=require('fs');var jayson=require(_0x88a2('0x3'));var BPromise=require(_0x88a2('0x4'));var uuid=require(_0x88a2('0x5'));var config=require(_0x88a2('0x6'));var logger=require(_0x88a2('0x7'))('routing');var rpc={'openchannelQueueReport':require(_0x88a2('0x8')),'mailQueueReport':require(_0x88a2('0x9')),'chatQueueReport':require(_0x88a2('0xa')),'smsQueueReport':require(_0x88a2('0xb')),'faxQueueReport':require(_0x88a2('0xc')),'whatsappQueueReport':require('../rpc/whatsappQueueReport')};var RuleEngine=require(_0x88a2('0xd'));function getId(_0x5406ed,_0x1cdf6b,_0x2a8576){return util[_0x88a2('0xe')](_0x88a2('0xf'),_0x5406ed,_0x1cdf6b||0x0,_0x2a8576||0x0);}function getIntervals(_0x2db7dd,_0x2d0fce){var _0x183ca3=[];if(!_[_0x88a2('0x10')](_0x2d0fce[_0x88a2('0x11')])&&!_['isNil'](_0x2db7dd[_0x2d0fce['IntervalId']])&&!_[_0x88a2('0x10')](_0x2db7dd[_0x2d0fce[_0x88a2('0x11')]][_0x88a2('0x12')])){_0x183ca3=_[_0x88a2('0x13')](_0x2db7dd[_0x2d0fce[_0x88a2('0x11')]]['Intervals'],_0x88a2('0x14'));}else if(!_[_0x88a2('0x10')](_0x2d0fce[_0x88a2('0x14')])){_0x183ca3=[_0x2d0fce[_0x88a2('0x14')]];}return _0x183ca3;}exports['listen']=function(_0x923706){_0x923706[_0x88a2('0x15')]={};return new BPromise(function(_0x3b83ce,_0x59801e){var _0x34f19e={'Start':function(_0x207b12){var _0x462b7b=this;return new Promise(function(_0x42facc,_0x1beccd){if(_0x207b12){try{var _0x209d30=[];var _0xf48d17=getId(_0x207b12[_0x88a2('0x16')],_0x207b12[_0x88a2('0x17')]?_0x207b12[_0x88a2('0x17')]['id']:0x0,_0x207b12[_0x88a2('0x18')]?_0x207b12[_0x88a2('0x18')]['id']:0x0);if(_0x207b12[_0x88a2('0x19')]&&_[_0x88a2('0x1a')](_0x207b12[_0x88a2('0x19')])&&_0x207b12[_0x88a2('0x19')][_0x88a2('0x1b')]){for(var _0x46ed82=0x0;_0x46ed82<_0x207b12['applications'][_0x88a2('0x1b')];_0x46ed82+=0x1){var _0x282a27=_0x207b12['applications'][_0x46ed82];if(fs['existsSync'](path[_0x88a2('0x1c')](__dirname,_0x88a2('0x19'),_0x282a27[_0x88a2('0x1d')]+_0x88a2('0x1e')))){var _0x2039ee=[];switch(_0x282a27['app'][_0x88a2('0x1f')]()){case _0x88a2('0x20'):var _0x36cc2c=_0x282a27[_0x88a2('0x21')]['split'](',');if(_0x207b12[_0x88a2('0x16')]==='mail'){_0x2039ee['push'](_0x36cc2c[0x0]);_0x2039ee[_0x88a2('0x22')](_0x36cc2c[0x1]);_0x2039ee[_0x88a2('0x22')](_0x36cc2c['slice'](0x2,_0x36cc2c['length'])[_0x88a2('0x1c')](','));}else{_0x2039ee[_0x88a2('0x22')](_0x36cc2c[0x0]);_0x2039ee['push'](_0x36cc2c[_0x88a2('0x23')](0x1,_0x36cc2c[_0x88a2('0x1b')])[_0x88a2('0x1c')](','));}break;case _0x88a2('0x24'):var _0x54873b=_0x282a27[_0x88a2('0x21')]['split'](',');_0x2039ee[_0x88a2('0x22')](_0x54873b[0x0]);_0x2039ee[_0x88a2('0x22')](_0x54873b[0x1]);_0x2039ee[_0x88a2('0x22')](_0x54873b['slice'](0x2,_0x54873b[_0x88a2('0x1b')])[_0x88a2('0x1c')](','));break;case'dialogflowv2':var _0x21bd1b=_0x282a27[_0x88a2('0x21')]['split'](',');_0x2039ee['push'](_0x21bd1b[0x0]);_0x2039ee[_0x88a2('0x22')](_0x21bd1b[0x1]);_0x2039ee[_0x88a2('0x22')](_0x21bd1b[0x2]);_0x2039ee[_0x88a2('0x22')](_0x21bd1b[0x3]);_0x2039ee[_0x88a2('0x22')](_0x21bd1b[_0x88a2('0x23')](0x4,_0x21bd1b[_0x88a2('0x1b')])['join'](','));break;case _0x88a2('0x25'):var _0x1a3739=_0x282a27[_0x88a2('0x21')][_0x88a2('0x26')](',');_0x2039ee[_0x88a2('0x22')](_0x1a3739[0x0]);_0x2039ee[_0x88a2('0x22')](_0x1a3739[0x1]);_0x2039ee[_0x88a2('0x22')](_0x1a3739[0x2]);_0x2039ee[_0x88a2('0x22')](_0x1a3739[0x3]);_0x2039ee[_0x88a2('0x22')](_0x1a3739[_0x88a2('0x23')](0x4,_0x1a3739['length'])['join'](','));break;case _0x88a2('0x17'):_0x2039ee[_0x88a2('0x22')](_0x282a27[_0x88a2('0x21')]);break;case'tag':_0x2039ee[_0x88a2('0x22')](_0x282a27[_0x88a2('0x27')]);_0x2039ee['push'](_0x282a27[_0x88a2('0x21')]);break;default:_0x2039ee=_[_0x88a2('0x10')](_0x282a27[_0x88a2('0x21')])?[]:_0x282a27['appdata']['split'](',');}var _0xd8b70b={'id':util[_0x88a2('0xe')]('%s:%s:%s',_0xf48d17,_0x282a27['id'],_0x282a27[_0x88a2('0x1d')]),'condition':require(_0x88a2('0x28'))[_0x88a2('0x29')](_0xf48d17,getIntervals(_0x923706[_0x88a2('0x2a')],_0x282a27),_0x923706),'consequence':require(_0x88a2('0x2b')+_0x282a27[_0x88a2('0x1d')])[_0x88a2('0x2c')](_0xf48d17,_0x2039ee,_0x923706,_0x207b12),'p':_0x282a27[_0x88a2('0x2d')]||0x0};logger['info'](_0x88a2('0x2e'),_0xd8b70b['id'],_0x88a2('0x2f'),getIntervals(_0x923706['intervals'],_0x282a27)[_0x88a2('0x1c')](_0x88a2('0x30')));_0x209d30[_0x88a2('0x22')](_0xd8b70b);}else{logger[_0x88a2('0x31')](_0x88a2('0x32'),path[_0x88a2('0x1c')](__dirname,_0x88a2('0x19'),_0x282a27[_0x88a2('0x1d')]+_0x88a2('0x1e')));}}_0x923706[_0x88a2('0x15')][_0xf48d17]=new RuleEngine(_0x209d30);_0x923706[_0x88a2('0x15')][_0xf48d17][_0x88a2('0x33')](_0x207b12,function(_0x1f2bc5){logger[_0x88a2('0x34')](_0x88a2('0x35'),JSON[_0x88a2('0x36')](_0x1f2bc5[_0x88a2('0x37')]));if(_0x923706[_0x88a2('0x15')][_0xf48d17]){_0x923706[_0x88a2('0x15')][_0xf48d17][_0x88a2('0x38')](_0x88a2('0x39'));}});return _0x42facc({'code':0xc8,'message':_0x207b12});}else{logger[_0x88a2('0x34')](_0x88a2('0x3a'));return _0x42facc({'code':0xc8,'message':_0x207b12});}}catch(_0x3df13d){logger[_0x88a2('0x31')](_0x3df13d['stack']);return _0x1beccd(_0x462b7b['error'](0x1f4,_0x3df13d));}}return _0x1beccd(_0x88a2('0x3b'));});},'AcceptMessage':function(_0x37af7f){var _0x555415=this;return new Promise(function(_0x11e426,_0x4333f6){if(_0x37af7f[_0x88a2('0x3c')]&&_0x37af7f[_0x88a2('0x3c')]['id']){if(_0x923706[_0x88a2('0x3d')][_0x37af7f[_0x88a2('0x3c')]['id']]){_0x923706['agents'][_0x37af7f[_0x88a2('0x3c')]['id']][_0x88a2('0x3e')]=![];_0x923706[_0x88a2('0x3d')][_0x37af7f['agent']['id']][_0x88a2('0x3f')]=undefined;}}if(_0x37af7f[_0x88a2('0x17')]&&_0x37af7f[_0x88a2('0x17')]['id']){try{var _0x2ac23a=getId(_0x37af7f['channel'],_0x37af7f[_0x88a2('0x17')]?_0x37af7f[_0x88a2('0x17')]['id']:0x0,_0x37af7f[_0x88a2('0x18')]?_0x37af7f[_0x88a2('0x18')]['id']:0x0);if(_0x923706[_0x88a2('0x15')][_0x2ac23a]){_0x923706[_0x88a2('0x15')][_0x2ac23a][_0x88a2('0x38')](_0x88a2('0x40'));if(rpc[_0x37af7f[_0x88a2('0x16')]+_0x88a2('0x41')]){rpc[_0x37af7f[_0x88a2('0x16')]+_0x88a2('0x41')][_0x88a2('0x42')](_0x2ac23a,{'UserId':_0x37af7f[_0x88a2('0x17')][_0x88a2('0x43')]})[_0x88a2('0x44')](function(_0x318913){logger[_0x88a2('0x31')](_0x88a2('0x45'));});}}return _0x11e426({'code':0xc8,'message':_0x37af7f});}catch(_0x3fd34d){logger['error'](_0x3fd34d[_0x88a2('0x46')]);return _0x4333f6(_0x555415['error'](0x1f4,_0x3fd34d));}}logger[_0x88a2('0x31')]('message\x20is\x20empty');return _0x4333f6(_0x555415[_0x88a2('0x31')](0x1f4,_0x88a2('0x47')));});},'RejectMessage':function(_0x1c5e6b){var _0x37319d=this;return new Promise(function(_0x2ace4b,_0x172562){if(_0x1c5e6b[_0x88a2('0x3c')]&&_0x1c5e6b[_0x88a2('0x3c')]['id']){try{if(_0x923706['agents'][_0x1c5e6b[_0x88a2('0x3c')]['id']]){_0x923706[_0x88a2('0x3d')][_0x1c5e6b['agent']['id']][_0x88a2('0x3e')]=![];_0x923706['agents'][_0x1c5e6b[_0x88a2('0x3c')]['id']]['busyQueue']=undefined;}return _0x2ace4b({'code':0xc8,'message':_0x1c5e6b});}catch(_0x4dcaa5){logger[_0x88a2('0x31')](_0x4dcaa5[_0x88a2('0x46')]);return _0x172562(_0x37319d[_0x88a2('0x31')](0x1f4,_0x4dcaa5));}}logger[_0x88a2('0x31')](_0x88a2('0x47'));return _0x172562(_0x37319d[_0x88a2('0x31')](0x1f4,_0x88a2('0x47')));});},'AbandonInteraction':function(_0x2cf43b){var _0x33e64c=this;return new Promise(function(_0x3887c5,_0x193d5e){if(_0x2cf43b['channel']&&_0x2cf43b['interaction']&&_0x2cf43b[_0x88a2('0x18')]['id']){try{var _0x889d30=getId(_0x2cf43b[_0x88a2('0x16')],_0x2cf43b[_0x88a2('0x17')]?_0x2cf43b[_0x88a2('0x17')]['id']:0x0,_0x2cf43b['interaction']?_0x2cf43b[_0x88a2('0x18')]['id']:0x0);if(_0x923706[_0x88a2('0x15')][_0x889d30]){_0x923706['rules'][_0x889d30][_0x88a2('0x38')](_0x88a2('0x40'));}return _0x3887c5({'code':0xc8,'message':_0x2cf43b});}catch(_0x588fcf){logger[_0x88a2('0x31')](_0x588fcf['stack']);return _0x193d5e(_0x33e64c[_0x88a2('0x31')](0x1f4,_0x588fcf));}}logger[_0x88a2('0x31')](_0x88a2('0x47'));return _0x193d5e(_0x33e64c[_0x88a2('0x31')](0x1f4,_0x88a2('0x47')));});},'AgentCapacity':function(_0x9b675d){var _0x50f58c=this;return new Promise(function(_0x9f2786,_0x10327e){if(_0x923706[_0x88a2('0x3d')]['hasOwnProperty'](_0x9b675d['id'])){for(var _0x1b95d3 in _0x9b675d[_0x88a2('0x48')]){if(_0x9b675d['capacity'][_0x88a2('0x49')](_0x1b95d3)){if(_0x923706[_0x88a2('0x3d')][_0x9b675d['id']]['hasOwnProperty'](_0x1b95d3+'CurrentCapacity')){_0x923706[_0x88a2('0x3d')][_0x9b675d['id']][_0x1b95d3+_0x88a2('0x4a')]=parseInt(_0x9b675d['capacity'][_0x1b95d3],0xa);}}}_0x923706[_0x88a2('0x3d')][_0x9b675d['id']][_0x88a2('0x4b')]();return _0x9f2786({'code':0xc8,'message':_0x923706[_0x88a2('0x3d')][_0x9b675d['id']]});}else{return _0x10327e(_0x50f58c[_0x88a2('0x31')](0x1f4,_0x88a2('0x4c')));}});},'Agents':function(_0x35ac79){return new Promise(function(_0x606c52,_0x53519f){var _0x3222f1=_[_0x88a2('0x4d')](_0x923706[_0x88a2('0x3d')]);_0x606c52({'count':_0x3222f1['length'],'rows':_0x3222f1});});},'ChatQueues':function(_0x5bbb2d){return new Promise(function(_0x53f380,_0x3b96ef){var _0x3907e7=[];for(var _0x435077 in _0x923706['chatQueues']){if(_0x923706[_0x88a2('0x4e')][_0x88a2('0x49')](_0x435077)){var _0x33b528=_0x923706[_0x88a2('0x4e')][_0x435077];var _0x25c090={'id':_0x33b528['id'],'available':_0x33b528[_0x88a2('0x4f')],'loggedIn':_0x33b528['loggedIn'],'name':_0x33b528['name'],'paused':_0x33b528[_0x88a2('0x50')],'waiting':_0x33b528['waiting']};_0x3907e7['push'](_0x25c090);}}_0x53f380({'count':_0x3907e7[_0x88a2('0x1b')],'rows':_0x3907e7});});},'MailQueues':function(_0x2cb78a){return new Promise(function(_0x46457c,_0x291f1f){var _0x21b09c=[];for(var _0x53e1af in _0x923706['mailQueues']){if(_0x923706[_0x88a2('0x51')][_0x88a2('0x49')](_0x53e1af)){var _0x1c264a=_0x923706[_0x88a2('0x51')][_0x53e1af];var _0x40bd68={'id':_0x1c264a['id'],'available':_0x1c264a[_0x88a2('0x4f')],'loggedIn':_0x1c264a[_0x88a2('0x52')],'name':_0x1c264a[_0x88a2('0x53')],'paused':_0x1c264a[_0x88a2('0x50')],'waiting':_0x1c264a['waiting']};_0x21b09c['push'](_0x40bd68);}}_0x46457c({'count':_0x21b09c[_0x88a2('0x1b')],'rows':_0x21b09c});});},'OpenchannelQueues':function(_0x21f140){return new Promise(function(_0x48623a,_0x4b3930){var _0x5e538c=[];for(var _0x27ecac in _0x923706[_0x88a2('0x54')]){if(_0x923706[_0x88a2('0x54')][_0x88a2('0x49')](_0x27ecac)){var _0x5638db=_0x923706[_0x88a2('0x54')][_0x27ecac];var _0x3b76bf={'id':_0x5638db['id'],'available':_0x5638db[_0x88a2('0x4f')],'loggedIn':_0x5638db[_0x88a2('0x52')],'name':_0x5638db[_0x88a2('0x53')],'paused':_0x5638db[_0x88a2('0x50')],'waiting':_0x5638db[_0x88a2('0x55')]};_0x5e538c['push'](_0x3b76bf);}}_0x48623a({'count':_0x5e538c['length'],'rows':_0x5e538c});});},'SmsQueues':function(_0x298ae7){return new Promise(function(_0x505211,_0x5f56e2){var _0x1a7619=[];for(var _0x3aaeba in _0x923706[_0x88a2('0x56')]){if(_0x923706[_0x88a2('0x56')][_0x88a2('0x49')](_0x3aaeba)){var _0x58b8b4=_0x923706[_0x88a2('0x56')][_0x3aaeba];var _0x481f44={'id':_0x58b8b4['id'],'available':_0x58b8b4[_0x88a2('0x4f')],'loggedIn':_0x58b8b4['loggedIn'],'name':_0x58b8b4[_0x88a2('0x53')],'paused':_0x58b8b4['paused'],'waiting':_0x58b8b4[_0x88a2('0x55')]};_0x1a7619[_0x88a2('0x22')](_0x481f44);}}_0x505211({'count':_0x1a7619[_0x88a2('0x1b')],'rows':_0x1a7619});});},'FaxQueues':function(_0x34873b){return new Promise(function(_0xcdbd8,_0xd98ec5){var _0x70213e=[];for(var _0x1d6efb in _0x923706[_0x88a2('0x57')]){if(_0x923706[_0x88a2('0x57')]['hasOwnProperty'](_0x1d6efb)){var _0x4784be=_0x923706[_0x88a2('0x57')][_0x1d6efb];var _0x4efe3f={'id':_0x4784be['id'],'available':_0x4784be[_0x88a2('0x4f')],'loggedIn':_0x4784be['loggedIn'],'name':_0x4784be['name'],'paused':_0x4784be[_0x88a2('0x50')],'waiting':_0x4784be[_0x88a2('0x55')]};_0x70213e[_0x88a2('0x22')](_0x4efe3f);}}_0xcdbd8({'count':_0x70213e[_0x88a2('0x1b')],'rows':_0x70213e});});},'WhatsappQueues':function(_0x1f1006){return new Promise(function(_0x2d4f9a,_0x502bdc){var _0x40edff=[];for(var _0x185c5a in _0x923706[_0x88a2('0x58')]){if(_0x923706[_0x88a2('0x58')]['hasOwnProperty'](_0x185c5a)){var _0x5d2deb=_0x923706[_0x88a2('0x58')][_0x185c5a];var _0xbaa029={'id':_0x5d2deb['id'],'available':_0x5d2deb['available'],'loggedIn':_0x5d2deb[_0x88a2('0x52')],'name':_0x5d2deb[_0x88a2('0x53')],'paused':_0x5d2deb[_0x88a2('0x50')],'waiting':_0x5d2deb[_0x88a2('0x55')]};_0x40edff['push'](_0xbaa029);}}_0x2d4f9a({'count':_0x40edff[_0x88a2('0x1b')],'rows':_0x40edff});});},'ChatQueuesWaitingInteractions':function(_0x14c831){return new Promise(function(_0x1db40c,_0x1350b2){var _0x54b65d=_[_0x88a2('0x4d')](_0x923706['chatQueuesWaitingInteractions']);_0x1db40c({'count':_0x54b65d[_0x88a2('0x1b')],'rows':_0x54b65d});});},'MailQueuesWaitingInteractions':function(_0x1452ed){return new Promise(function(_0x17b8bd,_0x59e359){var _0x38a5ad=_[_0x88a2('0x4d')](_0x923706['mailQueuesWaitingInteractions']);_0x17b8bd({'count':_0x38a5ad[_0x88a2('0x1b')],'rows':_0x38a5ad});});},'OpenchannelQueuesWaitingInteractions':function(_0x2cb588){return new Promise(function(_0x1bf412,_0xf9f243){var _0x3f7f74=_[_0x88a2('0x4d')](_0x923706[_0x88a2('0x59')]);_0x1bf412({'count':_0x3f7f74[_0x88a2('0x1b')],'rows':_0x3f7f74});});},'SmsQueuesWaitingInteractions':function(_0x539453){return new Promise(function(_0x527dca,_0x26e7c6){var _0x20d161=_[_0x88a2('0x4d')](_0x923706['smsQueuesWaitingInteractions']);_0x527dca({'count':_0x20d161[_0x88a2('0x1b')],'rows':_0x20d161});});},'FaxQueuesWaitingInteractions':function(_0x4dd6bd){return new Promise(function(_0x24cd6e,_0x442910){var _0x56640b=_[_0x88a2('0x4d')](_0x923706[_0x88a2('0x5a')]);_0x24cd6e({'count':_0x56640b[_0x88a2('0x1b')],'rows':_0x56640b});});},'WhatsappQueuesWaitingInteractions':function(_0x3617ef){return new Promise(function(_0xcb1375,_0x4f628a){var _0x30749e=_[_0x88a2('0x4d')](_0x923706[_0x88a2('0x5b')]);_0xcb1375({'count':_0x30749e[_0x88a2('0x1b')],'rows':_0x30749e});});}};var _0x9d26d1=jayson[_0x88a2('0x5c')](_0x34f19e)[_0x88a2('0x5d')]();_0x9d26d1['on']('error',function(_0x3e5b8f){logger[_0x88a2('0x31')](JSON['stringify'](_0x3e5b8f));return _0x59801e(_0x3e5b8f);});_0x9d26d1[_0x88a2('0x5e')](0x232c,_0x88a2('0x5f'),function(){console['log'](_0x88a2('0x60'),0x232c);return _0x3b83ce(_0x88a2('0x61')+0x232c);});});};