d30f6803b906b241066765e912ec46c018c93168
[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 _0x44bb=['name','QueueReport','create','catch','error','create\x20queue\x20report','accepted','[%s][STOP:QUEUE][NAME:%s]','update','update\x20queue\x20report','timeout','[%s][END:QUEUE][NAME:%s]','toLowerCase','validate','length','[%s][QUEUE]\x20%s','map','join','find','createQueue','atimeout','qtimeout','rules','end','[%s][QUEUE]\x20ROUTING\x20STOPPED','close','complete','[%s][QUEUE]\x20APPLICATION\x20END','strategy','rrmemory','beepall','stop','util','moment','ioredis','../../../../config/environment','../../../../config/logger','routing','./strategy','defaults','socket.io-emitter','redis','queue_id','account','queue','from','clone','capitalize','InteractionId','format','%sQueuesWaitingInteractions','pick','%s_queue_waiting_interaction:%s','save','remove','../../rpc/mailQueueReport','../../rpc/chatQueueReport','../../rpc/faxQueueReport','../../rpc/whatsappQueueReport','../../rpc/chatInteraction','../../rpc/mailInteraction','../../rpc/openchannelInteraction','string','name\x20is\x20a\x20string\x20and\x20it\x20is\x20required','number','timeout\x20is\x20a\x20number','waiting','body','YYYY-MM-DD\x20HH:mm:ss','contact','list','upperFirst','channel','QueueId','interaction','message','AccountId','WebsiteId','info','[%s][START:QUEUE][NAME:%s]'];(function(_0x5f3f88,_0x562baf){var _0xd08119=function(_0x42dbf7){while(--_0x42dbf7){_0x5f3f88['push'](_0x5f3f88['shift']());}};_0xd08119(++_0x562baf);}(_0x44bb,0x110));var _0xb44b=function(_0x1a55d9,_0x1c130b){_0x1a55d9=_0x1a55d9-0x0;var _0x3bdb9c=_0x44bb[_0x1a55d9];return _0x3bdb9c;};'use strict';var schema=require('validate');var _=require('lodash');var util=require(_0xb44b('0x0'));var seqqueue=require('seq-queue');var moment=require(_0xb44b('0x1'));var Redis=require(_0xb44b('0x2'));var config=require(_0xb44b('0x3'));var logger=require(_0xb44b('0x4'))(_0xb44b('0x5'));var strategy=require(_0xb44b('0x6'));config['redis']=_[_0xb44b('0x7')](config['redis'],{'host':'localhost','port':0x18eb});var io=require(_0xb44b('0x8'))(new Redis(config[_0xb44b('0x9')]));var attributes=['id',_0xb44b('0xa'),_0xb44b('0xb'),_0xb44b('0xc'),_0xb44b('0xd'),'joinAt'];function emit(_0x41f46e,_0x8d7907,_0x247331){io['to'](_0x41f46e)['emit'](_0x8d7907,_0x247331);}function emitChannelQueueInteraction(_0x45e2ea,_0x21c7a5,_0x359905,_0x14b39e,_0xbba1d1,_0x43d1d2,_0x416259,_0x149ed7){var _0x46b36f=_[_0xb44b('0xe')](_0x359905);_0x46b36f[_0xb44b('0xa')]=_0x149ed7;_0x46b36f[_0xb44b('0xc')]=_0x21c7a5;_0x46b36f[_0xb44b('0xb')]=_0x14b39e;_0x46b36f['id']=_0xbba1d1;if(_0x43d1d2){attributes['push'](_[_0xb44b('0xf')](_0x45e2ea)+_0xb44b('0x10'));_0x416259[util[_0xb44b('0x11')](_0xb44b('0x12'),_0x45e2ea)][_0xbba1d1]=_[_0xb44b('0x13')](_0x46b36f,attributes);}else{delete _0x416259[util[_0xb44b('0x11')](_0xb44b('0x12'),_0x45e2ea)][_0xbba1d1];}emit(util[_0xb44b('0x11')]('%s:queue:%s',_0x45e2ea,_0x21c7a5),util[_0xb44b('0x11')](_0xb44b('0x14'),_0x45e2ea,_0x43d1d2?_0xb44b('0x15'):_0xb44b('0x16')),_[_0xb44b('0x13')](_0x46b36f,attributes));}var rpc={'openchannelQueueReport':require('../../rpc/openchannelQueueReport'),'mailQueueReport':require(_0xb44b('0x17')),'chatQueueReport':require(_0xb44b('0x18')),'smsQueueReport':require('../../rpc/smsQueueReport'),'faxQueueReport':require(_0xb44b('0x19')),'whatsappQueueReport':require(_0xb44b('0x1a')),'chatInteraction':require(_0xb44b('0x1b')),'faxInteraction':require('../../rpc/faxInteraction'),'mailInteraction':require(_0xb44b('0x1c')),'openchannelInteraction':require(_0xb44b('0x1d')),'smsInteraction':require('../../rpc/smsInteraction'),'whatsappInteraction':require('../../rpc/whatsappInteraction')};var queue=schema({'name':{'type':_0xb44b('0x1e'),'required':!![],'message':_0xb44b('0x1f')},'timeout':{'type':_0xb44b('0x20'),'message':_0xb44b('0x21')}});function startRouting(_0x4e3c1a,_0x3b975f,_0x5f086a,_0x2db4fe){if(_0x4e3c1a&&_0x3b975f&&_0x5f086a){_0x3b975f[_0xb44b('0x22')]+=0x1;var _0x55f1b4={'uniqueid':_0x4e3c1a,'from':_0x5f086a[_0xb44b('0x23')]?_0x5f086a[_0xb44b('0x23')][_0xb44b('0xd')]:'','joinAt':moment()[_0xb44b('0x11')](_0xb44b('0x24')),'ContactId':_0x5f086a[_0xb44b('0x25')]?_0x5f086a[_0xb44b('0x25')]['id']:null,'ListId':_0x5f086a[_0xb44b('0x26')]?_0x5f086a[_0xb44b('0x26')]['id']:null};_0x55f1b4[_[_0xb44b('0x27')](_0x5f086a[_0xb44b('0x28')])+_0xb44b('0x29')]=_0x3b975f['id']||null;_0x55f1b4[_['upperFirst'](_0x5f086a[_0xb44b('0x28')])+_0xb44b('0x10')]=_0x5f086a[_0xb44b('0x2a')]?_0x5f086a['interaction']['id']:null;_0x55f1b4[_['upperFirst'](_0x5f086a[_0xb44b('0x28')])+'MessageId']=_0x5f086a[_0xb44b('0x2b')]?_0x5f086a[_0xb44b('0x2b')]['id']:null;_0x55f1b4[_['upperFirst'](_0x5f086a['channel'])+(_0x5f086a[_0xb44b('0x28')]!=='chat'?_0xb44b('0x2c'):_0xb44b('0x2d'))]=_0x5f086a[_0xb44b('0xb')]?_0x5f086a[_0xb44b('0xb')]['id']:null;logger[_0xb44b('0x2e')](util[_0xb44b('0x11')](_0xb44b('0x2f'),_0x4e3c1a,_0x3b975f[_0xb44b('0x30')]));emitChannelQueueInteraction(_0x5f086a[_0xb44b('0x28')],_0x3b975f[_0xb44b('0x30')],_0x55f1b4,_0x5f086a['account']['name'],_0x5f086a[_0xb44b('0x2b')]['id'],!![],_0x2db4fe,_0x3b975f['id']);if(rpc[_0x5f086a[_0xb44b('0x28')]+_0xb44b('0x31')]){return rpc[_0x5f086a[_0xb44b('0x28')]+_0xb44b('0x31')][_0xb44b('0x32')](_0x55f1b4)[_0xb44b('0x33')](function(_0x4fc309){logger[_0xb44b('0x34')](_0xb44b('0x35'),_0x4fc309);});}}}function stopRouting(_0x49ca28,_0x814e72,_0x11ef91,_0xfd9a53){if(_0x49ca28&&_0x814e72){_0x814e72[_0xb44b('0x22')]-=0x1;var _0xf5e81a={'leaveAt':moment()[_0xb44b('0x11')](_0xb44b('0x24')),'acceptAt':moment()['format'](_0xb44b('0x24')),'reason':_0xb44b('0x36')};logger[_0xb44b('0x2e')](util['format'](_0xb44b('0x37'),_0x49ca28,_0x814e72[_0xb44b('0x30')]));emitChannelQueueInteraction(_0x11ef91[_0xb44b('0x28')],_0x814e72[_0xb44b('0x30')],_0x11ef91['body'],_0x11ef91[_0xb44b('0xb')]['name'],_0x11ef91[_0xb44b('0x2b')]['id'],![],_0xfd9a53,_0x814e72['id']);return rpc[_0x11ef91[_0xb44b('0x28')]+'Interaction'][_0xb44b('0x38')](_0x11ef91[_0xb44b('0x2a')]['id'],{'queueId':_0x814e72['id']})['then'](function(){if(rpc[_0x11ef91[_0xb44b('0x28')]+_0xb44b('0x31')]){return rpc[_0x11ef91[_0xb44b('0x28')]+_0xb44b('0x31')][_0xb44b('0x38')](_0x49ca28,_0xf5e81a)[_0xb44b('0x33')](function(_0x20e429){logger[_0xb44b('0x34')](_0xb44b('0x39'),_0x20e429);});}});}}function endRouting(_0x43bba9,_0x337bce,_0x42f0f0,_0xcf97f6){if(_0x43bba9&&_0x337bce){_0x337bce[_0xb44b('0x22')]-=0x1;var _0x34e40f={'leaveAt':moment()['format'](_0xb44b('0x24')),'exitAt':moment()['format'](_0xb44b('0x24')),'reason':_0xb44b('0x3a')};logger['info'](util[_0xb44b('0x11')](_0xb44b('0x3b'),_0x43bba9,_0x337bce[_0xb44b('0x30')]));emitChannelQueueInteraction(_0x42f0f0['channel'],_0x337bce[_0xb44b('0x30')],_0x42f0f0[_0xb44b('0x23')],_0x42f0f0[_0xb44b('0xb')][_0xb44b('0x30')],_0x42f0f0[_0xb44b('0x2b')]['id'],![],_0xcf97f6);if(rpc[_0x42f0f0[_0xb44b('0x28')]+_0xb44b('0x31')]){return rpc[_0x42f0f0['channel']+_0xb44b('0x31')]['update'](_0x43bba9,_0x34e40f)['catch'](function(_0x2be89b){logger[_0xb44b('0x34')](_0xb44b('0x39'),_0x2be89b);});}}}function handleFindQueue(_0x44d4c0){return function(_0x333e70){return _0x333e70[_0xb44b('0x30')][_0xb44b('0x3c')]()===_0x44d4c0[_0xb44b('0x3c')]();};}exports['consequence']=function(_0x5b4a05,_0x38d86f,_0x1a8457,_0x1b4a1e){return function(_0xf83b33){try{var _0x61fff3={'name':_0x38d86f[0x0],'timeout':_0x38d86f[0x1]};var _0x2dd746=queue[_0xb44b('0x3d')](_0x61fff3,{'typecast':!![]});if(_0x2dd746[_0xb44b('0x3e')]){logger[_0xb44b('0x34')](_0xb44b('0x3f'),_0x5b4a05,_[_0xb44b('0x40')](_0x2dd746,_0xb44b('0x2b'))[_0xb44b('0x41')](',\x20'));_0xf83b33['stop']();}else{var _0x597a42=_[_0xb44b('0x42')](_0x1a8457[_0x1b4a1e[_0xb44b('0x28')]+'Queues'],handleFindQueue(_0x61fff3[_0xb44b('0x30')]));if(_0x597a42){startRouting(_0x5b4a05,_0x597a42,_0x1b4a1e,_0x1a8457);var _0x3f5ce1=(_0x597a42[_0xb44b('0x3a')]||0xa)*0x3e8;var _0x2009b4=(_0x61fff3[_0xb44b('0x3a')]||0x14)*0x3e8;var _0xcc6c91;if(_0x3f5ce1>_0x2009b4){_0x2009b4=_0x3f5ce1;}var _0x5226e0=seqqueue[_0xb44b('0x43')](_0x3f5ce1);_0x5226e0['id']=_0x5b4a05;_0x5226e0[_0xb44b('0x44')]=_0x3f5ce1;_0x5226e0[_0xb44b('0x45')]=_0x2009b4;_0x5226e0[_0xb44b('0x28')]=_0x1b4a1e[_0xb44b('0x28')];_0x1a8457[_0xb44b('0x46')][_0x5b4a05]['on'](_0xb44b('0x47'),function(){logger[_0xb44b('0x2e')](_0xb44b('0x48'),_0x5b4a05);stopRouting(_0x5b4a05,_0x597a42,_0x1b4a1e,_0x1a8457);clearTimeout(_0xcc6c91);_0xcc6c91=undefined;_0x5226e0[_0xb44b('0x49')](!![]);if(_0x1a8457[_0xb44b('0x46')][_0x5b4a05]){delete _0x1a8457[_0xb44b('0x46')][_0x5b4a05];}_0xf83b33['stop']();});_0x1a8457['rules'][_0x5b4a05]['on'](_0xb44b('0x4a'),function(){if(_0x1a8457[_0xb44b('0x46')][_0x5b4a05]){delete _0x1a8457[_0xb44b('0x46')][_0x5b4a05];}});_0xcc6c91=setTimeout(function(){logger['info'](_0xb44b('0x4b'),_0x5b4a05);endRouting(_0x5b4a05,_0x597a42,_0x1b4a1e,_0x1a8457);clearTimeout(_0xcc6c91);_0x5226e0[_0xb44b('0x49')](!![]);_0xf83b33['next']();},_0x2009b4);switch(_0x597a42[_0xb44b('0x4c')]){case _0xb44b('0x4d'):strategy[_0xb44b('0x4d')](_0x5226e0,_0x597a42,_0x1a8457,_0x1b4a1e);break;default:strategy[_0xb44b('0x4e')](_0x5226e0,_0x597a42,_0x1a8457,_0x1b4a1e);}}else{logger[_0xb44b('0x34')]('[%s][QUEUE]\x20queue\x20not\x20found\x20%s',_0x5b4a05,_0x61fff3['name']);_0xf83b33[_0xb44b('0x4f')]();}}}catch(_0x664dac){logger[_0xb44b('0x34')](_0xb44b('0x3f'),_0x5b4a05,_0x664dac['stack']);_0xf83b33[_0xb44b('0x4f')]();}};};