26863d678d351ed08463c777fac2a92e52d1d008
[motion2.git] / server / services / routing / server / applications / queue.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 _0x8f9b=['MessageId','message','chat','AccountId','WebsiteId','info','[%s][START:QUEUE][NAME:%s]','name','QueueReport','create','catch','error','create\x20queue\x20report','waiting','accepted','update','update\x20queue\x20report','timeout','[%s][END:QUEUE][NAME:%s]','toLowerCase','consequence','validate','length','[%s][QUEUE]\x20%s','map','join','stop','Queues','createQueue','atimeout','qtimeout','rules','end','[%s][QUEUE]\x20ROUTING\x20STOPPED','close','complete','next','strategy','rrmemory','beepall','[%s][QUEUE]\x20queue\x20not\x20found\x20%s','log','stack','util','moment','ioredis','../../../../config/logger','routing','./strategy','redis','defaults','localhost','socket.io-emitter','queue_id','account','queue','joinAt','clone','format','%sQueuesWaitingInteractions','pick','%s:queue:%s','%s_queue_waiting_interaction:%s','remove','../../rpc/mailQueueReport','../../rpc/chatQueueReport','../../rpc/faxQueueReport','string','name\x20is\x20a\x20string\x20and\x20it\x20is\x20required','timeout\x20is\x20a\x20number','body','from','YYYY-MM-DD\x20HH:mm:ss','contact','list','upperFirst','channel','QueueId','InteractionId','interaction'];(function(_0x162799,_0x438388){var _0x1dd583=function(_0x3b0ef9){while(--_0x3b0ef9){_0x162799['push'](_0x162799['shift']());}};_0x1dd583(++_0x438388);}(_0x8f9b,0x1bb));var _0xb8f9=function(_0x34fc4e,_0x4c42af){_0x34fc4e=_0x34fc4e-0x0;var _0xcdd3fb=_0x8f9b[_0x34fc4e];return _0xcdd3fb;};'use strict';var schema=require('validate');var _=require('lodash');var util=require(_0xb8f9('0x0'));var seqqueue=require('seq-queue');var moment=require(_0xb8f9('0x1'));var Redis=require(_0xb8f9('0x2'));var config=require('../../../../config/environment');var logger=require(_0xb8f9('0x3'))(_0xb8f9('0x4'));var strategy=require(_0xb8f9('0x5'));config[_0xb8f9('0x6')]=_[_0xb8f9('0x7')](config[_0xb8f9('0x6')],{'host':_0xb8f9('0x8'),'port':0x18eb});var io=require(_0xb8f9('0x9'))(new Redis(config[_0xb8f9('0x6')]));var attributes=['id',_0xb8f9('0xa'),_0xb8f9('0xb'),_0xb8f9('0xc'),'from',_0xb8f9('0xd')];function emit(_0x37841f,_0x5c74aa,_0x5a4652){io['to'](_0x37841f)['emit'](_0x5c74aa,_0x5a4652);}function emitChannelQueueInteraction(_0x3c8c18,_0x4dd243,_0x561ccb,_0x2db0a9,_0x350635,_0x5f5410,_0x58d6b6,_0x440662){var _0x16f327=_[_0xb8f9('0xe')](_0x561ccb);_0x16f327['queue_id']=_0x440662;_0x16f327['queue']=_0x4dd243;_0x16f327[_0xb8f9('0xb')]=_0x2db0a9;_0x16f327['id']=_0x350635;if(_0x5f5410){_0x58d6b6[util[_0xb8f9('0xf')](_0xb8f9('0x10'),_0x3c8c18)][_0x350635]=_[_0xb8f9('0x11')](_0x16f327,attributes);}else{delete _0x58d6b6[util[_0xb8f9('0xf')](_0xb8f9('0x10'),_0x3c8c18)][_0x350635];}emit(util[_0xb8f9('0xf')](_0xb8f9('0x12'),_0x3c8c18,_0x4dd243),util[_0xb8f9('0xf')](_0xb8f9('0x13'),_0x3c8c18,_0x5f5410?'save':_0xb8f9('0x14')),_[_0xb8f9('0x11')](_0x16f327,attributes));}var rpc={'openchannelQueueReport':require('../../rpc/openchannelQueueReport'),'mailQueueReport':require(_0xb8f9('0x15')),'chatQueueReport':require(_0xb8f9('0x16')),'smsQueueReport':require('../../rpc/smsQueueReport'),'faxQueueReport':require(_0xb8f9('0x17')),'whatsappQueueReport':require('../../rpc/whatsappQueueReport')};var queue=schema({'name':{'type':_0xb8f9('0x18'),'required':!![],'message':_0xb8f9('0x19')},'timeout':{'type':'number','message':_0xb8f9('0x1a')}});function startRouting(_0x315860,_0x1b84a5,_0xe9cb1a,_0x234e40){if(_0x315860&&_0x1b84a5&&_0xe9cb1a){_0x1b84a5['waiting']+=0x1;var _0x370890={'uniqueid':_0x315860,'from':_0xe9cb1a[_0xb8f9('0x1b')]?_0xe9cb1a[_0xb8f9('0x1b')][_0xb8f9('0x1c')]:'','joinAt':moment()[_0xb8f9('0xf')](_0xb8f9('0x1d')),'ContactId':_0xe9cb1a['contact']?_0xe9cb1a[_0xb8f9('0x1e')]['id']:null,'ListId':_0xe9cb1a['list']?_0xe9cb1a[_0xb8f9('0x1f')]['id']:null};_0x370890[_[_0xb8f9('0x20')](_0xe9cb1a[_0xb8f9('0x21')])+_0xb8f9('0x22')]=_0x1b84a5['id']||null;_0x370890[_[_0xb8f9('0x20')](_0xe9cb1a[_0xb8f9('0x21')])+_0xb8f9('0x23')]=_0xe9cb1a[_0xb8f9('0x24')]?_0xe9cb1a[_0xb8f9('0x24')]['id']:null;_0x370890[_[_0xb8f9('0x20')](_0xe9cb1a[_0xb8f9('0x21')])+_0xb8f9('0x25')]=_0xe9cb1a[_0xb8f9('0x26')]?_0xe9cb1a[_0xb8f9('0x26')]['id']:null;_0x370890[_[_0xb8f9('0x20')](_0xe9cb1a['channel'])+(_0xe9cb1a[_0xb8f9('0x21')]!==_0xb8f9('0x27')?_0xb8f9('0x28'):_0xb8f9('0x29'))]=_0xe9cb1a[_0xb8f9('0xb')]?_0xe9cb1a['account']['id']:null;logger[_0xb8f9('0x2a')](util[_0xb8f9('0xf')](_0xb8f9('0x2b'),_0x315860,_0x1b84a5[_0xb8f9('0x2c')]));emitChannelQueueInteraction(_0xe9cb1a[_0xb8f9('0x21')],_0x1b84a5[_0xb8f9('0x2c')],_0x370890,_0xe9cb1a[_0xb8f9('0xb')][_0xb8f9('0x2c')],_0xe9cb1a[_0xb8f9('0x26')]['id'],!![],_0x234e40,_0x1b84a5['id']);if(rpc[_0xe9cb1a['channel']+_0xb8f9('0x2d')]){return rpc[_0xe9cb1a[_0xb8f9('0x21')]+_0xb8f9('0x2d')][_0xb8f9('0x2e')](_0x370890)[_0xb8f9('0x2f')](function(_0xe77425){logger[_0xb8f9('0x30')](_0xb8f9('0x31'));});}}}function stopRouting(_0x2ec8d3,_0x2b695b,_0x4eace2,_0x33e620){if(_0x2ec8d3&&_0x2b695b){_0x2b695b[_0xb8f9('0x32')]-=0x1;var _0x3693fd={'leaveAt':moment()[_0xb8f9('0xf')](_0xb8f9('0x1d')),'acceptAt':moment()[_0xb8f9('0xf')](_0xb8f9('0x1d')),'reason':_0xb8f9('0x33')};logger['info'](util[_0xb8f9('0xf')]('[%s][STOP:QUEUE][NAME:%s]',_0x2ec8d3,_0x2b695b['name']));emitChannelQueueInteraction(_0x4eace2['channel'],_0x2b695b['name'],_0x4eace2[_0xb8f9('0x1b')],_0x4eace2[_0xb8f9('0xb')][_0xb8f9('0x2c')],_0x4eace2['message']['id'],![],_0x33e620,_0x2b695b['id']);if(rpc[_0x4eace2[_0xb8f9('0x21')]+_0xb8f9('0x2d')]){return rpc[_0x4eace2[_0xb8f9('0x21')]+_0xb8f9('0x2d')][_0xb8f9('0x34')](_0x2ec8d3,_0x3693fd)[_0xb8f9('0x2f')](function(_0x6bf60d){logger[_0xb8f9('0x30')](_0xb8f9('0x35'));});}}}function endRouting(_0x3e0014,_0x2a1122,_0x2f1b21,_0x36c98c){if(_0x3e0014&&_0x2a1122){_0x2a1122[_0xb8f9('0x32')]-=0x1;var _0x387cfd={'leaveAt':moment()['format'](_0xb8f9('0x1d')),'exitAt':moment()[_0xb8f9('0xf')](_0xb8f9('0x1d')),'reason':_0xb8f9('0x36')};logger[_0xb8f9('0x2a')](util[_0xb8f9('0xf')](_0xb8f9('0x37'),_0x3e0014,_0x2a1122[_0xb8f9('0x2c')]));emitChannelQueueInteraction(_0x2f1b21[_0xb8f9('0x21')],_0x2a1122[_0xb8f9('0x2c')],_0x2f1b21['body'],_0x2f1b21['account'][_0xb8f9('0x2c')],_0x2f1b21[_0xb8f9('0x26')]['id'],![],_0x36c98c);if(rpc[_0x2f1b21[_0xb8f9('0x21')]+_0xb8f9('0x2d')]){return rpc[_0x2f1b21['channel']+_0xb8f9('0x2d')][_0xb8f9('0x34')](_0x3e0014,_0x387cfd)['catch'](function(_0x56a916){logger[_0xb8f9('0x30')](_0xb8f9('0x35'));});}}}function handleFindQueue(_0x34eb39){return function(_0x4e49ba){return _0x4e49ba[_0xb8f9('0x2c')][_0xb8f9('0x38')]()===_0x34eb39['toLowerCase']();};}exports[_0xb8f9('0x39')]=function(_0x22f080,_0x4e4719,_0x1f8810,_0x215efa){return function(_0x415b4f){try{var _0xbcef2e={'name':_0x4e4719[0x0],'timeout':_0x4e4719[0x1]};var _0x1c80bb=queue[_0xb8f9('0x3a')](_0xbcef2e,{'typecast':!![]});if(_0x1c80bb[_0xb8f9('0x3b')]){logger[_0xb8f9('0x30')](_0xb8f9('0x3c'),_0x22f080,_[_0xb8f9('0x3d')](_0x1c80bb,_0xb8f9('0x26'))[_0xb8f9('0x3e')](',\x20'));_0x415b4f[_0xb8f9('0x3f')]();}else{var _0x32dcbc=_['find'](_0x1f8810[_0x215efa['channel']+_0xb8f9('0x40')],handleFindQueue(_0xbcef2e[_0xb8f9('0x2c')]));if(_0x32dcbc){startRouting(_0x22f080,_0x32dcbc,_0x215efa,_0x1f8810);var _0x2c57e1=(_0x32dcbc[_0xb8f9('0x36')]||0xa)*0x3e8;var _0x43b1fa=(_0xbcef2e[_0xb8f9('0x36')]||0x14)*0x3e8;var _0x17356f,_0x45c865;if(_0x2c57e1>_0x43b1fa){_0x43b1fa=_0x2c57e1;}var _0x4f3c31=seqqueue[_0xb8f9('0x41')](_0x2c57e1);_0x4f3c31['id']=_0x22f080;_0x4f3c31[_0xb8f9('0x42')]=_0x2c57e1;_0x4f3c31[_0xb8f9('0x43')]=_0x43b1fa;_0x4f3c31[_0xb8f9('0x21')]=_0x215efa[_0xb8f9('0x21')];_0x1f8810[_0xb8f9('0x44')][_0x22f080]['on'](_0xb8f9('0x45'),function(){logger['info'](_0xb8f9('0x46'),_0x22f080);stopRouting(_0x22f080,_0x32dcbc,_0x215efa,_0x1f8810);clearTimeout(_0x17356f);_0x17356f=undefined;_0x4f3c31[_0xb8f9('0x47')](!![]);if(_0x1f8810[_0xb8f9('0x44')][_0x22f080]){delete _0x1f8810['rules'][_0x22f080];}_0x415b4f['stop']();});_0x1f8810[_0xb8f9('0x44')][_0x22f080]['on'](_0xb8f9('0x48'),function(){if(_0x1f8810[_0xb8f9('0x44')][_0x22f080]){delete _0x1f8810['rules'][_0x22f080];}});_0x17356f=setTimeout(function(){logger[_0xb8f9('0x2a')]('[%s][QUEUE]\x20APPLICATION\x20END',_0x22f080);endRouting(_0x22f080,_0x32dcbc,_0x215efa,_0x1f8810);clearTimeout(_0x17356f);_0x4f3c31[_0xb8f9('0x47')](!![]);_0x415b4f[_0xb8f9('0x49')]();},_0x43b1fa);switch(_0x32dcbc[_0xb8f9('0x4a')]){case'rrmemory':strategy[_0xb8f9('0x4b')](_0x4f3c31,_0x32dcbc,_0x1f8810,_0x215efa);break;default:strategy[_0xb8f9('0x4c')](_0x4f3c31,_0x32dcbc,_0x1f8810,_0x215efa);}}else{logger[_0xb8f9('0x30')](_0xb8f9('0x4d'),_0x22f080,_0xbcef2e[_0xb8f9('0x2c')]);_0x415b4f[_0xb8f9('0x3f')]();}}}catch(_0x498e37){console[_0xb8f9('0x4e')](_0x498e37);logger[_0xb8f9('0x30')](_0xb8f9('0x3c'),_0x22f080,_0x498e37[_0xb8f9('0x4f')]);_0x415b4f[_0xb8f9('0x3f')]();}};};