Built motion from commit (unavailable).|2.5.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 _0x581f=['push','LogoutUser','findOne','destroy','LoginUser','mobile','phone','count','agent','../../config/license/util','getLicense','agents','Maximum\x20login\x20has\x20been\x20reached','upsert','loginInPause','all','merge','LOGIN','callysquare','LOGININPAUSE','bulkCreate','concat','util','moment','../../mysqldb','../../config/utils','../../config/logger','client','http','defaults','redis','socket.io-emitter','./user.socket','register','request','then','User,\x20%s,\x20%s','User,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','message','info','catch','findAll','options','raw','where','attributes','limit','include','map','model','GetUser','debug','ShowUser','User','PauseUser','internal','UserVoiceQueueRt','update','MemberReport','name','PAUSE','length','getChannels','forEach','isNil','interface','format','SIP/%s','YYYY-MM-DD\x20HH:mm:ss','body','type','role','DEFAULT\x20PAUSE','channel','system','mail','chat','sms','whatsapp','find','set','%s%s','Pause'];(function(_0x4e9d3c,_0x1f4b98){var _0x2d0825=function(_0x384676){while(--_0x384676){_0x4e9d3c['push'](_0x4e9d3c['shift']());}};_0x2d0825(++_0x1f4b98);}(_0x581f,0x6d));var _0xf581=function(_0x14bf67,_0x49df21){_0x14bf67=_0x14bf67-0x0;var _0xe26936=_0x581f[_0x14bf67];return _0xe26936;};'use strict';var _=require('lodash');var util=require(_0xf581('0x0'));var moment=require(_0xf581('0x1'));var BPromise=require('bluebird');var rs=require('randomstring');var fs=require('fs');var Redis=require('ioredis');var db=require(_0xf581('0x2'))['db'];var utils=require(_0xf581('0x3'));var logger=require(_0xf581('0x4'))('rpc');var config=require('../../config/environment');var jayson=require('jayson/promise');var client=jayson[_0xf581('0x5')][_0xf581('0x6')]({'port':0x232a});config['redis']=_[_0xf581('0x7')](config[_0xf581('0x8')],{'host':'localhost','port':0x18eb});var socket=require(_0xf581('0x9'))(new Redis(config['redis']));require(_0xf581('0xa'))[_0xf581('0xb')](socket);function respondWithRpcPromise(_0x19ba01,_0x44f3d7,_0x52f0e7){return new BPromise(function(_0x303147,_0x119399){return client[_0xf581('0xc')](_0x19ba01,_0x52f0e7)[_0xf581('0xd')](function(_0x3160e4){logger['info'](_0xf581('0xe'),_0x44f3d7,'request\x20sent');logger['debug'](_0xf581('0xf'),_0x44f3d7,_0xf581('0x10'),JSON[_0xf581('0x11')](_0x3160e4));if(_0x3160e4['error']){if(_0x3160e4[_0xf581('0x12')][_0xf581('0x13')]===0x1f4){logger[_0xf581('0x12')](_0xf581('0xe'),_0x44f3d7,_0x3160e4[_0xf581('0x12')][_0xf581('0x14')]);return _0x119399(_0x3160e4[_0xf581('0x12')][_0xf581('0x14')]);}logger[_0xf581('0x12')](_0xf581('0xe'),_0x44f3d7,_0x3160e4[_0xf581('0x12')][_0xf581('0x14')]);return _0x303147(_0x3160e4[_0xf581('0x12')][_0xf581('0x14')]);}else{logger[_0xf581('0x15')](_0xf581('0xe'),_0x44f3d7,_0xf581('0x10'));_0x303147(_0x3160e4['result'][_0xf581('0x14')]);}})[_0xf581('0x16')](function(_0x7e1220){logger[_0xf581('0x12')](_0xf581('0xe'),_0x44f3d7,_0x7e1220);_0x119399(_0x7e1220);});});}exports['GetUser']=function(_0x1a5a7a){var _0x163fa4=this;return new Promise(function(_0x4de875,_0xf0df36){return db['User'][_0xf581('0x17')]({'raw':_0x1a5a7a[_0xf581('0x18')]?_0x1a5a7a['options'][_0xf581('0x19')]===undefined?!![]:![]:!![],'where':_0x1a5a7a[_0xf581('0x18')]?_0x1a5a7a[_0xf581('0x18')][_0xf581('0x1a')]||null:null,'attributes':_0x1a5a7a['options']?_0x1a5a7a['options'][_0xf581('0x1b')]||null:null,'limit':_0x1a5a7a['options']?_0x1a5a7a[_0xf581('0x18')][_0xf581('0x1c')]||null:null,'include':_0x1a5a7a['options']?_0x1a5a7a[_0xf581('0x18')][_0xf581('0x1d')]?_[_0xf581('0x1e')](_0x1a5a7a['options']['include'],function(_0x582b25){return{'model':db[_0x582b25[_0xf581('0x1f')]],'as':_0x582b25['as'],'attributes':_0x582b25[_0xf581('0x1b')],'include':_0x582b25[_0xf581('0x1d')]?_[_0xf581('0x1e')](_0x582b25[_0xf581('0x1d')],function(_0x3889bb){return{'model':db[_0x3889bb[_0xf581('0x1f')]],'as':_0x3889bb['as'],'attributes':_0x3889bb[_0xf581('0x1b')],'include':_0x3889bb[_0xf581('0x1d')]?_[_0xf581('0x1e')](_0x3889bb['include'],function(_0xb10de0){return{'model':db[_0xb10de0[_0xf581('0x1f')]],'as':_0xb10de0['as'],'attributes':_0xb10de0[_0xf581('0x1b')]};}):[]};}):[]};}):[]:[]})[_0xf581('0xd')](function(_0x13a0bb){logger['info'](_0xf581('0x20'),_0x1a5a7a);logger[_0xf581('0x21')](_0xf581('0x20'),_0x1a5a7a,JSON['stringify'](_0x13a0bb));_0x4de875(_0x13a0bb);})[_0xf581('0x16')](function(_0x2d7662){logger['error'](_0xf581('0x20'),_0x2d7662[_0xf581('0x14')],_0x1a5a7a);_0xf0df36(_0x163fa4[_0xf581('0x12')](0x1f4,_0x2d7662['message']));});});};exports[_0xf581('0x22')]=function(_0x587a6d){var _0xca0aae=this;return new Promise(function(_0x1aab93,_0xf59f63){return db[_0xf581('0x23')]['find']({'raw':_0x587a6d[_0xf581('0x18')]?_0x587a6d[_0xf581('0x18')][_0xf581('0x19')]===undefined?!![]:![]:!![],'where':_0x587a6d[_0xf581('0x18')]?_0x587a6d[_0xf581('0x18')][_0xf581('0x1a')]||null:null,'attributes':_0x587a6d[_0xf581('0x18')]?_0x587a6d['options'][_0xf581('0x1b')]||null:null,'include':_0x587a6d[_0xf581('0x18')]?_0x587a6d['options'][_0xf581('0x1d')]?_[_0xf581('0x1e')](_0x587a6d[_0xf581('0x18')]['include'],function(_0x22ce51){return{'model':db[_0x22ce51['model']],'as':_0x22ce51['as'],'attributes':_0x22ce51[_0xf581('0x1b')],'include':_0x22ce51['include']?_['map'](_0x22ce51[_0xf581('0x1d')],function(_0x3aa527){return{'model':db[_0x3aa527['model']],'as':_0x3aa527['as'],'attributes':_0x3aa527[_0xf581('0x1b')],'include':_0x3aa527['include']?_['map'](_0x3aa527['include'],function(_0x58d92f){return{'model':db[_0x58d92f['model']],'as':_0x58d92f['as'],'attributes':_0x58d92f[_0xf581('0x1b')]};}):[]};}):[]};}):[]:[]})[_0xf581('0xd')](function(_0x2efa2a){logger[_0xf581('0x15')]('ShowUser',_0x587a6d);logger[_0xf581('0x21')](_0xf581('0x22'),_0x587a6d,JSON[_0xf581('0x11')](_0x2efa2a));_0x1aab93(_0x2efa2a);})[_0xf581('0x16')](function(_0x4201ec){logger[_0xf581('0x12')](_0xf581('0x22'),_0x4201ec['message'],_0x587a6d);_0xf59f63(_0xca0aae[_0xf581('0x12')](0x1f4,_0x4201ec[_0xf581('0x14')]));});});};exports[_0xf581('0x24')]=function(_0x5c20b1){var _0x2fcb24=this;return new Promise(function(_0x21eb08,_0x34c9ff){var _0xf1d6c3;return db[_0xf581('0x23')]['find']({'where':_0x5c20b1[_0xf581('0x18')][_0xf581('0x1a')]||{},'attributes':['id','name','role',_0xf581('0x25'),'interface']})['then'](function(_0x337d6e){if(_0x337d6e){_0xf1d6c3=_0x337d6e;return db[_0xf581('0x26')][_0xf581('0x27')]({'paused':0x1},{'where':{'UserId':_0xf1d6c3['id']},'individualHooks':!![]});}})['then'](function(){if(_0xf1d6c3){return db[_0xf581('0x28')]['findAll']({'where':{'membername':_0xf1d6c3[_0xf581('0x29')],'type':_0xf581('0x2a'),'exitAt':null}});}})[_0xf581('0xd')](function(_0x3eda5a){if(_0xf1d6c3){if(_0x3eda5a&&_0x3eda5a['length']){var _0x4b0406=moment()['format']('YYYY-MM-DD\x20HH:mm:ss');for(var _0x57ce53=0x0,_0x43273b=_0x3eda5a[_0xf581('0x2b')];_0x57ce53<_0x43273b;_0x57ce53++){_0x3eda5a[_0x57ce53]['updateAttributes']({'exitAt':_0x4b0406});}}}})[_0xf581('0xd')](function(){if(_0xf1d6c3){var _0xa5e794=[];var _0x5436db=utils[_0xf581('0x2c')]();_0x5436db[_0xf581('0x2d')](function(_0x20baeb){_0xa5e794['push']({'channel':_0x20baeb,'membername':_0xf1d6c3[_0xf581('0x29')],'interface':_[_0xf581('0x2e')](_0xf1d6c3[_0xf581('0x2f')])?util[_0xf581('0x30')](_0xf581('0x31'),_0xf1d6c3[_0xf581('0x29')]):_0xf1d6c3[_0xf581('0x2f')],'type':_0xf581('0x2a'),'enterAt':moment()['format'](_0xf581('0x32')),'data1':_0x5c20b1[_0xf581('0x33')][_0xf581('0x34')]||'DEFAULT\x20PAUSE','role':_0xf1d6c3[_0xf581('0x35')],'internal':_0xf1d6c3[_0xf581('0x25')],'uniqueid':_0x5c20b1['body']['uniqueid']||null});});return db[_0xf581('0x28')]['bulkCreate'](_0xa5e794,{'individualHooks':!![]});}})[_0xf581('0xd')](function(){if(_0xf1d6c3){return _0xf1d6c3[_0xf581('0x27')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x5c20b1[_0xf581('0x33')][_0xf581('0x34')]||_0xf581('0x36'),'lastPauseAt':moment()['format'](_0xf581('0x32'))});}})[_0xf581('0xd')](function(_0x27fd45){_0x21eb08(_0x27fd45);})[_0xf581('0x16')](function(_0x38af95){_0x34c9ff(_0x2fcb24[_0xf581('0x12')](0x1f4,_0x38af95[_0xf581('0x14')]));});});};exports['UnpauseUser']=function(_0x3e4b99){var _0x3607c0=this;return new Promise(function(_0x588804,_0x1fe8f2){var _0x5f554e;return db['User']['findOne']({'where':_0x3e4b99[_0xf581('0x18')][_0xf581('0x1a')]||{},'attributes':['id',_0xf581('0x29')]})['then'](function(_0x1eb1a8){if(_0x1eb1a8){_0x5f554e=_0x1eb1a8;if(_0x3e4b99[_0xf581('0x33')][_0xf581('0x37')]&&!utils['isValidChannel'](_0x3e4b99[_0xf581('0x33')]['channel'])){throw new Error('Unknown\x20channel');}switch(_0x3e4b99[_0xf581('0x33')][_0xf581('0x37')]){case'voice':return db[_0xf581('0x26')][_0xf581('0x27')]({'paused':0x0},{'where':{'UserId':_0x5f554e['id']}})['then'](function(){return db[_0xf581('0x28')]['find']({'where':{'channel':_0x3e4b99[_0xf581('0x33')]['channel'],'membername':_0x5f554e[_0xf581('0x29')],'type':'PAUSE','exitAt':null}});})['then'](function(_0x5d0c2d){if(_0x5d0c2d){return _0x5d0c2d[_0xf581('0x27')]({'exitAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'data2':_0xf581('0x38')},{'individualHooks':!![]});}return null;})['then'](function(){return _0x5f554e['update']({'voicePause':![]});});case _0xf581('0x39'):case _0xf581('0x3a'):case'fax':case _0xf581('0x3b'):case'openchannel':case _0xf581('0x3c'):return db[_0xf581('0x28')][_0xf581('0x3d')]({'where':{'channel':_0x3e4b99[_0xf581('0x33')][_0xf581('0x37')],'membername':_0x5f554e[_0xf581('0x29')],'type':'PAUSE','exitAt':null}})[_0xf581('0xd')](function(_0x43055d){return _0x43055d[_0xf581('0x27')]({'exitAt':moment()[_0xf581('0x30')](_0xf581('0x32')),'data2':_0xf581('0x38')},{'individualHooks':!![]});})[_0xf581('0xd')](function(){var _0x29f732={};_[_0xf581('0x3e')](_0x29f732,util[_0xf581('0x30')](_0xf581('0x3f'),_0x3e4b99[_0xf581('0x33')][_0xf581('0x37')],_0xf581('0x40')),![]);return _0x5f554e[_0xf581('0x27')](_0x29f732);});default:return db[_0xf581('0x26')][_0xf581('0x27')]({'paused':0x0},{'where':{'UserId':_0x5f554e['id']}})['then'](function(){return db[_0xf581('0x28')][_0xf581('0x17')]({'where':{'membername':_0x5f554e[_0xf581('0x29')],'type':'PAUSE','exitAt':null}});})[_0xf581('0xd')](function(_0x58207f){var _0x3b67f1=[];_0x58207f[_0xf581('0x2d')](function(_0x5dbcbc){_0x3b67f1[_0xf581('0x41')](_0x5dbcbc[_0xf581('0x27')]({'exitAt':moment()[_0xf581('0x30')](_0xf581('0x32')),'data2':_0xf581('0x38')}));});return BPromise['all'](_0x3b67f1);})[_0xf581('0xd')](function(){return _0x5f554e['update']({'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![],'whatsappPause':![]});});}}})[_0xf581('0xd')](function(_0x3a92ce){_0x588804(_0x3a92ce);})[_0xf581('0x16')](function(_0x29ffd8){_0x1fe8f2(_0x3607c0[_0xf581('0x12')](0x1f4,_0x29ffd8[_0xf581('0x14')]));});});};exports[_0xf581('0x42')]=function(_0x798445){var _0x251362=this;return new Promise(function(_0x3c1610,_0x25da8f){var _0x460629;return db[_0xf581('0x23')][_0xf581('0x43')]({'where':_0x798445[_0xf581('0x18')]['where']||{},'attributes':['id','name']})[_0xf581('0xd')](function(_0xcdd713){if(_0xcdd713){_0x460629=_0xcdd713;return db['UserVoiceQueueRt'][_0xf581('0x44')]({'where':{'membername':_0x460629[_0xf581('0x29')]},'individualHooks':!![]});}})['then'](function(){if(_0x460629){return db['MemberReport'][_0xf581('0x17')]({'where':{'membername':_0x460629[_0xf581('0x29')],'exitAt':null}});}})['then'](function(_0x580b81){if(_0x460629){if(_0x580b81&&_0x580b81[_0xf581('0x2b')]){var _0x866700=[];_0x580b81['forEach'](function(_0x253800){_0x866700[_0xf581('0x41')](_0x253800[_0xf581('0x27')]({'exitAt':moment()['format'](_0xf581('0x32'))}));});return BPromise['all'](_0x866700);}}})[_0xf581('0xd')](function(){if(_0x460629){return _0x460629[_0xf581('0x27')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0xf581('0xd')](function(_0x5e6b4a){_0x3c1610(_0x5e6b4a);})[_0xf581('0x16')](function(_0x155f57){_0x25da8f(_0x251362[_0xf581('0x12')](0x1f4,_0x155f57[_0xf581('0x14')]));});});};exports[_0xf581('0x45')]=function(_0x5bd0fa){var _0x1059bc=this;return new Promise(function(_0x284e6f,_0x40ba15){var _0x3265d2;var _0x30131a;var _0x16fc82;var _0x584bfc;return db[_0xf581('0x23')][_0xf581('0x43')]({'where':_0x5bd0fa[_0xf581('0x18')][_0xf581('0x1a')]||{},'attributes':['id',_0xf581('0x29'),_0xf581('0x35'),_0xf581('0x46'),_0xf581('0x47'),'internal','loginInPause',_0xf581('0x2f')]})[_0xf581('0xd')](function(_0x543ec4){if(_0x543ec4){_0x3265d2=_0x543ec4;_0x584bfc=_0x5bd0fa[_0xf581('0x33')]['interface']?_0x5bd0fa[_0xf581('0x33')]['interface']:util['format']('SIP/%s',_0x3265d2[_0xf581('0x29')]);return db['User'][_0xf581('0x48')]({'where':{'role':_0xf581('0x49'),'online':!![],'id':{'$ne':_0x3265d2['id']}}});}})[_0xf581('0xd')](function(_0x2bd9d2){if(_0x2bd9d2){_0x16fc82=_0x2bd9d2;return require(_0xf581('0x4a'))[_0xf581('0x4b')]();}})['then'](function(_0x3e83ad){if(_0x3e83ad&&_0x3e83ad[_0xf581('0x4c')]&&_0x16fc82>=_0x3e83ad['agents']){throw new Error(_0xf581('0x4d'));}})[_0xf581('0xd')](function(){if(_0x3265d2){return db[_0xf581('0x26')][_0xf581('0x44')]({'where':{'membername':_0x3265d2[_0xf581('0x29')]},'individualHooks':!![]});}})[_0xf581('0xd')](function(){if(_0x3265d2){return _0x3265d2['getVoiceQueues']({'raw':!![]});}})[_0xf581('0xd')](function(_0x3d5f21){if(_0x3265d2){if(_0x3d5f21){var _0xa1ac7c=[];_0x30131a=_0x3d5f21;if(_0x30131a[_0xf581('0x2b')]){for(var _0xc787f6=0x0;_0xc787f6<_0x30131a[_0xf581('0x2b')];_0xc787f6++){_0xa1ac7c[_0xf581('0x41')](db[_0xf581('0x26')][_0xf581('0x4e')]({'membername':_0x3265d2[_0xf581('0x29')],'UserId':_0x3265d2['id'],'queue_name':_0x30131a[_0xc787f6]['name'],'VoiceQueueId':_0x30131a[_0xc787f6]['id'],'interface':_0x584bfc,'paused':_0x3265d2[_0xf581('0x4f')]||![],'penalty':_0x30131a[_0xc787f6]['UserVoiceQueue.penalty']||0x0}));}return BPromise['all'](_0xa1ac7c);}}}})[_0xf581('0xd')](function(){if(_0x3265d2){return db[_0xf581('0x28')][_0xf581('0x17')]({'where':{'membername':_0x3265d2[_0xf581('0x29')],'exitAt':null}});}})[_0xf581('0xd')](function(_0x4e1cfe){if(_0x3265d2){if(_0x4e1cfe&&_0x4e1cfe[_0xf581('0x2b')]){var _0x4c58e6=[];_0x4e1cfe[_0xf581('0x2d')](function(_0x62c87b){_0x4c58e6[_0xf581('0x41')](_0x62c87b[_0xf581('0x27')]({'exitAt':moment()[_0xf581('0x30')](_0xf581('0x32'))}));});return BPromise[_0xf581('0x50')](_0x4c58e6);}}})[_0xf581('0xd')](function(_0x4d088f){if(_0x3265d2){var _0x56c48c=[];var _0x1cb8d1=[];var _0x49c5b3=utils[_0xf581('0x2c')]();var _0x142ffd={'membername':_0x3265d2['name'],'interface':_0x584bfc,'enterAt':moment()[_0xf581('0x30')](_0xf581('0x32')),'role':_0x3265d2[_0xf581('0x35')],'internal':_0x3265d2[_0xf581('0x25')]};_0x49c5b3[_0xf581('0x2d')](function(_0xa6b981){_0x56c48c[_0xf581('0x41')](_[_0xf581('0x51')]({'channel':_0xa6b981,'type':_0xf581('0x52'),'data1':_0xf581('0x53'),'data2':_0x584bfc},_0x142ffd));if(_0x3265d2[_0xf581('0x4f')]){_0x1cb8d1[_0xf581('0x41')](_[_0xf581('0x51')]({'channel':_0xa6b981,'type':_0xf581('0x2a'),'data1':_0xf581('0x54')},_0x142ffd));}});return db[_0xf581('0x28')][_0xf581('0x55')](_0x56c48c[_0xf581('0x56')](_0x1cb8d1),{'individualHooks':!![]});}})[_0xf581('0xd')](function(){if(_0x3265d2){return _0x3265d2[_0xf581('0x27')]({'online':!![],'lastLoginAt':moment()['format'](_0xf581('0x32')),'voicePause':_0x3265d2[_0xf581('0x4f')]||![],'mailPause':_0x3265d2['loginInPause']||![],'chatPause':_0x3265d2['loginInPause']||![],'faxPause':_0x3265d2[_0xf581('0x4f')]||![],'smsPause':_0x3265d2[_0xf581('0x4f')]||![],'openchannelPause':_0x3265d2['loginInPause']||![],'pauseType':_0x3265d2['loginInPause']?_0xf581('0x54'):null,'lastPauseAt':moment()[_0xf581('0x30')](_0xf581('0x32')),'interface':_0x584bfc});}})[_0xf581('0xd')](function(_0x183398){_0x284e6f(_0x183398);})[_0xf581('0x16')](function(_0x102e86){_0x40ba15(_0x1059bc['error'](0x1f4,_0x102e86['message']));});});};