Built motion from commit 538092ac.|2.6.10
[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 _0xc1db=['lodash','bluebird','../mysqldb','./environment','name','Agents','role','agent','ivr','findOne','then','autoAssociation','VoiceQueues','ChatQueues','FaxQueues','MailQueues','WhatsappQueues','findAll','Trunks','IvrCampaigns','map','resourceId','User','Campaign','where','format','%s:queue:%s','query','ChatInteraction','MailInteraction','MailInteractions','FaxInteraction','FaxInteractions','SmsInteraction','SmsInteractions','OpenchannelInteraction','WhatsappInteraction','user:%s','admin','voice:inbound','voice:outbound','hasOwnProperty','include','push','VoiceQueue','ChatQueue','FaxQueue','SmsQueue','OpenchannelQueue','WhatsappQueue','Trunk','userProfileId','MailQueue','OpenchannelQueues','Telephones','getVoiceQueues','getChatQueues','getSmsQueues','getOpenchannelQueues','length','chat','fax','mail','openchannel','whatsapp','telephone:%s','trunk:%s','campaign:%s:%s','type','leave','emit','removeMailInteractions','user_has_mail_interactions:remove','user_has_fax_interactions:remove','removeSmsInteractions','user_has_sms_interactions:remove','removeOpenchannelInteractions','user_has_openchannel_interactions:remove','WhatsappInteractions','removeWhatsappInteractions','user_has_whatsapp_interactions:remove','catch','log','join','default','connection','setMaxListeners','address','request','remoteAddress','remotePort','connectedAt','SocketIO\x20','nsp','DISCONNECTED','CONNECTED'];(function(_0x43bd18,_0x543717){var _0x45c504=function(_0x54f138){while(--_0x54f138){_0x43bd18['push'](_0x43bd18['shift']());}};_0x45c504(++_0x543717);}(_0xc1db,0x120));var _0xbc1d=function(_0x211b47,_0x36e680){_0x211b47=_0x211b47-0x0;var _0x4e3cb7=_0xc1db[_0x211b47];return _0x4e3cb7;};'use strict';var _=require(_0xbc1d('0x0'));var util=require('util');var BPromise=require(_0xbc1d('0x1'));var db=require(_0xbc1d('0x2'))['db'];var config=require(_0xbc1d('0x3'));var queryQueues={'attributes':['id',_0xbc1d('0x4')],'include':[{'as':_0xbc1d('0x5'),'model':db['User'],'attributes':['id',_0xbc1d('0x4'),_0xbc1d('0x6')],'raw':!![],'where':{'role':_0xbc1d('0x7')},'required':![]}]};var queryTelephones={'raw':!![],'attributes':['id','name'],'where':{'role':'telephone'}};var queryTrunks={'raw':!![],'attributes':['id',_0xbc1d('0x4')]};var queryAgents={'raw':!![],'attributes':['id','name'],'where':{'role':'agent'}};var queryCampaigns={'raw':!![],'attributes':['id',_0xbc1d('0x4'),'type'],'where':{'type':_0xbc1d('0x8')}};function getAssociatedResources(_0x451e8d,_0x4c2d30,_0x45468e){return db['UserProfileSection'][_0xbc1d('0x9')]({'where':{'name':_0x451e8d,'userProfileId':_0x4c2d30},'raw':!![]})[_0xbc1d('0xa')](function(_0x51282b){if(_0x51282b){if(_0x51282b[_0xbc1d('0xb')]){switch(_0x451e8d){case _0xbc1d('0xc'):case _0xbc1d('0xd'):case _0xbc1d('0xe'):case'SmsQueues':case _0xbc1d('0xf'):case'OpenchannelQueues':case _0xbc1d('0x10'):return db[_0x45468e][_0xbc1d('0x11')](queryQueues);case'Telephones':return db[_0x45468e][_0xbc1d('0x11')](queryTelephones);case _0xbc1d('0x12'):return db[_0x45468e]['findAll'](queryTrunks);case _0xbc1d('0x13'):return db[_0x45468e][_0xbc1d('0x11')](queryCampaigns);}}else{return db['UserProfileResource'][_0xbc1d('0x11')]({'attributes':['resourceId'],'where':{'sectionId':_0x51282b['id']},'raw':!![]})[_0xbc1d('0xa')](function(_0x238c0d){if(_0x238c0d){var _0xfe7015={'where':{'id':_[_0xbc1d('0x14')](_0x238c0d,_0xbc1d('0x15'))},'raw':!![]};switch(_0x45468e){case _0xbc1d('0x16'):_0xfe7015['where'][_0xbc1d('0x6')]='telephone';break;case _0xbc1d('0x17'):_0xfe7015[_0xbc1d('0x18')]['type']='ivr';break;}return db[_0x45468e]['findAll'](_0xfe7015);}else{return[];}});}}else{return[];}})[_0xbc1d('0xa')](function(_0x403afc){return _0x403afc;});}function joinOrLeaveQueues(_0x44887e,_0x1bd6d7,_0x51415f,_0x284987){_0x44887e[_0x1bd6d7](util[_0xbc1d('0x19')](_0xbc1d('0x1a'),_0x51415f,_0x284987[_0xbc1d('0x4')]));if(_0x284987['Agents']){for(var _0x5ab558=0x0;_0x5ab558<_0x284987[_0xbc1d('0x5')]['length'];_0x5ab558+=0x1){_0x44887e[_0x1bd6d7](util[_0xbc1d('0x19')]('user:%s',_0x284987[_0xbc1d('0x5')][_0x5ab558][_0xbc1d('0x4')]));}}}function joinOrLeave(_0x5c026d,_0x1951a1,_0x1890c0){var _0x3955ab;if(_0x5c026d['handshake']['query']['id']){db[_0xbc1d('0x16')]['find']({'attributes':['id',_0xbc1d('0x4'),_0xbc1d('0x6'),'userProfileId'],'where':{'id':_0x5c026d['handshake'][_0xbc1d('0x1b')]['id']},'include':[{'model':db[_0xbc1d('0x1c')],'as':'ChatInteractions','attributes':['id'],'raw':!![]},{'model':db[_0xbc1d('0x1d')],'as':_0xbc1d('0x1e'),'attributes':['id'],'raw':!![]},{'model':db[_0xbc1d('0x1f')],'as':_0xbc1d('0x20'),'attributes':['id'],'raw':!![]},{'model':db[_0xbc1d('0x21')],'as':_0xbc1d('0x22'),'attributes':['id'],'raw':!![]},{'model':db[_0xbc1d('0x23')],'as':'OpenchannelInteractions','attributes':['id'],'raw':!![]},{'model':db[_0xbc1d('0x24')],'as':'WhatsappInteractions','attributes':['id'],'raw':!![]}]})['then'](function(_0x40d1e2){if(_0x40d1e2){_0x3955ab=_0x40d1e2;_0x5c026d[_0x1951a1](util[_0xbc1d('0x19')](_0xbc1d('0x25'),_0x40d1e2[_0xbc1d('0x4')]));var _0x507310=[];switch(_0x40d1e2[_0xbc1d('0x6')]){case _0xbc1d('0x26'):_0x5c026d[_0x1951a1](_0xbc1d('0x27'));_0x5c026d[_0x1951a1](_0xbc1d('0x28'));if(queryQueues[_0xbc1d('0x29')](_0xbc1d('0x2a'))){delete queryQueues[_0xbc1d('0x2a')];}_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x2c')][_0xbc1d('0x11')](queryQueues));_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x2d')][_0xbc1d('0x11')](queryQueues));_0x507310['push'](db[_0xbc1d('0x2e')][_0xbc1d('0x11')](queryQueues));_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x2f')][_0xbc1d('0x11')](queryQueues));_0x507310[_0xbc1d('0x2b')](db['MailQueue'][_0xbc1d('0x11')](queryQueues));_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x30')]['findAll'](queryQueues));_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x31')][_0xbc1d('0x11')](queryQueues));_0x507310['push'](db[_0xbc1d('0x16')][_0xbc1d('0x11')](queryTelephones));_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x32')]['findAll'](queryTrunks));_0x507310['push'](db[_0xbc1d('0x17')][_0xbc1d('0x11')](queryCampaigns));_0x507310[_0xbc1d('0x2b')](db['User']['findAll'](queryAgents));break;case'user':_0x5c026d[_0x1951a1]('voice:inbound');_0x5c026d[_0x1951a1](_0xbc1d('0x28'));_0x507310[_0xbc1d('0x2b')](getAssociatedResources(_0xbc1d('0xc'),_0x40d1e2[_0xbc1d('0x33')],_0xbc1d('0x2c')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources('ChatQueues',_0x40d1e2[_0xbc1d('0x33')],'ChatQueue'));_0x507310[_0xbc1d('0x2b')](getAssociatedResources('FaxQueues',_0x40d1e2[_0xbc1d('0x33')],_0xbc1d('0x2e')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources('SmsQueues',_0x40d1e2[_0xbc1d('0x33')],_0xbc1d('0x2f')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources(_0xbc1d('0xf'),_0x40d1e2['userProfileId'],_0xbc1d('0x34')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources(_0xbc1d('0x35'),_0x40d1e2[_0xbc1d('0x33')],_0xbc1d('0x30')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources(_0xbc1d('0x10'),_0x40d1e2[_0xbc1d('0x33')],'WhatsappQueue'));_0x507310[_0xbc1d('0x2b')](getAssociatedResources(_0xbc1d('0x36'),_0x40d1e2['userProfileId'],_0xbc1d('0x16')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources('Trunks',_0x40d1e2['userProfileId'],_0xbc1d('0x32')));_0x507310[_0xbc1d('0x2b')](getAssociatedResources(_0xbc1d('0x13'),_0x40d1e2['userProfileId'],_0xbc1d('0x17')));_0x507310[_0xbc1d('0x2b')](db[_0xbc1d('0x16')][_0xbc1d('0x11')](queryAgents));break;case'agent':_0x5c026d[_0x1951a1](_0xbc1d('0x27'));_0x5c026d[_0x1951a1]('voice:outbound');_0x507310[_0xbc1d('0x2b')](_0x40d1e2[_0xbc1d('0x37')]({'attributes':['id','name']}));_0x507310[_0xbc1d('0x2b')](_0x40d1e2[_0xbc1d('0x38')]({'attributes':['id',_0xbc1d('0x4')]}));_0x507310[_0xbc1d('0x2b')](_0x40d1e2['getFaxQueues']({'attributes':['id',_0xbc1d('0x4')]}));_0x507310['push'](_0x40d1e2[_0xbc1d('0x39')]({'attributes':['id','name']}));_0x507310[_0xbc1d('0x2b')](_0x40d1e2['getMailQueues']({'attributes':['id',_0xbc1d('0x4')]}));_0x507310[_0xbc1d('0x2b')](_0x40d1e2[_0xbc1d('0x3a')]({'attributes':['id',_0xbc1d('0x4')]}));_0x507310[_0xbc1d('0x2b')](_0x40d1e2['getWhatsappQueues']({'attributes':['id',_0xbc1d('0x4')]}));break;}return BPromise['all'](_0x507310);}})['then'](function(_0x398f1a){if(_0x398f1a&&_0x398f1a[_0xbc1d('0x3b')]){for(var _0x25f94c=0x0;_0x25f94c<_0x398f1a[_0xbc1d('0x3b')];_0x25f94c+=0x1){for(var _0x390b4d=0x0;_0x390b4d<_0x398f1a[_0x25f94c]['length'];_0x390b4d+=0x1){switch(_0x25f94c){case 0x0:joinOrLeaveQueues(_0x5c026d,_0x1951a1,'voice',_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x1:joinOrLeaveQueues(_0x5c026d,_0x1951a1,_0xbc1d('0x3c'),_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x2:joinOrLeaveQueues(_0x5c026d,_0x1951a1,_0xbc1d('0x3d'),_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x3:joinOrLeaveQueues(_0x5c026d,_0x1951a1,'sms',_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x4:joinOrLeaveQueues(_0x5c026d,_0x1951a1,_0xbc1d('0x3e'),_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x5:joinOrLeaveQueues(_0x5c026d,_0x1951a1,_0xbc1d('0x3f'),_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x6:joinOrLeaveQueues(_0x5c026d,_0x1951a1,_0xbc1d('0x40'),_0x398f1a[_0x25f94c][_0x390b4d]);break;case 0x7:if(_0x3955ab[_0xbc1d('0x6')]!==_0xbc1d('0x7'))_0x5c026d[_0x1951a1](util[_0xbc1d('0x19')](_0xbc1d('0x41'),_0x398f1a[_0x25f94c][_0x390b4d][_0xbc1d('0x4')]));break;case 0x8:if(_0x3955ab[_0xbc1d('0x6')]!==_0xbc1d('0x7'))_0x5c026d[_0x1951a1](util[_0xbc1d('0x19')](_0xbc1d('0x42'),_0x398f1a[_0x25f94c][_0x390b4d][_0xbc1d('0x4')]));break;case 0x9:if(_0x3955ab[_0xbc1d('0x6')]!==_0xbc1d('0x7'))_0x5c026d[_0x1951a1](util[_0xbc1d('0x19')](_0xbc1d('0x43'),_0x398f1a[_0x25f94c][_0x390b4d][_0xbc1d('0x44')],_0x398f1a[_0x25f94c][_0x390b4d]['name']));break;case 0xa:_0x5c026d[_0x1951a1](util[_0xbc1d('0x19')](_0xbc1d('0x25'),_0x398f1a[_0x25f94c][_0x390b4d][_0xbc1d('0x4')]));break;}}}}})[_0xbc1d('0xa')](function(){if(_0x1951a1===_0xbc1d('0x45')){var _0x2300b9=_[_0xbc1d('0x14')](_0x3955ab['ChatInteractions'],'id');return _0x3955ab['removeChatInteractions'](_0x2300b9)[_0xbc1d('0xa')](function(){for(var _0x132fe5=0x0;_0x132fe5<_0x2300b9[_0xbc1d('0x3b')];_0x132fe5+=0x1){_0x1890c0[_0xbc1d('0x46')]('user_has_chat_interactions:remove',{'ChatInteractionId':Number(_0x2300b9[_0x132fe5]),'UserId':Number(_0x3955ab['id'])});}});}})[_0xbc1d('0xa')](function(){if(_0x1951a1===_0xbc1d('0x45')){var _0x1971fe=_[_0xbc1d('0x14')](_0x3955ab[_0xbc1d('0x1e')],'id');return _0x3955ab[_0xbc1d('0x47')](_0x1971fe)['then'](function(){for(var _0x3b15a4=0x0;_0x3b15a4<_0x1971fe[_0xbc1d('0x3b')];_0x3b15a4+=0x1){_0x1890c0[_0xbc1d('0x46')](_0xbc1d('0x48'),{'MailInteractionId':Number(_0x1971fe[_0x3b15a4]),'UserId':Number(_0x3955ab['id'])});}});}})[_0xbc1d('0xa')](function(){if(_0x1951a1===_0xbc1d('0x45')){var _0x239bfb=_[_0xbc1d('0x14')](_0x3955ab[_0xbc1d('0x20')],'id');return _0x3955ab['removeFaxInteractions'](_0x239bfb)[_0xbc1d('0xa')](function(){for(var _0x271a2b=0x0;_0x271a2b<_0x239bfb[_0xbc1d('0x3b')];_0x271a2b+=0x1){_0x1890c0[_0xbc1d('0x46')](_0xbc1d('0x49'),{'FaxInteractionId':Number(_0x239bfb[_0x271a2b]),'UserId':Number(_0x3955ab['id'])});}});}})[_0xbc1d('0xa')](function(){if(_0x1951a1===_0xbc1d('0x45')){var _0x12d0fe=_[_0xbc1d('0x14')](_0x3955ab[_0xbc1d('0x22')],'id');return _0x3955ab[_0xbc1d('0x4a')](_0x12d0fe)[_0xbc1d('0xa')](function(){for(var _0xc8af7a=0x0;_0xc8af7a<_0x12d0fe[_0xbc1d('0x3b')];_0xc8af7a+=0x1){_0x1890c0['emit'](_0xbc1d('0x4b'),{'SmsInteractionId':Number(_0x12d0fe[_0xc8af7a]),'UserId':Number(_0x3955ab['id'])});}});}})[_0xbc1d('0xa')](function(){if(_0x1951a1===_0xbc1d('0x45')){var _0x3ef112=_['map'](_0x3955ab['OpenchannelInteractions'],'id');return _0x3955ab[_0xbc1d('0x4c')](_0x3ef112)[_0xbc1d('0xa')](function(){for(var _0x2dd522=0x0;_0x2dd522<_0x3ef112[_0xbc1d('0x3b')];_0x2dd522+=0x1){_0x1890c0[_0xbc1d('0x46')](_0xbc1d('0x4d'),{'OpenchannelInteractionId':Number(_0x3ef112[_0x2dd522]),'UserId':Number(_0x3955ab['id'])});}});}})[_0xbc1d('0xa')](function(){if(_0x1951a1===_0xbc1d('0x45')){var _0x57ee3b=_['map'](_0x3955ab[_0xbc1d('0x4e')],'id');return _0x3955ab[_0xbc1d('0x4f')](_0x57ee3b)[_0xbc1d('0xa')](function(){for(var _0x4ca3f3=0x0;_0x4ca3f3<_0x57ee3b[_0xbc1d('0x3b')];_0x4ca3f3+=0x1){_0x1890c0['emit'](_0xbc1d('0x50'),{'WhatsappInteractionId':Number(_0x57ee3b[_0x4ca3f3]),'UserId':Number(_0x3955ab['id'])});}});}})[_0xbc1d('0x51')](function(_0x29d919){_0x5c026d[_0xbc1d('0x52')](_0x29d919);});}}function onDisconnect(_0x51b4cd,_0x1a6848){joinOrLeave(_0x51b4cd,_0xbc1d('0x45'),_0x1a6848);}function onConnect(_0x3604f9){joinOrLeave(_0x3604f9,_0xbc1d('0x53'));}exports[_0xbc1d('0x54')]=function(_0x330570){_0x330570['on'](_0xbc1d('0x55'),function(_0x4a4c9e){_0x4a4c9e[_0xbc1d('0x56')](0x0);_0x4a4c9e[_0xbc1d('0x57')]=_0x4a4c9e[_0xbc1d('0x58')][_0xbc1d('0x55')][_0xbc1d('0x59')]+':'+_0x4a4c9e['request'][_0xbc1d('0x55')][_0xbc1d('0x5a')];_0x4a4c9e[_0xbc1d('0x5b')]=new Date();_0x4a4c9e[_0xbc1d('0x52')]=function(_0x289b98){console[_0xbc1d('0x52')](_0xbc1d('0x5c')+_0x4a4c9e[_0xbc1d('0x5d')][_0xbc1d('0x4')]+'['+_0x4a4c9e[_0xbc1d('0x57')]+']',_0x289b98);};_0x4a4c9e['on']('disconnect',function(){onDisconnect(_0x4a4c9e,_0x330570);_0x4a4c9e[_0xbc1d('0x52')](_0xbc1d('0x5e'));});onConnect(_0x4a4c9e);_0x4a4c9e['log'](_0xbc1d('0x5f'));});};