08e660cb3f257ca6e2845327787f9755f37d5239
[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 _0x1b7b=['telephone','Campaign','ivr','%s:queue:%s','length','handshake','query','find','userProfileId','ChatInteractions','MailInteraction','FaxInteraction','SmsInteraction','SmsInteractions','OpenchannelInteractions','WhatsappInteraction','WhatsappInteractions','format','user:%s','admin','include','push','ChatQueue','MailQueue','OpenchannelQueue','WhatsappQueue','Trunk','user','voice:inbound','voice:outbound','VoiceQueue','FaxQueue','SmsQueue','Telephones','IvrCampaigns','getVoiceQueues','getChatQueues','getFaxQueues','getSmsQueues','getMailQueues','getOpenchannelQueues','getWhatsappQueues','all','voice','chat','fax','sms','openchannel','trunk:%s','campaign:%s:%s','leave','removeChatInteractions','emit','MailInteractions','user_has_mail_interactions:remove','removeFaxInteractions','user_has_fax_interactions:remove','removeSmsInteractions','user_has_sms_interactions:remove','user_has_openchannel_interactions:remove','removeWhatsappInteractions','user_has_whatsapp_interactions:remove','log','default','connection','address','request','connectedAt','nsp','disconnect','lodash','util','bluebird','../mysqldb','name','Agents','User','role','agent','type','UserProfileSection','autoAssociation','VoiceQueues','ChatQueues','FaxQueues','SmsQueues','OpenchannelQueues','WhatsappQueues','findAll','Trunks','UserProfileResource','resourceId','then','map','where'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0x1b7b,0x104));var _0xb1b7=function(_0x3bc324,_0xddf774){_0x3bc324=_0x3bc324-0x0;var _0x50dc6b=_0x1b7b[_0x3bc324];return _0x50dc6b;};'use strict';var _=require(_0xb1b7('0x0'));var util=require(_0xb1b7('0x1'));var BPromise=require(_0xb1b7('0x2'));var db=require(_0xb1b7('0x3'))['db'];var config=require('./environment');var queryQueues={'attributes':['id',_0xb1b7('0x4')],'include':[{'as':_0xb1b7('0x5'),'model':db[_0xb1b7('0x6')],'attributes':['id',_0xb1b7('0x4'),_0xb1b7('0x7')],'raw':!![],'where':{'role':'agent'},'required':![]}]};var queryTelephones={'raw':!![],'attributes':['id',_0xb1b7('0x4')],'where':{'role':'telephone'}};var queryTrunks={'raw':!![],'attributes':['id',_0xb1b7('0x4')]};var queryAgents={'raw':!![],'attributes':['id','name'],'where':{'role':_0xb1b7('0x8')}};var queryCampaigns={'raw':!![],'attributes':['id','name',_0xb1b7('0x9')],'where':{'type':'ivr'}};function getAssociatedResources(_0x4d10cf,_0x8456f2,_0x4acf92){return db[_0xb1b7('0xa')]['findOne']({'where':{'name':_0x4d10cf,'userProfileId':_0x8456f2},'raw':!![]})['then'](function(_0xcc6f6d){if(_0xcc6f6d){if(_0xcc6f6d[_0xb1b7('0xb')]){switch(_0x4d10cf){case _0xb1b7('0xc'):case _0xb1b7('0xd'):case _0xb1b7('0xe'):case _0xb1b7('0xf'):case'MailQueues':case _0xb1b7('0x10'):case _0xb1b7('0x11'):return db[_0x4acf92][_0xb1b7('0x12')](queryQueues);case'Telephones':return db[_0x4acf92][_0xb1b7('0x12')](queryTelephones);case _0xb1b7('0x13'):return db[_0x4acf92][_0xb1b7('0x12')](queryTrunks);case'IvrCampaigns':return db[_0x4acf92][_0xb1b7('0x12')](queryCampaigns);}}else{return db[_0xb1b7('0x14')][_0xb1b7('0x12')]({'attributes':[_0xb1b7('0x15')],'where':{'sectionId':_0xcc6f6d['id']},'raw':!![]})[_0xb1b7('0x16')](function(_0x29ff17){if(_0x29ff17){var _0x39d018={'where':{'id':_[_0xb1b7('0x17')](_0x29ff17,_0xb1b7('0x15'))},'raw':!![]};switch(_0x4acf92){case _0xb1b7('0x6'):_0x39d018[_0xb1b7('0x18')][_0xb1b7('0x7')]=_0xb1b7('0x19');break;case _0xb1b7('0x1a'):_0x39d018[_0xb1b7('0x18')][_0xb1b7('0x9')]=_0xb1b7('0x1b');break;}return db[_0x4acf92][_0xb1b7('0x12')](_0x39d018);}else{return[];}});}}else{return[];}})[_0xb1b7('0x16')](function(_0x166895){return _0x166895;});}function joinOrLeaveQueues(_0x34bb2b,_0x37d9b7,_0x21232a,_0x1f3bfc){_0x34bb2b[_0x37d9b7](util['format'](_0xb1b7('0x1c'),_0x21232a,_0x1f3bfc[_0xb1b7('0x4')]));if(_0x1f3bfc[_0xb1b7('0x5')]){for(var _0x21943b=0x0;_0x21943b<_0x1f3bfc[_0xb1b7('0x5')][_0xb1b7('0x1d')];_0x21943b+=0x1){_0x34bb2b[_0x37d9b7](util['format']('user:%s',_0x1f3bfc[_0xb1b7('0x5')][_0x21943b][_0xb1b7('0x4')]));}}}function joinOrLeave(_0x2ed0c1,_0x53b66c,_0x1ed4b6){var _0x18f50b;if(_0x2ed0c1[_0xb1b7('0x1e')][_0xb1b7('0x1f')]['id']){db[_0xb1b7('0x6')][_0xb1b7('0x20')]({'attributes':['id',_0xb1b7('0x4'),'role',_0xb1b7('0x21')],'where':{'id':_0x2ed0c1[_0xb1b7('0x1e')][_0xb1b7('0x1f')]['id']},'include':[{'model':db['ChatInteraction'],'as':_0xb1b7('0x22'),'attributes':['id'],'raw':!![]},{'model':db[_0xb1b7('0x23')],'as':'MailInteractions','attributes':['id'],'raw':!![]},{'model':db[_0xb1b7('0x24')],'as':'FaxInteractions','attributes':['id'],'raw':!![]},{'model':db[_0xb1b7('0x25')],'as':_0xb1b7('0x26'),'attributes':['id'],'raw':!![]},{'model':db['OpenchannelInteraction'],'as':_0xb1b7('0x27'),'attributes':['id'],'raw':!![]},{'model':db[_0xb1b7('0x28')],'as':_0xb1b7('0x29'),'attributes':['id'],'raw':!![]}]})['then'](function(_0xce3269){if(_0xce3269){_0x18f50b=_0xce3269;_0x2ed0c1[_0x53b66c](util[_0xb1b7('0x2a')](_0xb1b7('0x2b'),_0xce3269[_0xb1b7('0x4')]));var _0x12fe8a=[];switch(_0xce3269[_0xb1b7('0x7')]){case _0xb1b7('0x2c'):_0x2ed0c1[_0x53b66c]('voice:inbound');_0x2ed0c1[_0x53b66c]('voice:outbound');if(queryQueues['hasOwnProperty'](_0xb1b7('0x2d'))){delete queryQueues[_0xb1b7('0x2d')];}_0x12fe8a[_0xb1b7('0x2e')](db['VoiceQueue'][_0xb1b7('0x12')](queryQueues));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x2f')][_0xb1b7('0x12')](queryQueues));_0x12fe8a[_0xb1b7('0x2e')](db['FaxQueue'][_0xb1b7('0x12')](queryQueues));_0x12fe8a['push'](db['SmsQueue'][_0xb1b7('0x12')](queryQueues));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x30')][_0xb1b7('0x12')](queryQueues));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x31')][_0xb1b7('0x12')](queryQueues));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x32')][_0xb1b7('0x12')](queryQueues));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x6')][_0xb1b7('0x12')](queryTelephones));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x33')]['findAll'](queryTrunks));_0x12fe8a['push'](db['Campaign'][_0xb1b7('0x12')](queryCampaigns));_0x12fe8a[_0xb1b7('0x2e')](db['User'][_0xb1b7('0x12')](queryAgents));break;case _0xb1b7('0x34'):_0x2ed0c1[_0x53b66c](_0xb1b7('0x35'));_0x2ed0c1[_0x53b66c](_0xb1b7('0x36'));_0x12fe8a['push'](getAssociatedResources(_0xb1b7('0xc'),_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x37')));_0x12fe8a['push'](getAssociatedResources(_0xb1b7('0xd'),_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x2f')));_0x12fe8a[_0xb1b7('0x2e')](getAssociatedResources(_0xb1b7('0xe'),_0xce3269['userProfileId'],_0xb1b7('0x38')));_0x12fe8a[_0xb1b7('0x2e')](getAssociatedResources('SmsQueues',_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x39')));_0x12fe8a[_0xb1b7('0x2e')](getAssociatedResources('MailQueues',_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x30')));_0x12fe8a['push'](getAssociatedResources(_0xb1b7('0x10'),_0xce3269[_0xb1b7('0x21')],'OpenchannelQueue'));_0x12fe8a[_0xb1b7('0x2e')](getAssociatedResources(_0xb1b7('0x11'),_0xce3269['userProfileId'],'WhatsappQueue'));_0x12fe8a[_0xb1b7('0x2e')](getAssociatedResources(_0xb1b7('0x3a'),_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x6')));_0x12fe8a[_0xb1b7('0x2e')](getAssociatedResources(_0xb1b7('0x13'),_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x33')));_0x12fe8a['push'](getAssociatedResources(_0xb1b7('0x3b'),_0xce3269[_0xb1b7('0x21')],_0xb1b7('0x1a')));_0x12fe8a[_0xb1b7('0x2e')](db[_0xb1b7('0x6')]['findAll'](queryAgents));break;case _0xb1b7('0x8'):_0x2ed0c1[_0x53b66c](_0xb1b7('0x35'));_0x2ed0c1[_0x53b66c]('voice:outbound');_0x12fe8a[_0xb1b7('0x2e')](_0xce3269[_0xb1b7('0x3c')]({'attributes':['id','name']}));_0x12fe8a[_0xb1b7('0x2e')](_0xce3269[_0xb1b7('0x3d')]({'attributes':['id',_0xb1b7('0x4')]}));_0x12fe8a[_0xb1b7('0x2e')](_0xce3269[_0xb1b7('0x3e')]({'attributes':['id',_0xb1b7('0x4')]}));_0x12fe8a[_0xb1b7('0x2e')](_0xce3269[_0xb1b7('0x3f')]({'attributes':['id','name']}));_0x12fe8a[_0xb1b7('0x2e')](_0xce3269[_0xb1b7('0x40')]({'attributes':['id','name']}));_0x12fe8a[_0xb1b7('0x2e')](_0xce3269[_0xb1b7('0x41')]({'attributes':['id','name']}));_0x12fe8a['push'](_0xce3269[_0xb1b7('0x42')]({'attributes':['id','name']}));break;}return BPromise[_0xb1b7('0x43')](_0x12fe8a);}})[_0xb1b7('0x16')](function(_0x2d7c48){if(_0x2d7c48&&_0x2d7c48[_0xb1b7('0x1d')]){for(var _0x4b9a94=0x0;_0x4b9a94<_0x2d7c48[_0xb1b7('0x1d')];_0x4b9a94+=0x1){for(var _0x4fef70=0x0;_0x4fef70<_0x2d7c48[_0x4b9a94][_0xb1b7('0x1d')];_0x4fef70+=0x1){switch(_0x4b9a94){case 0x0:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,_0xb1b7('0x44'),_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x1:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,_0xb1b7('0x45'),_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x2:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,_0xb1b7('0x46'),_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x3:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,_0xb1b7('0x47'),_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x4:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,'mail',_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x5:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,_0xb1b7('0x48'),_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x6:joinOrLeaveQueues(_0x2ed0c1,_0x53b66c,'whatsapp',_0x2d7c48[_0x4b9a94][_0x4fef70]);break;case 0x7:if(_0x18f50b['role']!=='agent')_0x2ed0c1[_0x53b66c](util[_0xb1b7('0x2a')]('telephone:%s',_0x2d7c48[_0x4b9a94][_0x4fef70][_0xb1b7('0x4')]));break;case 0x8:if(_0x18f50b['role']!==_0xb1b7('0x8'))_0x2ed0c1[_0x53b66c](util['format'](_0xb1b7('0x49'),_0x2d7c48[_0x4b9a94][_0x4fef70][_0xb1b7('0x4')]));break;case 0x9:if(_0x18f50b['role']!==_0xb1b7('0x8'))_0x2ed0c1[_0x53b66c](util['format'](_0xb1b7('0x4a'),_0x2d7c48[_0x4b9a94][_0x4fef70]['type'],_0x2d7c48[_0x4b9a94][_0x4fef70][_0xb1b7('0x4')]));break;case 0xa:_0x2ed0c1[_0x53b66c](util[_0xb1b7('0x2a')](_0xb1b7('0x2b'),_0x2d7c48[_0x4b9a94][_0x4fef70][_0xb1b7('0x4')]));break;}}}}})[_0xb1b7('0x16')](function(){if(_0x53b66c===_0xb1b7('0x4b')){var _0x37f8da=_[_0xb1b7('0x17')](_0x18f50b['ChatInteractions'],'id');return _0x18f50b[_0xb1b7('0x4c')](_0x37f8da)[_0xb1b7('0x16')](function(){for(var _0x1a43f6=0x0;_0x1a43f6<_0x37f8da['length'];_0x1a43f6+=0x1){_0x1ed4b6[_0xb1b7('0x4d')]('user_has_chat_interactions:remove',{'ChatInteractionId':Number(_0x37f8da[_0x1a43f6]),'UserId':Number(_0x18f50b['id'])});}});}})['then'](function(){if(_0x53b66c===_0xb1b7('0x4b')){var _0x2f4555=_[_0xb1b7('0x17')](_0x18f50b[_0xb1b7('0x4e')],'id');return _0x18f50b['removeMailInteractions'](_0x2f4555)[_0xb1b7('0x16')](function(){for(var _0x5c5a69=0x0;_0x5c5a69<_0x2f4555[_0xb1b7('0x1d')];_0x5c5a69+=0x1){_0x1ed4b6[_0xb1b7('0x4d')](_0xb1b7('0x4f'),{'MailInteractionId':Number(_0x2f4555[_0x5c5a69]),'UserId':Number(_0x18f50b['id'])});}});}})[_0xb1b7('0x16')](function(){if(_0x53b66c===_0xb1b7('0x4b')){var _0x5a31f8=_[_0xb1b7('0x17')](_0x18f50b['FaxInteractions'],'id');return _0x18f50b[_0xb1b7('0x50')](_0x5a31f8)['then'](function(){for(var _0x162928=0x0;_0x162928<_0x5a31f8[_0xb1b7('0x1d')];_0x162928+=0x1){_0x1ed4b6[_0xb1b7('0x4d')](_0xb1b7('0x51'),{'FaxInteractionId':Number(_0x5a31f8[_0x162928]),'UserId':Number(_0x18f50b['id'])});}});}})[_0xb1b7('0x16')](function(){if(_0x53b66c===_0xb1b7('0x4b')){var _0xf8c6e2=_[_0xb1b7('0x17')](_0x18f50b['SmsInteractions'],'id');return _0x18f50b[_0xb1b7('0x52')](_0xf8c6e2)['then'](function(){for(var _0x918a04=0x0;_0x918a04<_0xf8c6e2[_0xb1b7('0x1d')];_0x918a04+=0x1){_0x1ed4b6[_0xb1b7('0x4d')](_0xb1b7('0x53'),{'SmsInteractionId':Number(_0xf8c6e2[_0x918a04]),'UserId':Number(_0x18f50b['id'])});}});}})[_0xb1b7('0x16')](function(){if(_0x53b66c===_0xb1b7('0x4b')){var _0x562e76=_[_0xb1b7('0x17')](_0x18f50b[_0xb1b7('0x27')],'id');return _0x18f50b['removeOpenchannelInteractions'](_0x562e76)[_0xb1b7('0x16')](function(){for(var _0x1cc97c=0x0;_0x1cc97c<_0x562e76['length'];_0x1cc97c+=0x1){_0x1ed4b6[_0xb1b7('0x4d')](_0xb1b7('0x54'),{'OpenchannelInteractionId':Number(_0x562e76[_0x1cc97c]),'UserId':Number(_0x18f50b['id'])});}});}})[_0xb1b7('0x16')](function(){if(_0x53b66c===_0xb1b7('0x4b')){var _0xf92ac=_[_0xb1b7('0x17')](_0x18f50b[_0xb1b7('0x29')],'id');return _0x18f50b[_0xb1b7('0x55')](_0xf92ac)[_0xb1b7('0x16')](function(){for(var _0xd880a6=0x0;_0xd880a6<_0xf92ac[_0xb1b7('0x1d')];_0xd880a6+=0x1){_0x1ed4b6[_0xb1b7('0x4d')](_0xb1b7('0x56'),{'WhatsappInteractionId':Number(_0xf92ac[_0xd880a6]),'UserId':Number(_0x18f50b['id'])});}});}})['catch'](function(_0x31bf0c){_0x2ed0c1[_0xb1b7('0x57')](_0x31bf0c);});}}function onDisconnect(_0x41f85a,_0xdbe628){joinOrLeave(_0x41f85a,_0xb1b7('0x4b'),_0xdbe628);}function onConnect(_0x3ee670){joinOrLeave(_0x3ee670,'join');}exports[_0xb1b7('0x58')]=function(_0x3e15c7){_0x3e15c7['on'](_0xb1b7('0x59'),function(_0x58e293){_0x58e293['setMaxListeners'](0x0);_0x58e293[_0xb1b7('0x5a')]=_0x58e293[_0xb1b7('0x5b')]['connection']['remoteAddress']+':'+_0x58e293['request'][_0xb1b7('0x59')]['remotePort'];_0x58e293[_0xb1b7('0x5c')]=new Date();_0x58e293[_0xb1b7('0x57')]=function(_0x1ecf38){console['log']('SocketIO\x20'+_0x58e293[_0xb1b7('0x5d')][_0xb1b7('0x4')]+'['+_0x58e293[_0xb1b7('0x5a')]+']',_0x1ecf38);};_0x58e293['on'](_0xb1b7('0x5e'),function(){onDisconnect(_0x58e293,_0x3e15c7);_0x58e293[_0xb1b7('0x57')]('DISCONNECTED');});onConnect(_0x58e293);_0x58e293[_0xb1b7('0x57')]('CONNECTED');});};