ff362ce54058927fa1e0b69b3098b4a19e8d6e89
[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 _0x7bfa=['save','remove','../../rpc/openchannelQueueReport','../../rpc/chatQueueReport','../../rpc/smsQueueReport','../../rpc/faxQueueReport','../../rpc/openchannelInteraction','../../rpc/whatsappInteraction','string','number','body','YYYY-MM-DD\x20HH:mm:ss','list','upperFirst','channel','InteractionId','interaction','MessageId','message','AccountId','WebsiteId','info','[%s][START:QUEUE][NAME:%s]','name','QueueReport','create','catch','error','create\x20queue\x20report','waiting','accepted','[%s][STOP:QUEUE][NAME:%s]','Interaction','update','then','timeout','[%s][END:QUEUE][NAME:%s]','update\x20queue\x20report','toLowerCase','consequence','length','map','join','Queues','createQueue','atimeout','rules','end','[%s][QUEUE]\x20ROUTING\x20STOPPED','stop','complete','[%s][QUEUE]\x20APPLICATION\x20END','next','strategy','rrmemory','beepall','[%s][QUEUE]\x20queue\x20not\x20found\x20%s','[%s][QUEUE]\x20%s','stack','lodash','util','seq-queue','moment','../../../../config/environment','routing','./strategy','defaults','redis','localhost','socket.io-emitter','queue_id','queue','from','emit','clone','account','format','%sQueuesWaitingInteractions','pick','%s:queue:%s','%s_queue_waiting_interaction:%s'];(function(_0x34dbff,_0x3bf1d1){var _0x80354b=function(_0x1c81b4){while(--_0x1c81b4){_0x34dbff['push'](_0x34dbff['shift']());}};_0x80354b(++_0x3bf1d1);}(_0x7bfa,0xdd));var _0xa7bf=function(_0x2b3478,_0x58afd7){_0x2b3478=_0x2b3478-0x0;var _0x1c106f=_0x7bfa[_0x2b3478];return _0x1c106f;};'use strict';var schema=require('validate');var _=require(_0xa7bf('0x0'));var util=require(_0xa7bf('0x1'));var seqqueue=require(_0xa7bf('0x2'));var moment=require(_0xa7bf('0x3'));var Redis=require('ioredis');var config=require(_0xa7bf('0x4'));var logger=require('../../../../config/logger')(_0xa7bf('0x5'));var strategy=require(_0xa7bf('0x6'));config['redis']=_[_0xa7bf('0x7')](config[_0xa7bf('0x8')],{'host':_0xa7bf('0x9'),'port':0x18eb});var io=require(_0xa7bf('0xa'))(new Redis(config[_0xa7bf('0x8')]));var attributes=['id',_0xa7bf('0xb'),'account',_0xa7bf('0xc'),_0xa7bf('0xd'),'joinAt'];function emit(_0xb78c92,_0x2529a3,_0x210efb){io['to'](_0xb78c92)[_0xa7bf('0xe')](_0x2529a3,_0x210efb);}function emitChannelQueueInteraction(_0xa6e2d5,_0x406514,_0x2892ab,_0x4ccb5f,_0x24a95b,_0x1aeb9a,_0x3e0413,_0x7ba7da){var _0x35ee5b=_[_0xa7bf('0xf')](_0x2892ab);_0x35ee5b[_0xa7bf('0xb')]=_0x7ba7da;_0x35ee5b[_0xa7bf('0xc')]=_0x406514;_0x35ee5b[_0xa7bf('0x10')]=_0x4ccb5f;_0x35ee5b['id']=_0x24a95b;if(_0x1aeb9a){attributes['push'](_['capitalize'](_0xa6e2d5)+'InteractionId');_0x3e0413[util[_0xa7bf('0x11')](_0xa7bf('0x12'),_0xa6e2d5)][_0x24a95b]=_[_0xa7bf('0x13')](_0x35ee5b,attributes);}else{delete _0x3e0413[util[_0xa7bf('0x11')](_0xa7bf('0x12'),_0xa6e2d5)][_0x24a95b];}emit(util['format'](_0xa7bf('0x14'),_0xa6e2d5,_0x406514),util[_0xa7bf('0x11')](_0xa7bf('0x15'),_0xa6e2d5,_0x1aeb9a?_0xa7bf('0x16'):_0xa7bf('0x17')),_[_0xa7bf('0x13')](_0x35ee5b,attributes));}var rpc={'openchannelQueueReport':require(_0xa7bf('0x18')),'mailQueueReport':require('../../rpc/mailQueueReport'),'chatQueueReport':require(_0xa7bf('0x19')),'smsQueueReport':require(_0xa7bf('0x1a')),'faxQueueReport':require(_0xa7bf('0x1b')),'whatsappQueueReport':require('../../rpc/whatsappQueueReport'),'chatInteraction':require('../../rpc/chatInteraction'),'faxInteraction':require('../../rpc/faxInteraction'),'mailInteraction':require('../../rpc/mailInteraction'),'openchannelInteraction':require(_0xa7bf('0x1c')),'smsInteraction':require('../../rpc/smsInteraction'),'whatsappInteraction':require(_0xa7bf('0x1d'))};var queue=schema({'name':{'type':_0xa7bf('0x1e'),'required':!![],'message':'name\x20is\x20a\x20string\x20and\x20it\x20is\x20required'},'timeout':{'type':_0xa7bf('0x1f'),'message':'timeout\x20is\x20a\x20number'}});function startRouting(_0x1deb90,_0x101012,_0x35bf40,_0x55776f){if(_0x1deb90&&_0x101012&&_0x35bf40){_0x101012['waiting']+=0x1;var _0x3ff6ef={'uniqueid':_0x1deb90,'from':_0x35bf40[_0xa7bf('0x20')]?_0x35bf40[_0xa7bf('0x20')][_0xa7bf('0xd')]:'','joinAt':moment()[_0xa7bf('0x11')](_0xa7bf('0x21')),'ContactId':_0x35bf40['contact']?_0x35bf40['contact']['id']:null,'ListId':_0x35bf40[_0xa7bf('0x22')]?_0x35bf40['list']['id']:null};_0x3ff6ef[_[_0xa7bf('0x23')](_0x35bf40[_0xa7bf('0x24')])+'QueueId']=_0x101012['id']||null;_0x3ff6ef[_['upperFirst'](_0x35bf40[_0xa7bf('0x24')])+_0xa7bf('0x25')]=_0x35bf40[_0xa7bf('0x26')]?_0x35bf40[_0xa7bf('0x26')]['id']:null;_0x3ff6ef[_[_0xa7bf('0x23')](_0x35bf40[_0xa7bf('0x24')])+_0xa7bf('0x27')]=_0x35bf40[_0xa7bf('0x28')]?_0x35bf40[_0xa7bf('0x28')]['id']:null;_0x3ff6ef[_[_0xa7bf('0x23')](_0x35bf40[_0xa7bf('0x24')])+(_0x35bf40['channel']!=='chat'?_0xa7bf('0x29'):_0xa7bf('0x2a'))]=_0x35bf40['account']?_0x35bf40[_0xa7bf('0x10')]['id']:null;logger[_0xa7bf('0x2b')](util[_0xa7bf('0x11')](_0xa7bf('0x2c'),_0x1deb90,_0x101012['name']));emitChannelQueueInteraction(_0x35bf40[_0xa7bf('0x24')],_0x101012[_0xa7bf('0x2d')],_0x3ff6ef,_0x35bf40[_0xa7bf('0x10')][_0xa7bf('0x2d')],_0x35bf40[_0xa7bf('0x28')]['id'],!![],_0x55776f,_0x101012['id']);if(rpc[_0x35bf40[_0xa7bf('0x24')]+_0xa7bf('0x2e')]){return rpc[_0x35bf40[_0xa7bf('0x24')]+_0xa7bf('0x2e')][_0xa7bf('0x2f')](_0x3ff6ef)[_0xa7bf('0x30')](function(_0x43d2c0){logger[_0xa7bf('0x31')](_0xa7bf('0x32'),_0x43d2c0);});}}}function stopRouting(_0x5116cd,_0x58241c,_0x36f8d3,_0x396614){if(_0x5116cd&&_0x58241c){_0x58241c[_0xa7bf('0x33')]-=0x1;var _0x4c006={'leaveAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'acceptAt':moment()[_0xa7bf('0x11')]('YYYY-MM-DD\x20HH:mm:ss'),'reason':_0xa7bf('0x34')};logger[_0xa7bf('0x2b')](util['format'](_0xa7bf('0x35'),_0x5116cd,_0x58241c[_0xa7bf('0x2d')]));emitChannelQueueInteraction(_0x36f8d3[_0xa7bf('0x24')],_0x58241c[_0xa7bf('0x2d')],_0x36f8d3['body'],_0x36f8d3['account'][_0xa7bf('0x2d')],_0x36f8d3[_0xa7bf('0x28')]['id'],![],_0x396614,_0x58241c['id']);return rpc[_0x36f8d3['channel']+_0xa7bf('0x36')][_0xa7bf('0x37')](_0x36f8d3['interaction']['id'],{'queueId':_0x58241c['id']})[_0xa7bf('0x38')](function(){if(rpc[_0x36f8d3[_0xa7bf('0x24')]+_0xa7bf('0x2e')]){return rpc[_0x36f8d3[_0xa7bf('0x24')]+_0xa7bf('0x2e')][_0xa7bf('0x37')](_0x5116cd,_0x4c006)[_0xa7bf('0x30')](function(_0x578c27){logger[_0xa7bf('0x31')]('update\x20queue\x20report',_0x578c27);});}});}}function endRouting(_0x5726ce,_0x4b3bf4,_0x2fc006,_0x45f1c5){if(_0x5726ce&&_0x4b3bf4){_0x4b3bf4[_0xa7bf('0x33')]-=0x1;var _0x4bbae8={'leaveAt':moment()[_0xa7bf('0x11')](_0xa7bf('0x21')),'exitAt':moment()['format'](_0xa7bf('0x21')),'reason':_0xa7bf('0x39')};logger['info'](util[_0xa7bf('0x11')](_0xa7bf('0x3a'),_0x5726ce,_0x4b3bf4['name']));emitChannelQueueInteraction(_0x2fc006[_0xa7bf('0x24')],_0x4b3bf4[_0xa7bf('0x2d')],_0x2fc006[_0xa7bf('0x20')],_0x2fc006[_0xa7bf('0x10')][_0xa7bf('0x2d')],_0x2fc006[_0xa7bf('0x28')]['id'],![],_0x45f1c5);if(rpc[_0x2fc006['channel']+_0xa7bf('0x2e')]){return rpc[_0x2fc006[_0xa7bf('0x24')]+_0xa7bf('0x2e')][_0xa7bf('0x37')](_0x5726ce,_0x4bbae8)[_0xa7bf('0x30')](function(_0x3d6b2f){logger[_0xa7bf('0x31')](_0xa7bf('0x3b'),_0x3d6b2f);});}}}function handleFindQueue(_0x537ed0){return function(_0x1b2753){return _0x1b2753[_0xa7bf('0x2d')][_0xa7bf('0x3c')]()===_0x537ed0['toLowerCase']();};}exports[_0xa7bf('0x3d')]=function(_0x1e22f4,_0x4d2b7a,_0x1430a4,_0x31709e){return function(_0x38266b){try{var _0x3eaf51={'name':_0x4d2b7a[0x0],'timeout':_0x4d2b7a[0x1]};var _0x33bc3f=queue['validate'](_0x3eaf51,{'typecast':!![]});if(_0x33bc3f[_0xa7bf('0x3e')]){logger['error']('[%s][QUEUE]\x20%s',_0x1e22f4,_[_0xa7bf('0x3f')](_0x33bc3f,_0xa7bf('0x28'))[_0xa7bf('0x40')](',\x20'));_0x38266b['stop']();}else{var _0x48ebf0=_['find'](_0x1430a4[_0x31709e[_0xa7bf('0x24')]+_0xa7bf('0x41')],handleFindQueue(_0x3eaf51[_0xa7bf('0x2d')]));if(_0x48ebf0){startRouting(_0x1e22f4,_0x48ebf0,_0x31709e,_0x1430a4);var _0x3cc686=(_0x48ebf0[_0xa7bf('0x39')]||0xa)*0x3e8;var _0xa2aa68=(_0x3eaf51[_0xa7bf('0x39')]||0x14)*0x3e8;var _0x5fdfef;if(_0x3cc686>_0xa2aa68){_0xa2aa68=_0x3cc686;}var _0x1e7490=seqqueue[_0xa7bf('0x42')](_0x3cc686);_0x1e7490['id']=_0x1e22f4;_0x1e7490[_0xa7bf('0x43')]=_0x3cc686;_0x1e7490['qtimeout']=_0xa2aa68;_0x1e7490[_0xa7bf('0x24')]=_0x31709e[_0xa7bf('0x24')];_0x1430a4[_0xa7bf('0x44')][_0x1e22f4]['on'](_0xa7bf('0x45'),function(){logger[_0xa7bf('0x2b')](_0xa7bf('0x46'),_0x1e22f4);stopRouting(_0x1e22f4,_0x48ebf0,_0x31709e,_0x1430a4);clearTimeout(_0x5fdfef);_0x5fdfef=undefined;_0x1e7490['close'](!![]);if(_0x1430a4[_0xa7bf('0x44')][_0x1e22f4]){delete _0x1430a4[_0xa7bf('0x44')][_0x1e22f4];}_0x38266b[_0xa7bf('0x47')]();});_0x1430a4[_0xa7bf('0x44')][_0x1e22f4]['on'](_0xa7bf('0x48'),function(){if(_0x1430a4[_0xa7bf('0x44')][_0x1e22f4]){delete _0x1430a4[_0xa7bf('0x44')][_0x1e22f4];}});_0x5fdfef=setTimeout(function(){logger[_0xa7bf('0x2b')](_0xa7bf('0x49'),_0x1e22f4);endRouting(_0x1e22f4,_0x48ebf0,_0x31709e,_0x1430a4);clearTimeout(_0x5fdfef);_0x1e7490['close'](!![]);_0x38266b[_0xa7bf('0x4a')]();},_0xa2aa68);switch(_0x48ebf0[_0xa7bf('0x4b')]){case _0xa7bf('0x4c'):strategy[_0xa7bf('0x4c')](_0x1e7490,_0x48ebf0,_0x1430a4,_0x31709e);break;default:strategy[_0xa7bf('0x4d')](_0x1e7490,_0x48ebf0,_0x1430a4,_0x31709e);}}else{logger[_0xa7bf('0x31')](_0xa7bf('0x4e'),_0x1e22f4,_0x3eaf51[_0xa7bf('0x2d')]);_0x38266b[_0xa7bf('0x47')]();}}}catch(_0x1a1b43){logger[_0xa7bf('0x31')](_0xa7bf('0x4f'),_0x1e22f4,_0x1a1b43[_0xa7bf('0x50')]);_0x38266b[_0xa7bf('0x47')]();}};};