415287f74f68de6a7a5b283af18f0f4bd1f7a825
[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 _0x9f76=['channel','message','interaction','isArray','applications','length','existsSync','join','app','toLowerCase','autoreply','split','push','slice','dialogflow','appdata','amazonlex','tag','%s:%s:%s','condition','./applications/','consequence','priority','info','on\x20interval','intervals','\x20|\x20','.js','rules','Applications\x20executed','stringify','matchPath','emit','complete','error','stack','routing\x20is\x20empty','end','QueueReport','UserId','catch','error\x20update\x20queue\x20report','message\x20is\x20empty','agent','agents','hasOwnProperty','capacity','CurrentCapacity','agent\x20not\x20found','values','chatQueues','available','loggedIn','name','paused','waiting','mailQueues','openchannelQueues','smsQueues','faxQueues','whatsappQueues','chatQueuesWaitingInteractions','mailQueuesWaitingInteractions','openchannelQueuesWaitingInteractions','smsQueuesWaitingInteractions','faxQueuesWaitingInteractions','server','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','../../../config/environment','../../../config/logger','../rpc/openchannelQueueReport','../rpc/mailQueueReport','../rpc/chatQueueReport','../rpc/faxQueueReport','../rpc/whatsappQueueReport','../lib/rule-engine','format','%s.%s.%s','isNil','IntervalId','Intervals','map','interval','listen'];(function(_0x17c030,_0x23f9f5){var _0x573b32=function(_0x2f702e){while(--_0x2f702e){_0x17c030['push'](_0x17c030['shift']());}};_0x573b32(++_0x23f9f5);}(_0x9f76,0x1bc));var _0x69f7=function(_0xc4001c,_0x1bf8f0){_0xc4001c=_0xc4001c-0x0;var _0x49f7bc=_0x9f76[_0xc4001c];return _0x49f7bc;};'use strict';var _=require(_0x69f7('0x0'));var util=require(_0x69f7('0x1'));var path=require(_0x69f7('0x2'));var fs=require('fs');var jayson=require(_0x69f7('0x3'));var BPromise=require(_0x69f7('0x4'));var uuid=require('uuid');var config=require(_0x69f7('0x5'));var logger=require(_0x69f7('0x6'))('routing');var rpc={'openchannelQueueReport':require(_0x69f7('0x7')),'mailQueueReport':require(_0x69f7('0x8')),'chatQueueReport':require(_0x69f7('0x9')),'smsQueueReport':require('../rpc/smsQueueReport'),'faxQueueReport':require(_0x69f7('0xa')),'whatsappQueueReport':require(_0x69f7('0xb'))};var RuleEngine=require(_0x69f7('0xc'));function getId(_0x3955bb,_0x1a9d50,_0x15552d){return util[_0x69f7('0xd')](_0x69f7('0xe'),_0x3955bb,_0x1a9d50||0x0,_0x15552d||0x0);}function getIntervals(_0x441383,_0x3f05e0){var _0x569363=[];if(!_['isNil'](_0x3f05e0['IntervalId'])&&!_[_0x69f7('0xf')](_0x441383[_0x3f05e0[_0x69f7('0x10')]])&&!_[_0x69f7('0xf')](_0x441383[_0x3f05e0['IntervalId']][_0x69f7('0x11')])){_0x569363=_[_0x69f7('0x12')](_0x441383[_0x3f05e0[_0x69f7('0x10')]][_0x69f7('0x11')],_0x69f7('0x13'));}else if(!_[_0x69f7('0xf')](_0x3f05e0[_0x69f7('0x13')])){_0x569363=[_0x3f05e0[_0x69f7('0x13')]];}return _0x569363;}exports[_0x69f7('0x14')]=function(_0x3acdbe){_0x3acdbe['rules']={};return new BPromise(function(_0xda6988,_0x47d9fc){var _0x1eea31={'Start':function(_0x499ead){var _0x1b0dfc=this;return new Promise(function(_0x1d060b,_0x589976){if(_0x499ead){try{var _0x1f6f53=[];var _0xddc318=getId(_0x499ead[_0x69f7('0x15')],_0x499ead['message']?_0x499ead[_0x69f7('0x16')]['id']:0x0,_0x499ead[_0x69f7('0x17')]?_0x499ead['interaction']['id']:0x0);if(_0x499ead['applications']&&_[_0x69f7('0x18')](_0x499ead[_0x69f7('0x19')])&&_0x499ead[_0x69f7('0x19')][_0x69f7('0x1a')]){for(var _0x5b7d63=0x0;_0x5b7d63<_0x499ead['applications'][_0x69f7('0x1a')];_0x5b7d63+=0x1){var _0x586693=_0x499ead[_0x69f7('0x19')][_0x5b7d63];if(fs[_0x69f7('0x1b')](path[_0x69f7('0x1c')](__dirname,_0x69f7('0x19'),_0x586693[_0x69f7('0x1d')]+'.js'))){var _0x5c8b45=[];switch(_0x586693['app'][_0x69f7('0x1e')]()){case _0x69f7('0x1f'):var _0x4d27ac=_0x586693['appdata'][_0x69f7('0x20')](',');_0x5c8b45['push'](_0x4d27ac[0x0]);_0x5c8b45[_0x69f7('0x21')](_0x4d27ac[_0x69f7('0x22')](0x1,_0x4d27ac['length'])[_0x69f7('0x1c')](','));break;case _0x69f7('0x23'):var _0x1531b3=_0x586693[_0x69f7('0x24')][_0x69f7('0x20')](',');_0x5c8b45[_0x69f7('0x21')](_0x1531b3[0x0]);_0x5c8b45[_0x69f7('0x21')](_0x1531b3[0x1]);_0x5c8b45[_0x69f7('0x21')](_0x1531b3[_0x69f7('0x22')](0x2,_0x1531b3[_0x69f7('0x1a')])[_0x69f7('0x1c')](','));break;case'dialogflowv2':var _0xb565df=_0x586693[_0x69f7('0x24')][_0x69f7('0x20')](',');_0x5c8b45['push'](_0xb565df[0x0]);_0x5c8b45[_0x69f7('0x21')](_0xb565df[0x1]);_0x5c8b45[_0x69f7('0x21')](_0xb565df[0x2]);_0x5c8b45[_0x69f7('0x21')](_0xb565df[0x3]);_0x5c8b45['push'](_0xb565df[_0x69f7('0x22')](0x4,_0xb565df[_0x69f7('0x1a')])[_0x69f7('0x1c')](','));break;case _0x69f7('0x25'):var _0x579828=_0x586693[_0x69f7('0x24')][_0x69f7('0x20')](',');_0x5c8b45[_0x69f7('0x21')](_0x579828[0x0]);_0x5c8b45[_0x69f7('0x21')](_0x579828[0x1]);_0x5c8b45[_0x69f7('0x21')](_0x579828[0x2]);_0x5c8b45['push'](_0x579828[0x3]);_0x5c8b45[_0x69f7('0x21')](_0x579828[_0x69f7('0x22')](0x4,_0x579828[_0x69f7('0x1a')])[_0x69f7('0x1c')](','));break;case _0x69f7('0x16'):_0x5c8b45[_0x69f7('0x21')](_0x586693[_0x69f7('0x24')]);break;case _0x69f7('0x26'):_0x5c8b45['push'](_0x586693['TagId']);_0x5c8b45[_0x69f7('0x21')](_0x586693[_0x69f7('0x24')]);break;default:_0x5c8b45=_[_0x69f7('0xf')](_0x586693[_0x69f7('0x24')])?[]:_0x586693[_0x69f7('0x24')]['split'](',');}var _0xdf66fa={'id':util[_0x69f7('0xd')](_0x69f7('0x27'),_0xddc318,_0x586693['id'],_0x586693[_0x69f7('0x1d')]),'condition':require('./interval')[_0x69f7('0x28')](_0xddc318,getIntervals(_0x3acdbe['intervals'],_0x586693),_0x3acdbe),'consequence':require(_0x69f7('0x29')+_0x586693[_0x69f7('0x1d')])[_0x69f7('0x2a')](_0xddc318,_0x5c8b45,_0x3acdbe,_0x499ead),'p':_0x586693[_0x69f7('0x2b')]||0x0};logger[_0x69f7('0x2c')]('Push\x20new\x20rule',_0xdf66fa['id'],_0x69f7('0x2d'),getIntervals(_0x3acdbe[_0x69f7('0x2e')],_0x586693)[_0x69f7('0x1c')](_0x69f7('0x2f')));_0x1f6f53[_0x69f7('0x21')](_0xdf66fa);}else{logger['error']('Please\x20create\x20a\x20new\x20file\x20under',path['join'](__dirname,_0x69f7('0x19'),_0x586693[_0x69f7('0x1d')]+_0x69f7('0x30')));}}_0x3acdbe[_0x69f7('0x31')][_0xddc318]=new RuleEngine(_0x1f6f53);_0x3acdbe['rules'][_0xddc318]['execute'](_0x499ead,function(_0x5d9de9){logger['info'](_0x69f7('0x32'),JSON[_0x69f7('0x33')](_0x5d9de9[_0x69f7('0x34')]));if(_0x3acdbe[_0x69f7('0x31')][_0xddc318]){_0x3acdbe[_0x69f7('0x31')][_0xddc318][_0x69f7('0x35')](_0x69f7('0x36'));}});return _0x1d060b({'code':0xc8,'message':_0x499ead});}else{logger[_0x69f7('0x2c')]('No\x20applications\x20to\x20execute');return _0x1d060b({'code':0xc8,'message':_0x499ead});}}catch(_0x574796){logger[_0x69f7('0x37')](_0x574796[_0x69f7('0x38')]);return _0x589976(_0x1b0dfc['error'](0x1f4,_0x574796));}}return _0x589976(_0x69f7('0x39'));});},'AcceptMessage':function(_0x3fcf6b){var _0x3c8dd8=this;return new Promise(function(_0x2f1f2a,_0x112ac9){if(_0x3fcf6b[_0x69f7('0x16')]&&_0x3fcf6b[_0x69f7('0x16')]['id']){try{var _0x590128=getId(_0x3fcf6b[_0x69f7('0x15')],_0x3fcf6b[_0x69f7('0x16')]?_0x3fcf6b[_0x69f7('0x16')]['id']:0x0,_0x3fcf6b[_0x69f7('0x17')]?_0x3fcf6b['interaction']['id']:0x0);if(_0x3acdbe['rules'][_0x590128]){_0x3acdbe[_0x69f7('0x31')][_0x590128][_0x69f7('0x35')](_0x69f7('0x3a'));if(rpc[_0x3fcf6b[_0x69f7('0x15')]+_0x69f7('0x3b')]){rpc[_0x3fcf6b[_0x69f7('0x15')]+'QueueReport']['update'](_0x590128,{'UserId':_0x3fcf6b[_0x69f7('0x16')][_0x69f7('0x3c')]})[_0x69f7('0x3d')](function(_0x2a81fd){logger[_0x69f7('0x37')](_0x69f7('0x3e'));});}}return _0x2f1f2a({'code':0xc8,'message':_0x3fcf6b});}catch(_0x1cc653){logger[_0x69f7('0x37')](_0x1cc653[_0x69f7('0x38')]);return _0x112ac9(_0x3c8dd8[_0x69f7('0x37')](0x1f4,_0x1cc653));}}logger[_0x69f7('0x37')](_0x69f7('0x3f'));return _0x112ac9(_0x3c8dd8[_0x69f7('0x37')](0x1f4,_0x69f7('0x3f')));});},'RejectMessage':function(_0x429ae0){var _0x1e5dec=this;return new Promise(function(_0x29f1ab,_0x39f083){if(_0x429ae0[_0x69f7('0x40')]&&_0x429ae0[_0x69f7('0x40')]['id']){try{if(_0x3acdbe[_0x69f7('0x41')][_0x429ae0['agent']['id']]){_0x3acdbe['agents'][_0x429ae0['agent']['id']]['busy']=![];_0x3acdbe[_0x69f7('0x41')][_0x429ae0[_0x69f7('0x40')]['id']]['busyQueue']=undefined;}return _0x29f1ab({'code':0xc8,'message':_0x429ae0});}catch(_0x31f65d){logger['error'](_0x31f65d[_0x69f7('0x38')]);return _0x39f083(_0x1e5dec[_0x69f7('0x37')](0x1f4,_0x31f65d));}}logger[_0x69f7('0x37')](_0x69f7('0x3f'));return _0x39f083(_0x1e5dec[_0x69f7('0x37')](0x1f4,_0x69f7('0x3f')));});},'AbandonInteraction':function(_0x4c4315){var _0x4d490d=this;return new Promise(function(_0x1ec648,_0x4fc0f4){if(_0x4c4315['channel']&&_0x4c4315[_0x69f7('0x17')]&&_0x4c4315[_0x69f7('0x17')]['id']){try{var _0x477b97=getId(_0x4c4315[_0x69f7('0x15')],_0x4c4315[_0x69f7('0x16')]?_0x4c4315[_0x69f7('0x16')]['id']:0x0,_0x4c4315[_0x69f7('0x17')]?_0x4c4315['interaction']['id']:0x0);if(_0x3acdbe[_0x69f7('0x31')][_0x477b97]){_0x3acdbe['rules'][_0x477b97][_0x69f7('0x35')](_0x69f7('0x3a'));}return _0x1ec648({'code':0xc8,'message':_0x4c4315});}catch(_0x1b8aef){logger['error'](_0x1b8aef[_0x69f7('0x38')]);return _0x4fc0f4(_0x4d490d[_0x69f7('0x37')](0x1f4,_0x1b8aef));}}logger[_0x69f7('0x37')](_0x69f7('0x3f'));return _0x4fc0f4(_0x4d490d[_0x69f7('0x37')](0x1f4,_0x69f7('0x3f')));});},'AgentCapacity':function(_0x562c58){var _0x2635b5=this;return new Promise(function(_0x54b2bd,_0xfac5c2){if(_0x3acdbe['agents'][_0x69f7('0x42')](_0x562c58['id'])){for(var _0x59d8d6 in _0x562c58[_0x69f7('0x43')]){if(_0x562c58[_0x69f7('0x43')][_0x69f7('0x42')](_0x59d8d6)){if(_0x3acdbe[_0x69f7('0x41')][_0x562c58['id']][_0x69f7('0x42')](_0x59d8d6+_0x69f7('0x44'))){_0x3acdbe['agents'][_0x562c58['id']][_0x59d8d6+_0x69f7('0x44')]=parseInt(_0x562c58[_0x69f7('0x43')][_0x59d8d6],0xa);}}}_0x3acdbe['agents'][_0x562c58['id']]['updateRealtime']();return _0x54b2bd({'code':0xc8,'message':_0x3acdbe[_0x69f7('0x41')][_0x562c58['id']]});}else{return _0xfac5c2(_0x2635b5['error'](0x1f4,_0x69f7('0x45')));}});},'Agents':function(_0x40cd40){return new Promise(function(_0x4eed73,_0x3210be){var _0x5e7a73=_[_0x69f7('0x46')](_0x3acdbe[_0x69f7('0x41')]);_0x4eed73({'count':_0x5e7a73[_0x69f7('0x1a')],'rows':_0x5e7a73});});},'ChatQueues':function(_0x3757ca){return new Promise(function(_0x24743b,_0xaf4265){var _0x3e2ee5=[];for(var _0x49760b in _0x3acdbe[_0x69f7('0x47')]){if(_0x3acdbe[_0x69f7('0x47')][_0x69f7('0x42')](_0x49760b)){var _0x3e4421=_0x3acdbe[_0x69f7('0x47')][_0x49760b];var _0x30e2c9={'id':_0x3e4421['id'],'available':_0x3e4421[_0x69f7('0x48')],'loggedIn':_0x3e4421[_0x69f7('0x49')],'name':_0x3e4421[_0x69f7('0x4a')],'paused':_0x3e4421[_0x69f7('0x4b')],'waiting':_0x3e4421[_0x69f7('0x4c')]};_0x3e2ee5[_0x69f7('0x21')](_0x30e2c9);}}_0x24743b({'count':_0x3e2ee5[_0x69f7('0x1a')],'rows':_0x3e2ee5});});},'MailQueues':function(_0x1faac9){return new Promise(function(_0x1400d1,_0x124973){var _0x585c0e=[];for(var _0x2955df in _0x3acdbe[_0x69f7('0x4d')]){if(_0x3acdbe['mailQueues'][_0x69f7('0x42')](_0x2955df)){var _0x17060b=_0x3acdbe[_0x69f7('0x4d')][_0x2955df];var _0x1e83c9={'id':_0x17060b['id'],'available':_0x17060b[_0x69f7('0x48')],'loggedIn':_0x17060b['loggedIn'],'name':_0x17060b[_0x69f7('0x4a')],'paused':_0x17060b[_0x69f7('0x4b')],'waiting':_0x17060b['waiting']};_0x585c0e['push'](_0x1e83c9);}}_0x1400d1({'count':_0x585c0e['length'],'rows':_0x585c0e});});},'OpenchannelQueues':function(_0x578f5b){return new Promise(function(_0x153b60,_0x37f7e4){var _0x40b51b=[];for(var _0x50cf67 in _0x3acdbe[_0x69f7('0x4e')]){if(_0x3acdbe[_0x69f7('0x4e')][_0x69f7('0x42')](_0x50cf67)){var _0x18be70=_0x3acdbe[_0x69f7('0x4e')][_0x50cf67];var _0x4d4a91={'id':_0x18be70['id'],'available':_0x18be70[_0x69f7('0x48')],'loggedIn':_0x18be70[_0x69f7('0x49')],'name':_0x18be70[_0x69f7('0x4a')],'paused':_0x18be70[_0x69f7('0x4b')],'waiting':_0x18be70[_0x69f7('0x4c')]};_0x40b51b[_0x69f7('0x21')](_0x4d4a91);}}_0x153b60({'count':_0x40b51b[_0x69f7('0x1a')],'rows':_0x40b51b});});},'SmsQueues':function(_0x7678fa){return new Promise(function(_0x213969,_0x4cb272){var _0x5df72f=[];for(var _0xfa37a3 in _0x3acdbe[_0x69f7('0x4f')]){if(_0x3acdbe[_0x69f7('0x4f')][_0x69f7('0x42')](_0xfa37a3)){var _0x445093=_0x3acdbe[_0x69f7('0x4f')][_0xfa37a3];var _0x2892bf={'id':_0x445093['id'],'available':_0x445093[_0x69f7('0x48')],'loggedIn':_0x445093['loggedIn'],'name':_0x445093[_0x69f7('0x4a')],'paused':_0x445093['paused'],'waiting':_0x445093[_0x69f7('0x4c')]};_0x5df72f[_0x69f7('0x21')](_0x2892bf);}}_0x213969({'count':_0x5df72f[_0x69f7('0x1a')],'rows':_0x5df72f});});},'FaxQueues':function(_0x4ff5ff){return new Promise(function(_0x1a2b90,_0x3d3a67){var _0x278e43=[];for(var _0x212110 in _0x3acdbe['faxQueues']){if(_0x3acdbe[_0x69f7('0x50')][_0x69f7('0x42')](_0x212110)){var _0x3649ee=_0x3acdbe[_0x69f7('0x50')][_0x212110];var _0x6efbff={'id':_0x3649ee['id'],'available':_0x3649ee[_0x69f7('0x48')],'loggedIn':_0x3649ee['loggedIn'],'name':_0x3649ee[_0x69f7('0x4a')],'paused':_0x3649ee[_0x69f7('0x4b')],'waiting':_0x3649ee[_0x69f7('0x4c')]};_0x278e43[_0x69f7('0x21')](_0x6efbff);}}_0x1a2b90({'count':_0x278e43[_0x69f7('0x1a')],'rows':_0x278e43});});},'WhatsappQueues':function(_0x291c50){return new Promise(function(_0x4e1f61,_0x1ff1e3){var _0x50f530=[];for(var _0x23d2f2 in _0x3acdbe['whatsappQueues']){if(_0x3acdbe[_0x69f7('0x51')]['hasOwnProperty'](_0x23d2f2)){var _0x4a51b7=_0x3acdbe[_0x69f7('0x51')][_0x23d2f2];var _0x4c501d={'id':_0x4a51b7['id'],'available':_0x4a51b7[_0x69f7('0x48')],'loggedIn':_0x4a51b7['loggedIn'],'name':_0x4a51b7[_0x69f7('0x4a')],'paused':_0x4a51b7[_0x69f7('0x4b')],'waiting':_0x4a51b7[_0x69f7('0x4c')]};_0x50f530[_0x69f7('0x21')](_0x4c501d);}}_0x4e1f61({'count':_0x50f530[_0x69f7('0x1a')],'rows':_0x50f530});});},'ChatQueuesWaitingInteractions':function(_0x30a4b4){return new Promise(function(_0x528dc7,_0x3f2c8b){var _0x7e9367=_['values'](_0x3acdbe[_0x69f7('0x52')]);_0x528dc7({'count':_0x7e9367[_0x69f7('0x1a')],'rows':_0x7e9367});});},'MailQueuesWaitingInteractions':function(_0x187c66){return new Promise(function(_0x37c616,_0x405601){var _0x31bf05=_[_0x69f7('0x46')](_0x3acdbe[_0x69f7('0x53')]);_0x37c616({'count':_0x31bf05[_0x69f7('0x1a')],'rows':_0x31bf05});});},'OpenchannelQueuesWaitingInteractions':function(_0x16ee57){return new Promise(function(_0x517a79,_0x42d6b2){var _0x3177c7=_['values'](_0x3acdbe[_0x69f7('0x54')]);_0x517a79({'count':_0x3177c7['length'],'rows':_0x3177c7});});},'SmsQueuesWaitingInteractions':function(_0x2b36ce){return new Promise(function(_0x427317,_0x5cef05){var _0x4b7b65=_[_0x69f7('0x46')](_0x3acdbe[_0x69f7('0x55')]);_0x427317({'count':_0x4b7b65[_0x69f7('0x1a')],'rows':_0x4b7b65});});},'FaxQueuesWaitingInteractions':function(_0x9bdd96){return new Promise(function(_0x189bdc,_0x41e40b){var _0x164881=_['values'](_0x3acdbe[_0x69f7('0x56')]);_0x189bdc({'count':_0x164881[_0x69f7('0x1a')],'rows':_0x164881});});},'WhatsappQueuesWaitingInteractions':function(_0x38a55d){return new Promise(function(_0x3f534c,_0x53411f){var _0x167a50=_['values'](_0x3acdbe['whatsappQueuesWaitingInteractions']);_0x3f534c({'count':_0x167a50[_0x69f7('0x1a')],'rows':_0x167a50});});}};var _0x3377b1=jayson[_0x69f7('0x57')](_0x1eea31)[_0x69f7('0x58')]();_0x3377b1['on']('error',function(_0x8b981c){logger['error'](JSON['stringify'](_0x8b981c));return _0x47d9fc(_0x8b981c);});_0x3377b1[_0x69f7('0x14')](0x232c,_0x69f7('0x59'),function(){console[_0x69f7('0x5a')](_0x69f7('0x5b'),0x232c);return _0xda6988(_0x69f7('0x5c')+0x232c);});});};