761be9f75fe887c509ac8719b375191a4d60b7fd
[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 _0x5e30=['UnpauseUser','findOne','channel','isValidChannel','Unknown\x20channel','system','mail','fax','whatsapp','set','%s%s','Pause','all','destroy','LoginUser','mobile','phone','loginInPause','count','agent','../../config/license/util','getLicense','agents','Maximum\x20login\x20has\x20been\x20reached','getVoiceQueues','upsert','merge','LOGIN','callysquare','LOGININPAUSE','concat','lodash','util','moment','bluebird','randomstring','../../config/logger','rpc','../../config/environment','jayson/promise','client','http','redis','localhost','./user.socket','register','request','then','User,\x20%s,\x20%s','request\x20sent','User,\x20%s,\x20%s,\x20%s','stringify','error','code','message','info','catch','GetUser','User','options','where','attributes','include','map','model','debug','find','ShowUser','PauseUser','role','internal','UserVoiceQueueRt','update','MemberReport','findAll','name','length','updateAttributes','getChannels','forEach','push','isNil','interface','format','SIP/%s','PAUSE','YYYY-MM-DD\x20HH:mm:ss','body','type','uniqueid','bulkCreate','DEFAULT\x20PAUSE'];(function(_0xa1c0f5,_0x3c9548){var _0x58937c=function(_0x544326){while(--_0x544326){_0xa1c0f5['push'](_0xa1c0f5['shift']());}};_0x58937c(++_0x3c9548);}(_0x5e30,0x7b));var _0x05e3=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x5e30[_0x3dd15e];return _0x231fd0;};'use strict';var _=require(_0x05e3('0x0'));var util=require(_0x05e3('0x1'));var moment=require(_0x05e3('0x2'));var BPromise=require(_0x05e3('0x3'));var rs=require(_0x05e3('0x4'));var fs=require('fs');var Redis=require('ioredis');var db=require('../../mysqldb')['db'];var utils=require('../../config/utils');var logger=require(_0x05e3('0x5'))(_0x05e3('0x6'));var config=require(_0x05e3('0x7'));var jayson=require(_0x05e3('0x8'));var client=jayson[_0x05e3('0x9')][_0x05e3('0xa')]({'port':0x232a});config['redis']=_['defaults'](config[_0x05e3('0xb')],{'host':_0x05e3('0xc'),'port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config[_0x05e3('0xb')]));require(_0x05e3('0xd'))[_0x05e3('0xe')](socket);function respondWithRpcPromise(_0x2afc34,_0x57ad6d,_0x5a98f8){return new BPromise(function(_0xb9bc1e,_0x54cda5){return client[_0x05e3('0xf')](_0x2afc34,_0x5a98f8)[_0x05e3('0x10')](function(_0x20851e){logger['info'](_0x05e3('0x11'),_0x57ad6d,_0x05e3('0x12'));logger['debug'](_0x05e3('0x13'),_0x57ad6d,'request\x20sent',JSON[_0x05e3('0x14')](_0x20851e));if(_0x20851e[_0x05e3('0x15')]){if(_0x20851e[_0x05e3('0x15')][_0x05e3('0x16')]===0x1f4){logger['error'](_0x05e3('0x11'),_0x57ad6d,_0x20851e['error'][_0x05e3('0x17')]);return _0x54cda5(_0x20851e['error']['message']);}logger[_0x05e3('0x15')](_0x05e3('0x11'),_0x57ad6d,_0x20851e[_0x05e3('0x15')]['message']);return _0xb9bc1e(_0x20851e[_0x05e3('0x15')][_0x05e3('0x17')]);}else{logger[_0x05e3('0x18')](_0x05e3('0x11'),_0x57ad6d,_0x05e3('0x12'));_0xb9bc1e(_0x20851e['result'][_0x05e3('0x17')]);}})[_0x05e3('0x19')](function(_0x1bcac9){logger[_0x05e3('0x15')](_0x05e3('0x11'),_0x57ad6d,_0x1bcac9);_0x54cda5(_0x1bcac9);});});}exports[_0x05e3('0x1a')]=function(_0x57c59f){var _0x493237=this;return new Promise(function(_0x53e346,_0x26242d){return db[_0x05e3('0x1b')]['findAll']({'raw':_0x57c59f[_0x05e3('0x1c')]?_0x57c59f[_0x05e3('0x1c')]['raw']===undefined?!![]:![]:!![],'where':_0x57c59f[_0x05e3('0x1c')]?_0x57c59f[_0x05e3('0x1c')][_0x05e3('0x1d')]||null:null,'attributes':_0x57c59f[_0x05e3('0x1c')]?_0x57c59f[_0x05e3('0x1c')][_0x05e3('0x1e')]||null:null,'limit':_0x57c59f[_0x05e3('0x1c')]?_0x57c59f[_0x05e3('0x1c')]['limit']||null:null,'include':_0x57c59f['options']?_0x57c59f[_0x05e3('0x1c')][_0x05e3('0x1f')]?_[_0x05e3('0x20')](_0x57c59f[_0x05e3('0x1c')]['include'],function(_0x522a45){return{'model':db[_0x522a45[_0x05e3('0x21')]],'as':_0x522a45['as'],'attributes':_0x522a45[_0x05e3('0x1e')],'include':_0x522a45[_0x05e3('0x1f')]?_['map'](_0x522a45['include'],function(_0x155a61){return{'model':db[_0x155a61[_0x05e3('0x21')]],'as':_0x155a61['as'],'attributes':_0x155a61[_0x05e3('0x1e')],'include':_0x155a61[_0x05e3('0x1f')]?_[_0x05e3('0x20')](_0x155a61['include'],function(_0x27d7e7){return{'model':db[_0x27d7e7[_0x05e3('0x21')]],'as':_0x27d7e7['as'],'attributes':_0x27d7e7[_0x05e3('0x1e')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x14a6ed){logger['info'](_0x05e3('0x1a'),_0x57c59f);logger[_0x05e3('0x22')]('GetUser',_0x57c59f,JSON[_0x05e3('0x14')](_0x14a6ed));_0x53e346(_0x14a6ed);})[_0x05e3('0x19')](function(_0x439309){logger[_0x05e3('0x15')](_0x05e3('0x1a'),_0x439309[_0x05e3('0x17')],_0x57c59f);_0x26242d(_0x493237[_0x05e3('0x15')](0x1f4,_0x439309[_0x05e3('0x17')]));});});};exports['ShowUser']=function(_0xfe2282){var _0x1fc38d=this;return new Promise(function(_0x22668d,_0x4cac45){return db[_0x05e3('0x1b')][_0x05e3('0x23')]({'raw':_0xfe2282['options']?_0xfe2282[_0x05e3('0x1c')]['raw']===undefined?!![]:![]:!![],'where':_0xfe2282['options']?_0xfe2282[_0x05e3('0x1c')][_0x05e3('0x1d')]||null:null,'attributes':_0xfe2282[_0x05e3('0x1c')]?_0xfe2282[_0x05e3('0x1c')][_0x05e3('0x1e')]||null:null,'include':_0xfe2282['options']?_0xfe2282[_0x05e3('0x1c')][_0x05e3('0x1f')]?_['map'](_0xfe2282['options'][_0x05e3('0x1f')],function(_0x5f4f42){return{'model':db[_0x5f4f42['model']],'as':_0x5f4f42['as'],'attributes':_0x5f4f42['attributes'],'include':_0x5f4f42['include']?_[_0x05e3('0x20')](_0x5f4f42[_0x05e3('0x1f')],function(_0x9dbeb2){return{'model':db[_0x9dbeb2[_0x05e3('0x21')]],'as':_0x9dbeb2['as'],'attributes':_0x9dbeb2[_0x05e3('0x1e')],'include':_0x9dbeb2[_0x05e3('0x1f')]?_[_0x05e3('0x20')](_0x9dbeb2[_0x05e3('0x1f')],function(_0x2aa618){return{'model':db[_0x2aa618[_0x05e3('0x21')]],'as':_0x2aa618['as'],'attributes':_0x2aa618[_0x05e3('0x1e')]};}):[]};}):[]};}):[]:[]})[_0x05e3('0x10')](function(_0x56794e){logger[_0x05e3('0x18')](_0x05e3('0x24'),_0xfe2282);logger[_0x05e3('0x22')]('ShowUser',_0xfe2282,JSON[_0x05e3('0x14')](_0x56794e));_0x22668d(_0x56794e);})[_0x05e3('0x19')](function(_0x46669c){logger[_0x05e3('0x15')](_0x05e3('0x24'),_0x46669c['message'],_0xfe2282);_0x4cac45(_0x1fc38d[_0x05e3('0x15')](0x1f4,_0x46669c[_0x05e3('0x17')]));});});};exports[_0x05e3('0x25')]=function(_0xe9b1aa){var _0x41f645=this;return new Promise(function(_0x5e6320,_0x95094d){var _0x448c75;return db[_0x05e3('0x1b')]['find']({'where':_0xe9b1aa[_0x05e3('0x1c')][_0x05e3('0x1d')]||{},'attributes':['id','name',_0x05e3('0x26'),_0x05e3('0x27'),'interface']})[_0x05e3('0x10')](function(_0x2906fc){if(_0x2906fc){_0x448c75=_0x2906fc;return db[_0x05e3('0x28')][_0x05e3('0x29')]({'paused':0x1},{'where':{'UserId':_0x448c75['id']},'individualHooks':!![]});}})[_0x05e3('0x10')](function(){if(_0x448c75){return db[_0x05e3('0x2a')][_0x05e3('0x2b')]({'where':{'membername':_0x448c75[_0x05e3('0x2c')],'type':'PAUSE','exitAt':null}});}})[_0x05e3('0x10')](function(_0x4a97d1){if(_0x448c75){if(_0x4a97d1&&_0x4a97d1['length']){var _0x1847e0=moment()['format']('YYYY-MM-DD\x20HH:mm:ss');for(var _0x6db49a=0x0,_0x2ffd5d=_0x4a97d1[_0x05e3('0x2d')];_0x6db49a<_0x2ffd5d;_0x6db49a++){_0x4a97d1[_0x6db49a][_0x05e3('0x2e')]({'exitAt':_0x1847e0});}}}})[_0x05e3('0x10')](function(){if(_0x448c75){var _0x5b1bcd=[];var _0x291b4f=utils[_0x05e3('0x2f')]();_0x291b4f[_0x05e3('0x30')](function(_0x358e74){_0x5b1bcd[_0x05e3('0x31')]({'channel':_0x358e74,'membername':_0x448c75[_0x05e3('0x2c')],'interface':_[_0x05e3('0x32')](_0x448c75[_0x05e3('0x33')])?util[_0x05e3('0x34')](_0x05e3('0x35'),_0x448c75[_0x05e3('0x2c')]):_0x448c75[_0x05e3('0x33')],'type':_0x05e3('0x36'),'enterAt':moment()[_0x05e3('0x34')](_0x05e3('0x37')),'data1':_0xe9b1aa[_0x05e3('0x38')][_0x05e3('0x39')]||'DEFAULT\x20PAUSE','role':_0x448c75['role'],'internal':_0x448c75['internal'],'uniqueid':_0xe9b1aa[_0x05e3('0x38')][_0x05e3('0x3a')]||null});});return db[_0x05e3('0x2a')][_0x05e3('0x3b')](_0x5b1bcd,{'individualHooks':!![]});}})[_0x05e3('0x10')](function(){if(_0x448c75){return _0x448c75['update']({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0xe9b1aa[_0x05e3('0x38')]['type']||_0x05e3('0x3c'),'lastPauseAt':moment()['format'](_0x05e3('0x37'))});}})['then'](function(_0x36c148){_0x5e6320(_0x36c148);})['catch'](function(_0x28b605){_0x95094d(_0x41f645['error'](0x1f4,_0x28b605[_0x05e3('0x17')]));});});};exports[_0x05e3('0x3d')]=function(_0x1e4942){var _0x46e4c0=this;return new Promise(function(_0x222b30,_0x20612f){var _0x2f5451;return db[_0x05e3('0x1b')][_0x05e3('0x3e')]({'where':_0x1e4942['options'][_0x05e3('0x1d')]||{},'attributes':['id',_0x05e3('0x2c')]})[_0x05e3('0x10')](function(_0x233e8e){if(_0x233e8e){_0x2f5451=_0x233e8e;if(_0x1e4942[_0x05e3('0x38')][_0x05e3('0x3f')]&&!utils[_0x05e3('0x40')](_0x1e4942[_0x05e3('0x38')][_0x05e3('0x3f')])){throw new Error(_0x05e3('0x41'));}switch(_0x1e4942['body']['channel']){case'voice':return db[_0x05e3('0x28')]['update']({'paused':0x0},{'where':{'UserId':_0x2f5451['id']}})[_0x05e3('0x10')](function(){return db[_0x05e3('0x2a')][_0x05e3('0x23')]({'where':{'channel':_0x1e4942['body'][_0x05e3('0x3f')],'membername':_0x2f5451[_0x05e3('0x2c')],'type':_0x05e3('0x36'),'exitAt':null}});})[_0x05e3('0x10')](function(_0x48f281){if(_0x48f281){return _0x48f281[_0x05e3('0x29')]({'exitAt':moment()[_0x05e3('0x34')](_0x05e3('0x37')),'data2':_0x05e3('0x42')},{'individualHooks':!![]});}return null;})[_0x05e3('0x10')](function(){return _0x2f5451[_0x05e3('0x29')]({'voicePause':![]});});case _0x05e3('0x43'):case'chat':case _0x05e3('0x44'):case'sms':case'openchannel':case _0x05e3('0x45'):return db['MemberReport'][_0x05e3('0x23')]({'where':{'channel':_0x1e4942[_0x05e3('0x38')][_0x05e3('0x3f')],'membername':_0x2f5451[_0x05e3('0x2c')],'type':_0x05e3('0x36'),'exitAt':null}})[_0x05e3('0x10')](function(_0x2eb94a){return _0x2eb94a['update']({'exitAt':moment()[_0x05e3('0x34')](_0x05e3('0x37')),'data2':_0x05e3('0x42')},{'individualHooks':!![]});})['then'](function(){var _0x48588f={};_[_0x05e3('0x46')](_0x48588f,util[_0x05e3('0x34')](_0x05e3('0x47'),_0x1e4942[_0x05e3('0x38')][_0x05e3('0x3f')],_0x05e3('0x48')),![]);return _0x2f5451['update'](_0x48588f);});default:return db[_0x05e3('0x28')][_0x05e3('0x29')]({'paused':0x0},{'where':{'UserId':_0x2f5451['id']}})[_0x05e3('0x10')](function(){return db[_0x05e3('0x2a')][_0x05e3('0x2b')]({'where':{'membername':_0x2f5451[_0x05e3('0x2c')],'type':_0x05e3('0x36'),'exitAt':null}});})[_0x05e3('0x10')](function(_0x38f1bf){var _0x1160f1=[];_0x38f1bf[_0x05e3('0x30')](function(_0x41ae8b){_0x1160f1[_0x05e3('0x31')](_0x41ae8b[_0x05e3('0x29')]({'exitAt':moment()['format'](_0x05e3('0x37')),'data2':_0x05e3('0x42')}));});return BPromise[_0x05e3('0x49')](_0x1160f1);})[_0x05e3('0x10')](function(){return _0x2f5451[_0x05e3('0x29')]({'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![],'whatsappPause':![]});});}}})[_0x05e3('0x10')](function(_0x47d7fa){_0x222b30(_0x47d7fa);})[_0x05e3('0x19')](function(_0x35c813){_0x20612f(_0x46e4c0[_0x05e3('0x15')](0x1f4,_0x35c813[_0x05e3('0x17')]));});});};exports['LogoutUser']=function(_0x42d7ee){var _0x164c2f=this;return new Promise(function(_0x146296,_0x1709f6){var _0x54cbe7;return db[_0x05e3('0x1b')][_0x05e3('0x3e')]({'where':_0x42d7ee[_0x05e3('0x1c')]['where']||{},'attributes':['id',_0x05e3('0x2c')]})['then'](function(_0x202ba7){if(_0x202ba7){_0x54cbe7=_0x202ba7;return db[_0x05e3('0x28')][_0x05e3('0x4a')]({'where':{'membername':_0x54cbe7[_0x05e3('0x2c')]},'individualHooks':!![]});}})[_0x05e3('0x10')](function(){if(_0x54cbe7){return db[_0x05e3('0x2a')]['findAll']({'where':{'membername':_0x54cbe7[_0x05e3('0x2c')],'exitAt':null}});}})['then'](function(_0x576063){if(_0x54cbe7){if(_0x576063&&_0x576063[_0x05e3('0x2d')]){var _0x2d5068=[];_0x576063[_0x05e3('0x30')](function(_0x532493){_0x2d5068['push'](_0x532493['update']({'exitAt':moment()[_0x05e3('0x34')](_0x05e3('0x37'))}));});return BPromise[_0x05e3('0x49')](_0x2d5068);}}})[_0x05e3('0x10')](function(){if(_0x54cbe7){return _0x54cbe7['update']({'online':![],'lastLoginAt':null,'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0x05e3('0x10')](function(_0x570d08){_0x146296(_0x570d08);})['catch'](function(_0x1cb724){_0x1709f6(_0x164c2f[_0x05e3('0x15')](0x1f4,_0x1cb724[_0x05e3('0x17')]));});});};exports[_0x05e3('0x4b')]=function(_0x2c1475){var _0x213a28=this;return new Promise(function(_0x2848d6,_0x26e7ed){var _0x10f039;var _0x201e14;var _0x3e9c73;var _0x91339f;return db['User'][_0x05e3('0x3e')]({'where':_0x2c1475[_0x05e3('0x1c')]['where']||{},'attributes':['id','name',_0x05e3('0x26'),_0x05e3('0x4c'),_0x05e3('0x4d'),_0x05e3('0x27'),_0x05e3('0x4e'),_0x05e3('0x33')]})['then'](function(_0x4b2b21){if(_0x4b2b21){_0x10f039=_0x4b2b21;_0x91339f=_0x2c1475['body'][_0x05e3('0x33')]?_0x2c1475['body'][_0x05e3('0x33')]:util[_0x05e3('0x34')]('SIP/%s',_0x10f039[_0x05e3('0x2c')]);return db['User'][_0x05e3('0x4f')]({'where':{'role':_0x05e3('0x50'),'online':!![],'id':{'$ne':_0x10f039['id']}}});}})[_0x05e3('0x10')](function(_0x591c83){if(_0x591c83){_0x3e9c73=_0x591c83;return require(_0x05e3('0x51'))[_0x05e3('0x52')]();}})[_0x05e3('0x10')](function(_0x2ce41f){if(_0x2ce41f&&_0x2ce41f['agents']&&_0x3e9c73>=_0x2ce41f[_0x05e3('0x53')]){throw new Error(_0x05e3('0x54'));}})['then'](function(){if(_0x10f039){return db['UserVoiceQueueRt']['destroy']({'where':{'membername':_0x10f039[_0x05e3('0x2c')]},'individualHooks':!![]});}})[_0x05e3('0x10')](function(){if(_0x10f039){return _0x10f039[_0x05e3('0x55')]({'raw':!![]});}})['then'](function(_0x33af90){if(_0x10f039){if(_0x33af90){var _0x571596=[];_0x201e14=_0x33af90;if(_0x201e14['length']){for(var _0x3d5b6e=0x0;_0x3d5b6e<_0x201e14[_0x05e3('0x2d')];_0x3d5b6e++){_0x571596['push'](db[_0x05e3('0x28')][_0x05e3('0x56')]({'membername':_0x10f039[_0x05e3('0x2c')],'UserId':_0x10f039['id'],'queue_name':_0x201e14[_0x3d5b6e][_0x05e3('0x2c')],'VoiceQueueId':_0x201e14[_0x3d5b6e]['id'],'interface':_0x91339f,'paused':_0x10f039['loginInPause']||![],'penalty':_0x201e14[_0x3d5b6e]['UserVoiceQueue.penalty']||0x0}));}return BPromise['all'](_0x571596);}}}})[_0x05e3('0x10')](function(){if(_0x10f039){return db[_0x05e3('0x2a')][_0x05e3('0x2b')]({'where':{'membername':_0x10f039['name'],'exitAt':null}});}})[_0x05e3('0x10')](function(_0x3e6bb1){if(_0x10f039){if(_0x3e6bb1&&_0x3e6bb1[_0x05e3('0x2d')]){var _0x27bb10=[];_0x3e6bb1[_0x05e3('0x30')](function(_0x3fcf93){_0x27bb10[_0x05e3('0x31')](_0x3fcf93[_0x05e3('0x29')]({'exitAt':moment()[_0x05e3('0x34')]('YYYY-MM-DD\x20HH:mm:ss')}));});return BPromise['all'](_0x27bb10);}}})[_0x05e3('0x10')](function(_0x4e68b7){if(_0x10f039){var _0x4bddff=[];var _0x3d9f33=[];var _0x46f4eb=utils[_0x05e3('0x2f')]();var _0x2de9ef={'membername':_0x10f039['name'],'interface':_0x91339f,'enterAt':moment()['format'](_0x05e3('0x37')),'role':_0x10f039[_0x05e3('0x26')],'internal':_0x10f039[_0x05e3('0x27')]};_0x46f4eb['forEach'](function(_0x4c6703){_0x4bddff[_0x05e3('0x31')](_[_0x05e3('0x57')]({'channel':_0x4c6703,'type':_0x05e3('0x58'),'data1':_0x05e3('0x59'),'data2':_0x91339f},_0x2de9ef));if(_0x10f039[_0x05e3('0x4e')]){_0x3d9f33['push'](_['merge']({'channel':_0x4c6703,'type':_0x05e3('0x36'),'data1':_0x05e3('0x5a')},_0x2de9ef));}});return db[_0x05e3('0x2a')]['bulkCreate'](_0x4bddff[_0x05e3('0x5b')](_0x3d9f33),{'individualHooks':!![]});}})['then'](function(){if(_0x10f039){return _0x10f039[_0x05e3('0x29')]({'online':!![],'lastLoginAt':moment()[_0x05e3('0x34')](_0x05e3('0x37')),'voicePause':_0x10f039[_0x05e3('0x4e')]||![],'mailPause':_0x10f039['loginInPause']||![],'chatPause':_0x10f039[_0x05e3('0x4e')]||![],'faxPause':_0x10f039[_0x05e3('0x4e')]||![],'smsPause':_0x10f039[_0x05e3('0x4e')]||![],'openchannelPause':_0x10f039[_0x05e3('0x4e')]||![],'pauseType':_0x10f039[_0x05e3('0x4e')]?_0x05e3('0x5a'):null,'lastPauseAt':moment()[_0x05e3('0x34')](_0x05e3('0x37')),'interface':_0x91339f});}})[_0x05e3('0x10')](function(_0x1f11b5){_0x2848d6(_0x1f11b5);})[_0x05e3('0x19')](function(_0x4c97fc){_0x26e7ed(_0x213a28['error'](0x1f4,_0x4c97fc['message']));});});};