Built motion from commit 2b9af5da.|2.6.26
[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 _0x6535=['User','findAll','options','raw','where','include','map','model','attributes','then','info','GetUser','debug','stringify','catch','error','message','ShowUser','find','PauseUser','role','internal','format','User\x20with\x20id\x20%s\x20not\x20found','params','UserVoiceQueueRt','update','PAUSE','length','YYYY-MM-DD\x20HH:mm:ss','getChannels','forEach','push','name','interface','SIP/%s','body','type','MemberReport','bulkCreate','DEFAULT\x20PAUSE','UnpauseUser','resolve','channel','isValidChannel','Channel\x20parameter\x20%s\x20is\x20invalid','online','voicePause','mailPause','chatPause','openchannelPause','smsPause','pauseType','lastPauseAt','system','findOne','destroy','all','LoginUser','mobile','phone','loginInPause','count','agent','../../config/license/util','getLicense','agents','Maximum\x20login\x20has\x20been\x20reached','getVoiceQueues','upsert','UserVoiceQueue.penalty','LOGIN','merge','LOGININPAUSE','concat','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','redis','defaults','socket.io-emitter','./user.socket','register'];(function(_0x4fabfd,_0x31c214){var _0x40089f=function(_0xef7e0b){while(--_0xef7e0b){_0x4fabfd['push'](_0x4fabfd['shift']());}};_0x40089f(++_0x31c214);}(_0x6535,0x1a3));var _0x5653=function(_0x3d42d8,_0x4cffaa){_0x3d42d8=_0x3d42d8-0x0;var _0x27fc41=_0x6535[_0x3d42d8];return _0x27fc41;};'use strict';var _=require('lodash');var util=require('util');var moment=require('moment');var BPromise=require('bluebird');var Redis=require(_0x5653('0x0'));var db=require(_0x5653('0x1'))['db'];var utils=require(_0x5653('0x2'));var logger=require(_0x5653('0x3'))(_0x5653('0x4'));var config=require(_0x5653('0x5'));config[_0x5653('0x6')]=_[_0x5653('0x7')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0x5653('0x8'))(new Redis(config['redis']));require(_0x5653('0x9'))[_0x5653('0xa')](socket);exports['GetUser']=function(_0x2086b1){var _0x345680=this;return new Promise(function(_0x5c3d2e,_0x471aa2){return db[_0x5653('0xb')][_0x5653('0xc')]({'raw':_0x2086b1[_0x5653('0xd')]?_0x2086b1['options'][_0x5653('0xe')]===undefined?!![]:![]:!![],'where':_0x2086b1[_0x5653('0xd')]?_0x2086b1['options'][_0x5653('0xf')]||null:null,'attributes':_0x2086b1[_0x5653('0xd')]?_0x2086b1['options']['attributes']||null:null,'limit':_0x2086b1[_0x5653('0xd')]?_0x2086b1[_0x5653('0xd')]['limit']||null:null,'include':_0x2086b1['options']?_0x2086b1['options'][_0x5653('0x10')]?_[_0x5653('0x11')](_0x2086b1[_0x5653('0xd')]['include'],function(_0x2d746b){return{'model':db[_0x2d746b[_0x5653('0x12')]],'as':_0x2d746b['as'],'attributes':_0x2d746b[_0x5653('0x13')],'include':_0x2d746b[_0x5653('0x10')]?_[_0x5653('0x11')](_0x2d746b[_0x5653('0x10')],function(_0x3230f0){return{'model':db[_0x3230f0[_0x5653('0x12')]],'as':_0x3230f0['as'],'attributes':_0x3230f0[_0x5653('0x13')],'include':_0x3230f0['include']?_['map'](_0x3230f0[_0x5653('0x10')],function(_0x2b9d58){return{'model':db[_0x2b9d58[_0x5653('0x12')]],'as':_0x2b9d58['as'],'attributes':_0x2b9d58[_0x5653('0x13')]};}):[]};}):[]};}):[]:[]})[_0x5653('0x14')](function(_0x31d2cf){logger[_0x5653('0x15')](_0x5653('0x16'),_0x2086b1);logger[_0x5653('0x17')]('GetUser',_0x2086b1,JSON[_0x5653('0x18')](_0x31d2cf));_0x5c3d2e(_0x31d2cf);})[_0x5653('0x19')](function(_0x59cfe3){logger[_0x5653('0x1a')](_0x5653('0x16'),_0x59cfe3['message'],_0x2086b1);_0x471aa2(_0x345680[_0x5653('0x1a')](0x1f4,_0x59cfe3[_0x5653('0x1b')]));});});};exports[_0x5653('0x1c')]=function(_0x42f960){var _0x59b504=this;return new Promise(function(_0x1bf21c,_0x35b7b8){return db[_0x5653('0xb')][_0x5653('0x1d')]({'raw':_0x42f960['options']?_0x42f960['options'][_0x5653('0xe')]===undefined?!![]:![]:!![],'where':_0x42f960[_0x5653('0xd')]?_0x42f960['options'][_0x5653('0xf')]||null:null,'attributes':_0x42f960['options']?_0x42f960[_0x5653('0xd')][_0x5653('0x13')]||null:null,'include':_0x42f960[_0x5653('0xd')]?_0x42f960[_0x5653('0xd')][_0x5653('0x10')]?_['map'](_0x42f960[_0x5653('0xd')][_0x5653('0x10')],function(_0x16b72d){return{'model':db[_0x16b72d[_0x5653('0x12')]],'as':_0x16b72d['as'],'attributes':_0x16b72d['attributes'],'include':_0x16b72d[_0x5653('0x10')]?_[_0x5653('0x11')](_0x16b72d['include'],function(_0x1aa91f){return{'model':db[_0x1aa91f[_0x5653('0x12')]],'as':_0x1aa91f['as'],'attributes':_0x1aa91f[_0x5653('0x13')],'include':_0x1aa91f[_0x5653('0x10')]?_[_0x5653('0x11')](_0x1aa91f[_0x5653('0x10')],function(_0x10a1fa){return{'model':db[_0x10a1fa[_0x5653('0x12')]],'as':_0x10a1fa['as'],'attributes':_0x10a1fa[_0x5653('0x13')]};}):[]};}):[]};}):[]:[]})[_0x5653('0x14')](function(_0x3c77d0){logger[_0x5653('0x15')](_0x5653('0x1c'),_0x42f960);logger[_0x5653('0x17')](_0x5653('0x1c'),_0x42f960,JSON[_0x5653('0x18')](_0x3c77d0));_0x1bf21c(_0x3c77d0);})[_0x5653('0x19')](function(_0x56283c){logger[_0x5653('0x1a')]('ShowUser',_0x56283c[_0x5653('0x1b')],_0x42f960);_0x35b7b8(_0x59b504[_0x5653('0x1a')](0x1f4,_0x56283c['message']));});});};exports[_0x5653('0x1e')]=function(_0x27aac1){var _0x320119;return db['User'][_0x5653('0x1d')]({'where':_0x27aac1[_0x5653('0xd')][_0x5653('0xf')]||{},'attributes':['id','name',_0x5653('0x1f'),_0x5653('0x20'),'interface']})['then'](function(_0x126ae3){if(!_0x126ae3){throw new Error(util[_0x5653('0x21')](_0x5653('0x22'),_0x27aac1[_0x5653('0x23')]['id']));}_0x320119=_0x126ae3;return db[_0x5653('0x24')][_0x5653('0x25')]({'paused':0x1},{'where':{'UserId':_0x320119['id']},'individualHooks':!![]});})[_0x5653('0x14')](function(){return db['MemberReport']['findAll']({'where':{'membername':_0x320119['name'],'type':_0x5653('0x26'),'exitAt':null}});})[_0x5653('0x14')](function(_0x22464b){if(_0x22464b&&_0x22464b[_0x5653('0x27')]){var _0x45299b=moment()[_0x5653('0x21')](_0x5653('0x28'));for(var _0x4efcfb=0x0,_0x5cb220=_0x22464b['length'];_0x4efcfb<_0x5cb220;_0x4efcfb++){_0x22464b[_0x4efcfb]['updateAttributes']({'exitAt':_0x45299b});}}})[_0x5653('0x14')](function(){var _0x3969fa=[];var _0x237e27=utils[_0x5653('0x29')]();_0x237e27[_0x5653('0x2a')](function(_0xd75c7e){_0x3969fa[_0x5653('0x2b')]({'channel':_0xd75c7e,'membername':_0x320119[_0x5653('0x2c')],'interface':_['isNil'](_0x320119[_0x5653('0x2d')])?util[_0x5653('0x21')](_0x5653('0x2e'),_0x320119[_0x5653('0x2c')]):_0x320119[_0x5653('0x2d')],'type':_0x5653('0x26'),'enterAt':moment()[_0x5653('0x21')](_0x5653('0x28')),'data1':_0x27aac1[_0x5653('0x2f')][_0x5653('0x30')]||'DEFAULT\x20PAUSE','role':_0x320119[_0x5653('0x1f')],'internal':_0x320119[_0x5653('0x20')],'uniqueid':_0x27aac1['body']['uniqueid']||null});});return db[_0x5653('0x31')][_0x5653('0x32')](_0x3969fa,{'individualHooks':!![]});})['then'](function(){return _0x320119[_0x5653('0x25')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x27aac1[_0x5653('0x2f')][_0x5653('0x30')]||_0x5653('0x33'),'lastPauseAt':moment()[_0x5653('0x21')](_0x5653('0x28'))});})[_0x5653('0x19')](function(_0x4e742d){throw _0x4e742d;});};exports[_0x5653('0x34')]=function(_0x5bb22a){return Promise[_0x5653('0x35')]()[_0x5653('0x14')](function(){if(_0x5bb22a[_0x5653('0x2f')][_0x5653('0x36')]&&!utils[_0x5653('0x37')](_0x5bb22a[_0x5653('0x2f')][_0x5653('0x36')])){throw new Error(util[_0x5653('0x21')](_0x5653('0x38'),_0x5bb22a[_0x5653('0x2f')][_0x5653('0x36')]));}return db[_0x5653('0xb')]['findOne']({'where':_0x5bb22a[_0x5653('0xd')][_0x5653('0xf')]||{},'attributes':['id',_0x5653('0x2c'),_0x5653('0x1f'),'internal',_0x5653('0x2d'),_0x5653('0x39'),_0x5653('0x3a'),_0x5653('0x3b'),_0x5653('0x3c'),'faxPause',_0x5653('0x3d'),_0x5653('0x3e'),'whatsappPause',_0x5653('0x3f'),_0x5653('0x40')]});})[_0x5653('0x14')](function(_0x8b7a0d){if(!_0x8b7a0d){throw new Error(util[_0x5653('0x21')](_0x5653('0x22'),_0x5bb22a[_0x5653('0x23')]['id']));}return _0x8b7a0d['unpause'](_0x5bb22a[_0x5653('0x2f')][_0x5653('0x36')],_0x5653('0x41'));})[_0x5653('0x19')](function(_0x464d4b){throw _0x464d4b;});};exports['LogoutUser']=function(_0x4217af){var _0x4c271d=this;return new Promise(function(_0x23bcd3,_0x261bac){var _0x59bc35;return db['User'][_0x5653('0x42')]({'where':_0x4217af[_0x5653('0xd')][_0x5653('0xf')]||{},'attributes':['id','name']})[_0x5653('0x14')](function(_0x41ca71){if(_0x41ca71){_0x59bc35=_0x41ca71;return db[_0x5653('0x24')][_0x5653('0x43')]({'where':{'membername':_0x59bc35[_0x5653('0x2c')]},'individualHooks':!![]});}})['then'](function(){if(_0x59bc35){return db['MemberReport'][_0x5653('0xc')]({'where':{'membername':_0x59bc35[_0x5653('0x2c')],'exitAt':null}});}})[_0x5653('0x14')](function(_0x2c2047){if(_0x59bc35){if(_0x2c2047&&_0x2c2047[_0x5653('0x27')]){var _0x2ebe07=[];_0x2c2047[_0x5653('0x2a')](function(_0x5cef50){_0x2ebe07['push'](_0x5cef50[_0x5653('0x25')]({'exitAt':moment()[_0x5653('0x21')](_0x5653('0x28'))}));});return BPromise[_0x5653('0x44')](_0x2ebe07);}}})[_0x5653('0x14')](function(){if(_0x59bc35){return _0x59bc35[_0x5653('0x25')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0x5653('0x14')](function(_0x5e9ece){_0x23bcd3(_0x5e9ece);})[_0x5653('0x19')](function(_0x59c829){_0x261bac(_0x4c271d['error'](0x1f4,_0x59c829[_0x5653('0x1b')]));});});};exports[_0x5653('0x45')]=function(_0x11233e){var _0x4119cf=this;return new Promise(function(_0xac4368,_0x4fa76b){var _0x2cb7ff;var _0x5dac9b;var _0x40f44f;var _0x5594c7;return db[_0x5653('0xb')][_0x5653('0x42')]({'where':_0x11233e[_0x5653('0xd')][_0x5653('0xf')]||{},'attributes':['id','name','role',_0x5653('0x46'),_0x5653('0x47'),_0x5653('0x20'),_0x5653('0x48'),_0x5653('0x2d')]})['then'](function(_0xbdc4b3){if(_0xbdc4b3){_0x2cb7ff=_0xbdc4b3;_0x5594c7=_0x11233e[_0x5653('0x2f')][_0x5653('0x2d')]?_0x11233e['body'][_0x5653('0x2d')]:util[_0x5653('0x21')](_0x5653('0x2e'),_0x2cb7ff[_0x5653('0x2c')]);return db[_0x5653('0xb')][_0x5653('0x49')]({'where':{'role':_0x5653('0x4a'),'online':!![],'id':{'$ne':_0x2cb7ff['id']}}});}})['then'](function(_0x18235f){if(_0x18235f){_0x40f44f=_0x18235f;return require(_0x5653('0x4b'))[_0x5653('0x4c')]();}})[_0x5653('0x14')](function(_0x579bfb){if(_0x579bfb&&_0x579bfb['agents']&&_0x40f44f>=_0x579bfb[_0x5653('0x4d')]){throw new Error(_0x5653('0x4e'));}})[_0x5653('0x14')](function(){if(_0x2cb7ff){return db[_0x5653('0x24')]['destroy']({'where':{'membername':_0x2cb7ff['name']},'individualHooks':!![]});}})[_0x5653('0x14')](function(){if(_0x2cb7ff){return _0x2cb7ff[_0x5653('0x4f')]({'raw':!![]});}})[_0x5653('0x14')](function(_0x311fc2){if(_0x2cb7ff){if(_0x311fc2){var _0x398d96=[];_0x5dac9b=_0x311fc2;if(_0x5dac9b[_0x5653('0x27')]){for(var _0x46a2e8=0x0;_0x46a2e8<_0x5dac9b['length'];_0x46a2e8++){_0x398d96['push'](db[_0x5653('0x24')][_0x5653('0x50')]({'membername':_0x2cb7ff['name'],'UserId':_0x2cb7ff['id'],'queue_name':_0x5dac9b[_0x46a2e8][_0x5653('0x2c')],'VoiceQueueId':_0x5dac9b[_0x46a2e8]['id'],'interface':_0x5594c7,'paused':_0x2cb7ff[_0x5653('0x48')]||![],'penalty':_0x5dac9b[_0x46a2e8][_0x5653('0x51')]||0x0}));}return BPromise['all'](_0x398d96);}}}})[_0x5653('0x14')](function(){if(_0x2cb7ff){return db[_0x5653('0x31')][_0x5653('0xc')]({'where':{'membername':_0x2cb7ff[_0x5653('0x2c')],'exitAt':null}});}})[_0x5653('0x14')](function(_0xb0f45b){if(_0x2cb7ff){if(_0xb0f45b&&_0xb0f45b[_0x5653('0x27')]){var _0x171cc5=[];_0xb0f45b[_0x5653('0x2a')](function(_0x2c29c4){_0x171cc5['push'](_0x2c29c4[_0x5653('0x25')]({'exitAt':moment()['format'](_0x5653('0x28'))}));});return BPromise[_0x5653('0x44')](_0x171cc5);}}})['then'](function(){if(_0x2cb7ff){var _0x113f15=[];var _0x37705c=[];var _0x1242e9=utils['getChannels']();var _0xf67b7b={'membername':_0x2cb7ff['name'],'interface':_0x5594c7,'enterAt':moment()[_0x5653('0x21')](_0x5653('0x28')),'role':_0x2cb7ff[_0x5653('0x1f')],'internal':_0x2cb7ff[_0x5653('0x20')]};_0x1242e9[_0x5653('0x2a')](function(_0x80e89a){_0x113f15[_0x5653('0x2b')](_['merge']({'channel':_0x80e89a,'type':_0x5653('0x52'),'data1':'callysquare','data2':_0x5594c7},_0xf67b7b));if(_0x2cb7ff['loginInPause']){_0x37705c[_0x5653('0x2b')](_[_0x5653('0x53')]({'channel':_0x80e89a,'type':_0x5653('0x26'),'data1':_0x5653('0x54')},_0xf67b7b));}});return db[_0x5653('0x31')]['bulkCreate'](_0x113f15[_0x5653('0x55')](_0x37705c),{'individualHooks':!![]});}})[_0x5653('0x14')](function(){if(_0x2cb7ff){return _0x2cb7ff['update']({'online':!![],'lastLoginAt':moment()[_0x5653('0x21')]('YYYY-MM-DD\x20HH:mm:ss'),'voicePause':_0x2cb7ff[_0x5653('0x48')]||![],'mailPause':_0x2cb7ff[_0x5653('0x48')]||![],'chatPause':_0x2cb7ff[_0x5653('0x48')]||![],'faxPause':_0x2cb7ff[_0x5653('0x48')]||![],'smsPause':_0x2cb7ff[_0x5653('0x48')]||![],'openchannelPause':_0x2cb7ff['loginInPause']||![],'pauseType':_0x2cb7ff['loginInPause']?_0x5653('0x54'):null,'lastPauseAt':moment()[_0x5653('0x21')](_0x5653('0x28')),'interface':_0x5594c7});}})['then'](function(_0x18efb3){_0xac4368(_0x18efb3);})[_0x5653('0x19')](function(_0xb7f690){_0x4fa76b(_0x4119cf[_0x5653('0x1a')](0x1f4,_0xb7f690['message']));});});};