Built motion from commit 10af8726.|2.6.34
[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 _0x88f6=['faxPause','openchannelPause','smsPause','whatsappPause','pauseType','lastPauseAt','params','unpause','system','LogoutUser','findOne','destroy','all','phone','loginInPause','count','agents','Maximum\x20login\x20has\x20been\x20reached','getVoiceQueues','upsert','UserVoiceQueue.penalty','getChannels','merge','LOGIN','callysquare','LOGININPAUSE','concat','util','moment','bluebird','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','redis','defaults','localhost','socket.io-emitter','./user.socket','GetUser','User','options','where','attributes','limit','include','model','map','info','debug','stringify','message','error','ShowUser','find','raw','then','catch','name','role','internal','interface','format','User\x20with\x20id\x20%s\x20not\x20found','UserVoiceQueueRt','MemberReport','findAll','PAUSE','length','YYYY-MM-DD\x20HH:mm:ss','updateAttributes','forEach','push','isNil','body','type','DEFAULT\x20PAUSE','uniqueid','bulkCreate','update','UnpauseUser','channel','isValidChannel','online','voicePause','mailPause','chatPause'];(function(_0x570ad8,_0x425ec5){var _0x1eb874=function(_0x32f8fd){while(--_0x32f8fd){_0x570ad8['push'](_0x570ad8['shift']());}};_0x1eb874(++_0x425ec5);}(_0x88f6,0x123));var _0x688f=function(_0x313be4,_0x4fb127){_0x313be4=_0x313be4-0x0;var _0x22a92b=_0x88f6[_0x313be4];return _0x22a92b;};'use strict';var _=require('lodash');var util=require(_0x688f('0x0'));var moment=require(_0x688f('0x1'));var BPromise=require(_0x688f('0x2'));var Redis=require('ioredis');var db=require(_0x688f('0x3'))['db'];var utils=require(_0x688f('0x4'));var logger=require(_0x688f('0x5'))(_0x688f('0x6'));var config=require(_0x688f('0x7'));config[_0x688f('0x8')]=_[_0x688f('0x9')](config[_0x688f('0x8')],{'host':_0x688f('0xa'),'port':0x18eb});var socket=require(_0x688f('0xb'))(new Redis(config['redis']));require(_0x688f('0xc'))['register'](socket);exports[_0x688f('0xd')]=function(_0x384f89){var _0x42ab27=this;return new Promise(function(_0x48c302,_0x5b84f0){return db[_0x688f('0xe')]['findAll']({'raw':_0x384f89[_0x688f('0xf')]?_0x384f89[_0x688f('0xf')]['raw']===undefined?!![]:![]:!![],'where':_0x384f89[_0x688f('0xf')]?_0x384f89[_0x688f('0xf')][_0x688f('0x10')]||null:null,'attributes':_0x384f89[_0x688f('0xf')]?_0x384f89[_0x688f('0xf')][_0x688f('0x11')]||null:null,'limit':_0x384f89[_0x688f('0xf')]?_0x384f89['options'][_0x688f('0x12')]||null:null,'include':_0x384f89[_0x688f('0xf')]?_0x384f89[_0x688f('0xf')][_0x688f('0x13')]?_['map'](_0x384f89[_0x688f('0xf')][_0x688f('0x13')],function(_0x4c7ceb){return{'model':db[_0x4c7ceb[_0x688f('0x14')]],'as':_0x4c7ceb['as'],'attributes':_0x4c7ceb[_0x688f('0x11')],'include':_0x4c7ceb[_0x688f('0x13')]?_[_0x688f('0x15')](_0x4c7ceb[_0x688f('0x13')],function(_0xad981d){return{'model':db[_0xad981d[_0x688f('0x14')]],'as':_0xad981d['as'],'attributes':_0xad981d[_0x688f('0x11')],'include':_0xad981d[_0x688f('0x13')]?_[_0x688f('0x15')](_0xad981d[_0x688f('0x13')],function(_0x4b2948){return{'model':db[_0x4b2948[_0x688f('0x14')]],'as':_0x4b2948['as'],'attributes':_0x4b2948[_0x688f('0x11')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x367bbe){logger[_0x688f('0x16')](_0x688f('0xd'),_0x384f89);logger[_0x688f('0x17')](_0x688f('0xd'),_0x384f89,JSON[_0x688f('0x18')](_0x367bbe));_0x48c302(_0x367bbe);})['catch'](function(_0x2db0c1){logger['error'](_0x688f('0xd'),_0x2db0c1[_0x688f('0x19')],_0x384f89);_0x5b84f0(_0x42ab27[_0x688f('0x1a')](0x1f4,_0x2db0c1[_0x688f('0x19')]));});});};exports[_0x688f('0x1b')]=function(_0x3de33f){var _0x21fb32=this;return new Promise(function(_0x213ed4,_0x1c1686){return db[_0x688f('0xe')][_0x688f('0x1c')]({'raw':_0x3de33f['options']?_0x3de33f['options'][_0x688f('0x1d')]===undefined?!![]:![]:!![],'where':_0x3de33f['options']?_0x3de33f[_0x688f('0xf')][_0x688f('0x10')]||null:null,'attributes':_0x3de33f[_0x688f('0xf')]?_0x3de33f[_0x688f('0xf')][_0x688f('0x11')]||null:null,'include':_0x3de33f[_0x688f('0xf')]?_0x3de33f[_0x688f('0xf')][_0x688f('0x13')]?_[_0x688f('0x15')](_0x3de33f[_0x688f('0xf')][_0x688f('0x13')],function(_0x293131){return{'model':db[_0x293131[_0x688f('0x14')]],'as':_0x293131['as'],'attributes':_0x293131[_0x688f('0x11')],'include':_0x293131[_0x688f('0x13')]?_['map'](_0x293131[_0x688f('0x13')],function(_0x2cfc27){return{'model':db[_0x2cfc27[_0x688f('0x14')]],'as':_0x2cfc27['as'],'attributes':_0x2cfc27['attributes'],'include':_0x2cfc27[_0x688f('0x13')]?_[_0x688f('0x15')](_0x2cfc27[_0x688f('0x13')],function(_0xcb82f1){return{'model':db[_0xcb82f1[_0x688f('0x14')]],'as':_0xcb82f1['as'],'attributes':_0xcb82f1[_0x688f('0x11')]};}):[]};}):[]};}):[]:[]})[_0x688f('0x1e')](function(_0x30eb28){logger['info']('ShowUser',_0x3de33f);logger[_0x688f('0x17')](_0x688f('0x1b'),_0x3de33f,JSON[_0x688f('0x18')](_0x30eb28));_0x213ed4(_0x30eb28);})[_0x688f('0x1f')](function(_0x54c0b1){logger[_0x688f('0x1a')](_0x688f('0x1b'),_0x54c0b1['message'],_0x3de33f);_0x1c1686(_0x21fb32[_0x688f('0x1a')](0x1f4,_0x54c0b1[_0x688f('0x19')]));});});};exports['PauseUser']=function(_0x3bfafe){var _0x13aad2;return db[_0x688f('0xe')][_0x688f('0x1c')]({'where':_0x3bfafe[_0x688f('0xf')][_0x688f('0x10')]||{},'attributes':['id',_0x688f('0x20'),_0x688f('0x21'),_0x688f('0x22'),_0x688f('0x23')]})['then'](function(_0x387e35){if(!_0x387e35){throw new Error(util[_0x688f('0x24')](_0x688f('0x25'),_0x3bfafe['params']['id']));}_0x13aad2=_0x387e35;return db[_0x688f('0x26')]['update']({'paused':0x1},{'where':{'UserId':_0x13aad2['id']},'individualHooks':!![]});})[_0x688f('0x1e')](function(){return db[_0x688f('0x27')][_0x688f('0x28')]({'where':{'membername':_0x13aad2['name'],'type':_0x688f('0x29'),'exitAt':null}});})[_0x688f('0x1e')](function(_0x27a478){if(_0x27a478&&_0x27a478[_0x688f('0x2a')]){var _0x42e897=moment()['format'](_0x688f('0x2b'));for(var _0x5eea20=0x0,_0x5b83d4=_0x27a478[_0x688f('0x2a')];_0x5eea20<_0x5b83d4;_0x5eea20++){_0x27a478[_0x5eea20][_0x688f('0x2c')]({'exitAt':_0x42e897});}}})['then'](function(){var _0x4e03d9=[];var _0x21da33=utils['getChannels']();_0x21da33[_0x688f('0x2d')](function(_0x54e702){_0x4e03d9[_0x688f('0x2e')]({'channel':_0x54e702,'membername':_0x13aad2[_0x688f('0x20')],'interface':_[_0x688f('0x2f')](_0x13aad2[_0x688f('0x23')])?util[_0x688f('0x24')]('SIP/%s',_0x13aad2[_0x688f('0x20')]):_0x13aad2['interface'],'type':_0x688f('0x29'),'enterAt':moment()[_0x688f('0x24')](_0x688f('0x2b')),'data1':_0x3bfafe[_0x688f('0x30')][_0x688f('0x31')]||_0x688f('0x32'),'role':_0x13aad2[_0x688f('0x21')],'internal':_0x13aad2['internal'],'uniqueid':_0x3bfafe[_0x688f('0x30')][_0x688f('0x33')]||null});});return db[_0x688f('0x27')][_0x688f('0x34')](_0x4e03d9,{'individualHooks':!![]});})[_0x688f('0x1e')](function(){return _0x13aad2[_0x688f('0x35')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x3bfafe[_0x688f('0x30')][_0x688f('0x31')]||_0x688f('0x32'),'lastPauseAt':moment()[_0x688f('0x24')](_0x688f('0x2b'))});})[_0x688f('0x1f')](function(_0x1e4f30){throw _0x1e4f30;});};exports[_0x688f('0x36')]=function(_0x466244){return Promise['resolve']()[_0x688f('0x1e')](function(){if(_0x466244[_0x688f('0x30')][_0x688f('0x37')]&&!utils[_0x688f('0x38')](_0x466244[_0x688f('0x30')][_0x688f('0x37')])){throw new Error(util[_0x688f('0x24')]('Channel\x20parameter\x20%s\x20is\x20invalid',_0x466244['body'][_0x688f('0x37')]));}return db[_0x688f('0xe')]['findOne']({'where':_0x466244['options'][_0x688f('0x10')]||{},'attributes':['id',_0x688f('0x20'),_0x688f('0x21'),_0x688f('0x22'),_0x688f('0x23'),_0x688f('0x39'),_0x688f('0x3a'),_0x688f('0x3b'),_0x688f('0x3c'),_0x688f('0x3d'),_0x688f('0x3e'),_0x688f('0x3f'),_0x688f('0x40'),_0x688f('0x41'),_0x688f('0x42')]});})[_0x688f('0x1e')](function(_0x159dfc){if(!_0x159dfc){throw new Error(util[_0x688f('0x24')]('User\x20with\x20id\x20%s\x20not\x20found',_0x466244[_0x688f('0x43')]['id']));}return _0x159dfc[_0x688f('0x44')](_0x466244['body'][_0x688f('0x37')],_0x688f('0x45'));})[_0x688f('0x1f')](function(_0x57d4d5){throw _0x57d4d5;});};exports[_0x688f('0x46')]=function(_0x40bc66){var _0x58210f=this;return new Promise(function(_0x331f53,_0xfc8f61){var _0x4cd87a;return db['User'][_0x688f('0x47')]({'where':_0x40bc66[_0x688f('0xf')][_0x688f('0x10')]||{},'attributes':['id','name']})['then'](function(_0x1c981c){if(_0x1c981c){_0x4cd87a=_0x1c981c;return db[_0x688f('0x26')][_0x688f('0x48')]({'where':{'membername':_0x4cd87a[_0x688f('0x20')]},'individualHooks':!![]});}})[_0x688f('0x1e')](function(){if(_0x4cd87a){return db['MemberReport'][_0x688f('0x28')]({'where':{'membername':_0x4cd87a[_0x688f('0x20')],'exitAt':null}});}})[_0x688f('0x1e')](function(_0x1ed71b){if(_0x4cd87a){if(_0x1ed71b&&_0x1ed71b[_0x688f('0x2a')]){var _0x560992=[];_0x1ed71b[_0x688f('0x2d')](function(_0x4a3336){_0x560992[_0x688f('0x2e')](_0x4a3336[_0x688f('0x35')]({'exitAt':moment()[_0x688f('0x24')](_0x688f('0x2b'))}));});return BPromise[_0x688f('0x49')](_0x560992);}}})['then'](function(){if(_0x4cd87a){return _0x4cd87a[_0x688f('0x35')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})['then'](function(_0x232dee){_0x331f53(_0x232dee);})[_0x688f('0x1f')](function(_0x4bfd96){_0xfc8f61(_0x58210f[_0x688f('0x1a')](0x1f4,_0x4bfd96['message']));});});};exports['LoginUser']=function(_0x31b06a){var _0x1d977b=this;return new Promise(function(_0x4c78e8,_0x4e9c20){var _0x5f492f;var _0x2a3f7f;var _0x59f1bc;var _0x2a126d;return db[_0x688f('0xe')][_0x688f('0x47')]({'where':_0x31b06a[_0x688f('0xf')][_0x688f('0x10')]||{},'attributes':['id',_0x688f('0x20'),_0x688f('0x21'),'mobile',_0x688f('0x4a'),_0x688f('0x22'),_0x688f('0x4b'),'interface']})['then'](function(_0x369ab7){if(_0x369ab7){_0x5f492f=_0x369ab7;_0x2a126d=_0x31b06a[_0x688f('0x30')][_0x688f('0x23')]?_0x31b06a['body'][_0x688f('0x23')]:util[_0x688f('0x24')]('SIP/%s',_0x5f492f['name']);return db[_0x688f('0xe')][_0x688f('0x4c')]({'where':{'role':'agent','online':!![],'id':{'$ne':_0x5f492f['id']}}});}})[_0x688f('0x1e')](function(_0x50748f){if(_0x50748f){_0x59f1bc=_0x50748f;return require('../../config/license/util')['getLicense']();}})['then'](function(_0x1ea729){if(_0x1ea729&&_0x1ea729['agents']&&_0x59f1bc>=_0x1ea729[_0x688f('0x4d')]){throw new Error(_0x688f('0x4e'));}})[_0x688f('0x1e')](function(){if(_0x5f492f){return db[_0x688f('0x26')][_0x688f('0x48')]({'where':{'membername':_0x5f492f[_0x688f('0x20')]},'individualHooks':!![]});}})[_0x688f('0x1e')](function(){if(_0x5f492f){return _0x5f492f[_0x688f('0x4f')]({'raw':!![]});}})['then'](function(_0x19fc0e){if(_0x5f492f){if(_0x19fc0e){var _0x5b7756=[];_0x2a3f7f=_0x19fc0e;if(_0x2a3f7f[_0x688f('0x2a')]){for(var _0xb1a06d=0x0;_0xb1a06d<_0x2a3f7f[_0x688f('0x2a')];_0xb1a06d++){_0x5b7756[_0x688f('0x2e')](db[_0x688f('0x26')][_0x688f('0x50')]({'membername':_0x5f492f['name'],'UserId':_0x5f492f['id'],'queue_name':_0x2a3f7f[_0xb1a06d]['name'],'VoiceQueueId':_0x2a3f7f[_0xb1a06d]['id'],'interface':_0x2a126d,'paused':_0x5f492f[_0x688f('0x4b')]||![],'penalty':_0x2a3f7f[_0xb1a06d][_0x688f('0x51')]||0x0}));}return BPromise[_0x688f('0x49')](_0x5b7756);}}}})[_0x688f('0x1e')](function(){if(_0x5f492f){return db[_0x688f('0x27')][_0x688f('0x28')]({'where':{'membername':_0x5f492f['name'],'exitAt':null}});}})[_0x688f('0x1e')](function(_0x7cfdd2){if(_0x5f492f){if(_0x7cfdd2&&_0x7cfdd2[_0x688f('0x2a')]){var _0x2a68d6=[];_0x7cfdd2[_0x688f('0x2d')](function(_0x230a49){_0x2a68d6[_0x688f('0x2e')](_0x230a49[_0x688f('0x35')]({'exitAt':moment()[_0x688f('0x24')](_0x688f('0x2b'))}));});return BPromise[_0x688f('0x49')](_0x2a68d6);}}})[_0x688f('0x1e')](function(){if(_0x5f492f){var _0x1562d8=[];var _0x27fd74=[];var _0x1606f9=utils[_0x688f('0x52')]();var _0x59b27f={'membername':_0x5f492f[_0x688f('0x20')],'interface':_0x2a126d,'enterAt':moment()[_0x688f('0x24')](_0x688f('0x2b')),'role':_0x5f492f[_0x688f('0x21')],'internal':_0x5f492f[_0x688f('0x22')]};_0x1606f9[_0x688f('0x2d')](function(_0x30a4dd){_0x1562d8[_0x688f('0x2e')](_[_0x688f('0x53')]({'channel':_0x30a4dd,'type':_0x688f('0x54'),'data1':_0x688f('0x55'),'data2':_0x2a126d},_0x59b27f));if(_0x5f492f[_0x688f('0x4b')]){_0x27fd74[_0x688f('0x2e')](_['merge']({'channel':_0x30a4dd,'type':_0x688f('0x29'),'data1':_0x688f('0x56')},_0x59b27f));}});return db[_0x688f('0x27')][_0x688f('0x34')](_0x1562d8[_0x688f('0x57')](_0x27fd74),{'individualHooks':!![]});}})[_0x688f('0x1e')](function(){if(_0x5f492f){return _0x5f492f[_0x688f('0x35')]({'online':!![],'lastLoginAt':moment()[_0x688f('0x24')](_0x688f('0x2b')),'voicePause':_0x5f492f[_0x688f('0x4b')]||![],'mailPause':_0x5f492f[_0x688f('0x4b')]||![],'chatPause':_0x5f492f[_0x688f('0x4b')]||![],'faxPause':_0x5f492f[_0x688f('0x4b')]||![],'smsPause':_0x5f492f['loginInPause']||![],'openchannelPause':_0x5f492f[_0x688f('0x4b')]||![],'pauseType':_0x5f492f[_0x688f('0x4b')]?_0x688f('0x56'):null,'lastPauseAt':moment()['format'](_0x688f('0x2b')),'interface':_0x2a126d});}})[_0x688f('0x1e')](function(_0x2f0ce0){_0x4c78e8(_0x2f0ce0);})[_0x688f('0x1f')](function(_0x1542bd){_0x4e9c20(_0x1d977b['error'](0x1f4,_0x1542bd['message']));});});};