f30f88aa730837585d710c02692b7b2ca2906867
[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 _0x046a=['from','joinAt','emit','clone','queue','format','%sQueuesWaitingInteractions','pick','%s_queue_waiting_interaction:%s','remove','../../rpc/openchannelQueueReport','../../rpc/smsQueueReport','../../rpc/faxQueueReport','../../rpc/whatsappQueueReport','string','name\x20is\x20a\x20string\x20and\x20it\x20is\x20required','number','waiting','body','YYYY-MM-DD\x20HH:mm:ss','contact','list','upperFirst','channel','QueueId','InteractionId','interaction','MessageId','message','chat','AccountId','info','[%s][START:QUEUE][NAME:%s]','name','QueueReport','create','catch','error','create\x20queue\x20report','accepted','update','timeout','[%s][END:QUEUE][NAME:%s]','update\x20queue\x20report','toLowerCase','consequence','length','[%s][QUEUE]\x20%s','map','join','stop','find','Queues','createQueue','qtimeout','rules','end','[%s][QUEUE]\x20ROUTING\x20STOPPED','close','complete','[%s][QUEUE]\x20APPLICATION\x20END','next','rrmemory','[%s][QUEUE]\x20queue\x20not\x20found\x20%s','stack','validate','util','seq-queue','moment','ioredis','redis','defaults','localhost','queue_id','account'];(function(_0x1f6f84,_0x2bab55){var _0x177f40=function(_0x384ab4){while(--_0x384ab4){_0x1f6f84['push'](_0x1f6f84['shift']());}};_0x177f40(++_0x2bab55);}(_0x046a,0x122));var _0xa046=function(_0x297fd3,_0x115406){_0x297fd3=_0x297fd3-0x0;var _0x2af381=_0x046a[_0x297fd3];return _0x2af381;};'use strict';var schema=require(_0xa046('0x0'));var _=require('lodash');var util=require(_0xa046('0x1'));var seqqueue=require(_0xa046('0x2'));var moment=require(_0xa046('0x3'));var Redis=require(_0xa046('0x4'));var config=require('../../../../config/environment');var logger=require('../../../../config/logger')('routing');var strategy=require('./strategy');config[_0xa046('0x5')]=_[_0xa046('0x6')](config[_0xa046('0x5')],{'host':_0xa046('0x7'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xa046('0x5')]));var attributes=['id',_0xa046('0x8'),_0xa046('0x9'),'queue',_0xa046('0xa'),_0xa046('0xb')];function emit(_0xb23697,_0x1e89ff,_0x5102b5){io['to'](_0xb23697)[_0xa046('0xc')](_0x1e89ff,_0x5102b5);}function emitChannelQueueInteraction(_0x407e5f,_0x164e46,_0x3cdd42,_0x127154,_0x23e339,_0x211f03,_0x5e121a,_0x3a9623){var _0x17ce0d=_[_0xa046('0xd')](_0x3cdd42);_0x17ce0d['queue_id']=_0x3a9623;_0x17ce0d[_0xa046('0xe')]=_0x164e46;_0x17ce0d[_0xa046('0x9')]=_0x127154;_0x17ce0d['id']=_0x23e339;if(_0x211f03){_0x5e121a[util[_0xa046('0xf')](_0xa046('0x10'),_0x407e5f)][_0x23e339]=_[_0xa046('0x11')](_0x17ce0d,attributes);}else{delete _0x5e121a[util[_0xa046('0xf')](_0xa046('0x10'),_0x407e5f)][_0x23e339];}emit(util[_0xa046('0xf')]('%s:queue:%s',_0x407e5f,_0x164e46),util['format'](_0xa046('0x12'),_0x407e5f,_0x211f03?'save':_0xa046('0x13')),_[_0xa046('0x11')](_0x17ce0d,attributes));}var rpc={'openchannelQueueReport':require(_0xa046('0x14')),'mailQueueReport':require('../../rpc/mailQueueReport'),'chatQueueReport':require('../../rpc/chatQueueReport'),'smsQueueReport':require(_0xa046('0x15')),'faxQueueReport':require(_0xa046('0x16')),'whatsappQueueReport':require(_0xa046('0x17'))};var queue=schema({'name':{'type':_0xa046('0x18'),'required':!![],'message':_0xa046('0x19')},'timeout':{'type':_0xa046('0x1a'),'message':'timeout\x20is\x20a\x20number'}});function startRouting(_0x3b8408,_0x1e8dbe,_0x3991ef,_0x3adb22){if(_0x3b8408&&_0x1e8dbe&&_0x3991ef){_0x1e8dbe[_0xa046('0x1b')]+=0x1;var _0x28e69d={'uniqueid':_0x3b8408,'from':_0x3991ef[_0xa046('0x1c')]?_0x3991ef[_0xa046('0x1c')][_0xa046('0xa')]:'','joinAt':moment()[_0xa046('0xf')](_0xa046('0x1d')),'ContactId':_0x3991ef[_0xa046('0x1e')]?_0x3991ef['contact']['id']:null,'ListId':_0x3991ef[_0xa046('0x1f')]?_0x3991ef[_0xa046('0x1f')]['id']:null};_0x28e69d[_[_0xa046('0x20')](_0x3991ef[_0xa046('0x21')])+_0xa046('0x22')]=_0x1e8dbe['id']||null;_0x28e69d[_[_0xa046('0x20')](_0x3991ef[_0xa046('0x21')])+_0xa046('0x23')]=_0x3991ef['interaction']?_0x3991ef[_0xa046('0x24')]['id']:null;_0x28e69d[_[_0xa046('0x20')](_0x3991ef['channel'])+_0xa046('0x25')]=_0x3991ef[_0xa046('0x26')]?_0x3991ef[_0xa046('0x26')]['id']:null;_0x28e69d[_['upperFirst'](_0x3991ef[_0xa046('0x21')])+(_0x3991ef[_0xa046('0x21')]!==_0xa046('0x27')?_0xa046('0x28'):'WebsiteId')]=_0x3991ef[_0xa046('0x9')]?_0x3991ef[_0xa046('0x9')]['id']:null;logger[_0xa046('0x29')](util['format'](_0xa046('0x2a'),_0x3b8408,_0x1e8dbe['name']));emitChannelQueueInteraction(_0x3991ef['channel'],_0x1e8dbe[_0xa046('0x2b')],_0x28e69d,_0x3991ef['account'][_0xa046('0x2b')],_0x3991ef[_0xa046('0x26')]['id'],!![],_0x3adb22,_0x1e8dbe['id']);if(rpc[_0x3991ef[_0xa046('0x21')]+'QueueReport']){return rpc[_0x3991ef[_0xa046('0x21')]+_0xa046('0x2c')][_0xa046('0x2d')](_0x28e69d)[_0xa046('0x2e')](function(_0x3b1093){logger[_0xa046('0x2f')](_0xa046('0x30'));});}}}function stopRouting(_0x1eb0fb,_0x22de60,_0x52ee37,_0x376ac4){if(_0x1eb0fb&&_0x22de60){_0x22de60[_0xa046('0x1b')]-=0x1;var _0x30490f={'leaveAt':moment()[_0xa046('0xf')](_0xa046('0x1d')),'acceptAt':moment()[_0xa046('0xf')](_0xa046('0x1d')),'reason':_0xa046('0x31')};logger[_0xa046('0x29')](util[_0xa046('0xf')]('[%s][STOP:QUEUE][NAME:%s]',_0x1eb0fb,_0x22de60[_0xa046('0x2b')]));emitChannelQueueInteraction(_0x52ee37[_0xa046('0x21')],_0x22de60[_0xa046('0x2b')],_0x52ee37[_0xa046('0x1c')],_0x52ee37[_0xa046('0x9')][_0xa046('0x2b')],_0x52ee37[_0xa046('0x26')]['id'],![],_0x376ac4,_0x22de60['id']);if(rpc[_0x52ee37[_0xa046('0x21')]+_0xa046('0x2c')]){return rpc[_0x52ee37[_0xa046('0x21')]+_0xa046('0x2c')][_0xa046('0x32')](_0x1eb0fb,_0x30490f)['catch'](function(_0x3a5139){logger[_0xa046('0x2f')]('update\x20queue\x20report');});}}}function endRouting(_0x243129,_0x4916a7,_0x292394,_0x33f0ac){if(_0x243129&&_0x4916a7){_0x4916a7['waiting']-=0x1;var _0x19ae24={'leaveAt':moment()['format'](_0xa046('0x1d')),'exitAt':moment()[_0xa046('0xf')]('YYYY-MM-DD\x20HH:mm:ss'),'reason':_0xa046('0x33')};logger[_0xa046('0x29')](util[_0xa046('0xf')](_0xa046('0x34'),_0x243129,_0x4916a7['name']));emitChannelQueueInteraction(_0x292394[_0xa046('0x21')],_0x4916a7[_0xa046('0x2b')],_0x292394[_0xa046('0x1c')],_0x292394['account'][_0xa046('0x2b')],_0x292394[_0xa046('0x26')]['id'],![],_0x33f0ac);if(rpc[_0x292394[_0xa046('0x21')]+_0xa046('0x2c')]){return rpc[_0x292394[_0xa046('0x21')]+'QueueReport'][_0xa046('0x32')](_0x243129,_0x19ae24)[_0xa046('0x2e')](function(_0x551e6a){logger['error'](_0xa046('0x35'));});}}}function handleFindQueue(_0x3b4443){return function(_0x24cd46){return _0x24cd46['name'][_0xa046('0x36')]()===_0x3b4443[_0xa046('0x36')]();};}exports[_0xa046('0x37')]=function(_0x4e5f38,_0x3d64a4,_0x534004,_0x386520){return function(_0xeacb5c){try{var _0xe2cfa={'name':_0x3d64a4[0x0],'timeout':_0x3d64a4[0x1]};var _0x2bb1a5=queue[_0xa046('0x0')](_0xe2cfa,{'typecast':!![]});if(_0x2bb1a5[_0xa046('0x38')]){logger[_0xa046('0x2f')](_0xa046('0x39'),_0x4e5f38,_[_0xa046('0x3a')](_0x2bb1a5,_0xa046('0x26'))[_0xa046('0x3b')](',\x20'));_0xeacb5c[_0xa046('0x3c')]();}else{var _0x3186e5=_[_0xa046('0x3d')](_0x534004[_0x386520['channel']+_0xa046('0x3e')],handleFindQueue(_0xe2cfa['name']));if(_0x3186e5){startRouting(_0x4e5f38,_0x3186e5,_0x386520,_0x534004);var _0x357b5d=(_0x3186e5[_0xa046('0x33')]||0xa)*0x3e8;var _0x392f60=(_0xe2cfa[_0xa046('0x33')]||0x14)*0x3e8;var _0x1ab23e,_0x16da48;if(_0x357b5d>_0x392f60){_0x392f60=_0x357b5d;}var _0x411384=seqqueue[_0xa046('0x3f')](_0x357b5d);_0x411384['id']=_0x4e5f38;_0x411384['atimeout']=_0x357b5d;_0x411384[_0xa046('0x40')]=_0x392f60;_0x411384['channel']=_0x386520[_0xa046('0x21')];_0x534004[_0xa046('0x41')][_0x4e5f38]['on'](_0xa046('0x42'),function(){logger['info'](_0xa046('0x43'),_0x4e5f38);stopRouting(_0x4e5f38,_0x3186e5,_0x386520,_0x534004);clearTimeout(_0x1ab23e);_0x1ab23e=undefined;_0x411384[_0xa046('0x44')](!![]);if(_0x534004[_0xa046('0x41')][_0x4e5f38]){delete _0x534004[_0xa046('0x41')][_0x4e5f38];}_0xeacb5c[_0xa046('0x3c')]();});_0x534004[_0xa046('0x41')][_0x4e5f38]['on'](_0xa046('0x45'),function(){if(_0x534004[_0xa046('0x41')][_0x4e5f38]){delete _0x534004[_0xa046('0x41')][_0x4e5f38];}});_0x1ab23e=setTimeout(function(){logger[_0xa046('0x29')](_0xa046('0x46'),_0x4e5f38);endRouting(_0x4e5f38,_0x3186e5,_0x386520,_0x534004);clearTimeout(_0x1ab23e);_0x411384[_0xa046('0x44')](!![]);_0xeacb5c[_0xa046('0x47')]();},_0x392f60);switch(_0x3186e5['strategy']){case'rrmemory':strategy[_0xa046('0x48')](_0x411384,_0x3186e5,_0x534004,_0x386520);break;default:strategy['beepall'](_0x411384,_0x3186e5,_0x534004,_0x386520);}}else{logger[_0xa046('0x2f')](_0xa046('0x49'),_0x4e5f38,_0xe2cfa[_0xa046('0x2b')]);_0xeacb5c[_0xa046('0x3c')]();}}}catch(_0x4a4b73){console['log'](_0x4a4b73);logger['error'](_0xa046('0x39'),_0x4e5f38,_0x4a4b73[_0xa046('0x4a')]);_0xeacb5c['stop']();}};};