Built motion from commit 4ffc18b1.|2.6.31
[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 _0x957f=['../../../../config/environment','../../../../config/logger','./strategy','redis','localhost','socket.io-emitter','queue_id','queue','from','joinAt','clone','account','capitalize','InteractionId','format','%sQueuesWaitingInteractions','pick','%s:queue:%s','%s_queue_waiting_interaction:%s','save','remove','../../rpc/openchannelQueueReport','../../rpc/mailQueueReport','../../rpc/smsQueueReport','../../rpc/faxQueueReport','../../rpc/whatsappQueueReport','../../rpc/chatInteraction','../../rpc/faxInteraction','../../rpc/mailInteraction','../../rpc/openchannelInteraction','../../rpc/smsInteraction','../../rpc/whatsappInteraction','string','name\x20is\x20a\x20string\x20and\x20it\x20is\x20required','number','timeout\x20is\x20a\x20number','body','YYYY-MM-DD\x20HH:mm:ss','contact','list','upperFirst','channel','interaction','message','AccountId','WebsiteId','info','[%s][START:QUEUE][NAME:%s]','name','QueueReport','create','catch','create\x20queue\x20report','waiting','accepted','[%s][STOP:QUEUE][NAME:%s]','Interaction','update','update\x20queue\x20report','timeout','error','toLowerCase','consequence','length','[%s][QUEUE]\x20%s','map','join','stop','find','createQueue','atimeout','qtimeout','rules','end','[%s][QUEUE]\x20ROUTING\x20STOPPED','close','[%s][QUEUE]\x20APPLICATION\x20END','next','strategy','rrmemory','[%s][QUEUE]\x20queue\x20not\x20found\x20%s','stack','validate','lodash','util','seq-queue','moment','ioredis'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x957f,0xaa));var _0xf957=function(_0x43b5a7,_0xf5e3d6){_0x43b5a7=_0x43b5a7-0x0;var _0x5461fe=_0x957f[_0x43b5a7];return _0x5461fe;};'use strict';var schema=require(_0xf957('0x0'));var _=require(_0xf957('0x1'));var util=require(_0xf957('0x2'));var seqqueue=require(_0xf957('0x3'));var moment=require(_0xf957('0x4'));var Redis=require(_0xf957('0x5'));var config=require(_0xf957('0x6'));var logger=require(_0xf957('0x7'))('routing');var strategy=require(_0xf957('0x8'));config[_0xf957('0x9')]=_['defaults'](config[_0xf957('0x9')],{'host':_0xf957('0xa'),'port':0x18eb});var io=require(_0xf957('0xb'))(new Redis(config[_0xf957('0x9')]));var attributes=['id',_0xf957('0xc'),'account',_0xf957('0xd'),_0xf957('0xe'),_0xf957('0xf')];function emit(_0x1dd4c5,_0x535943,_0x544234){io['to'](_0x1dd4c5)['emit'](_0x535943,_0x544234);}function emitChannelQueueInteraction(_0x586358,_0x2cd92a,_0x681a80,_0x43fc87,_0x47d15e,_0x47933f,_0x3533d2,_0x5e364e){var _0x1d4ff6=_[_0xf957('0x10')](_0x681a80);_0x1d4ff6[_0xf957('0xc')]=_0x5e364e;_0x1d4ff6[_0xf957('0xd')]=_0x2cd92a;_0x1d4ff6[_0xf957('0x11')]=_0x43fc87;_0x1d4ff6['id']=_0x47d15e;if(_0x47933f){attributes['push'](_[_0xf957('0x12')](_0x586358)+_0xf957('0x13'));_0x3533d2[util[_0xf957('0x14')](_0xf957('0x15'),_0x586358)][_0x47d15e]=_[_0xf957('0x16')](_0x1d4ff6,attributes);}else{delete _0x3533d2[util['format'](_0xf957('0x15'),_0x586358)][_0x47d15e];}emit(util[_0xf957('0x14')](_0xf957('0x17'),_0x586358,_0x2cd92a),util[_0xf957('0x14')](_0xf957('0x18'),_0x586358,_0x47933f?_0xf957('0x19'):_0xf957('0x1a')),_['pick'](_0x1d4ff6,attributes));}var rpc={'openchannelQueueReport':require(_0xf957('0x1b')),'mailQueueReport':require(_0xf957('0x1c')),'chatQueueReport':require('../../rpc/chatQueueReport'),'smsQueueReport':require(_0xf957('0x1d')),'faxQueueReport':require(_0xf957('0x1e')),'whatsappQueueReport':require(_0xf957('0x1f')),'chatInteraction':require(_0xf957('0x20')),'faxInteraction':require(_0xf957('0x21')),'mailInteraction':require(_0xf957('0x22')),'openchannelInteraction':require(_0xf957('0x23')),'smsInteraction':require(_0xf957('0x24')),'whatsappInteraction':require(_0xf957('0x25'))};var queue=schema({'name':{'type':_0xf957('0x26'),'required':!![],'message':_0xf957('0x27')},'timeout':{'type':_0xf957('0x28'),'message':_0xf957('0x29')}});function startRouting(_0x2e24c4,_0x3122fc,_0x14d224,_0x4cbe5a){if(_0x2e24c4&&_0x3122fc&&_0x14d224){_0x3122fc['waiting']+=0x1;var _0x17bce3={'uniqueid':_0x2e24c4,'from':_0x14d224['body']?_0x14d224[_0xf957('0x2a')][_0xf957('0xe')]:'','joinAt':moment()['format'](_0xf957('0x2b')),'ContactId':_0x14d224[_0xf957('0x2c')]?_0x14d224[_0xf957('0x2c')]['id']:null,'ListId':_0x14d224['list']?_0x14d224[_0xf957('0x2d')]['id']:null};_0x17bce3[_[_0xf957('0x2e')](_0x14d224[_0xf957('0x2f')])+'QueueId']=_0x3122fc['id']||null;_0x17bce3[_[_0xf957('0x2e')](_0x14d224[_0xf957('0x2f')])+_0xf957('0x13')]=_0x14d224[_0xf957('0x30')]?_0x14d224[_0xf957('0x30')]['id']:null;_0x17bce3[_[_0xf957('0x2e')](_0x14d224['channel'])+'MessageId']=_0x14d224[_0xf957('0x31')]?_0x14d224['message']['id']:null;_0x17bce3[_[_0xf957('0x2e')](_0x14d224[_0xf957('0x2f')])+(_0x14d224[_0xf957('0x2f')]!=='chat'?_0xf957('0x32'):_0xf957('0x33'))]=_0x14d224[_0xf957('0x11')]?_0x14d224[_0xf957('0x11')]['id']:null;logger[_0xf957('0x34')](util[_0xf957('0x14')](_0xf957('0x35'),_0x2e24c4,_0x3122fc['name']));emitChannelQueueInteraction(_0x14d224[_0xf957('0x2f')],_0x3122fc['name'],_0x17bce3,_0x14d224[_0xf957('0x11')][_0xf957('0x36')],_0x14d224['message']['id'],!![],_0x4cbe5a,_0x3122fc['id']);if(rpc[_0x14d224[_0xf957('0x2f')]+'QueueReport']){return rpc[_0x14d224[_0xf957('0x2f')]+_0xf957('0x37')][_0xf957('0x38')](_0x17bce3)[_0xf957('0x39')](function(_0x5c975e){logger['error'](_0xf957('0x3a'),_0x5c975e);});}}}function stopRouting(_0xc852e,_0x17584d,_0x49f318,_0xbfbd1d){if(_0xc852e&&_0x17584d){_0x17584d[_0xf957('0x3b')]-=0x1;var _0x350b89={'leaveAt':moment()[_0xf957('0x14')]('YYYY-MM-DD\x20HH:mm:ss'),'acceptAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'reason':_0xf957('0x3c')};logger[_0xf957('0x34')](util[_0xf957('0x14')](_0xf957('0x3d'),_0xc852e,_0x17584d['name']));emitChannelQueueInteraction(_0x49f318[_0xf957('0x2f')],_0x17584d[_0xf957('0x36')],_0x49f318[_0xf957('0x2a')],_0x49f318[_0xf957('0x11')][_0xf957('0x36')],_0x49f318['message']['id'],![],_0xbfbd1d,_0x17584d['id']);return rpc[_0x49f318['channel']+_0xf957('0x3e')][_0xf957('0x3f')](_0x49f318[_0xf957('0x30')]['id'],{'queueId':_0x17584d['id']})['then'](function(){if(rpc[_0x49f318['channel']+_0xf957('0x37')]){return rpc[_0x49f318[_0xf957('0x2f')]+_0xf957('0x37')][_0xf957('0x3f')](_0xc852e,_0x350b89)[_0xf957('0x39')](function(_0x5142bb){logger['error'](_0xf957('0x40'),_0x5142bb);});}});}}function endRouting(_0x1ee8f3,_0x13d376,_0x3e7832,_0x2c2a93){if(_0x1ee8f3&&_0x13d376){_0x13d376[_0xf957('0x3b')]-=0x1;var _0x3d5ef4={'leaveAt':moment()[_0xf957('0x14')](_0xf957('0x2b')),'exitAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'reason':_0xf957('0x41')};logger['info'](util['format']('[%s][END:QUEUE][NAME:%s]',_0x1ee8f3,_0x13d376[_0xf957('0x36')]));emitChannelQueueInteraction(_0x3e7832[_0xf957('0x2f')],_0x13d376[_0xf957('0x36')],_0x3e7832['body'],_0x3e7832['account'][_0xf957('0x36')],_0x3e7832[_0xf957('0x31')]['id'],![],_0x2c2a93);if(rpc[_0x3e7832[_0xf957('0x2f')]+_0xf957('0x37')]){return rpc[_0x3e7832['channel']+_0xf957('0x37')][_0xf957('0x3f')](_0x1ee8f3,_0x3d5ef4)['catch'](function(_0x30ad8e){logger[_0xf957('0x42')](_0xf957('0x40'),_0x30ad8e);});}}}function handleFindQueue(_0x3d5353){return function(_0x3f2ee9){return _0x3f2ee9[_0xf957('0x36')][_0xf957('0x43')]()===_0x3d5353[_0xf957('0x43')]();};}exports[_0xf957('0x44')]=function(_0x57717a,_0x147a64,_0x512c33,_0x308406){return function(_0x24d5eb){try{var _0x1da837={'name':_0x147a64[0x0],'timeout':_0x147a64[0x1]};var _0x104539=queue[_0xf957('0x0')](_0x1da837,{'typecast':!![]});if(_0x104539[_0xf957('0x45')]){logger[_0xf957('0x42')](_0xf957('0x46'),_0x57717a,_[_0xf957('0x47')](_0x104539,_0xf957('0x31'))[_0xf957('0x48')](',\x20'));_0x24d5eb[_0xf957('0x49')]();}else{var _0x10b563=_[_0xf957('0x4a')](_0x512c33[_0x308406[_0xf957('0x2f')]+'Queues'],handleFindQueue(_0x1da837['name']));if(_0x10b563){startRouting(_0x57717a,_0x10b563,_0x308406,_0x512c33);var _0x3592ff=(_0x10b563[_0xf957('0x41')]||0xa)*0x3e8;var _0x237c04=(_0x1da837[_0xf957('0x41')]||0x14)*0x3e8;var _0x5d4dad;if(_0x3592ff>_0x237c04){_0x237c04=_0x3592ff;}var _0x51fd37=seqqueue[_0xf957('0x4b')](_0x3592ff);_0x51fd37['id']=_0x57717a;_0x51fd37[_0xf957('0x4c')]=_0x3592ff;_0x51fd37[_0xf957('0x4d')]=_0x237c04;_0x51fd37[_0xf957('0x2f')]=_0x308406[_0xf957('0x2f')];_0x512c33[_0xf957('0x4e')][_0x57717a]['on'](_0xf957('0x4f'),function(){logger[_0xf957('0x34')](_0xf957('0x50'),_0x57717a);stopRouting(_0x57717a,_0x10b563,_0x308406,_0x512c33);clearTimeout(_0x5d4dad);_0x5d4dad=undefined;_0x51fd37[_0xf957('0x51')](!![]);if(_0x512c33[_0xf957('0x4e')][_0x57717a]){delete _0x512c33[_0xf957('0x4e')][_0x57717a];}_0x24d5eb['stop']();});_0x512c33[_0xf957('0x4e')][_0x57717a]['on']('complete',function(){if(_0x512c33[_0xf957('0x4e')][_0x57717a]){delete _0x512c33[_0xf957('0x4e')][_0x57717a];}});_0x5d4dad=setTimeout(function(){logger[_0xf957('0x34')](_0xf957('0x52'),_0x57717a);endRouting(_0x57717a,_0x10b563,_0x308406,_0x512c33);clearTimeout(_0x5d4dad);_0x51fd37[_0xf957('0x51')](!![]);_0x24d5eb[_0xf957('0x53')]();},_0x237c04);switch(_0x10b563[_0xf957('0x54')]){case _0xf957('0x55'):strategy[_0xf957('0x55')](_0x51fd37,_0x10b563,_0x512c33,_0x308406);break;default:strategy['beepall'](_0x51fd37,_0x10b563,_0x512c33,_0x308406);}}else{logger[_0xf957('0x42')](_0xf957('0x56'),_0x57717a,_0x1da837[_0xf957('0x36')]);_0x24d5eb[_0xf957('0x49')]();}}}catch(_0x5b3519){logger[_0xf957('0x42')]('[%s][QUEUE]\x20%s',_0x57717a,_0x5b3519[_0xf957('0x57')]);_0x24d5eb[_0xf957('0x49')]();}};};