Built motion from commit (unavailable).|2.5.6
[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 _0xf373=['fax','sms','openchannel','set','all','LogoutUser','UserVoiceQueueRt','destroy','LoginUser','mobile','phone','agent','getLicense','agents','upsert','loginInPause','UserVoiceQueue.penalty','merge','LOGIN','callysquare','LOGININPAUSE','bulkCreate','concat','lodash','util','bluebird','randomstring','ioredis','../../config/utils','rpc','../../config/environment','jayson/promise','client','http','defaults','redis','localhost','socket.io-emitter','./user.socket','register','request','info','User,\x20%s,\x20%s','request\x20sent','debug','User,\x20%s,\x20%s,\x20%s','stringify','error','code','message','catch','GetUser','options','raw','where','attributes','limit','map','include','model','ShowUser','find','PauseUser','name','internal','interface','then','update','MemberReport','findAll','PAUSE','length','YYYY-MM-DD\x20HH:mm:ss','updateAttributes','getChannels','forEach','push','isNil','format','body','DEFAULT\x20PAUSE','role','uniqueid','type','UnpauseUser','User','findOne','channel','isValidChannel','Unknown\x20channel','voice','system','chat'];(function(_0x87867b,_0x2cc4fb){var _0xc95e2e=function(_0x5e2300){while(--_0x5e2300){_0x87867b['push'](_0x87867b['shift']());}};_0xc95e2e(++_0x2cc4fb);}(_0xf373,0x1e8));var _0x3f37=function(_0x10a39a,_0x2c6f68){_0x10a39a=_0x10a39a-0x0;var _0x1fa08b=_0xf373[_0x10a39a];return _0x1fa08b;};'use strict';var _=require(_0x3f37('0x0'));var util=require(_0x3f37('0x1'));var moment=require('moment');var BPromise=require(_0x3f37('0x2'));var rs=require(_0x3f37('0x3'));var fs=require('fs');var Redis=require(_0x3f37('0x4'));var db=require('../../mysqldb')['db'];var utils=require(_0x3f37('0x5'));var logger=require('../../config/logger')(_0x3f37('0x6'));var config=require(_0x3f37('0x7'));var jayson=require(_0x3f37('0x8'));var client=jayson[_0x3f37('0x9')][_0x3f37('0xa')]({'port':0x232a});config['redis']=_[_0x3f37('0xb')](config[_0x3f37('0xc')],{'host':_0x3f37('0xd'),'port':0x18eb});var socket=require(_0x3f37('0xe'))(new Redis(config[_0x3f37('0xc')]));require(_0x3f37('0xf'))[_0x3f37('0x10')](socket);function respondWithRpcPromise(_0x59774a,_0xf81ac4,_0x55f04a){return new BPromise(function(_0x5c58dd,_0xab9451){return client[_0x3f37('0x11')](_0x59774a,_0x55f04a)['then'](function(_0x2e4e51){logger[_0x3f37('0x12')](_0x3f37('0x13'),_0xf81ac4,_0x3f37('0x14'));logger[_0x3f37('0x15')](_0x3f37('0x16'),_0xf81ac4,'request\x20sent',JSON[_0x3f37('0x17')](_0x2e4e51));if(_0x2e4e51[_0x3f37('0x18')]){if(_0x2e4e51[_0x3f37('0x18')][_0x3f37('0x19')]===0x1f4){logger[_0x3f37('0x18')]('User,\x20%s,\x20%s',_0xf81ac4,_0x2e4e51[_0x3f37('0x18')]['message']);return _0xab9451(_0x2e4e51[_0x3f37('0x18')]['message']);}logger[_0x3f37('0x18')]('User,\x20%s,\x20%s',_0xf81ac4,_0x2e4e51[_0x3f37('0x18')][_0x3f37('0x1a')]);return _0x5c58dd(_0x2e4e51[_0x3f37('0x18')][_0x3f37('0x1a')]);}else{logger[_0x3f37('0x12')](_0x3f37('0x13'),_0xf81ac4,_0x3f37('0x14'));_0x5c58dd(_0x2e4e51['result'][_0x3f37('0x1a')]);}})[_0x3f37('0x1b')](function(_0x51a2a8){logger[_0x3f37('0x18')](_0x3f37('0x13'),_0xf81ac4,_0x51a2a8);_0xab9451(_0x51a2a8);});});}exports[_0x3f37('0x1c')]=function(_0x2fa7a8){var _0x324214=this;return new Promise(function(_0x106717,_0x1eaa92){return db['User']['findAll']({'raw':_0x2fa7a8[_0x3f37('0x1d')]?_0x2fa7a8[_0x3f37('0x1d')][_0x3f37('0x1e')]===undefined?!![]:![]:!![],'where':_0x2fa7a8[_0x3f37('0x1d')]?_0x2fa7a8[_0x3f37('0x1d')][_0x3f37('0x1f')]||null:null,'attributes':_0x2fa7a8['options']?_0x2fa7a8[_0x3f37('0x1d')][_0x3f37('0x20')]||null:null,'limit':_0x2fa7a8[_0x3f37('0x1d')]?_0x2fa7a8[_0x3f37('0x1d')][_0x3f37('0x21')]||null:null,'include':_0x2fa7a8[_0x3f37('0x1d')]?_0x2fa7a8[_0x3f37('0x1d')]['include']?_[_0x3f37('0x22')](_0x2fa7a8['options'][_0x3f37('0x23')],function(_0x3bf98){return{'model':db[_0x3bf98['model']],'as':_0x3bf98['as'],'attributes':_0x3bf98['attributes'],'include':_0x3bf98[_0x3f37('0x23')]?_[_0x3f37('0x22')](_0x3bf98[_0x3f37('0x23')],function(_0x4b188e){return{'model':db[_0x4b188e[_0x3f37('0x24')]],'as':_0x4b188e['as'],'attributes':_0x4b188e[_0x3f37('0x20')],'include':_0x4b188e['include']?_[_0x3f37('0x22')](_0x4b188e[_0x3f37('0x23')],function(_0x49f8c4){return{'model':db[_0x49f8c4[_0x3f37('0x24')]],'as':_0x49f8c4['as'],'attributes':_0x49f8c4[_0x3f37('0x20')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x2ad0dd){logger[_0x3f37('0x12')](_0x3f37('0x1c'),_0x2fa7a8);logger[_0x3f37('0x15')](_0x3f37('0x1c'),_0x2fa7a8,JSON['stringify'](_0x2ad0dd));_0x106717(_0x2ad0dd);})[_0x3f37('0x1b')](function(_0x4af2a5){logger[_0x3f37('0x18')](_0x3f37('0x1c'),_0x4af2a5[_0x3f37('0x1a')],_0x2fa7a8);_0x1eaa92(_0x324214['error'](0x1f4,_0x4af2a5['message']));});});};exports[_0x3f37('0x25')]=function(_0x18b6fd){var _0x546c2d=this;return new Promise(function(_0x1a2fa8,_0x10505a){return db['User'][_0x3f37('0x26')]({'raw':_0x18b6fd['options']?_0x18b6fd[_0x3f37('0x1d')][_0x3f37('0x1e')]===undefined?!![]:![]:!![],'where':_0x18b6fd[_0x3f37('0x1d')]?_0x18b6fd[_0x3f37('0x1d')][_0x3f37('0x1f')]||null:null,'attributes':_0x18b6fd[_0x3f37('0x1d')]?_0x18b6fd[_0x3f37('0x1d')][_0x3f37('0x20')]||null:null,'include':_0x18b6fd[_0x3f37('0x1d')]?_0x18b6fd[_0x3f37('0x1d')]['include']?_[_0x3f37('0x22')](_0x18b6fd[_0x3f37('0x1d')][_0x3f37('0x23')],function(_0x164f81){return{'model':db[_0x164f81[_0x3f37('0x24')]],'as':_0x164f81['as'],'attributes':_0x164f81[_0x3f37('0x20')],'include':_0x164f81['include']?_[_0x3f37('0x22')](_0x164f81[_0x3f37('0x23')],function(_0x569bd4){return{'model':db[_0x569bd4[_0x3f37('0x24')]],'as':_0x569bd4['as'],'attributes':_0x569bd4[_0x3f37('0x20')],'include':_0x569bd4[_0x3f37('0x23')]?_[_0x3f37('0x22')](_0x569bd4['include'],function(_0x20227){return{'model':db[_0x20227['model']],'as':_0x20227['as'],'attributes':_0x20227[_0x3f37('0x20')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x2252a5){logger['info']('ShowUser',_0x18b6fd);logger[_0x3f37('0x15')](_0x3f37('0x25'),_0x18b6fd,JSON['stringify'](_0x2252a5));_0x1a2fa8(_0x2252a5);})['catch'](function(_0x15a974){logger['error'](_0x3f37('0x25'),_0x15a974[_0x3f37('0x1a')],_0x18b6fd);_0x10505a(_0x546c2d[_0x3f37('0x18')](0x1f4,_0x15a974[_0x3f37('0x1a')]));});});};exports[_0x3f37('0x27')]=function(_0x59735c){var _0x227825=this;return new Promise(function(_0x3bbcd1,_0x1fe8a4){var _0x276be0;return db['User'][_0x3f37('0x26')]({'where':_0x59735c[_0x3f37('0x1d')][_0x3f37('0x1f')]||{},'attributes':['id',_0x3f37('0x28'),'role',_0x3f37('0x29'),_0x3f37('0x2a')]})[_0x3f37('0x2b')](function(_0x10739b){if(_0x10739b){_0x276be0=_0x10739b;return db['UserVoiceQueueRt'][_0x3f37('0x2c')]({'paused':0x1},{'where':{'UserId':_0x276be0['id']},'individualHooks':!![]});}})['then'](function(){if(_0x276be0){return db[_0x3f37('0x2d')][_0x3f37('0x2e')]({'where':{'membername':_0x276be0[_0x3f37('0x28')],'type':_0x3f37('0x2f'),'exitAt':null}});}})[_0x3f37('0x2b')](function(_0x4f80dc){if(_0x276be0){if(_0x4f80dc&&_0x4f80dc[_0x3f37('0x30')]){var _0x3e2745=moment()['format'](_0x3f37('0x31'));for(var _0x45834a=0x0,_0x263d2a=_0x4f80dc[_0x3f37('0x30')];_0x45834a<_0x263d2a;_0x45834a++){_0x4f80dc[_0x45834a][_0x3f37('0x32')]({'exitAt':_0x3e2745});}}}})[_0x3f37('0x2b')](function(){if(_0x276be0){var _0x68d9f9=[];var _0x24d4b7=utils[_0x3f37('0x33')]();_0x24d4b7[_0x3f37('0x34')](function(_0x1ce78b){_0x68d9f9[_0x3f37('0x35')]({'channel':_0x1ce78b,'membername':_0x276be0['name'],'interface':_[_0x3f37('0x36')](_0x276be0['interface'])?util[_0x3f37('0x37')]('SIP/%s',_0x276be0[_0x3f37('0x28')]):_0x276be0[_0x3f37('0x2a')],'type':'PAUSE','enterAt':moment()[_0x3f37('0x37')](_0x3f37('0x31')),'data1':_0x59735c[_0x3f37('0x38')]['type']||_0x3f37('0x39'),'role':_0x276be0[_0x3f37('0x3a')],'internal':_0x276be0['internal'],'uniqueid':_0x59735c['body'][_0x3f37('0x3b')]||null});});return db[_0x3f37('0x2d')]['bulkCreate'](_0x68d9f9,{'individualHooks':!![]});}})['then'](function(){if(_0x276be0){return _0x276be0['update']({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'pauseType':_0x59735c['body'][_0x3f37('0x3c')]||_0x3f37('0x39'),'lastPauseAt':moment()['format'](_0x3f37('0x31'))});}})[_0x3f37('0x2b')](function(_0x58574b){_0x3bbcd1(_0x58574b);})[_0x3f37('0x1b')](function(_0x98e22b){_0x1fe8a4(_0x227825['error'](0x1f4,_0x98e22b['message']));});});};exports[_0x3f37('0x3d')]=function(_0x7fd09b){var _0x186c9d=this;return new Promise(function(_0x100f72,_0x49a117){var _0x4799d5;return db[_0x3f37('0x3e')][_0x3f37('0x3f')]({'where':_0x7fd09b['options'][_0x3f37('0x1f')]||{},'attributes':['id',_0x3f37('0x28')]})[_0x3f37('0x2b')](function(_0x28d5be){if(_0x28d5be){_0x4799d5=_0x28d5be;if(_0x7fd09b[_0x3f37('0x38')][_0x3f37('0x40')]&&!utils[_0x3f37('0x41')](_0x7fd09b[_0x3f37('0x38')][_0x3f37('0x40')])){throw new Error(_0x3f37('0x42'));}switch(_0x7fd09b[_0x3f37('0x38')][_0x3f37('0x40')]){case _0x3f37('0x43'):return db['UserVoiceQueueRt'][_0x3f37('0x2c')]({'paused':0x0},{'where':{'UserId':_0x4799d5['id']}})[_0x3f37('0x2b')](function(){return db[_0x3f37('0x2d')][_0x3f37('0x26')]({'where':{'channel':_0x7fd09b[_0x3f37('0x38')][_0x3f37('0x40')],'membername':_0x4799d5['name'],'type':_0x3f37('0x2f'),'exitAt':null}});})[_0x3f37('0x2b')](function(_0x2ad608){if(_0x2ad608){return _0x2ad608['update']({'exitAt':moment()[_0x3f37('0x37')](_0x3f37('0x31')),'data2':_0x3f37('0x44')},{'individualHooks':!![]});}return null;})[_0x3f37('0x2b')](function(){return _0x4799d5[_0x3f37('0x2c')]({'voicePause':![]});});case'mail':case _0x3f37('0x45'):case _0x3f37('0x46'):case _0x3f37('0x47'):case _0x3f37('0x48'):return db[_0x3f37('0x2d')][_0x3f37('0x26')]({'where':{'channel':_0x7fd09b[_0x3f37('0x38')]['channel'],'membername':_0x4799d5[_0x3f37('0x28')],'type':'PAUSE','exitAt':null}})[_0x3f37('0x2b')](function(_0x46952e){return _0x46952e[_0x3f37('0x2c')]({'exitAt':moment()[_0x3f37('0x37')](_0x3f37('0x31')),'data2':_0x3f37('0x44')},{'individualHooks':!![]});})['then'](function(){var _0x48176f={};_[_0x3f37('0x49')](_0x48176f,util[_0x3f37('0x37')]('%s%s',_0x7fd09b['body'][_0x3f37('0x40')],'Pause'),![]);return _0x4799d5[_0x3f37('0x2c')](_0x48176f);});default:return db['UserVoiceQueueRt'][_0x3f37('0x2c')]({'paused':0x0},{'where':{'UserId':_0x4799d5['id']}})['then'](function(){return db[_0x3f37('0x2d')]['findAll']({'where':{'membername':_0x4799d5['name'],'type':'PAUSE','exitAt':null}});})[_0x3f37('0x2b')](function(_0x10e992){var _0x1a7b8a=[];_0x10e992[_0x3f37('0x34')](function(_0xf6a841){_0x1a7b8a['push'](_0xf6a841[_0x3f37('0x2c')]({'exitAt':moment()[_0x3f37('0x37')](_0x3f37('0x31')),'data2':_0x3f37('0x44')}));});return BPromise[_0x3f37('0x4a')](_0x1a7b8a);})['then'](function(){return _0x4799d5['update']({'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});});}}})[_0x3f37('0x2b')](function(_0x2de080){_0x100f72(_0x2de080);})[_0x3f37('0x1b')](function(_0x983ed0){_0x49a117(_0x186c9d[_0x3f37('0x18')](0x1f4,_0x983ed0[_0x3f37('0x1a')]));});});};exports[_0x3f37('0x4b')]=function(_0x280acd){var _0x2db462=this;return new Promise(function(_0x551d71,_0x180187){var _0x4159c0;return db[_0x3f37('0x3e')][_0x3f37('0x3f')]({'where':_0x280acd[_0x3f37('0x1d')]['where']||{},'attributes':['id',_0x3f37('0x28')]})[_0x3f37('0x2b')](function(_0x547317){if(_0x547317){_0x4159c0=_0x547317;return db[_0x3f37('0x4c')][_0x3f37('0x4d')]({'where':{'membername':_0x4159c0['name']},'individualHooks':!![]});}})[_0x3f37('0x2b')](function(){if(_0x4159c0){return db[_0x3f37('0x2d')]['findAll']({'where':{'membername':_0x4159c0['name'],'exitAt':null}});}})[_0x3f37('0x2b')](function(_0x481efd){if(_0x4159c0){if(_0x481efd&&_0x481efd[_0x3f37('0x30')]){var _0x112371=[];_0x481efd['forEach'](function(_0x497551){_0x112371[_0x3f37('0x35')](_0x497551[_0x3f37('0x2c')]({'exitAt':moment()[_0x3f37('0x37')]('YYYY-MM-DD\x20HH:mm:ss')}));});return BPromise[_0x3f37('0x4a')](_0x112371);}}})[_0x3f37('0x2b')](function(){if(_0x4159c0){return _0x4159c0[_0x3f37('0x2c')]({'online':![],'lastLoginAt':null,'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0x3f37('0x2b')](function(_0x4ab1c8){_0x551d71(_0x4ab1c8);})['catch'](function(_0x243eaf){_0x180187(_0x2db462['error'](0x1f4,_0x243eaf[_0x3f37('0x1a')]));});});};exports[_0x3f37('0x4e')]=function(_0x4c2308){var _0x3bed9e=this;return new Promise(function(_0x303fe7,_0x7ad191){var _0x36d981;var _0x357152;var _0x22d638;var _0x3dbaac;return db['User']['findOne']({'where':_0x4c2308['options'][_0x3f37('0x1f')]||{},'attributes':['id',_0x3f37('0x28'),'role',_0x3f37('0x4f'),_0x3f37('0x50'),_0x3f37('0x29'),'loginInPause','interface']})[_0x3f37('0x2b')](function(_0x2a3f3c){if(_0x2a3f3c){_0x36d981=_0x2a3f3c;_0x3dbaac=_0x4c2308[_0x3f37('0x38')][_0x3f37('0x2a')]?_0x4c2308['body']['interface']:util['format']('SIP/%s',_0x36d981[_0x3f37('0x28')]);return db[_0x3f37('0x3e')]['count']({'where':{'role':_0x3f37('0x51'),'online':!![],'id':{'$ne':_0x36d981['id']}}});}})['then'](function(_0x4bcc71){if(_0x4bcc71){_0x22d638=_0x4bcc71;return require('../../config/license/util')[_0x3f37('0x52')]();}})[_0x3f37('0x2b')](function(_0x278812){if(_0x278812&&_0x278812[_0x3f37('0x53')]&&_0x22d638>=_0x278812['agents']){throw new Error('Maximum\x20login\x20has\x20been\x20reached');}})['then'](function(){if(_0x36d981){return db[_0x3f37('0x4c')][_0x3f37('0x4d')]({'where':{'membername':_0x36d981[_0x3f37('0x28')]},'individualHooks':!![]});}})[_0x3f37('0x2b')](function(){if(_0x36d981){return _0x36d981['getVoiceQueues']({'raw':!![]});}})[_0x3f37('0x2b')](function(_0xaef540){if(_0x36d981){if(_0xaef540){var _0x3f9bb7=[];_0x357152=_0xaef540;if(_0x357152[_0x3f37('0x30')]){for(var _0x18704f=0x0;_0x18704f<_0x357152[_0x3f37('0x30')];_0x18704f++){_0x3f9bb7[_0x3f37('0x35')](db[_0x3f37('0x4c')][_0x3f37('0x54')]({'membername':_0x36d981['name'],'UserId':_0x36d981['id'],'queue_name':_0x357152[_0x18704f][_0x3f37('0x28')],'VoiceQueueId':_0x357152[_0x18704f]['id'],'interface':_0x3dbaac,'paused':_0x36d981[_0x3f37('0x55')]||![],'penalty':_0x357152[_0x18704f][_0x3f37('0x56')]||0x0}));}return BPromise[_0x3f37('0x4a')](_0x3f9bb7);}}}})[_0x3f37('0x2b')](function(){if(_0x36d981){return db[_0x3f37('0x2d')][_0x3f37('0x2e')]({'where':{'membername':_0x36d981[_0x3f37('0x28')],'exitAt':null}});}})[_0x3f37('0x2b')](function(_0x19245c){if(_0x36d981){if(_0x19245c&&_0x19245c['length']){var _0x5c5bbf=[];_0x19245c['forEach'](function(_0x1b42e9){_0x5c5bbf[_0x3f37('0x35')](_0x1b42e9[_0x3f37('0x2c')]({'exitAt':moment()['format'](_0x3f37('0x31'))}));});return BPromise[_0x3f37('0x4a')](_0x5c5bbf);}}})['then'](function(_0x179fe3){if(_0x36d981){var _0x527528=[];var _0x251e3e=[];var _0x52338d=utils[_0x3f37('0x33')]();var _0x246481={'membername':_0x36d981[_0x3f37('0x28')],'interface':_0x3dbaac,'enterAt':moment()[_0x3f37('0x37')](_0x3f37('0x31')),'role':_0x36d981[_0x3f37('0x3a')],'internal':_0x36d981['internal']};_0x52338d[_0x3f37('0x34')](function(_0xa4dc3f){_0x527528[_0x3f37('0x35')](_[_0x3f37('0x57')]({'channel':_0xa4dc3f,'type':_0x3f37('0x58'),'data1':_0x3f37('0x59'),'data2':_0x3dbaac},_0x246481));if(_0x36d981[_0x3f37('0x55')]){_0x251e3e[_0x3f37('0x35')](_[_0x3f37('0x57')]({'channel':_0xa4dc3f,'type':'PAUSE','data1':_0x3f37('0x5a')},_0x246481));}});return db[_0x3f37('0x2d')][_0x3f37('0x5b')](_0x527528[_0x3f37('0x5c')](_0x251e3e),{'individualHooks':!![]});}})[_0x3f37('0x2b')](function(){if(_0x36d981){return _0x36d981[_0x3f37('0x2c')]({'online':!![],'lastLoginAt':moment()['format'](_0x3f37('0x31')),'voicePause':_0x36d981[_0x3f37('0x55')]||![],'mailPause':_0x36d981['loginInPause']||![],'chatPause':_0x36d981[_0x3f37('0x55')]||![],'faxPause':_0x36d981[_0x3f37('0x55')]||![],'smsPause':_0x36d981[_0x3f37('0x55')]||![],'openchannelPause':_0x36d981[_0x3f37('0x55')]||![],'pauseType':_0x36d981['loginInPause']?_0x3f37('0x5a'):null,'lastPauseAt':moment()[_0x3f37('0x37')](_0x3f37('0x31')),'interface':_0x3dbaac});}})[_0x3f37('0x2b')](function(_0x19b5e1){_0x303fe7(_0x19b5e1);})[_0x3f37('0x1b')](function(_0x484e74){_0x7ad191(_0x3bed9e['error'](0x1f4,_0x484e74[_0x3f37('0x1a')]));});});};