Built motion from commit dc14b02d.|2.6.27
[motion2.git] / server / api / user / user.rpc.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 _0x6ef4=['model','then','info','GetUser','debug','stringify','error','message','ShowUser','catch','PauseUser','name','role','internal','format','UserVoiceQueueRt','update','MemberReport','length','YYYY-MM-DD\x20HH:mm:ss','getChannels','forEach','push','isNil','interface','SIP/%s','type','DEFAULT\x20PAUSE','body','uniqueid','bulkCreate','UnpauseUser','resolve','channel','isValidChannel','Channel\x20parameter\x20%s\x20is\x20invalid','findOne','voicePause','mailPause','chatPause','faxPause','openchannelPause','smsPause','lastPauseAt','User\x20with\x20id\x20%s\x20not\x20found','unpause','LogoutUser','all','LoginUser','phone','loginInPause','count','agent','../../config/license/util','getLicense','agents','Maximum\x20login\x20has\x20been\x20reached','destroy','upsert','UserVoiceQueue.penalty','merge','LOGIN','callysquare','PAUSE','LOGININPAUSE','lodash','util','bluebird','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','redis','localhost','socket.io-emitter','./user.socket','User','options','raw','where','attributes','limit','include','map'];(function(_0xe22ceb,_0x27dc83){var _0x45bb58=function(_0x4e1177){while(--_0x4e1177){_0xe22ceb['push'](_0xe22ceb['shift']());}};_0x45bb58(++_0x27dc83);}(_0x6ef4,0x97));var _0x46ef=function(_0x3c27df,_0xc6d32b){_0x3c27df=_0x3c27df-0x0;var _0x56f1f0=_0x6ef4[_0x3c27df];return _0x56f1f0;};'use strict';var _=require(_0x46ef('0x0'));var util=require(_0x46ef('0x1'));var moment=require('moment');var BPromise=require(_0x46ef('0x2'));var Redis=require(_0x46ef('0x3'));var db=require(_0x46ef('0x4'))['db'];var utils=require(_0x46ef('0x5'));var logger=require(_0x46ef('0x6'))(_0x46ef('0x7'));var config=require(_0x46ef('0x8'));config['redis']=_['defaults'](config[_0x46ef('0x9')],{'host':_0x46ef('0xa'),'port':0x18eb});var socket=require(_0x46ef('0xb'))(new Redis(config['redis']));require(_0x46ef('0xc'))['register'](socket);exports['GetUser']=function(_0x2aadfe){var _0x29855c=this;return new Promise(function(_0x4bc023,_0x21e58d){return db[_0x46ef('0xd')]['findAll']({'raw':_0x2aadfe[_0x46ef('0xe')]?_0x2aadfe[_0x46ef('0xe')][_0x46ef('0xf')]===undefined?!![]:![]:!![],'where':_0x2aadfe[_0x46ef('0xe')]?_0x2aadfe[_0x46ef('0xe')][_0x46ef('0x10')]||null:null,'attributes':_0x2aadfe[_0x46ef('0xe')]?_0x2aadfe[_0x46ef('0xe')][_0x46ef('0x11')]||null:null,'limit':_0x2aadfe['options']?_0x2aadfe[_0x46ef('0xe')][_0x46ef('0x12')]||null:null,'include':_0x2aadfe['options']?_0x2aadfe[_0x46ef('0xe')][_0x46ef('0x13')]?_[_0x46ef('0x14')](_0x2aadfe[_0x46ef('0xe')]['include'],function(_0x20720d){return{'model':db[_0x20720d[_0x46ef('0x15')]],'as':_0x20720d['as'],'attributes':_0x20720d['attributes'],'include':_0x20720d[_0x46ef('0x13')]?_[_0x46ef('0x14')](_0x20720d[_0x46ef('0x13')],function(_0x5b4308){return{'model':db[_0x5b4308['model']],'as':_0x5b4308['as'],'attributes':_0x5b4308[_0x46ef('0x11')],'include':_0x5b4308[_0x46ef('0x13')]?_[_0x46ef('0x14')](_0x5b4308['include'],function(_0x1df21c){return{'model':db[_0x1df21c[_0x46ef('0x15')]],'as':_0x1df21c['as'],'attributes':_0x1df21c[_0x46ef('0x11')]};}):[]};}):[]};}):[]:[]})[_0x46ef('0x16')](function(_0x5af6aa){logger[_0x46ef('0x17')](_0x46ef('0x18'),_0x2aadfe);logger[_0x46ef('0x19')]('GetUser',_0x2aadfe,JSON[_0x46ef('0x1a')](_0x5af6aa));_0x4bc023(_0x5af6aa);})['catch'](function(_0x57657a){logger[_0x46ef('0x1b')](_0x46ef('0x18'),_0x57657a[_0x46ef('0x1c')],_0x2aadfe);_0x21e58d(_0x29855c[_0x46ef('0x1b')](0x1f4,_0x57657a['message']));});});};exports[_0x46ef('0x1d')]=function(_0x50e237){var _0x2482ef=this;return new Promise(function(_0x492dd2,_0x3625a3){return db[_0x46ef('0xd')]['find']({'raw':_0x50e237[_0x46ef('0xe')]?_0x50e237[_0x46ef('0xe')]['raw']===undefined?!![]:![]:!![],'where':_0x50e237[_0x46ef('0xe')]?_0x50e237[_0x46ef('0xe')]['where']||null:null,'attributes':_0x50e237[_0x46ef('0xe')]?_0x50e237[_0x46ef('0xe')][_0x46ef('0x11')]||null:null,'include':_0x50e237[_0x46ef('0xe')]?_0x50e237[_0x46ef('0xe')][_0x46ef('0x13')]?_[_0x46ef('0x14')](_0x50e237['options'][_0x46ef('0x13')],function(_0xaf824b){return{'model':db[_0xaf824b[_0x46ef('0x15')]],'as':_0xaf824b['as'],'attributes':_0xaf824b[_0x46ef('0x11')],'include':_0xaf824b[_0x46ef('0x13')]?_[_0x46ef('0x14')](_0xaf824b['include'],function(_0x6f09c5){return{'model':db[_0x6f09c5['model']],'as':_0x6f09c5['as'],'attributes':_0x6f09c5[_0x46ef('0x11')],'include':_0x6f09c5[_0x46ef('0x13')]?_['map'](_0x6f09c5[_0x46ef('0x13')],function(_0x5e1081){return{'model':db[_0x5e1081['model']],'as':_0x5e1081['as'],'attributes':_0x5e1081['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x518ec3){logger['info']('ShowUser',_0x50e237);logger[_0x46ef('0x19')](_0x46ef('0x1d'),_0x50e237,JSON[_0x46ef('0x1a')](_0x518ec3));_0x492dd2(_0x518ec3);})[_0x46ef('0x1e')](function(_0x44ca6b){logger['error'](_0x46ef('0x1d'),_0x44ca6b[_0x46ef('0x1c')],_0x50e237);_0x3625a3(_0x2482ef[_0x46ef('0x1b')](0x1f4,_0x44ca6b[_0x46ef('0x1c')]));});});};exports[_0x46ef('0x1f')]=function(_0x2309a7){var _0x3f1a30;return db[_0x46ef('0xd')]['find']({'where':_0x2309a7[_0x46ef('0xe')][_0x46ef('0x10')]||{},'attributes':['id',_0x46ef('0x20'),_0x46ef('0x21'),_0x46ef('0x22'),'interface']})[_0x46ef('0x16')](function(_0x287e32){if(!_0x287e32){throw new Error(util[_0x46ef('0x23')]('User\x20with\x20id\x20%s\x20not\x20found',_0x2309a7['params']['id']));}_0x3f1a30=_0x287e32;return db[_0x46ef('0x24')][_0x46ef('0x25')]({'paused':0x1},{'where':{'UserId':_0x3f1a30['id']},'individualHooks':!![]});})['then'](function(){return db[_0x46ef('0x26')]['findAll']({'where':{'membername':_0x3f1a30[_0x46ef('0x20')],'type':'PAUSE','exitAt':null}});})[_0x46ef('0x16')](function(_0x293855){if(_0x293855&&_0x293855[_0x46ef('0x27')]){var _0x73c8e4=moment()['format'](_0x46ef('0x28'));for(var _0x21b042=0x0,_0xc915d9=_0x293855[_0x46ef('0x27')];_0x21b042<_0xc915d9;_0x21b042++){_0x293855[_0x21b042]['updateAttributes']({'exitAt':_0x73c8e4});}}})[_0x46ef('0x16')](function(){var _0xa09829=[];var _0x11665c=utils[_0x46ef('0x29')]();_0x11665c[_0x46ef('0x2a')](function(_0x15965f){_0xa09829[_0x46ef('0x2b')]({'channel':_0x15965f,'membername':_0x3f1a30['name'],'interface':_[_0x46ef('0x2c')](_0x3f1a30[_0x46ef('0x2d')])?util[_0x46ef('0x23')](_0x46ef('0x2e'),_0x3f1a30[_0x46ef('0x20')]):_0x3f1a30['interface'],'type':'PAUSE','enterAt':moment()[_0x46ef('0x23')](_0x46ef('0x28')),'data1':_0x2309a7['body'][_0x46ef('0x2f')]||_0x46ef('0x30'),'role':_0x3f1a30[_0x46ef('0x21')],'internal':_0x3f1a30[_0x46ef('0x22')],'uniqueid':_0x2309a7[_0x46ef('0x31')][_0x46ef('0x32')]||null});});return db[_0x46ef('0x26')][_0x46ef('0x33')](_0xa09829,{'individualHooks':!![]});})[_0x46ef('0x16')](function(){return _0x3f1a30[_0x46ef('0x25')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x2309a7[_0x46ef('0x31')][_0x46ef('0x2f')]||_0x46ef('0x30'),'lastPauseAt':moment()[_0x46ef('0x23')](_0x46ef('0x28'))});})[_0x46ef('0x1e')](function(_0x4d4b79){throw _0x4d4b79;});};exports[_0x46ef('0x34')]=function(_0x54a558){return Promise[_0x46ef('0x35')]()[_0x46ef('0x16')](function(){if(_0x54a558[_0x46ef('0x31')][_0x46ef('0x36')]&&!utils[_0x46ef('0x37')](_0x54a558[_0x46ef('0x31')]['channel'])){throw new Error(util[_0x46ef('0x23')](_0x46ef('0x38'),_0x54a558[_0x46ef('0x31')][_0x46ef('0x36')]));}return db[_0x46ef('0xd')][_0x46ef('0x39')]({'where':_0x54a558[_0x46ef('0xe')][_0x46ef('0x10')]||{},'attributes':['id',_0x46ef('0x20'),_0x46ef('0x21'),_0x46ef('0x22'),_0x46ef('0x2d'),'online',_0x46ef('0x3a'),_0x46ef('0x3b'),_0x46ef('0x3c'),_0x46ef('0x3d'),_0x46ef('0x3e'),_0x46ef('0x3f'),'whatsappPause','pauseType',_0x46ef('0x40')]});})[_0x46ef('0x16')](function(_0x1f0336){if(!_0x1f0336){throw new Error(util[_0x46ef('0x23')](_0x46ef('0x41'),_0x54a558['params']['id']));}return _0x1f0336[_0x46ef('0x42')](_0x54a558[_0x46ef('0x31')]['channel'],'system');})['catch'](function(_0x4de610){throw _0x4de610;});};exports[_0x46ef('0x43')]=function(_0x519dc8){var _0x23631a=this;return new Promise(function(_0x48c9e1,_0xaa44b3){var _0x5b51bd;return db[_0x46ef('0xd')]['findOne']({'where':_0x519dc8[_0x46ef('0xe')][_0x46ef('0x10')]||{},'attributes':['id',_0x46ef('0x20')]})[_0x46ef('0x16')](function(_0x3403aa){if(_0x3403aa){_0x5b51bd=_0x3403aa;return db['UserVoiceQueueRt']['destroy']({'where':{'membername':_0x5b51bd[_0x46ef('0x20')]},'individualHooks':!![]});}})[_0x46ef('0x16')](function(){if(_0x5b51bd){return db[_0x46ef('0x26')]['findAll']({'where':{'membername':_0x5b51bd[_0x46ef('0x20')],'exitAt':null}});}})[_0x46ef('0x16')](function(_0x32bd06){if(_0x5b51bd){if(_0x32bd06&&_0x32bd06[_0x46ef('0x27')]){var _0x3d0a9b=[];_0x32bd06[_0x46ef('0x2a')](function(_0x185da4){_0x3d0a9b[_0x46ef('0x2b')](_0x185da4[_0x46ef('0x25')]({'exitAt':moment()[_0x46ef('0x23')]('YYYY-MM-DD\x20HH:mm:ss')}));});return BPromise[_0x46ef('0x44')](_0x3d0a9b);}}})[_0x46ef('0x16')](function(){if(_0x5b51bd){return _0x5b51bd['update']({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0x46ef('0x16')](function(_0x4a72cb){_0x48c9e1(_0x4a72cb);})['catch'](function(_0x253ab6){_0xaa44b3(_0x23631a['error'](0x1f4,_0x253ab6[_0x46ef('0x1c')]));});});};exports[_0x46ef('0x45')]=function(_0x4ec12f){var _0x5a645b=this;return new Promise(function(_0x5c2480,_0x46fd4f){var _0x58c464;var _0x4eec8f;var _0x30e2ab;var _0x5a1749;return db[_0x46ef('0xd')][_0x46ef('0x39')]({'where':_0x4ec12f[_0x46ef('0xe')][_0x46ef('0x10')]||{},'attributes':['id',_0x46ef('0x20'),_0x46ef('0x21'),'mobile',_0x46ef('0x46'),_0x46ef('0x22'),_0x46ef('0x47'),'interface']})[_0x46ef('0x16')](function(_0xbc68ea){if(_0xbc68ea){_0x58c464=_0xbc68ea;_0x5a1749=_0x4ec12f[_0x46ef('0x31')][_0x46ef('0x2d')]?_0x4ec12f[_0x46ef('0x31')][_0x46ef('0x2d')]:util['format'](_0x46ef('0x2e'),_0x58c464[_0x46ef('0x20')]);return db['User'][_0x46ef('0x48')]({'where':{'role':_0x46ef('0x49'),'online':!![],'id':{'$ne':_0x58c464['id']}}});}})[_0x46ef('0x16')](function(_0xe3c6e0){if(_0xe3c6e0){_0x30e2ab=_0xe3c6e0;return require(_0x46ef('0x4a'))[_0x46ef('0x4b')]();}})[_0x46ef('0x16')](function(_0xf6e90b){if(_0xf6e90b&&_0xf6e90b[_0x46ef('0x4c')]&&_0x30e2ab>=_0xf6e90b[_0x46ef('0x4c')]){throw new Error(_0x46ef('0x4d'));}})[_0x46ef('0x16')](function(){if(_0x58c464){return db[_0x46ef('0x24')][_0x46ef('0x4e')]({'where':{'membername':_0x58c464[_0x46ef('0x20')]},'individualHooks':!![]});}})[_0x46ef('0x16')](function(){if(_0x58c464){return _0x58c464['getVoiceQueues']({'raw':!![]});}})[_0x46ef('0x16')](function(_0x4c3960){if(_0x58c464){if(_0x4c3960){var _0x18c5a2=[];_0x4eec8f=_0x4c3960;if(_0x4eec8f[_0x46ef('0x27')]){for(var _0x13ccd8=0x0;_0x13ccd8<_0x4eec8f[_0x46ef('0x27')];_0x13ccd8++){_0x18c5a2[_0x46ef('0x2b')](db[_0x46ef('0x24')][_0x46ef('0x4f')]({'membername':_0x58c464['name'],'UserId':_0x58c464['id'],'queue_name':_0x4eec8f[_0x13ccd8][_0x46ef('0x20')],'VoiceQueueId':_0x4eec8f[_0x13ccd8]['id'],'interface':_0x5a1749,'paused':_0x58c464[_0x46ef('0x47')]||![],'penalty':_0x4eec8f[_0x13ccd8][_0x46ef('0x50')]||0x0}));}return BPromise[_0x46ef('0x44')](_0x18c5a2);}}}})[_0x46ef('0x16')](function(){if(_0x58c464){return db[_0x46ef('0x26')]['findAll']({'where':{'membername':_0x58c464['name'],'exitAt':null}});}})[_0x46ef('0x16')](function(_0x23ba6b){if(_0x58c464){if(_0x23ba6b&&_0x23ba6b[_0x46ef('0x27')]){var _0x396a3d=[];_0x23ba6b[_0x46ef('0x2a')](function(_0x2a54a3){_0x396a3d[_0x46ef('0x2b')](_0x2a54a3[_0x46ef('0x25')]({'exitAt':moment()[_0x46ef('0x23')](_0x46ef('0x28'))}));});return BPromise[_0x46ef('0x44')](_0x396a3d);}}})[_0x46ef('0x16')](function(){if(_0x58c464){var _0x265565=[];var _0x4ca764=[];var _0x169d8a=utils[_0x46ef('0x29')]();var _0x9b3ef0={'membername':_0x58c464['name'],'interface':_0x5a1749,'enterAt':moment()[_0x46ef('0x23')](_0x46ef('0x28')),'role':_0x58c464[_0x46ef('0x21')],'internal':_0x58c464[_0x46ef('0x22')]};_0x169d8a[_0x46ef('0x2a')](function(_0x519eeb){_0x265565['push'](_[_0x46ef('0x51')]({'channel':_0x519eeb,'type':_0x46ef('0x52'),'data1':_0x46ef('0x53'),'data2':_0x5a1749},_0x9b3ef0));if(_0x58c464[_0x46ef('0x47')]){_0x4ca764[_0x46ef('0x2b')](_[_0x46ef('0x51')]({'channel':_0x519eeb,'type':_0x46ef('0x54'),'data1':_0x46ef('0x55')},_0x9b3ef0));}});return db['MemberReport'][_0x46ef('0x33')](_0x265565['concat'](_0x4ca764),{'individualHooks':!![]});}})[_0x46ef('0x16')](function(){if(_0x58c464){return _0x58c464[_0x46ef('0x25')]({'online':!![],'lastLoginAt':moment()[_0x46ef('0x23')](_0x46ef('0x28')),'voicePause':_0x58c464[_0x46ef('0x47')]||![],'mailPause':_0x58c464[_0x46ef('0x47')]||![],'chatPause':_0x58c464['loginInPause']||![],'faxPause':_0x58c464[_0x46ef('0x47')]||![],'smsPause':_0x58c464[_0x46ef('0x47')]||![],'openchannelPause':_0x58c464[_0x46ef('0x47')]||![],'pauseType':_0x58c464[_0x46ef('0x47')]?_0x46ef('0x55'):null,'lastPauseAt':moment()[_0x46ef('0x23')](_0x46ef('0x28')),'interface':_0x5a1749});}})[_0x46ef('0x16')](function(_0x1a83f2){_0x5c2480(_0x1a83f2);})[_0x46ef('0x1e')](function(_0x4f187e){_0x46fd4f(_0x5a645b['error'](0x1f4,_0x4f187e[_0x46ef('0x1c')]));});});};