a71bb949d1aaddac078c0b1b17992d388ae844bb
[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 _0x443b=['Maximum\x20login\x20has\x20been\x20reached','upsert','getChannels','callysquare','merge','bulkCreate','concat','lodash','moment','bluebird','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','redis','defaults','./user.socket','register','GetUser','User','findAll','options','raw','where','attributes','limit','include','model','map','then','info','stringify','catch','error','message','ShowUser','find','debug','PauseUser','interface','format','params','UserVoiceQueueRt','update','MemberReport','PAUSE','length','YYYY-MM-DD\x20HH:mm:ss','updateAttributes','forEach','push','name','SIP/%s','body','type','DEFAULT\x20PAUSE','role','uniqueid','UnpauseUser','channel','isValidChannel','Channel\x20parameter\x20%s\x20is\x20invalid','voicePause','chatPause','faxPause','openchannelPause','smsPause','whatsappPause','pauseType','lastPauseAt','system','findOne','destroy','all','LoginUser','phone','internal','loginInPause','count','agent','getLicense','agents'];(function(_0x5eb2ca,_0x528e2d){var _0x507803=function(_0x1a1b08){while(--_0x1a1b08){_0x5eb2ca['push'](_0x5eb2ca['shift']());}};_0x507803(++_0x528e2d);}(_0x443b,0x1a6));var _0xb443=function(_0x122ef9,_0x451d2a){_0x122ef9=_0x122ef9-0x0;var _0x42fba0=_0x443b[_0x122ef9];return _0x42fba0;};'use strict';var _=require(_0xb443('0x0'));var util=require('util');var moment=require(_0xb443('0x1'));var BPromise=require(_0xb443('0x2'));var Redis=require(_0xb443('0x3'));var db=require(_0xb443('0x4'))['db'];var utils=require(_0xb443('0x5'));var logger=require(_0xb443('0x6'))(_0xb443('0x7'));var config=require('../../config/environment');config[_0xb443('0x8')]=_[_0xb443('0x9')](config[_0xb443('0x8')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0xb443('0x8')]));require(_0xb443('0xa'))[_0xb443('0xb')](socket);exports[_0xb443('0xc')]=function(_0x49b908){var _0x4b3cb4=this;return new Promise(function(_0x3237ed,_0x57a5f9){return db[_0xb443('0xd')][_0xb443('0xe')]({'raw':_0x49b908[_0xb443('0xf')]?_0x49b908['options'][_0xb443('0x10')]===undefined?!![]:![]:!![],'where':_0x49b908[_0xb443('0xf')]?_0x49b908[_0xb443('0xf')][_0xb443('0x11')]||null:null,'attributes':_0x49b908['options']?_0x49b908[_0xb443('0xf')][_0xb443('0x12')]||null:null,'limit':_0x49b908[_0xb443('0xf')]?_0x49b908['options'][_0xb443('0x13')]||null:null,'include':_0x49b908[_0xb443('0xf')]?_0x49b908['options'][_0xb443('0x14')]?_['map'](_0x49b908[_0xb443('0xf')][_0xb443('0x14')],function(_0x533fdc){return{'model':db[_0x533fdc[_0xb443('0x15')]],'as':_0x533fdc['as'],'attributes':_0x533fdc[_0xb443('0x12')],'include':_0x533fdc[_0xb443('0x14')]?_[_0xb443('0x16')](_0x533fdc['include'],function(_0x4028df){return{'model':db[_0x4028df[_0xb443('0x15')]],'as':_0x4028df['as'],'attributes':_0x4028df['attributes'],'include':_0x4028df[_0xb443('0x14')]?_[_0xb443('0x16')](_0x4028df[_0xb443('0x14')],function(_0x4968af){return{'model':db[_0x4968af[_0xb443('0x15')]],'as':_0x4968af['as'],'attributes':_0x4968af[_0xb443('0x12')]};}):[]};}):[]};}):[]:[]})[_0xb443('0x17')](function(_0x1de14e){logger[_0xb443('0x18')](_0xb443('0xc'),_0x49b908);logger['debug']('GetUser',_0x49b908,JSON[_0xb443('0x19')](_0x1de14e));_0x3237ed(_0x1de14e);})[_0xb443('0x1a')](function(_0x34c33a){logger[_0xb443('0x1b')](_0xb443('0xc'),_0x34c33a[_0xb443('0x1c')],_0x49b908);_0x57a5f9(_0x4b3cb4[_0xb443('0x1b')](0x1f4,_0x34c33a[_0xb443('0x1c')]));});});};exports[_0xb443('0x1d')]=function(_0x5aae94){var _0x300215=this;return new Promise(function(_0x3e1487,_0x49f203){return db['User'][_0xb443('0x1e')]({'raw':_0x5aae94['options']?_0x5aae94['options'][_0xb443('0x10')]===undefined?!![]:![]:!![],'where':_0x5aae94[_0xb443('0xf')]?_0x5aae94[_0xb443('0xf')][_0xb443('0x11')]||null:null,'attributes':_0x5aae94[_0xb443('0xf')]?_0x5aae94[_0xb443('0xf')][_0xb443('0x12')]||null:null,'include':_0x5aae94['options']?_0x5aae94[_0xb443('0xf')][_0xb443('0x14')]?_[_0xb443('0x16')](_0x5aae94[_0xb443('0xf')]['include'],function(_0x497084){return{'model':db[_0x497084[_0xb443('0x15')]],'as':_0x497084['as'],'attributes':_0x497084[_0xb443('0x12')],'include':_0x497084['include']?_[_0xb443('0x16')](_0x497084[_0xb443('0x14')],function(_0x353f2e){return{'model':db[_0x353f2e[_0xb443('0x15')]],'as':_0x353f2e['as'],'attributes':_0x353f2e['attributes'],'include':_0x353f2e['include']?_[_0xb443('0x16')](_0x353f2e[_0xb443('0x14')],function(_0x15fc91){return{'model':db[_0x15fc91[_0xb443('0x15')]],'as':_0x15fc91['as'],'attributes':_0x15fc91[_0xb443('0x12')]};}):[]};}):[]};}):[]:[]})[_0xb443('0x17')](function(_0x3febb4){logger[_0xb443('0x18')](_0xb443('0x1d'),_0x5aae94);logger[_0xb443('0x1f')](_0xb443('0x1d'),_0x5aae94,JSON[_0xb443('0x19')](_0x3febb4));_0x3e1487(_0x3febb4);})[_0xb443('0x1a')](function(_0x1664a4){logger[_0xb443('0x1b')]('ShowUser',_0x1664a4[_0xb443('0x1c')],_0x5aae94);_0x49f203(_0x300215[_0xb443('0x1b')](0x1f4,_0x1664a4[_0xb443('0x1c')]));});});};exports[_0xb443('0x20')]=function(_0x4f6784){var _0x4faf99;return db['User'][_0xb443('0x1e')]({'where':_0x4f6784[_0xb443('0xf')]['where']||{},'attributes':['id','name','role','internal',_0xb443('0x21')]})[_0xb443('0x17')](function(_0x212943){if(!_0x212943){throw new Error(util[_0xb443('0x22')]('User\x20with\x20id\x20%s\x20not\x20found',_0x4f6784[_0xb443('0x23')]['id']));}_0x4faf99=_0x212943;return db[_0xb443('0x24')][_0xb443('0x25')]({'paused':0x1},{'where':{'UserId':_0x4faf99['id']},'individualHooks':!![]});})[_0xb443('0x17')](function(){return db[_0xb443('0x26')]['findAll']({'where':{'membername':_0x4faf99['name'],'type':_0xb443('0x27'),'exitAt':null}});})[_0xb443('0x17')](function(_0x2bcacc){if(_0x2bcacc&&_0x2bcacc[_0xb443('0x28')]){var _0x4e7f65=moment()[_0xb443('0x22')](_0xb443('0x29'));for(var _0x599673=0x0,_0x4cde44=_0x2bcacc['length'];_0x599673<_0x4cde44;_0x599673++){_0x2bcacc[_0x599673][_0xb443('0x2a')]({'exitAt':_0x4e7f65});}}})['then'](function(){var _0x4faef6=[];var _0x3f3a2a=utils['getChannels']();_0x3f3a2a[_0xb443('0x2b')](function(_0x34f68){_0x4faef6[_0xb443('0x2c')]({'channel':_0x34f68,'membername':_0x4faf99[_0xb443('0x2d')],'interface':_['isNil'](_0x4faf99[_0xb443('0x21')])?util['format'](_0xb443('0x2e'),_0x4faf99[_0xb443('0x2d')]):_0x4faf99[_0xb443('0x21')],'type':_0xb443('0x27'),'enterAt':moment()['format'](_0xb443('0x29')),'data1':_0x4f6784[_0xb443('0x2f')][_0xb443('0x30')]||_0xb443('0x31'),'role':_0x4faf99[_0xb443('0x32')],'internal':_0x4faf99['internal'],'uniqueid':_0x4f6784['body'][_0xb443('0x33')]||null});});return db[_0xb443('0x26')]['bulkCreate'](_0x4faef6,{'individualHooks':!![]});})[_0xb443('0x17')](function(){return _0x4faf99['update']({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x4f6784[_0xb443('0x2f')][_0xb443('0x30')]||'DEFAULT\x20PAUSE','lastPauseAt':moment()['format'](_0xb443('0x29'))});})[_0xb443('0x1a')](function(_0x269ce4){throw _0x269ce4;});};exports[_0xb443('0x34')]=function(_0x5b2c36){return Promise['resolve']()[_0xb443('0x17')](function(){if(_0x5b2c36[_0xb443('0x2f')][_0xb443('0x35')]&&!utils[_0xb443('0x36')](_0x5b2c36[_0xb443('0x2f')][_0xb443('0x35')])){throw new Error(util[_0xb443('0x22')](_0xb443('0x37'),_0x5b2c36['body'][_0xb443('0x35')]));}return db[_0xb443('0xd')]['findOne']({'where':_0x5b2c36[_0xb443('0xf')][_0xb443('0x11')]||{},'attributes':['id',_0xb443('0x2d'),_0xb443('0x32'),'internal',_0xb443('0x21'),'online',_0xb443('0x38'),'mailPause',_0xb443('0x39'),_0xb443('0x3a'),_0xb443('0x3b'),_0xb443('0x3c'),_0xb443('0x3d'),_0xb443('0x3e'),_0xb443('0x3f')]});})[_0xb443('0x17')](function(_0x2495c3){if(!_0x2495c3){throw new Error(util[_0xb443('0x22')]('User\x20with\x20id\x20%s\x20not\x20found',_0x5b2c36[_0xb443('0x23')]['id']));}return _0x2495c3['unpause'](_0x5b2c36[_0xb443('0x2f')][_0xb443('0x35')],_0xb443('0x40'));})['catch'](function(_0x3641f5){throw _0x3641f5;});};exports['LogoutUser']=function(_0x1b073b){var _0x2b5833=this;return new Promise(function(_0x49ece2,_0x5b8aa5){var _0x5e053a;return db[_0xb443('0xd')][_0xb443('0x41')]({'where':_0x1b073b[_0xb443('0xf')][_0xb443('0x11')]||{},'attributes':['id',_0xb443('0x2d')]})[_0xb443('0x17')](function(_0x12f75c){if(_0x12f75c){_0x5e053a=_0x12f75c;return db[_0xb443('0x24')][_0xb443('0x42')]({'where':{'membername':_0x5e053a[_0xb443('0x2d')]},'individualHooks':!![]});}})['then'](function(){if(_0x5e053a){return db[_0xb443('0x26')][_0xb443('0xe')]({'where':{'membername':_0x5e053a[_0xb443('0x2d')],'exitAt':null}});}})[_0xb443('0x17')](function(_0x464c4f){if(_0x5e053a){if(_0x464c4f&&_0x464c4f[_0xb443('0x28')]){var _0x4d3172=[];_0x464c4f['forEach'](function(_0xbd03e3){_0x4d3172['push'](_0xbd03e3[_0xb443('0x25')]({'exitAt':moment()[_0xb443('0x22')](_0xb443('0x29'))}));});return BPromise[_0xb443('0x43')](_0x4d3172);}}})[_0xb443('0x17')](function(){if(_0x5e053a){return _0x5e053a[_0xb443('0x25')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})['then'](function(_0xd35e7d){_0x49ece2(_0xd35e7d);})[_0xb443('0x1a')](function(_0x557a86){_0x5b8aa5(_0x2b5833[_0xb443('0x1b')](0x1f4,_0x557a86[_0xb443('0x1c')]));});});};exports[_0xb443('0x44')]=function(_0x52ef6e){var _0x49a1fa=this;return new Promise(function(_0x58b038,_0x5ca5c7){var _0x2ee6d7;var _0x3fb302;var _0x19cad4;var _0x1c47ab;return db['User']['findOne']({'where':_0x52ef6e['options'][_0xb443('0x11')]||{},'attributes':['id','name','role','mobile',_0xb443('0x45'),_0xb443('0x46'),_0xb443('0x47'),_0xb443('0x21')]})[_0xb443('0x17')](function(_0x4604bd){if(_0x4604bd){_0x2ee6d7=_0x4604bd;_0x1c47ab=_0x52ef6e[_0xb443('0x2f')][_0xb443('0x21')]?_0x52ef6e[_0xb443('0x2f')][_0xb443('0x21')]:util['format'](_0xb443('0x2e'),_0x2ee6d7['name']);return db['User'][_0xb443('0x48')]({'where':{'role':_0xb443('0x49'),'online':!![],'id':{'$ne':_0x2ee6d7['id']}}});}})[_0xb443('0x17')](function(_0x40af3e){if(_0x40af3e){_0x19cad4=_0x40af3e;return require('../../config/license/util')[_0xb443('0x4a')]();}})[_0xb443('0x17')](function(_0x1b2640){if(_0x1b2640&&_0x1b2640[_0xb443('0x4b')]&&_0x19cad4>=_0x1b2640['agents']){throw new Error(_0xb443('0x4c'));}})['then'](function(){if(_0x2ee6d7){return db[_0xb443('0x24')][_0xb443('0x42')]({'where':{'membername':_0x2ee6d7['name']},'individualHooks':!![]});}})[_0xb443('0x17')](function(){if(_0x2ee6d7){return _0x2ee6d7['getVoiceQueues']({'raw':!![]});}})[_0xb443('0x17')](function(_0x3d4bc1){if(_0x2ee6d7){if(_0x3d4bc1){var _0x413bdd=[];_0x3fb302=_0x3d4bc1;if(_0x3fb302['length']){for(var _0x2f62be=0x0;_0x2f62be<_0x3fb302[_0xb443('0x28')];_0x2f62be++){_0x413bdd[_0xb443('0x2c')](db[_0xb443('0x24')][_0xb443('0x4d')]({'membername':_0x2ee6d7[_0xb443('0x2d')],'UserId':_0x2ee6d7['id'],'queue_name':_0x3fb302[_0x2f62be]['name'],'VoiceQueueId':_0x3fb302[_0x2f62be]['id'],'interface':_0x1c47ab,'paused':_0x2ee6d7[_0xb443('0x47')]||![],'penalty':_0x3fb302[_0x2f62be]['UserVoiceQueue.penalty']||0x0}));}return BPromise[_0xb443('0x43')](_0x413bdd);}}}})[_0xb443('0x17')](function(){if(_0x2ee6d7){return db[_0xb443('0x26')][_0xb443('0xe')]({'where':{'membername':_0x2ee6d7['name'],'exitAt':null}});}})[_0xb443('0x17')](function(_0x20e565){if(_0x2ee6d7){if(_0x20e565&&_0x20e565[_0xb443('0x28')]){var _0x4bfcd3=[];_0x20e565[_0xb443('0x2b')](function(_0x1c1802){_0x4bfcd3[_0xb443('0x2c')](_0x1c1802[_0xb443('0x25')]({'exitAt':moment()[_0xb443('0x22')](_0xb443('0x29'))}));});return BPromise[_0xb443('0x43')](_0x4bfcd3);}}})[_0xb443('0x17')](function(){if(_0x2ee6d7){var _0x2de23c=[];var _0x25bf31=[];var _0x3d409f=utils[_0xb443('0x4e')]();var _0x544086={'membername':_0x2ee6d7[_0xb443('0x2d')],'interface':_0x1c47ab,'enterAt':moment()[_0xb443('0x22')](_0xb443('0x29')),'role':_0x2ee6d7[_0xb443('0x32')],'internal':_0x2ee6d7[_0xb443('0x46')]};_0x3d409f['forEach'](function(_0x59a0e7){_0x2de23c[_0xb443('0x2c')](_['merge']({'channel':_0x59a0e7,'type':'LOGIN','data1':_0xb443('0x4f'),'data2':_0x1c47ab},_0x544086));if(_0x2ee6d7['loginInPause']){_0x25bf31[_0xb443('0x2c')](_[_0xb443('0x50')]({'channel':_0x59a0e7,'type':_0xb443('0x27'),'data1':'LOGININPAUSE'},_0x544086));}});return db[_0xb443('0x26')][_0xb443('0x51')](_0x2de23c[_0xb443('0x52')](_0x25bf31),{'individualHooks':!![]});}})[_0xb443('0x17')](function(){if(_0x2ee6d7){return _0x2ee6d7['update']({'online':!![],'lastLoginAt':moment()[_0xb443('0x22')](_0xb443('0x29')),'voicePause':_0x2ee6d7['loginInPause']||![],'mailPause':_0x2ee6d7[_0xb443('0x47')]||![],'chatPause':_0x2ee6d7[_0xb443('0x47')]||![],'faxPause':_0x2ee6d7[_0xb443('0x47')]||![],'smsPause':_0x2ee6d7[_0xb443('0x47')]||![],'openchannelPause':_0x2ee6d7[_0xb443('0x47')]||![],'pauseType':_0x2ee6d7[_0xb443('0x47')]?'LOGININPAUSE':null,'lastPauseAt':moment()['format'](_0xb443('0x29')),'interface':_0x1c47ab});}})[_0xb443('0x17')](function(_0x12ab6e){_0x58b038(_0x12ab6e);})[_0xb443('0x1a')](function(_0x282a43){_0x5ca5c7(_0x49a1fa[_0xb443('0x1b')](0x1f4,_0x282a43['message']));});});};