598a4b5aa71225c802c1eecf59743a326a73b594
[motion2.git] / server / config / socketio.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=['include','push','FaxQueue','MailQueue','VoiceQueue','ChatQueue','OpenchannelQueue','getVoiceQueues','getChatQueues','getFaxQueues','getSmsQueues','getMailQueues','getOpenchannelQueues','all','voice','chat','fax','sms','mail','openchannel','telephone:%s','trunk:%s','leave','map','removeChatInteractions','emit','user_has_chat_interactions:remove','removeMailInteractions','user_has_mail_interactions:remove','removeFaxInteractions','user_has_fax_interactions:remove','SmsInteractions','removeSmsInteractions','removeOpenchannelInteractions','user_has_openchannel_interactions:remove','log','join','default','connection','setMaxListeners','address','request','remoteAddress','remotePort','SocketIO\x20','disconnect','CONNECTED','lodash','util','bluebird','./environment','name','agent','telephone','type','ivr','UserProfileSection','autoAssociation','VoiceQueues','ChatQueues','FaxQueues','SmsQueues','OpenchannelQueues','findAll','Trunks','IvrCampaigns','UserProfileResource','resourceId','then','User','where','Campaign','%s:queue:%s','Agents','length','format','handshake','find','role','userProfileId','ChatInteraction','ChatInteractions','MailInteraction','MailInteractions','FaxInteraction','FaxInteractions','OpenchannelInteraction','OpenchannelInteractions','user:%s','admin','voice:inbound','voice:outbound','hasOwnProperty'];(function(_0x24a10d,_0x4dd37c){var _0x87f80=function(_0x2938f9){while(--_0x2938f9){_0x24a10d['push'](_0x24a10d['shift']());}};_0x87f80(++_0x4dd37c);}(_0x7bfa,0xe9));var _0xa7bf=function(_0x5ed54b,_0x3a2827){_0x5ed54b=_0x5ed54b-0x0;var _0x4b58c7=_0x7bfa[_0x5ed54b];return _0x4b58c7;};'use strict';var _=require(_0xa7bf('0x0'));var util=require(_0xa7bf('0x1'));var BPromise=require(_0xa7bf('0x2'));var db=require('../mysqldb')['db'];var config=require(_0xa7bf('0x3'));var queryQueues={'attributes':['id',_0xa7bf('0x4')],'include':[{'as':'Agents','model':db['User'],'attributes':['id',_0xa7bf('0x4'),'role'],'raw':!![],'where':{'role':_0xa7bf('0x5')},'required':![]}]};var queryTelephones={'raw':!![],'attributes':['id',_0xa7bf('0x4')],'where':{'role':_0xa7bf('0x6')}};var queryTrunks={'raw':!![],'attributes':['id',_0xa7bf('0x4')]};var queryAgents={'raw':!![],'attributes':['id',_0xa7bf('0x4')],'where':{'role':_0xa7bf('0x5')}};var queryCampaigns={'raw':!![],'attributes':['id','name',_0xa7bf('0x7')],'where':{'type':_0xa7bf('0x8')}};function getAssociatedResources(_0xd90bed,_0x321bd8,_0x3ea2aa){return db[_0xa7bf('0x9')]['findOne']({'where':{'name':_0xd90bed,'userProfileId':_0x321bd8},'raw':!![]})['then'](function(_0x3b7b2c){if(_0x3b7b2c){if(_0x3b7b2c[_0xa7bf('0xa')]){switch(_0xd90bed){case _0xa7bf('0xb'):case _0xa7bf('0xc'):case _0xa7bf('0xd'):case _0xa7bf('0xe'):case'MailQueues':case _0xa7bf('0xf'):return db[_0x3ea2aa][_0xa7bf('0x10')](queryQueues);case'Telephones':return db[_0x3ea2aa][_0xa7bf('0x10')](queryTelephones);case _0xa7bf('0x11'):return db[_0x3ea2aa][_0xa7bf('0x10')](queryTrunks);case _0xa7bf('0x12'):return db[_0x3ea2aa][_0xa7bf('0x10')](queryCampaigns);}}else{return db[_0xa7bf('0x13')]['findAll']({'attributes':[_0xa7bf('0x14')],'where':{'sectionId':_0x3b7b2c['id']},'raw':!![]})[_0xa7bf('0x15')](function(_0x4f826a){if(_0x4f826a){var _0x2dc82a={'where':{'id':_['map'](_0x4f826a,_0xa7bf('0x14'))},'raw':!![]};switch(_0x3ea2aa){case _0xa7bf('0x16'):_0x2dc82a[_0xa7bf('0x17')]['role']='telephone';break;case _0xa7bf('0x18'):_0x2dc82a['where'][_0xa7bf('0x7')]='ivr';break;}return db[_0x3ea2aa][_0xa7bf('0x10')](_0x2dc82a);}else{return[];}});}}else{return[];}})['then'](function(_0x363c23){return _0x363c23;});}function joinOrLeaveQueues(_0x20d674,_0x10cf88,_0x56c2e0,_0x5ad2b7){_0x20d674[_0x10cf88](util['format'](_0xa7bf('0x19'),_0x56c2e0,_0x5ad2b7[_0xa7bf('0x4')]));if(_0x5ad2b7[_0xa7bf('0x1a')]){for(var _0x235f44=0x0;_0x235f44<_0x5ad2b7['Agents'][_0xa7bf('0x1b')];_0x235f44+=0x1){_0x20d674[_0x10cf88](util[_0xa7bf('0x1c')]('user:%s',_0x5ad2b7[_0xa7bf('0x1a')][_0x235f44][_0xa7bf('0x4')]));}}}function joinOrLeave(_0x4b3329,_0x5e2454,_0x2116f9){var _0x322f91;if(_0x4b3329[_0xa7bf('0x1d')]['query']['id']){db['User'][_0xa7bf('0x1e')]({'attributes':['id',_0xa7bf('0x4'),_0xa7bf('0x1f'),_0xa7bf('0x20')],'where':{'id':_0x4b3329[_0xa7bf('0x1d')]['query']['id']},'include':[{'model':db[_0xa7bf('0x21')],'as':_0xa7bf('0x22'),'attributes':['id'],'raw':!![]},{'model':db[_0xa7bf('0x23')],'as':_0xa7bf('0x24'),'attributes':['id'],'raw':!![]},{'model':db[_0xa7bf('0x25')],'as':_0xa7bf('0x26'),'attributes':['id'],'raw':!![]},{'model':db['SmsInteraction'],'as':'SmsInteractions','attributes':['id'],'raw':!![]},{'model':db[_0xa7bf('0x27')],'as':_0xa7bf('0x28'),'attributes':['id'],'raw':!![]}]})[_0xa7bf('0x15')](function(_0x2ee03b){if(_0x2ee03b){_0x322f91=_0x2ee03b;_0x4b3329[_0x5e2454](util[_0xa7bf('0x1c')](_0xa7bf('0x29'),_0x2ee03b['name']));var _0x14f15e=[];switch(_0x2ee03b[_0xa7bf('0x1f')]){case _0xa7bf('0x2a'):_0x4b3329[_0x5e2454](_0xa7bf('0x2b'));_0x4b3329[_0x5e2454](_0xa7bf('0x2c'));if(queryQueues[_0xa7bf('0x2d')](_0xa7bf('0x2e'))){delete queryQueues[_0xa7bf('0x2e')];}_0x14f15e[_0xa7bf('0x2f')](db['VoiceQueue'][_0xa7bf('0x10')](queryQueues));_0x14f15e['push'](db['ChatQueue']['findAll'](queryQueues));_0x14f15e['push'](db[_0xa7bf('0x30')]['findAll'](queryQueues));_0x14f15e[_0xa7bf('0x2f')](db['SmsQueue'][_0xa7bf('0x10')](queryQueues));_0x14f15e[_0xa7bf('0x2f')](db[_0xa7bf('0x31')][_0xa7bf('0x10')](queryQueues));_0x14f15e['push'](db['OpenchannelQueue'][_0xa7bf('0x10')](queryQueues));_0x14f15e[_0xa7bf('0x2f')](db[_0xa7bf('0x16')][_0xa7bf('0x10')](queryTelephones));_0x14f15e[_0xa7bf('0x2f')](db['Trunk'][_0xa7bf('0x10')](queryTrunks));_0x14f15e[_0xa7bf('0x2f')](db[_0xa7bf('0x18')][_0xa7bf('0x10')](queryCampaigns));_0x14f15e[_0xa7bf('0x2f')](db[_0xa7bf('0x16')][_0xa7bf('0x10')](queryAgents));break;case'user':_0x4b3329[_0x5e2454]('voice:inbound');_0x4b3329[_0x5e2454]('voice:outbound');_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources(_0xa7bf('0xb'),_0x2ee03b[_0xa7bf('0x20')],_0xa7bf('0x32')));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources(_0xa7bf('0xc'),_0x2ee03b[_0xa7bf('0x20')],_0xa7bf('0x33')));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources(_0xa7bf('0xd'),_0x2ee03b[_0xa7bf('0x20')],_0xa7bf('0x30')));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources(_0xa7bf('0xe'),_0x2ee03b[_0xa7bf('0x20')],'SmsQueue'));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources('MailQueues',_0x2ee03b[_0xa7bf('0x20')],'MailQueue'));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources(_0xa7bf('0xf'),_0x2ee03b['userProfileId'],_0xa7bf('0x34')));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources('Telephones',_0x2ee03b[_0xa7bf('0x20')],_0xa7bf('0x16')));_0x14f15e['push'](getAssociatedResources('Trunks',_0x2ee03b[_0xa7bf('0x20')],'Trunk'));_0x14f15e[_0xa7bf('0x2f')](getAssociatedResources(_0xa7bf('0x12'),_0x2ee03b['userProfileId'],'Campaign'));_0x14f15e[_0xa7bf('0x2f')](db[_0xa7bf('0x16')][_0xa7bf('0x10')](queryAgents));break;case _0xa7bf('0x5'):_0x4b3329[_0x5e2454](_0xa7bf('0x2b'));_0x4b3329[_0x5e2454](_0xa7bf('0x2c'));_0x14f15e[_0xa7bf('0x2f')](_0x2ee03b[_0xa7bf('0x35')]({'attributes':['id','name']}));_0x14f15e[_0xa7bf('0x2f')](_0x2ee03b[_0xa7bf('0x36')]({'attributes':['id','name']}));_0x14f15e[_0xa7bf('0x2f')](_0x2ee03b[_0xa7bf('0x37')]({'attributes':['id',_0xa7bf('0x4')]}));_0x14f15e[_0xa7bf('0x2f')](_0x2ee03b[_0xa7bf('0x38')]({'attributes':['id',_0xa7bf('0x4')]}));_0x14f15e[_0xa7bf('0x2f')](_0x2ee03b[_0xa7bf('0x39')]({'attributes':['id',_0xa7bf('0x4')]}));_0x14f15e[_0xa7bf('0x2f')](_0x2ee03b[_0xa7bf('0x3a')]({'attributes':['id',_0xa7bf('0x4')]}));break;}return BPromise[_0xa7bf('0x3b')](_0x14f15e);}})[_0xa7bf('0x15')](function(_0x360634){if(_0x360634&&_0x360634[_0xa7bf('0x1b')]){for(var _0x9b0ca8=0x0;_0x9b0ca8<_0x360634[_0xa7bf('0x1b')];_0x9b0ca8+=0x1){for(var _0x4fcd3a=0x0;_0x4fcd3a<_0x360634[_0x9b0ca8][_0xa7bf('0x1b')];_0x4fcd3a+=0x1){switch(_0x9b0ca8){case 0x0:joinOrLeaveQueues(_0x4b3329,_0x5e2454,_0xa7bf('0x3c'),_0x360634[_0x9b0ca8][_0x4fcd3a]);break;case 0x1:joinOrLeaveQueues(_0x4b3329,_0x5e2454,_0xa7bf('0x3d'),_0x360634[_0x9b0ca8][_0x4fcd3a]);break;case 0x2:joinOrLeaveQueues(_0x4b3329,_0x5e2454,_0xa7bf('0x3e'),_0x360634[_0x9b0ca8][_0x4fcd3a]);break;case 0x3:joinOrLeaveQueues(_0x4b3329,_0x5e2454,_0xa7bf('0x3f'),_0x360634[_0x9b0ca8][_0x4fcd3a]);break;case 0x4:joinOrLeaveQueues(_0x4b3329,_0x5e2454,_0xa7bf('0x40'),_0x360634[_0x9b0ca8][_0x4fcd3a]);break;case 0x5:joinOrLeaveQueues(_0x4b3329,_0x5e2454,_0xa7bf('0x41'),_0x360634[_0x9b0ca8][_0x4fcd3a]);break;case 0x6:_0x4b3329[_0x5e2454](util[_0xa7bf('0x1c')](_0xa7bf('0x42'),_0x360634[_0x9b0ca8][_0x4fcd3a]['name']));break;case 0x7:_0x4b3329[_0x5e2454](util[_0xa7bf('0x1c')](_0xa7bf('0x43'),_0x360634[_0x9b0ca8][_0x4fcd3a][_0xa7bf('0x4')]));break;case 0x8:_0x4b3329[_0x5e2454](util[_0xa7bf('0x1c')]('campaign:%s:%s',_0x360634[_0x9b0ca8][_0x4fcd3a][_0xa7bf('0x7')],_0x360634[_0x9b0ca8][_0x4fcd3a][_0xa7bf('0x4')]));break;case 0x9:_0x4b3329[_0x5e2454](util[_0xa7bf('0x1c')](_0xa7bf('0x29'),_0x360634[_0x9b0ca8][_0x4fcd3a][_0xa7bf('0x4')]));break;}}}}})['then'](function(){if(_0x5e2454===_0xa7bf('0x44')){var _0x73d31b=_[_0xa7bf('0x45')](_0x322f91[_0xa7bf('0x22')],'id');return _0x322f91[_0xa7bf('0x46')](_0x73d31b)['then'](function(){for(var _0x121ef3=0x0;_0x121ef3<_0x73d31b[_0xa7bf('0x1b')];_0x121ef3+=0x1){_0x2116f9[_0xa7bf('0x47')](_0xa7bf('0x48'),{'ChatInteractionId':Number(_0x73d31b[_0x121ef3]),'UserId':Number(_0x322f91['id'])});}});}})[_0xa7bf('0x15')](function(){if(_0x5e2454===_0xa7bf('0x44')){var _0x4685f4=_['map'](_0x322f91[_0xa7bf('0x24')],'id');return _0x322f91[_0xa7bf('0x49')](_0x4685f4)[_0xa7bf('0x15')](function(){for(var _0x174668=0x0;_0x174668<_0x4685f4[_0xa7bf('0x1b')];_0x174668+=0x1){_0x2116f9[_0xa7bf('0x47')](_0xa7bf('0x4a'),{'MailInteractionId':Number(_0x4685f4[_0x174668]),'UserId':Number(_0x322f91['id'])});}});}})[_0xa7bf('0x15')](function(){if(_0x5e2454===_0xa7bf('0x44')){var _0x8f83ce=_['map'](_0x322f91['FaxInteractions'],'id');return _0x322f91[_0xa7bf('0x4b')](_0x8f83ce)[_0xa7bf('0x15')](function(){for(var _0x541e59=0x0;_0x541e59<_0x8f83ce[_0xa7bf('0x1b')];_0x541e59+=0x1){_0x2116f9['emit'](_0xa7bf('0x4c'),{'FaxInteractionId':Number(_0x8f83ce[_0x541e59]),'UserId':Number(_0x322f91['id'])});}});}})[_0xa7bf('0x15')](function(){if(_0x5e2454===_0xa7bf('0x44')){var _0x14975e=_[_0xa7bf('0x45')](_0x322f91[_0xa7bf('0x4d')],'id');return _0x322f91[_0xa7bf('0x4e')](_0x14975e)[_0xa7bf('0x15')](function(){for(var _0x51e430=0x0;_0x51e430<_0x14975e[_0xa7bf('0x1b')];_0x51e430+=0x1){_0x2116f9[_0xa7bf('0x47')]('user_has_sms_interactions:remove',{'SmsInteractionId':Number(_0x14975e[_0x51e430]),'UserId':Number(_0x322f91['id'])});}});}})['then'](function(){if(_0x5e2454===_0xa7bf('0x44')){var _0x5316c1=_[_0xa7bf('0x45')](_0x322f91[_0xa7bf('0x28')],'id');return _0x322f91[_0xa7bf('0x4f')](_0x5316c1)[_0xa7bf('0x15')](function(){for(var _0x5d4462=0x0;_0x5d4462<_0x5316c1[_0xa7bf('0x1b')];_0x5d4462+=0x1){_0x2116f9[_0xa7bf('0x47')](_0xa7bf('0x50'),{'OpenchannelInteractionId':Number(_0x5316c1[_0x5d4462]),'UserId':Number(_0x322f91['id'])});}});}})['catch'](function(_0x13a24d){_0x4b3329[_0xa7bf('0x51')](_0x13a24d);});}}function onDisconnect(_0x2438e6,_0x1ef2af){joinOrLeave(_0x2438e6,_0xa7bf('0x44'),_0x1ef2af);}function onConnect(_0x18e191){joinOrLeave(_0x18e191,_0xa7bf('0x52'));}exports[_0xa7bf('0x53')]=function(_0x1747cc){_0x1747cc['on'](_0xa7bf('0x54'),function(_0x2a73c6){_0x2a73c6[_0xa7bf('0x55')](0x0);_0x2a73c6[_0xa7bf('0x56')]=_0x2a73c6[_0xa7bf('0x57')]['connection'][_0xa7bf('0x58')]+':'+_0x2a73c6[_0xa7bf('0x57')][_0xa7bf('0x54')][_0xa7bf('0x59')];_0x2a73c6['connectedAt']=new Date();_0x2a73c6[_0xa7bf('0x51')]=function(_0x1de777){console[_0xa7bf('0x51')](_0xa7bf('0x5a')+_0x2a73c6['nsp'][_0xa7bf('0x4')]+'['+_0x2a73c6[_0xa7bf('0x56')]+']',_0x1de777);};_0x2a73c6['on'](_0xa7bf('0x5b'),function(){onDisconnect(_0x2a73c6,_0x1747cc);_0x2a73c6[_0xa7bf('0x51')]('DISCONNECTED');});onConnect(_0x2a73c6);_0x2a73c6[_0xa7bf('0x51')](_0xa7bf('0x5c'));});};