Built motion from commit 0bd120ac.|2.6.25
[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 _0x40f3=['accepted','info','Interaction','update','then','update\x20queue\x20report','timeout','[%s][END:QUEUE][NAME:%s]','toLowerCase','consequence','error','[%s][QUEUE]\x20%s','map','stop','find','atimeout','qtimeout','rules','[%s][QUEUE]\x20ROUTING\x20STOPPED','close','complete','[%s][QUEUE]\x20APPLICATION\x20END','strategy','rrmemory','beepall','[%s][QUEUE]\x20queue\x20not\x20found\x20%s','stack','validate','lodash','util','seq-queue','../../../../config/environment','./strategy','redis','localhost','socket.io-emitter','queue_id','account','joinAt','clone','push','capitalize','format','%sQueuesWaitingInteractions','%s:queue:%s','%s_queue_waiting_interaction:%s','remove','pick','../../rpc/mailQueueReport','../../rpc/smsQueueReport','../../rpc/faxQueueReport','../../rpc/chatInteraction','../../rpc/openchannelInteraction','../../rpc/smsInteraction','string','name\x20is\x20a\x20string\x20and\x20it\x20is\x20required','number','timeout\x20is\x20a\x20number','waiting','body','from','YYYY-MM-DD\x20HH:mm:ss','contact','list','upperFirst','QueueId','channel','InteractionId','interaction','MessageId','message','chat','AccountId','WebsiteId','[%s][START:QUEUE][NAME:%s]','name','QueueReport','create','catch','create\x20queue\x20report'];(function(_0x834637,_0x1b0ce9){var _0x44d3a0=function(_0x10f79b){while(--_0x10f79b){_0x834637['push'](_0x834637['shift']());}};_0x44d3a0(++_0x1b0ce9);}(_0x40f3,0x6b));var _0x340f=function(_0x4e9d3c,_0x1f4b98){_0x4e9d3c=_0x4e9d3c-0x0;var _0x2d0825=_0x40f3[_0x4e9d3c];return _0x2d0825;};'use strict';var schema=require(_0x340f('0x0'));var _=require(_0x340f('0x1'));var util=require(_0x340f('0x2'));var seqqueue=require(_0x340f('0x3'));var moment=require('moment');var Redis=require('ioredis');var config=require(_0x340f('0x4'));var logger=require('../../../../config/logger')('routing');var strategy=require(_0x340f('0x5'));config[_0x340f('0x6')]=_['defaults'](config[_0x340f('0x6')],{'host':_0x340f('0x7'),'port':0x18eb});var io=require(_0x340f('0x8'))(new Redis(config[_0x340f('0x6')]));var attributes=['id',_0x340f('0x9'),_0x340f('0xa'),'queue','from',_0x340f('0xb')];function emit(_0x49933b,_0x44d40d,_0x116ce5){io['to'](_0x49933b)['emit'](_0x44d40d,_0x116ce5);}function emitChannelQueueInteraction(_0x17758a,_0x346cf7,_0x3478a4,_0x2c4991,_0x3ab30b,_0x1be226,_0x296456,_0x2e50f1){var _0x1c934a=_[_0x340f('0xc')](_0x3478a4);_0x1c934a[_0x340f('0x9')]=_0x2e50f1;_0x1c934a['queue']=_0x346cf7;_0x1c934a[_0x340f('0xa')]=_0x2c4991;_0x1c934a['id']=_0x3ab30b;if(_0x1be226){attributes[_0x340f('0xd')](_[_0x340f('0xe')](_0x17758a)+'InteractionId');_0x296456[util[_0x340f('0xf')](_0x340f('0x10'),_0x17758a)][_0x3ab30b]=_['pick'](_0x1c934a,attributes);}else{delete _0x296456[util['format']('%sQueuesWaitingInteractions',_0x17758a)][_0x3ab30b];}emit(util['format'](_0x340f('0x11'),_0x17758a,_0x346cf7),util[_0x340f('0xf')](_0x340f('0x12'),_0x17758a,_0x1be226?'save':_0x340f('0x13')),_[_0x340f('0x14')](_0x1c934a,attributes));}var rpc={'openchannelQueueReport':require('../../rpc/openchannelQueueReport'),'mailQueueReport':require(_0x340f('0x15')),'chatQueueReport':require('../../rpc/chatQueueReport'),'smsQueueReport':require(_0x340f('0x16')),'faxQueueReport':require(_0x340f('0x17')),'whatsappQueueReport':require('../../rpc/whatsappQueueReport'),'chatInteraction':require(_0x340f('0x18')),'faxInteraction':require('../../rpc/faxInteraction'),'mailInteraction':require('../../rpc/mailInteraction'),'openchannelInteraction':require(_0x340f('0x19')),'smsInteraction':require(_0x340f('0x1a')),'whatsappInteraction':require('../../rpc/whatsappInteraction')};var queue=schema({'name':{'type':_0x340f('0x1b'),'required':!![],'message':_0x340f('0x1c')},'timeout':{'type':_0x340f('0x1d'),'message':_0x340f('0x1e')}});function startRouting(_0x1d19be,_0x1ec63d,_0x19caf6,_0x3b854d){if(_0x1d19be&&_0x1ec63d&&_0x19caf6){_0x1ec63d[_0x340f('0x1f')]+=0x1;var _0x44a771={'uniqueid':_0x1d19be,'from':_0x19caf6[_0x340f('0x20')]?_0x19caf6[_0x340f('0x20')][_0x340f('0x21')]:'','joinAt':moment()[_0x340f('0xf')](_0x340f('0x22')),'ContactId':_0x19caf6[_0x340f('0x23')]?_0x19caf6[_0x340f('0x23')]['id']:null,'ListId':_0x19caf6[_0x340f('0x24')]?_0x19caf6[_0x340f('0x24')]['id']:null};_0x44a771[_[_0x340f('0x25')](_0x19caf6['channel'])+_0x340f('0x26')]=_0x1ec63d['id']||null;_0x44a771[_['upperFirst'](_0x19caf6[_0x340f('0x27')])+_0x340f('0x28')]=_0x19caf6['interaction']?_0x19caf6[_0x340f('0x29')]['id']:null;_0x44a771[_[_0x340f('0x25')](_0x19caf6[_0x340f('0x27')])+_0x340f('0x2a')]=_0x19caf6['message']?_0x19caf6[_0x340f('0x2b')]['id']:null;_0x44a771[_[_0x340f('0x25')](_0x19caf6['channel'])+(_0x19caf6['channel']!==_0x340f('0x2c')?_0x340f('0x2d'):_0x340f('0x2e'))]=_0x19caf6[_0x340f('0xa')]?_0x19caf6[_0x340f('0xa')]['id']:null;logger['info'](util[_0x340f('0xf')](_0x340f('0x2f'),_0x1d19be,_0x1ec63d[_0x340f('0x30')]));emitChannelQueueInteraction(_0x19caf6[_0x340f('0x27')],_0x1ec63d[_0x340f('0x30')],_0x44a771,_0x19caf6['account'][_0x340f('0x30')],_0x19caf6[_0x340f('0x2b')]['id'],!![],_0x3b854d,_0x1ec63d['id']);if(rpc[_0x19caf6[_0x340f('0x27')]+'QueueReport']){return rpc[_0x19caf6[_0x340f('0x27')]+_0x340f('0x31')][_0x340f('0x32')](_0x44a771)[_0x340f('0x33')](function(_0x3f4701){logger['error'](_0x340f('0x34'),_0x3f4701);});}}}function stopRouting(_0x1152fd,_0x2c63d7,_0x214b9d,_0xa8ea96){if(_0x1152fd&&_0x2c63d7){_0x2c63d7['waiting']-=0x1;var _0x53a078={'leaveAt':moment()[_0x340f('0xf')]('YYYY-MM-DD\x20HH:mm:ss'),'acceptAt':moment()[_0x340f('0xf')](_0x340f('0x22')),'reason':_0x340f('0x35')};logger[_0x340f('0x36')](util['format']('[%s][STOP:QUEUE][NAME:%s]',_0x1152fd,_0x2c63d7[_0x340f('0x30')]));emitChannelQueueInteraction(_0x214b9d[_0x340f('0x27')],_0x2c63d7['name'],_0x214b9d[_0x340f('0x20')],_0x214b9d[_0x340f('0xa')][_0x340f('0x30')],_0x214b9d[_0x340f('0x2b')]['id'],![],_0xa8ea96,_0x2c63d7['id']);return rpc[_0x214b9d['channel']+_0x340f('0x37')][_0x340f('0x38')](_0x214b9d['interaction']['id'],{'queueId':_0x2c63d7['id']})[_0x340f('0x39')](function(){if(rpc[_0x214b9d['channel']+'QueueReport']){return rpc[_0x214b9d[_0x340f('0x27')]+_0x340f('0x31')]['update'](_0x1152fd,_0x53a078)['catch'](function(_0x1e2dbf){logger['error'](_0x340f('0x3a'),_0x1e2dbf);});}});}}function endRouting(_0x5daef9,_0x1286ee,_0x25e220,_0x22fc61){if(_0x5daef9&&_0x1286ee){_0x1286ee[_0x340f('0x1f')]-=0x1;var _0x2e6a1f={'leaveAt':moment()[_0x340f('0xf')](_0x340f('0x22')),'exitAt':moment()[_0x340f('0xf')](_0x340f('0x22')),'reason':_0x340f('0x3b')};logger['info'](util[_0x340f('0xf')](_0x340f('0x3c'),_0x5daef9,_0x1286ee['name']));emitChannelQueueInteraction(_0x25e220['channel'],_0x1286ee[_0x340f('0x30')],_0x25e220[_0x340f('0x20')],_0x25e220[_0x340f('0xa')]['name'],_0x25e220[_0x340f('0x2b')]['id'],![],_0x22fc61);if(rpc[_0x25e220[_0x340f('0x27')]+'QueueReport']){return rpc[_0x25e220[_0x340f('0x27')]+_0x340f('0x31')][_0x340f('0x38')](_0x5daef9,_0x2e6a1f)[_0x340f('0x33')](function(_0x512c69){logger['error']('update\x20queue\x20report',_0x512c69);});}}}function handleFindQueue(_0x131b97){return function(_0x382cf5){return _0x382cf5[_0x340f('0x30')]['toLowerCase']()===_0x131b97[_0x340f('0x3d')]();};}exports[_0x340f('0x3e')]=function(_0x14dae0,_0x271142,_0x41acf9,_0x2ea26e){return function(_0x4b8f0a){try{var _0x3f67bd={'name':_0x271142[0x0],'timeout':_0x271142[0x1]};var _0x51491d=queue['validate'](_0x3f67bd,{'typecast':!![]});if(_0x51491d['length']){logger[_0x340f('0x3f')](_0x340f('0x40'),_0x14dae0,_[_0x340f('0x41')](_0x51491d,_0x340f('0x2b'))['join'](',\x20'));_0x4b8f0a[_0x340f('0x42')]();}else{var _0x37827c=_[_0x340f('0x43')](_0x41acf9[_0x2ea26e['channel']+'Queues'],handleFindQueue(_0x3f67bd[_0x340f('0x30')]));if(_0x37827c){startRouting(_0x14dae0,_0x37827c,_0x2ea26e,_0x41acf9);var _0x3efa8e=(_0x37827c['timeout']||0xa)*0x3e8;var _0x42c618=(_0x3f67bd['timeout']||0x14)*0x3e8;var _0x5eb7c1;if(_0x3efa8e>_0x42c618){_0x42c618=_0x3efa8e;}var _0x33048b=seqqueue['createQueue'](_0x3efa8e);_0x33048b['id']=_0x14dae0;_0x33048b[_0x340f('0x44')]=_0x3efa8e;_0x33048b[_0x340f('0x45')]=_0x42c618;_0x33048b[_0x340f('0x27')]=_0x2ea26e[_0x340f('0x27')];_0x41acf9[_0x340f('0x46')][_0x14dae0]['on']('end',function(){logger[_0x340f('0x36')](_0x340f('0x47'),_0x14dae0);stopRouting(_0x14dae0,_0x37827c,_0x2ea26e,_0x41acf9);clearTimeout(_0x5eb7c1);_0x5eb7c1=undefined;_0x33048b[_0x340f('0x48')](!![]);if(_0x41acf9[_0x340f('0x46')][_0x14dae0]){delete _0x41acf9['rules'][_0x14dae0];}_0x4b8f0a[_0x340f('0x42')]();});_0x41acf9['rules'][_0x14dae0]['on'](_0x340f('0x49'),function(){if(_0x41acf9['rules'][_0x14dae0]){delete _0x41acf9[_0x340f('0x46')][_0x14dae0];}});_0x5eb7c1=setTimeout(function(){logger[_0x340f('0x36')](_0x340f('0x4a'),_0x14dae0);endRouting(_0x14dae0,_0x37827c,_0x2ea26e,_0x41acf9);clearTimeout(_0x5eb7c1);_0x33048b['close'](!![]);_0x4b8f0a['next']();},_0x42c618);switch(_0x37827c[_0x340f('0x4b')]){case _0x340f('0x4c'):strategy[_0x340f('0x4c')](_0x33048b,_0x37827c,_0x41acf9,_0x2ea26e);break;default:strategy[_0x340f('0x4d')](_0x33048b,_0x37827c,_0x41acf9,_0x2ea26e);}}else{logger[_0x340f('0x3f')](_0x340f('0x4e'),_0x14dae0,_0x3f67bd[_0x340f('0x30')]);_0x4b8f0a[_0x340f('0x42')]();}}}catch(_0x330178){logger['error'](_0x340f('0x40'),_0x14dae0,_0x330178[_0x340f('0x4f')]);_0x4b8f0a[_0x340f('0x42')]();}};};