Built motion from commit 639a1a7f.|2.6.19
[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 _0x838a=['SocketIO\x20','nsp','disconnect','DISCONNECTED','bluebird','../mysqldb','./environment','name','Agents','User','role','agent','type','UserProfileSection','findOne','then','FaxQueues','MailQueues','OpenchannelQueues','Telephones','findAll','IvrCampaigns','resourceId','map','where','telephone','Campaign','ivr','format','%s:queue:%s','length','handshake','find','query','ChatInteraction','ChatInteractions','MailInteraction','MailInteractions','FaxInteraction','OpenchannelInteractions','WhatsappInteraction','user:%s','admin','voice:inbound','voice:outbound','hasOwnProperty','include','push','VoiceQueue','ChatQueue','SmsQueue','MailQueue','WhatsappQueue','Trunk','user','VoiceQueues','userProfileId','ChatQueues','FaxQueue','SmsQueues','OpenchannelQueue','WhatsappQueues','Trunks','getVoiceQueues','getChatQueues','getFaxQueues','getMailQueues','getOpenchannelQueues','getWhatsappQueues','all','chat','fax','sms','mail','openchannel','telephone:%s','trunk:%s','campaign:%s:%s','removeChatInteractions','emit','leave','removeMailInteractions','removeFaxInteractions','SmsInteractions','removeSmsInteractions','user_has_sms_interactions:remove','removeOpenchannelInteractions','removeWhatsappInteractions','catch','log','join','connection','setMaxListeners','address','request','remoteAddress','remotePort'];(function(_0x24415c,_0x32df9c){var _0x547db7=function(_0x5a820c){while(--_0x5a820c){_0x24415c['push'](_0x24415c['shift']());}};_0x547db7(++_0x32df9c);}(_0x838a,0x65));var _0xa838=function(_0x3ee5df,_0x583073){_0x3ee5df=_0x3ee5df-0x0;var _0x446d24=_0x838a[_0x3ee5df];return _0x446d24;};'use strict';var _=require('lodash');var util=require('util');var BPromise=require(_0xa838('0x0'));var db=require(_0xa838('0x1'))['db'];var config=require(_0xa838('0x2'));var queryQueues={'attributes':['id',_0xa838('0x3')],'include':[{'as':_0xa838('0x4'),'model':db[_0xa838('0x5')],'attributes':['id',_0xa838('0x3'),_0xa838('0x6')],'raw':!![],'where':{'role':_0xa838('0x7')},'required':![]}]};var queryTelephones={'raw':!![],'attributes':['id','name'],'where':{'role':'telephone'}};var queryTrunks={'raw':!![],'attributes':['id',_0xa838('0x3')]};var queryAgents={'raw':!![],'attributes':['id',_0xa838('0x3')],'where':{'role':_0xa838('0x7')}};var queryCampaigns={'raw':!![],'attributes':['id',_0xa838('0x3'),_0xa838('0x8')],'where':{'type':'ivr'}};function getAssociatedResources(_0x50bcc5,_0xf71015,_0x506d96){return db[_0xa838('0x9')][_0xa838('0xa')]({'where':{'name':_0x50bcc5,'userProfileId':_0xf71015},'raw':!![]})[_0xa838('0xb')](function(_0x586591){if(_0x586591){if(_0x586591['autoAssociation']){switch(_0x50bcc5){case'VoiceQueues':case'ChatQueues':case _0xa838('0xc'):case'SmsQueues':case _0xa838('0xd'):case _0xa838('0xe'):case'WhatsappQueues':return db[_0x506d96]['findAll'](queryQueues);case _0xa838('0xf'):return db[_0x506d96][_0xa838('0x10')](queryTelephones);case'Trunks':return db[_0x506d96][_0xa838('0x10')](queryTrunks);case _0xa838('0x11'):return db[_0x506d96][_0xa838('0x10')](queryCampaigns);}}else{return db['UserProfileResource'][_0xa838('0x10')]({'attributes':[_0xa838('0x12')],'where':{'sectionId':_0x586591['id']},'raw':!![]})[_0xa838('0xb')](function(_0x4b8166){if(_0x4b8166){var _0x3b6bdc={'where':{'id':_[_0xa838('0x13')](_0x4b8166,_0xa838('0x12'))},'raw':!![]};switch(_0x506d96){case _0xa838('0x5'):_0x3b6bdc[_0xa838('0x14')]['role']=_0xa838('0x15');break;case _0xa838('0x16'):_0x3b6bdc[_0xa838('0x14')]['type']=_0xa838('0x17');break;}return db[_0x506d96][_0xa838('0x10')](_0x3b6bdc);}else{return[];}});}}else{return[];}})[_0xa838('0xb')](function(_0x6ed679){return _0x6ed679;});}function joinOrLeaveQueues(_0x1a3060,_0x1c4803,_0xa85514,_0x2981cb){_0x1a3060[_0x1c4803](util[_0xa838('0x18')](_0xa838('0x19'),_0xa85514,_0x2981cb[_0xa838('0x3')]));if(_0x2981cb['Agents']){for(var _0x1fbf7a=0x0;_0x1fbf7a<_0x2981cb[_0xa838('0x4')][_0xa838('0x1a')];_0x1fbf7a+=0x1){_0x1a3060[_0x1c4803](util['format']('user:%s',_0x2981cb[_0xa838('0x4')][_0x1fbf7a][_0xa838('0x3')]));}}}function joinOrLeave(_0x279f8e,_0x1e5f91,_0x48a997){var _0x4a5b1e;if(_0x279f8e[_0xa838('0x1b')]['query']['id']){db[_0xa838('0x5')][_0xa838('0x1c')]({'attributes':['id',_0xa838('0x3'),_0xa838('0x6'),'userProfileId'],'where':{'id':_0x279f8e[_0xa838('0x1b')][_0xa838('0x1d')]['id']},'include':[{'model':db[_0xa838('0x1e')],'as':_0xa838('0x1f'),'attributes':['id'],'raw':!![]},{'model':db[_0xa838('0x20')],'as':_0xa838('0x21'),'attributes':['id'],'raw':!![]},{'model':db[_0xa838('0x22')],'as':'FaxInteractions','attributes':['id'],'raw':!![]},{'model':db['SmsInteraction'],'as':'SmsInteractions','attributes':['id'],'raw':!![]},{'model':db['OpenchannelInteraction'],'as':_0xa838('0x23'),'attributes':['id'],'raw':!![]},{'model':db[_0xa838('0x24')],'as':'WhatsappInteractions','attributes':['id'],'raw':!![]}]})[_0xa838('0xb')](function(_0x1a5ef5){if(_0x1a5ef5){_0x4a5b1e=_0x1a5ef5;_0x279f8e[_0x1e5f91](util['format'](_0xa838('0x25'),_0x1a5ef5['name']));var _0x549ef7=[];switch(_0x1a5ef5[_0xa838('0x6')]){case _0xa838('0x26'):_0x279f8e[_0x1e5f91](_0xa838('0x27'));_0x279f8e[_0x1e5f91](_0xa838('0x28'));if(queryQueues[_0xa838('0x29')](_0xa838('0x2a'))){delete queryQueues[_0xa838('0x2a')];}_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x2c')]['findAll'](queryQueues));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x2d')][_0xa838('0x10')](queryQueues));_0x549ef7[_0xa838('0x2b')](db['FaxQueue'][_0xa838('0x10')](queryQueues));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x2e')]['findAll'](queryQueues));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x2f')][_0xa838('0x10')](queryQueues));_0x549ef7[_0xa838('0x2b')](db['OpenchannelQueue'][_0xa838('0x10')](queryQueues));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x30')]['findAll'](queryQueues));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x5')][_0xa838('0x10')](queryTelephones));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x31')][_0xa838('0x10')](queryTrunks));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x16')][_0xa838('0x10')](queryCampaigns));_0x549ef7['push'](db[_0xa838('0x5')][_0xa838('0x10')](queryAgents));break;case _0xa838('0x32'):_0x279f8e[_0x1e5f91](_0xa838('0x27'));_0x279f8e[_0x1e5f91]('voice:outbound');_0x549ef7[_0xa838('0x2b')](getAssociatedResources(_0xa838('0x33'),_0x1a5ef5[_0xa838('0x34')],_0xa838('0x2c')));_0x549ef7[_0xa838('0x2b')](getAssociatedResources(_0xa838('0x35'),_0x1a5ef5['userProfileId'],'ChatQueue'));_0x549ef7[_0xa838('0x2b')](getAssociatedResources('FaxQueues',_0x1a5ef5[_0xa838('0x34')],_0xa838('0x36')));_0x549ef7[_0xa838('0x2b')](getAssociatedResources(_0xa838('0x37'),_0x1a5ef5[_0xa838('0x34')],_0xa838('0x2e')));_0x549ef7[_0xa838('0x2b')](getAssociatedResources(_0xa838('0xd'),_0x1a5ef5['userProfileId'],'MailQueue'));_0x549ef7['push'](getAssociatedResources(_0xa838('0xe'),_0x1a5ef5[_0xa838('0x34')],_0xa838('0x38')));_0x549ef7[_0xa838('0x2b')](getAssociatedResources(_0xa838('0x39'),_0x1a5ef5[_0xa838('0x34')],_0xa838('0x30')));_0x549ef7['push'](getAssociatedResources(_0xa838('0xf'),_0x1a5ef5[_0xa838('0x34')],_0xa838('0x5')));_0x549ef7[_0xa838('0x2b')](getAssociatedResources(_0xa838('0x3a'),_0x1a5ef5[_0xa838('0x34')],'Trunk'));_0x549ef7['push'](getAssociatedResources(_0xa838('0x11'),_0x1a5ef5[_0xa838('0x34')],_0xa838('0x16')));_0x549ef7[_0xa838('0x2b')](db[_0xa838('0x5')][_0xa838('0x10')](queryAgents));break;case _0xa838('0x7'):_0x279f8e[_0x1e5f91]('voice:inbound');_0x279f8e[_0x1e5f91](_0xa838('0x28'));_0x549ef7[_0xa838('0x2b')](_0x1a5ef5[_0xa838('0x3b')]({'attributes':['id',_0xa838('0x3')]}));_0x549ef7['push'](_0x1a5ef5[_0xa838('0x3c')]({'attributes':['id',_0xa838('0x3')]}));_0x549ef7['push'](_0x1a5ef5[_0xa838('0x3d')]({'attributes':['id',_0xa838('0x3')]}));_0x549ef7['push'](_0x1a5ef5['getSmsQueues']({'attributes':['id',_0xa838('0x3')]}));_0x549ef7[_0xa838('0x2b')](_0x1a5ef5[_0xa838('0x3e')]({'attributes':['id',_0xa838('0x3')]}));_0x549ef7[_0xa838('0x2b')](_0x1a5ef5[_0xa838('0x3f')]({'attributes':['id',_0xa838('0x3')]}));_0x549ef7['push'](_0x1a5ef5[_0xa838('0x40')]({'attributes':['id',_0xa838('0x3')]}));break;}return BPromise[_0xa838('0x41')](_0x549ef7);}})[_0xa838('0xb')](function(_0x47657e){if(_0x47657e&&_0x47657e[_0xa838('0x1a')]){for(var _0x579a38=0x0;_0x579a38<_0x47657e[_0xa838('0x1a')];_0x579a38+=0x1){for(var _0x15feae=0x0;_0x15feae<_0x47657e[_0x579a38][_0xa838('0x1a')];_0x15feae+=0x1){switch(_0x579a38){case 0x0:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,'voice',_0x47657e[_0x579a38][_0x15feae]);break;case 0x1:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,_0xa838('0x42'),_0x47657e[_0x579a38][_0x15feae]);break;case 0x2:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,_0xa838('0x43'),_0x47657e[_0x579a38][_0x15feae]);break;case 0x3:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,_0xa838('0x44'),_0x47657e[_0x579a38][_0x15feae]);break;case 0x4:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,_0xa838('0x45'),_0x47657e[_0x579a38][_0x15feae]);break;case 0x5:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,_0xa838('0x46'),_0x47657e[_0x579a38][_0x15feae]);break;case 0x6:joinOrLeaveQueues(_0x279f8e,_0x1e5f91,'whatsapp',_0x47657e[_0x579a38][_0x15feae]);break;case 0x7:if(_0x4a5b1e['role']!=='agent')_0x279f8e[_0x1e5f91](util[_0xa838('0x18')](_0xa838('0x47'),_0x47657e[_0x579a38][_0x15feae][_0xa838('0x3')]));break;case 0x8:if(_0x4a5b1e[_0xa838('0x6')]!==_0xa838('0x7'))_0x279f8e[_0x1e5f91](util[_0xa838('0x18')](_0xa838('0x48'),_0x47657e[_0x579a38][_0x15feae][_0xa838('0x3')]));break;case 0x9:if(_0x4a5b1e[_0xa838('0x6')]!==_0xa838('0x7'))_0x279f8e[_0x1e5f91](util['format'](_0xa838('0x49'),_0x47657e[_0x579a38][_0x15feae]['type'],_0x47657e[_0x579a38][_0x15feae][_0xa838('0x3')]));break;case 0xa:_0x279f8e[_0x1e5f91](util['format']('user:%s',_0x47657e[_0x579a38][_0x15feae][_0xa838('0x3')]));break;}}}}})[_0xa838('0xb')](function(){if(_0x1e5f91==='leave'){var _0x191da8=_['map'](_0x4a5b1e[_0xa838('0x1f')],'id');return _0x4a5b1e[_0xa838('0x4a')](_0x191da8)[_0xa838('0xb')](function(){for(var _0x18803e=0x0;_0x18803e<_0x191da8[_0xa838('0x1a')];_0x18803e+=0x1){_0x48a997[_0xa838('0x4b')]('user_has_chat_interactions:remove',{'ChatInteractionId':Number(_0x191da8[_0x18803e]),'UserId':Number(_0x4a5b1e['id'])});}});}})[_0xa838('0xb')](function(){if(_0x1e5f91===_0xa838('0x4c')){var _0x1cd88f=_[_0xa838('0x13')](_0x4a5b1e[_0xa838('0x21')],'id');return _0x4a5b1e[_0xa838('0x4d')](_0x1cd88f)[_0xa838('0xb')](function(){for(var _0x70511f=0x0;_0x70511f<_0x1cd88f[_0xa838('0x1a')];_0x70511f+=0x1){_0x48a997['emit']('user_has_mail_interactions:remove',{'MailInteractionId':Number(_0x1cd88f[_0x70511f]),'UserId':Number(_0x4a5b1e['id'])});}});}})[_0xa838('0xb')](function(){if(_0x1e5f91===_0xa838('0x4c')){var _0x2e1a7c=_['map'](_0x4a5b1e['FaxInteractions'],'id');return _0x4a5b1e[_0xa838('0x4e')](_0x2e1a7c)['then'](function(){for(var _0x15d6c6=0x0;_0x15d6c6<_0x2e1a7c[_0xa838('0x1a')];_0x15d6c6+=0x1){_0x48a997['emit']('user_has_fax_interactions:remove',{'FaxInteractionId':Number(_0x2e1a7c[_0x15d6c6]),'UserId':Number(_0x4a5b1e['id'])});}});}})[_0xa838('0xb')](function(){if(_0x1e5f91==='leave'){var _0x3b1cf9=_[_0xa838('0x13')](_0x4a5b1e[_0xa838('0x4f')],'id');return _0x4a5b1e[_0xa838('0x50')](_0x3b1cf9)[_0xa838('0xb')](function(){for(var _0x5e7c57=0x0;_0x5e7c57<_0x3b1cf9[_0xa838('0x1a')];_0x5e7c57+=0x1){_0x48a997['emit'](_0xa838('0x51'),{'SmsInteractionId':Number(_0x3b1cf9[_0x5e7c57]),'UserId':Number(_0x4a5b1e['id'])});}});}})[_0xa838('0xb')](function(){if(_0x1e5f91===_0xa838('0x4c')){var _0x2fdaeb=_[_0xa838('0x13')](_0x4a5b1e[_0xa838('0x23')],'id');return _0x4a5b1e[_0xa838('0x52')](_0x2fdaeb)[_0xa838('0xb')](function(){for(var _0x20d533=0x0;_0x20d533<_0x2fdaeb[_0xa838('0x1a')];_0x20d533+=0x1){_0x48a997[_0xa838('0x4b')]('user_has_openchannel_interactions:remove',{'OpenchannelInteractionId':Number(_0x2fdaeb[_0x20d533]),'UserId':Number(_0x4a5b1e['id'])});}});}})[_0xa838('0xb')](function(){if(_0x1e5f91===_0xa838('0x4c')){var _0x24682c=_[_0xa838('0x13')](_0x4a5b1e['WhatsappInteractions'],'id');return _0x4a5b1e[_0xa838('0x53')](_0x24682c)[_0xa838('0xb')](function(){for(var _0x310ca0=0x0;_0x310ca0<_0x24682c[_0xa838('0x1a')];_0x310ca0+=0x1){_0x48a997[_0xa838('0x4b')]('user_has_whatsapp_interactions:remove',{'WhatsappInteractionId':Number(_0x24682c[_0x310ca0]),'UserId':Number(_0x4a5b1e['id'])});}});}})[_0xa838('0x54')](function(_0x2a5339){_0x279f8e[_0xa838('0x55')](_0x2a5339);});}}function onDisconnect(_0xeda99b,_0x2393e3){joinOrLeave(_0xeda99b,_0xa838('0x4c'),_0x2393e3);}function onConnect(_0x31ab5f){joinOrLeave(_0x31ab5f,_0xa838('0x56'));}exports['default']=function(_0x51f3a2){_0x51f3a2['on'](_0xa838('0x57'),function(_0x33af31){_0x33af31[_0xa838('0x58')](0x0);_0x33af31[_0xa838('0x59')]=_0x33af31[_0xa838('0x5a')][_0xa838('0x57')][_0xa838('0x5b')]+':'+_0x33af31['request']['connection'][_0xa838('0x5c')];_0x33af31['connectedAt']=new Date();_0x33af31[_0xa838('0x55')]=function(_0x1d18cf){console[_0xa838('0x55')](_0xa838('0x5d')+_0x33af31[_0xa838('0x5e')]['name']+'['+_0x33af31[_0xa838('0x59')]+']',_0x1d18cf);};_0x33af31['on'](_0xa838('0x5f'),function(){onDisconnect(_0x33af31,_0x51f3a2);_0x33af31['log'](_0xa838('0x60'));});onConnect(_0x33af31);_0x33af31[_0xa838('0x55')]('CONNECTED');});};