Built motion from commit 43538d75.|2.5.35
[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 _0xa9fc=['catch','join','connection','address','request','remoteAddress','remotePort','log','nsp','DISCONNECTED','CONNECTED','lodash','util','bluebird','../mysqldb','./environment','Agents','name','role','agent','type','ivr','UserProfileSection','then','autoAssociation','VoiceQueues','FaxQueues','SmsQueues','OpenchannelQueues','WhatsappQueues','findAll','Telephones','Trunks','IvrCampaigns','UserProfileResource','map','resourceId','where','telephone','format','%s:queue:%s','length','user:%s','handshake','User','find','userProfileId','query','ChatInteraction','ChatInteractions','MailInteractions','FaxInteraction','FaxInteractions','SmsInteraction','SmsInteractions','WhatsappInteraction','voice:inbound','hasOwnProperty','include','push','VoiceQueue','FaxQueue','MailQueue','OpenchannelQueue','Trunk','Campaign','ChatQueues','ChatQueue','SmsQueue','WhatsappQueue','voice:outbound','getVoiceQueues','getChatQueues','getFaxQueues','getSmsQueues','getMailQueues','getOpenchannelQueues','getWhatsappQueues','all','chat','fax','sms','mail','openchannel','whatsapp','telephone:%s','trunk:%s','campaign:%s:%s','leave','removeChatInteractions','emit','user_has_chat_interactions:remove','removeMailInteractions','user_has_mail_interactions:remove','removeFaxInteractions','user_has_fax_interactions:remove','user_has_sms_interactions:remove','OpenchannelInteractions','user_has_openchannel_interactions:remove','WhatsappInteractions','removeWhatsappInteractions'];(function(_0xd51b2f,_0xe1150){var _0x416a25=function(_0x798c9e){while(--_0x798c9e){_0xd51b2f['push'](_0xd51b2f['shift']());}};_0x416a25(++_0xe1150);}(_0xa9fc,0x19f));var _0xca9f=function(_0x5cb1af,_0x4bdc8c){_0x5cb1af=_0x5cb1af-0x0;var _0x8d7940=_0xa9fc[_0x5cb1af];return _0x8d7940;};'use strict';var _=require(_0xca9f('0x0'));var util=require(_0xca9f('0x1'));var BPromise=require(_0xca9f('0x2'));var db=require(_0xca9f('0x3'))['db'];var config=require(_0xca9f('0x4'));var queryQueues={'attributes':['id','name'],'include':[{'as':_0xca9f('0x5'),'model':db['User'],'attributes':['id',_0xca9f('0x6'),_0xca9f('0x7')],'raw':!![],'where':{'role':'agent'},'required':![]}]};var queryTelephones={'raw':!![],'attributes':['id','name'],'where':{'role':'telephone'}};var queryTrunks={'raw':!![],'attributes':['id',_0xca9f('0x6')]};var queryAgents={'raw':!![],'attributes':['id','name'],'where':{'role':_0xca9f('0x8')}};var queryCampaigns={'raw':!![],'attributes':['id',_0xca9f('0x6'),_0xca9f('0x9')],'where':{'type':_0xca9f('0xa')}};function getAssociatedResources(_0x244d4d,_0xb48f1f,_0x4713f0){return db[_0xca9f('0xb')]['findOne']({'where':{'name':_0x244d4d,'userProfileId':_0xb48f1f},'raw':!![]})[_0xca9f('0xc')](function(_0x4326a5){if(_0x4326a5){if(_0x4326a5[_0xca9f('0xd')]){switch(_0x244d4d){case _0xca9f('0xe'):case'ChatQueues':case _0xca9f('0xf'):case _0xca9f('0x10'):case'MailQueues':case _0xca9f('0x11'):case _0xca9f('0x12'):return db[_0x4713f0][_0xca9f('0x13')](queryQueues);case _0xca9f('0x14'):return db[_0x4713f0][_0xca9f('0x13')](queryTelephones);case _0xca9f('0x15'):return db[_0x4713f0][_0xca9f('0x13')](queryTrunks);case _0xca9f('0x16'):return db[_0x4713f0][_0xca9f('0x13')](queryCampaigns);}}else{return db[_0xca9f('0x17')]['findAll']({'attributes':['resourceId'],'where':{'sectionId':_0x4326a5['id']},'raw':!![]})[_0xca9f('0xc')](function(_0x5ec217){if(_0x5ec217){var _0x510022={'where':{'id':_[_0xca9f('0x18')](_0x5ec217,_0xca9f('0x19'))},'raw':!![]};switch(_0x4713f0){case'User':_0x510022[_0xca9f('0x1a')]['role']=_0xca9f('0x1b');break;case'Campaign':_0x510022[_0xca9f('0x1a')][_0xca9f('0x9')]=_0xca9f('0xa');break;}return db[_0x4713f0][_0xca9f('0x13')](_0x510022);}else{return[];}});}}else{return[];}})[_0xca9f('0xc')](function(_0x59625b){return _0x59625b;});}function joinOrLeaveQueues(_0x982806,_0x4cd695,_0x14a7c3,_0x1ad141){_0x982806[_0x4cd695](util[_0xca9f('0x1c')](_0xca9f('0x1d'),_0x14a7c3,_0x1ad141[_0xca9f('0x6')]));if(_0x1ad141['Agents']){for(var _0x7246c7=0x0;_0x7246c7<_0x1ad141[_0xca9f('0x5')][_0xca9f('0x1e')];_0x7246c7+=0x1){_0x982806[_0x4cd695](util[_0xca9f('0x1c')](_0xca9f('0x1f'),_0x1ad141['Agents'][_0x7246c7]['name']));}}}function joinOrLeave(_0x200da6,_0x96b3ec,_0x162328){var _0x1066bc;if(_0x200da6[_0xca9f('0x20')]['query']['id']){db[_0xca9f('0x21')][_0xca9f('0x22')]({'attributes':['id',_0xca9f('0x6'),'role',_0xca9f('0x23')],'where':{'id':_0x200da6[_0xca9f('0x20')][_0xca9f('0x24')]['id']},'include':[{'model':db[_0xca9f('0x25')],'as':_0xca9f('0x26'),'attributes':['id'],'raw':!![]},{'model':db['MailInteraction'],'as':_0xca9f('0x27'),'attributes':['id'],'raw':!![]},{'model':db[_0xca9f('0x28')],'as':_0xca9f('0x29'),'attributes':['id'],'raw':!![]},{'model':db[_0xca9f('0x2a')],'as':_0xca9f('0x2b'),'attributes':['id'],'raw':!![]},{'model':db['OpenchannelInteraction'],'as':'OpenchannelInteractions','attributes':['id'],'raw':!![]},{'model':db[_0xca9f('0x2c')],'as':'WhatsappInteractions','attributes':['id'],'raw':!![]}]})[_0xca9f('0xc')](function(_0x16b189){if(_0x16b189){_0x1066bc=_0x16b189;_0x200da6[_0x96b3ec](util[_0xca9f('0x1c')](_0xca9f('0x1f'),_0x16b189['name']));var _0x596703=[];switch(_0x16b189[_0xca9f('0x7')]){case'admin':_0x200da6[_0x96b3ec](_0xca9f('0x2d'));_0x200da6[_0x96b3ec]('voice:outbound');if(queryQueues[_0xca9f('0x2e')](_0xca9f('0x2f'))){delete queryQueues[_0xca9f('0x2f')];}_0x596703[_0xca9f('0x30')](db[_0xca9f('0x31')][_0xca9f('0x13')](queryQueues));_0x596703['push'](db['ChatQueue'][_0xca9f('0x13')](queryQueues));_0x596703['push'](db[_0xca9f('0x32')][_0xca9f('0x13')](queryQueues));_0x596703[_0xca9f('0x30')](db['SmsQueue']['findAll'](queryQueues));_0x596703[_0xca9f('0x30')](db[_0xca9f('0x33')][_0xca9f('0x13')](queryQueues));_0x596703['push'](db[_0xca9f('0x34')][_0xca9f('0x13')](queryQueues));_0x596703['push'](db['WhatsappQueue'][_0xca9f('0x13')](queryQueues));_0x596703[_0xca9f('0x30')](db[_0xca9f('0x21')][_0xca9f('0x13')](queryTelephones));_0x596703[_0xca9f('0x30')](db[_0xca9f('0x35')][_0xca9f('0x13')](queryTrunks));_0x596703[_0xca9f('0x30')](db[_0xca9f('0x36')][_0xca9f('0x13')](queryCampaigns));_0x596703[_0xca9f('0x30')](db[_0xca9f('0x21')][_0xca9f('0x13')](queryAgents));break;case'user':_0x200da6[_0x96b3ec](_0xca9f('0x2d'));_0x200da6[_0x96b3ec]('voice:outbound');_0x596703[_0xca9f('0x30')](getAssociatedResources(_0xca9f('0xe'),_0x16b189['userProfileId'],_0xca9f('0x31')));_0x596703[_0xca9f('0x30')](getAssociatedResources(_0xca9f('0x37'),_0x16b189[_0xca9f('0x23')],_0xca9f('0x38')));_0x596703['push'](getAssociatedResources('FaxQueues',_0x16b189['userProfileId'],_0xca9f('0x32')));_0x596703[_0xca9f('0x30')](getAssociatedResources(_0xca9f('0x10'),_0x16b189['userProfileId'],_0xca9f('0x39')));_0x596703[_0xca9f('0x30')](getAssociatedResources('MailQueues',_0x16b189[_0xca9f('0x23')],_0xca9f('0x33')));_0x596703['push'](getAssociatedResources(_0xca9f('0x11'),_0x16b189[_0xca9f('0x23')],_0xca9f('0x34')));_0x596703[_0xca9f('0x30')](getAssociatedResources(_0xca9f('0x12'),_0x16b189['userProfileId'],_0xca9f('0x3a')));_0x596703[_0xca9f('0x30')](getAssociatedResources('Telephones',_0x16b189[_0xca9f('0x23')],_0xca9f('0x21')));_0x596703[_0xca9f('0x30')](getAssociatedResources(_0xca9f('0x15'),_0x16b189[_0xca9f('0x23')],_0xca9f('0x35')));_0x596703[_0xca9f('0x30')](getAssociatedResources(_0xca9f('0x16'),_0x16b189[_0xca9f('0x23')],_0xca9f('0x36')));_0x596703[_0xca9f('0x30')](db[_0xca9f('0x21')]['findAll'](queryAgents));break;case _0xca9f('0x8'):_0x200da6[_0x96b3ec](_0xca9f('0x2d'));_0x200da6[_0x96b3ec](_0xca9f('0x3b'));_0x596703[_0xca9f('0x30')](_0x16b189[_0xca9f('0x3c')]({'attributes':['id','name']}));_0x596703['push'](_0x16b189[_0xca9f('0x3d')]({'attributes':['id','name']}));_0x596703[_0xca9f('0x30')](_0x16b189[_0xca9f('0x3e')]({'attributes':['id',_0xca9f('0x6')]}));_0x596703[_0xca9f('0x30')](_0x16b189[_0xca9f('0x3f')]({'attributes':['id',_0xca9f('0x6')]}));_0x596703['push'](_0x16b189[_0xca9f('0x40')]({'attributes':['id',_0xca9f('0x6')]}));_0x596703['push'](_0x16b189[_0xca9f('0x41')]({'attributes':['id',_0xca9f('0x6')]}));_0x596703[_0xca9f('0x30')](_0x16b189[_0xca9f('0x42')]({'attributes':['id','name']}));break;}return BPromise[_0xca9f('0x43')](_0x596703);}})['then'](function(_0x405728){if(_0x405728&&_0x405728[_0xca9f('0x1e')]){for(var _0x1147bd=0x0;_0x1147bd<_0x405728[_0xca9f('0x1e')];_0x1147bd+=0x1){for(var _0x48c506=0x0;_0x48c506<_0x405728[_0x1147bd]['length'];_0x48c506+=0x1){switch(_0x1147bd){case 0x0:joinOrLeaveQueues(_0x200da6,_0x96b3ec,'voice',_0x405728[_0x1147bd][_0x48c506]);break;case 0x1:joinOrLeaveQueues(_0x200da6,_0x96b3ec,_0xca9f('0x44'),_0x405728[_0x1147bd][_0x48c506]);break;case 0x2:joinOrLeaveQueues(_0x200da6,_0x96b3ec,_0xca9f('0x45'),_0x405728[_0x1147bd][_0x48c506]);break;case 0x3:joinOrLeaveQueues(_0x200da6,_0x96b3ec,_0xca9f('0x46'),_0x405728[_0x1147bd][_0x48c506]);break;case 0x4:joinOrLeaveQueues(_0x200da6,_0x96b3ec,_0xca9f('0x47'),_0x405728[_0x1147bd][_0x48c506]);break;case 0x5:joinOrLeaveQueues(_0x200da6,_0x96b3ec,_0xca9f('0x48'),_0x405728[_0x1147bd][_0x48c506]);break;case 0x6:joinOrLeaveQueues(_0x200da6,_0x96b3ec,_0xca9f('0x49'),_0x405728[_0x1147bd][_0x48c506]);break;case 0x7:if(_0x1066bc[_0xca9f('0x7')]!==_0xca9f('0x8'))_0x200da6[_0x96b3ec](util[_0xca9f('0x1c')](_0xca9f('0x4a'),_0x405728[_0x1147bd][_0x48c506]['name']));break;case 0x8:if(_0x1066bc[_0xca9f('0x7')]!=='agent')_0x200da6[_0x96b3ec](util[_0xca9f('0x1c')](_0xca9f('0x4b'),_0x405728[_0x1147bd][_0x48c506]['name']));break;case 0x9:if(_0x1066bc[_0xca9f('0x7')]!==_0xca9f('0x8'))_0x200da6[_0x96b3ec](util[_0xca9f('0x1c')](_0xca9f('0x4c'),_0x405728[_0x1147bd][_0x48c506][_0xca9f('0x9')],_0x405728[_0x1147bd][_0x48c506][_0xca9f('0x6')]));break;case 0xa:_0x200da6[_0x96b3ec](util[_0xca9f('0x1c')](_0xca9f('0x1f'),_0x405728[_0x1147bd][_0x48c506]['name']));break;}}}}})['then'](function(){if(_0x96b3ec===_0xca9f('0x4d')){var _0x264757=_[_0xca9f('0x18')](_0x1066bc[_0xca9f('0x26')],'id');return _0x1066bc[_0xca9f('0x4e')](_0x264757)[_0xca9f('0xc')](function(){for(var _0x588d0d=0x0;_0x588d0d<_0x264757[_0xca9f('0x1e')];_0x588d0d+=0x1){_0x162328[_0xca9f('0x4f')](_0xca9f('0x50'),{'ChatInteractionId':Number(_0x264757[_0x588d0d]),'UserId':Number(_0x1066bc['id'])});}});}})['then'](function(){if(_0x96b3ec===_0xca9f('0x4d')){var _0x4772dc=_['map'](_0x1066bc['MailInteractions'],'id');return _0x1066bc[_0xca9f('0x51')](_0x4772dc)[_0xca9f('0xc')](function(){for(var _0x43e543=0x0;_0x43e543<_0x4772dc[_0xca9f('0x1e')];_0x43e543+=0x1){_0x162328[_0xca9f('0x4f')](_0xca9f('0x52'),{'MailInteractionId':Number(_0x4772dc[_0x43e543]),'UserId':Number(_0x1066bc['id'])});}});}})[_0xca9f('0xc')](function(){if(_0x96b3ec===_0xca9f('0x4d')){var _0x2e98d8=_[_0xca9f('0x18')](_0x1066bc[_0xca9f('0x29')],'id');return _0x1066bc[_0xca9f('0x53')](_0x2e98d8)[_0xca9f('0xc')](function(){for(var _0xdf486f=0x0;_0xdf486f<_0x2e98d8[_0xca9f('0x1e')];_0xdf486f+=0x1){_0x162328[_0xca9f('0x4f')](_0xca9f('0x54'),{'FaxInteractionId':Number(_0x2e98d8[_0xdf486f]),'UserId':Number(_0x1066bc['id'])});}});}})[_0xca9f('0xc')](function(){if(_0x96b3ec==='leave'){var _0x3ad946=_[_0xca9f('0x18')](_0x1066bc['SmsInteractions'],'id');return _0x1066bc['removeSmsInteractions'](_0x3ad946)[_0xca9f('0xc')](function(){for(var _0x36512d=0x0;_0x36512d<_0x3ad946[_0xca9f('0x1e')];_0x36512d+=0x1){_0x162328[_0xca9f('0x4f')](_0xca9f('0x55'),{'SmsInteractionId':Number(_0x3ad946[_0x36512d]),'UserId':Number(_0x1066bc['id'])});}});}})[_0xca9f('0xc')](function(){if(_0x96b3ec===_0xca9f('0x4d')){var _0x430789=_[_0xca9f('0x18')](_0x1066bc[_0xca9f('0x56')],'id');return _0x1066bc['removeOpenchannelInteractions'](_0x430789)[_0xca9f('0xc')](function(){for(var _0x5e17ee=0x0;_0x5e17ee<_0x430789['length'];_0x5e17ee+=0x1){_0x162328['emit'](_0xca9f('0x57'),{'OpenchannelInteractionId':Number(_0x430789[_0x5e17ee]),'UserId':Number(_0x1066bc['id'])});}});}})[_0xca9f('0xc')](function(){if(_0x96b3ec===_0xca9f('0x4d')){var _0x4d700c=_['map'](_0x1066bc[_0xca9f('0x58')],'id');return _0x1066bc[_0xca9f('0x59')](_0x4d700c)['then'](function(){for(var _0x5b07c4=0x0;_0x5b07c4<_0x4d700c[_0xca9f('0x1e')];_0x5b07c4+=0x1){_0x162328[_0xca9f('0x4f')]('user_has_whatsapp_interactions:remove',{'WhatsappInteractionId':Number(_0x4d700c[_0x5b07c4]),'UserId':Number(_0x1066bc['id'])});}});}})[_0xca9f('0x5a')](function(_0x159611){_0x200da6['log'](_0x159611);});}}function onDisconnect(_0x517eda,_0x367a01){joinOrLeave(_0x517eda,_0xca9f('0x4d'),_0x367a01);}function onConnect(_0x5a192e){joinOrLeave(_0x5a192e,_0xca9f('0x5b'));}exports['default']=function(_0x56e8e8){_0x56e8e8['on'](_0xca9f('0x5c'),function(_0x5ee08b){_0x5ee08b['setMaxListeners'](0x0);_0x5ee08b[_0xca9f('0x5d')]=_0x5ee08b[_0xca9f('0x5e')][_0xca9f('0x5c')][_0xca9f('0x5f')]+':'+_0x5ee08b['request'][_0xca9f('0x5c')][_0xca9f('0x60')];_0x5ee08b['connectedAt']=new Date();_0x5ee08b[_0xca9f('0x61')]=function(_0x1ef437){console[_0xca9f('0x61')]('SocketIO\x20'+_0x5ee08b[_0xca9f('0x62')][_0xca9f('0x6')]+'['+_0x5ee08b[_0xca9f('0x5d')]+']',_0x1ef437);};_0x5ee08b['on']('disconnect',function(){onDisconnect(_0x5ee08b,_0x56e8e8);_0x5ee08b[_0xca9f('0x61')](_0xca9f('0x63'));});onConnect(_0x5ee08b);_0x5ee08b[_0xca9f('0x61')](_0xca9f('0x64'));});};