Built motion from commit da617fac.|2.6.6
[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 _0x1f38=['complete','[%s][QUEUE]\x20APPLICATION\x20END','next','strategy','rrmemory','log','stack','lodash','util','seq-queue','moment','ioredis','../../../../config/logger','routing','redis','defaults','socket.io-emitter','queue_id','account','queue','from','joinAt','emit','format','pick','%sQueuesWaitingInteractions','%s:queue:%s','%s_queue_waiting_interaction:%s','remove','../../rpc/openchannelQueueReport','../../rpc/mailQueueReport','../../rpc/chatQueueReport','../../rpc/smsQueueReport','../../rpc/whatsappQueueReport','string','number','timeout\x20is\x20a\x20number','waiting','YYYY-MM-DD\x20HH:mm:ss','contact','list','upperFirst','channel','QueueId','InteractionId','interaction','MessageId','message','chat','WebsiteId','info','[%s][START:QUEUE][NAME:%s]','name','QueueReport','create','catch','error','create\x20queue\x20report','accepted','[%s][STOP:QUEUE][NAME:%s]','body','update','update\x20queue\x20report','[%s][END:QUEUE][NAME:%s]','toLowerCase','consequence','validate','[%s][QUEUE]\x20%s','map','stop','find','Queues','timeout','atimeout','qtimeout','rules','end','[%s][QUEUE]\x20ROUTING\x20STOPPED','close'];(function(_0x1c7ced,_0x23f7d5){var _0x1000b8=function(_0x5a994b){while(--_0x5a994b){_0x1c7ced['push'](_0x1c7ced['shift']());}};_0x1000b8(++_0x23f7d5);}(_0x1f38,0x192));var _0x81f3=function(_0x1ee358,_0x42fdfb){_0x1ee358=_0x1ee358-0x0;var _0x4d01b9=_0x1f38[_0x1ee358];return _0x4d01b9;};'use strict';var schema=require('validate');var _=require(_0x81f3('0x0'));var util=require(_0x81f3('0x1'));var seqqueue=require(_0x81f3('0x2'));var moment=require(_0x81f3('0x3'));var Redis=require(_0x81f3('0x4'));var config=require('../../../../config/environment');var logger=require(_0x81f3('0x5'))(_0x81f3('0x6'));var strategy=require('./strategy');config[_0x81f3('0x7')]=_[_0x81f3('0x8')](config[_0x81f3('0x7')],{'host':'localhost','port':0x18eb});var io=require(_0x81f3('0x9'))(new Redis(config['redis']));var attributes=['id',_0x81f3('0xa'),_0x81f3('0xb'),_0x81f3('0xc'),_0x81f3('0xd'),_0x81f3('0xe')];function emit(_0x4dd0c,_0x48ca8f,_0x13dcf7){io['to'](_0x4dd0c)[_0x81f3('0xf')](_0x48ca8f,_0x13dcf7);}function emitChannelQueueInteraction(_0x55206e,_0x544bc4,_0x5002e6,_0x263be9,_0x5084d5,_0x1402aa,_0x55d05a,_0x55044d){var _0x53b1a2=_['clone'](_0x5002e6);_0x53b1a2['queue_id']=_0x55044d;_0x53b1a2['queue']=_0x544bc4;_0x53b1a2['account']=_0x263be9;_0x53b1a2['id']=_0x5084d5;if(_0x1402aa){_0x55d05a[util[_0x81f3('0x10')]('%sQueuesWaitingInteractions',_0x55206e)][_0x5084d5]=_[_0x81f3('0x11')](_0x53b1a2,attributes);}else{delete _0x55d05a[util[_0x81f3('0x10')](_0x81f3('0x12'),_0x55206e)][_0x5084d5];}emit(util[_0x81f3('0x10')](_0x81f3('0x13'),_0x55206e,_0x544bc4),util['format'](_0x81f3('0x14'),_0x55206e,_0x1402aa?'save':_0x81f3('0x15')),_['pick'](_0x53b1a2,attributes));}var rpc={'openchannelQueueReport':require(_0x81f3('0x16')),'mailQueueReport':require(_0x81f3('0x17')),'chatQueueReport':require(_0x81f3('0x18')),'smsQueueReport':require(_0x81f3('0x19')),'faxQueueReport':require('../../rpc/faxQueueReport'),'whatsappQueueReport':require(_0x81f3('0x1a'))};var queue=schema({'name':{'type':_0x81f3('0x1b'),'required':!![],'message':'name\x20is\x20a\x20string\x20and\x20it\x20is\x20required'},'timeout':{'type':_0x81f3('0x1c'),'message':_0x81f3('0x1d')}});function startRouting(_0xbbe1ea,_0x4b8af5,_0x4a4a77,_0x2a12ea){if(_0xbbe1ea&&_0x4b8af5&&_0x4a4a77){_0x4b8af5[_0x81f3('0x1e')]+=0x1;var _0x2ddf6f={'uniqueid':_0xbbe1ea,'from':_0x4a4a77['body']?_0x4a4a77['body'][_0x81f3('0xd')]:'','joinAt':moment()['format'](_0x81f3('0x1f')),'ContactId':_0x4a4a77[_0x81f3('0x20')]?_0x4a4a77['contact']['id']:null,'ListId':_0x4a4a77[_0x81f3('0x21')]?_0x4a4a77[_0x81f3('0x21')]['id']:null};_0x2ddf6f[_[_0x81f3('0x22')](_0x4a4a77[_0x81f3('0x23')])+_0x81f3('0x24')]=_0x4b8af5['id']||null;_0x2ddf6f[_[_0x81f3('0x22')](_0x4a4a77[_0x81f3('0x23')])+_0x81f3('0x25')]=_0x4a4a77[_0x81f3('0x26')]?_0x4a4a77[_0x81f3('0x26')]['id']:null;_0x2ddf6f[_['upperFirst'](_0x4a4a77[_0x81f3('0x23')])+_0x81f3('0x27')]=_0x4a4a77['message']?_0x4a4a77[_0x81f3('0x28')]['id']:null;_0x2ddf6f[_[_0x81f3('0x22')](_0x4a4a77[_0x81f3('0x23')])+(_0x4a4a77[_0x81f3('0x23')]!==_0x81f3('0x29')?'AccountId':_0x81f3('0x2a'))]=_0x4a4a77['account']?_0x4a4a77['account']['id']:null;logger[_0x81f3('0x2b')](util['format'](_0x81f3('0x2c'),_0xbbe1ea,_0x4b8af5['name']));emitChannelQueueInteraction(_0x4a4a77[_0x81f3('0x23')],_0x4b8af5[_0x81f3('0x2d')],_0x2ddf6f,_0x4a4a77[_0x81f3('0xb')][_0x81f3('0x2d')],_0x4a4a77[_0x81f3('0x28')]['id'],!![],_0x2a12ea,_0x4b8af5['id']);if(rpc[_0x4a4a77['channel']+_0x81f3('0x2e')]){return rpc[_0x4a4a77['channel']+_0x81f3('0x2e')][_0x81f3('0x2f')](_0x2ddf6f)[_0x81f3('0x30')](function(_0x23dcce){logger[_0x81f3('0x31')](_0x81f3('0x32'));});}}}function stopRouting(_0x10c22b,_0x471e3e,_0x170c6c,_0x3183cb){if(_0x10c22b&&_0x471e3e){_0x471e3e['waiting']-=0x1;var _0x4ee300={'leaveAt':moment()['format'](_0x81f3('0x1f')),'acceptAt':moment()[_0x81f3('0x10')](_0x81f3('0x1f')),'reason':_0x81f3('0x33')};logger[_0x81f3('0x2b')](util[_0x81f3('0x10')](_0x81f3('0x34'),_0x10c22b,_0x471e3e[_0x81f3('0x2d')]));emitChannelQueueInteraction(_0x170c6c[_0x81f3('0x23')],_0x471e3e[_0x81f3('0x2d')],_0x170c6c[_0x81f3('0x35')],_0x170c6c['account']['name'],_0x170c6c[_0x81f3('0x28')]['id'],![],_0x3183cb,_0x471e3e['id']);if(rpc[_0x170c6c['channel']+'QueueReport']){return rpc[_0x170c6c[_0x81f3('0x23')]+_0x81f3('0x2e')][_0x81f3('0x36')](_0x10c22b,_0x4ee300)[_0x81f3('0x30')](function(_0x12d9ce){logger[_0x81f3('0x31')](_0x81f3('0x37'));});}}}function endRouting(_0x3a709d,_0x1d8aa3,_0x53d786,_0x311c82){if(_0x3a709d&&_0x1d8aa3){_0x1d8aa3['waiting']-=0x1;var _0x53c710={'leaveAt':moment()[_0x81f3('0x10')](_0x81f3('0x1f')),'exitAt':moment()[_0x81f3('0x10')](_0x81f3('0x1f')),'reason':'timeout'};logger[_0x81f3('0x2b')](util[_0x81f3('0x10')](_0x81f3('0x38'),_0x3a709d,_0x1d8aa3[_0x81f3('0x2d')]));emitChannelQueueInteraction(_0x53d786[_0x81f3('0x23')],_0x1d8aa3[_0x81f3('0x2d')],_0x53d786[_0x81f3('0x35')],_0x53d786[_0x81f3('0xb')]['name'],_0x53d786[_0x81f3('0x28')]['id'],![],_0x311c82);if(rpc[_0x53d786[_0x81f3('0x23')]+_0x81f3('0x2e')]){return rpc[_0x53d786[_0x81f3('0x23')]+_0x81f3('0x2e')]['update'](_0x3a709d,_0x53c710)[_0x81f3('0x30')](function(_0x2d4b52){logger[_0x81f3('0x31')](_0x81f3('0x37'));});}}}function handleFindQueue(_0x5966a7){return function(_0xece5d4){return _0xece5d4[_0x81f3('0x2d')][_0x81f3('0x39')]()===_0x5966a7[_0x81f3('0x39')]();};}exports[_0x81f3('0x3a')]=function(_0x1f1282,_0x1ee9a3,_0x43e8c2,_0x25f4b2){return function(_0x25d24f){try{var _0x4f903a={'name':_0x1ee9a3[0x0],'timeout':_0x1ee9a3[0x1]};var _0x2ada45=queue[_0x81f3('0x3b')](_0x4f903a,{'typecast':!![]});if(_0x2ada45['length']){logger[_0x81f3('0x31')](_0x81f3('0x3c'),_0x1f1282,_[_0x81f3('0x3d')](_0x2ada45,_0x81f3('0x28'))['join'](',\x20'));_0x25d24f[_0x81f3('0x3e')]();}else{var _0x16fd08=_[_0x81f3('0x3f')](_0x43e8c2[_0x25f4b2[_0x81f3('0x23')]+_0x81f3('0x40')],handleFindQueue(_0x4f903a[_0x81f3('0x2d')]));if(_0x16fd08){startRouting(_0x1f1282,_0x16fd08,_0x25f4b2,_0x43e8c2);var _0x47f140=(_0x16fd08[_0x81f3('0x41')]||0xa)*0x3e8;var _0x239238=(_0x4f903a[_0x81f3('0x41')]||0x14)*0x3e8;var _0x4cefcb,_0x4770fd;if(_0x47f140>_0x239238){_0x239238=_0x47f140;}var _0x1f9de9=seqqueue['createQueue'](_0x47f140);_0x1f9de9['id']=_0x1f1282;_0x1f9de9[_0x81f3('0x42')]=_0x47f140;_0x1f9de9[_0x81f3('0x43')]=_0x239238;_0x1f9de9[_0x81f3('0x23')]=_0x25f4b2[_0x81f3('0x23')];_0x43e8c2[_0x81f3('0x44')][_0x1f1282]['on'](_0x81f3('0x45'),function(){logger[_0x81f3('0x2b')](_0x81f3('0x46'),_0x1f1282);stopRouting(_0x1f1282,_0x16fd08,_0x25f4b2,_0x43e8c2);clearTimeout(_0x4cefcb);_0x4cefcb=undefined;_0x1f9de9[_0x81f3('0x47')](!![]);if(_0x43e8c2[_0x81f3('0x44')][_0x1f1282]){delete _0x43e8c2[_0x81f3('0x44')][_0x1f1282];}_0x25d24f[_0x81f3('0x3e')]();});_0x43e8c2[_0x81f3('0x44')][_0x1f1282]['on'](_0x81f3('0x48'),function(){if(_0x43e8c2[_0x81f3('0x44')][_0x1f1282]){delete _0x43e8c2[_0x81f3('0x44')][_0x1f1282];}});_0x4cefcb=setTimeout(function(){logger['info'](_0x81f3('0x49'),_0x1f1282);endRouting(_0x1f1282,_0x16fd08,_0x25f4b2,_0x43e8c2);clearTimeout(_0x4cefcb);_0x1f9de9[_0x81f3('0x47')](!![]);_0x25d24f[_0x81f3('0x4a')]();},_0x239238);switch(_0x16fd08[_0x81f3('0x4b')]){case _0x81f3('0x4c'):strategy[_0x81f3('0x4c')](_0x1f9de9,_0x16fd08,_0x43e8c2,_0x25f4b2);break;default:strategy['beepall'](_0x1f9de9,_0x16fd08,_0x43e8c2,_0x25f4b2);}}else{logger[_0x81f3('0x31')]('[%s][QUEUE]\x20queue\x20not\x20found\x20%s',_0x1f1282,_0x4f903a[_0x81f3('0x2d')]);_0x25d24f[_0x81f3('0x3e')]();}}}catch(_0x416997){console[_0x81f3('0x4d')](_0x416997);logger[_0x81f3('0x31')](_0x81f3('0x3c'),_0x1f1282,_0x416997[_0x81f3('0x4e')]);_0x25d24f[_0x81f3('0x3e')]();}};};