11746c782d35d4f7ee2ab42d2be6c539a478b091
[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 _0x9f28=['../../config/environment','redis','defaults','socket.io-emitter','./user.socket','register','request','info','debug','User,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','error','code','User,\x20%s,\x20%s','message','catch','GetUser','User','options','where','attributes','limit','include','map','model','then','ShowUser','find','raw','PauseUser','name','internal','UserVoiceQueueRt','MemberReport','PAUSE','format','length','getChannels','forEach','isNil','YYYY-MM-DD\x20HH:mm:ss','body','DEFAULT\x20PAUSE','role','bulkCreate','update','type','channel','Unknown\x20channel','voice','system','mail','chat','sms','openchannel','set','findAll','push','destroy','all','LoginUser','mobile','interface','SIP/%s','count','agent','getLicense','agents','getVoiceQueues','upsert','UserVoiceQueue.penalty','merge','loginInPause','LOGININPAUSE','concat','util','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger'];(function(_0x44c12a,_0x566a6f){var _0x3d0548=function(_0x51e04f){while(--_0x51e04f){_0x44c12a['push'](_0x44c12a['shift']());}};_0x3d0548(++_0x566a6f);}(_0x9f28,0xf0));var _0x89f2=function(_0x3f226b,_0x16169c){_0x3f226b=_0x3f226b-0x0;var _0x3b611a=_0x9f28[_0x3f226b];return _0x3b611a;};'use strict';var _=require('lodash');var util=require(_0x89f2('0x0'));var moment=require('moment');var BPromise=require('bluebird');var rs=require(_0x89f2('0x1'));var fs=require('fs');var Redis=require(_0x89f2('0x2'));var db=require(_0x89f2('0x3'))['db'];var utils=require(_0x89f2('0x4'));var logger=require(_0x89f2('0x5'))('rpc');var config=require(_0x89f2('0x6'));var jayson=require('jayson/promise');var client=jayson['client']['http']({'port':0x232a});config[_0x89f2('0x7')]=_[_0x89f2('0x8')](config['redis'],{'host':'localhost','port':0x18eb});var socket=require(_0x89f2('0x9'))(new Redis(config[_0x89f2('0x7')]));require(_0x89f2('0xa'))[_0x89f2('0xb')](socket);function respondWithRpcPromise(_0x590281,_0x42ef0d,_0x5ae818){return new BPromise(function(_0x3b7623,_0x259201){return client[_0x89f2('0xc')](_0x590281,_0x5ae818)['then'](function(_0x5a2183){logger[_0x89f2('0xd')]('User,\x20%s,\x20%s',_0x42ef0d,'request\x20sent');logger[_0x89f2('0xe')](_0x89f2('0xf'),_0x42ef0d,_0x89f2('0x10'),JSON[_0x89f2('0x11')](_0x5a2183));if(_0x5a2183[_0x89f2('0x12')]){if(_0x5a2183[_0x89f2('0x12')][_0x89f2('0x13')]===0x1f4){logger[_0x89f2('0x12')](_0x89f2('0x14'),_0x42ef0d,_0x5a2183[_0x89f2('0x12')][_0x89f2('0x15')]);return _0x259201(_0x5a2183[_0x89f2('0x12')][_0x89f2('0x15')]);}logger['error'](_0x89f2('0x14'),_0x42ef0d,_0x5a2183[_0x89f2('0x12')][_0x89f2('0x15')]);return _0x3b7623(_0x5a2183[_0x89f2('0x12')][_0x89f2('0x15')]);}else{logger[_0x89f2('0xd')](_0x89f2('0x14'),_0x42ef0d,_0x89f2('0x10'));_0x3b7623(_0x5a2183['result'][_0x89f2('0x15')]);}})[_0x89f2('0x16')](function(_0x4759e1){logger[_0x89f2('0x12')](_0x89f2('0x14'),_0x42ef0d,_0x4759e1);_0x259201(_0x4759e1);});});}exports[_0x89f2('0x17')]=function(_0x4ee8fa){var _0x3f9b12=this;return new Promise(function(_0x523401,_0x2878d9){return db[_0x89f2('0x18')]['findAll']({'raw':_0x4ee8fa[_0x89f2('0x19')]?_0x4ee8fa[_0x89f2('0x19')]['raw']===undefined?!![]:![]:!![],'where':_0x4ee8fa[_0x89f2('0x19')]?_0x4ee8fa[_0x89f2('0x19')][_0x89f2('0x1a')]||null:null,'attributes':_0x4ee8fa[_0x89f2('0x19')]?_0x4ee8fa['options'][_0x89f2('0x1b')]||null:null,'limit':_0x4ee8fa['options']?_0x4ee8fa[_0x89f2('0x19')][_0x89f2('0x1c')]||null:null,'include':_0x4ee8fa[_0x89f2('0x19')]?_0x4ee8fa[_0x89f2('0x19')][_0x89f2('0x1d')]?_[_0x89f2('0x1e')](_0x4ee8fa[_0x89f2('0x19')][_0x89f2('0x1d')],function(_0x3c1c2c){return{'model':db[_0x3c1c2c[_0x89f2('0x1f')]],'as':_0x3c1c2c['as'],'attributes':_0x3c1c2c[_0x89f2('0x1b')],'include':_0x3c1c2c['include']?_[_0x89f2('0x1e')](_0x3c1c2c[_0x89f2('0x1d')],function(_0x32a246){return{'model':db[_0x32a246[_0x89f2('0x1f')]],'as':_0x32a246['as'],'attributes':_0x32a246['attributes'],'include':_0x32a246[_0x89f2('0x1d')]?_[_0x89f2('0x1e')](_0x32a246[_0x89f2('0x1d')],function(_0x547a11){return{'model':db[_0x547a11[_0x89f2('0x1f')]],'as':_0x547a11['as'],'attributes':_0x547a11[_0x89f2('0x1b')]};}):[]};}):[]};}):[]:[]})[_0x89f2('0x20')](function(_0x364410){logger[_0x89f2('0xd')](_0x89f2('0x17'),_0x4ee8fa);logger[_0x89f2('0xe')](_0x89f2('0x17'),_0x4ee8fa,JSON[_0x89f2('0x11')](_0x364410));_0x523401(_0x364410);})['catch'](function(_0x58c1d4){logger[_0x89f2('0x12')](_0x89f2('0x17'),_0x58c1d4[_0x89f2('0x15')],_0x4ee8fa);_0x2878d9(_0x3f9b12['error'](0x1f4,_0x58c1d4[_0x89f2('0x15')]));});});};exports[_0x89f2('0x21')]=function(_0x18d023){var _0x4b3499=this;return new Promise(function(_0x58a43b,_0x446afe){return db[_0x89f2('0x18')][_0x89f2('0x22')]({'raw':_0x18d023['options']?_0x18d023[_0x89f2('0x19')][_0x89f2('0x23')]===undefined?!![]:![]:!![],'where':_0x18d023[_0x89f2('0x19')]?_0x18d023[_0x89f2('0x19')]['where']||null:null,'attributes':_0x18d023[_0x89f2('0x19')]?_0x18d023[_0x89f2('0x19')][_0x89f2('0x1b')]||null:null,'include':_0x18d023['options']?_0x18d023[_0x89f2('0x19')][_0x89f2('0x1d')]?_[_0x89f2('0x1e')](_0x18d023[_0x89f2('0x19')]['include'],function(_0x139589){return{'model':db[_0x139589[_0x89f2('0x1f')]],'as':_0x139589['as'],'attributes':_0x139589[_0x89f2('0x1b')],'include':_0x139589['include']?_[_0x89f2('0x1e')](_0x139589[_0x89f2('0x1d')],function(_0x1624df){return{'model':db[_0x1624df['model']],'as':_0x1624df['as'],'attributes':_0x1624df[_0x89f2('0x1b')],'include':_0x1624df[_0x89f2('0x1d')]?_['map'](_0x1624df['include'],function(_0x4a11d5){return{'model':db[_0x4a11d5[_0x89f2('0x1f')]],'as':_0x4a11d5['as'],'attributes':_0x4a11d5[_0x89f2('0x1b')]};}):[]};}):[]};}):[]:[]})[_0x89f2('0x20')](function(_0x18faae){logger['info']('ShowUser',_0x18d023);logger['debug'](_0x89f2('0x21'),_0x18d023,JSON[_0x89f2('0x11')](_0x18faae));_0x58a43b(_0x18faae);})['catch'](function(_0x545a6e){logger[_0x89f2('0x12')](_0x89f2('0x21'),_0x545a6e[_0x89f2('0x15')],_0x18d023);_0x446afe(_0x4b3499[_0x89f2('0x12')](0x1f4,_0x545a6e['message']));});});};exports[_0x89f2('0x24')]=function(_0x4ac476){var _0x228840=this;return new Promise(function(_0x399582,_0x4f7845){var _0x494213;return db['User'][_0x89f2('0x22')]({'where':_0x4ac476[_0x89f2('0x19')][_0x89f2('0x1a')]||{},'attributes':['id',_0x89f2('0x25'),'role',_0x89f2('0x26'),'interface']})['then'](function(_0x45fccb){if(_0x45fccb){_0x494213=_0x45fccb;return db[_0x89f2('0x27')]['update']({'paused':0x1},{'where':{'UserId':_0x494213['id']},'individualHooks':!![]});}})[_0x89f2('0x20')](function(){if(_0x494213){return db[_0x89f2('0x28')]['findAll']({'where':{'membername':_0x494213[_0x89f2('0x25')],'type':_0x89f2('0x29'),'exitAt':null}});}})[_0x89f2('0x20')](function(_0xd4a1a6){if(_0x494213){if(_0xd4a1a6&&_0xd4a1a6['length']){var _0x58d53a=moment()[_0x89f2('0x2a')]('YYYY-MM-DD\x20HH:mm:ss');for(var _0x2d25be=0x0,_0x2434f3=_0xd4a1a6[_0x89f2('0x2b')];_0x2d25be<_0x2434f3;_0x2d25be++){_0xd4a1a6[_0x2d25be]['updateAttributes']({'exitAt':_0x58d53a});}}}})[_0x89f2('0x20')](function(){if(_0x494213){var _0x5c5d87=[];var _0x1d67c8=utils[_0x89f2('0x2c')]();_0x1d67c8[_0x89f2('0x2d')](function(_0x58d5dc){_0x5c5d87['push']({'channel':_0x58d5dc,'membername':_0x494213[_0x89f2('0x25')],'interface':_[_0x89f2('0x2e')](_0x494213['interface'])?util[_0x89f2('0x2a')]('SIP/%s',_0x494213[_0x89f2('0x25')]):_0x494213['interface'],'type':_0x89f2('0x29'),'enterAt':moment()['format'](_0x89f2('0x2f')),'data1':_0x4ac476[_0x89f2('0x30')]['type']||_0x89f2('0x31'),'role':_0x494213[_0x89f2('0x32')],'internal':_0x494213[_0x89f2('0x26')],'uniqueid':_0x4ac476[_0x89f2('0x30')]['uniqueid']||null});});return db[_0x89f2('0x28')][_0x89f2('0x33')](_0x5c5d87,{'individualHooks':!![]});}})['then'](function(){if(_0x494213){return _0x494213[_0x89f2('0x34')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'pauseType':_0x4ac476['body'][_0x89f2('0x35')]||_0x89f2('0x31'),'lastPauseAt':moment()[_0x89f2('0x2a')](_0x89f2('0x2f'))});}})[_0x89f2('0x20')](function(_0xf869c2){_0x399582(_0xf869c2);})[_0x89f2('0x16')](function(_0x4f2c9e){_0x4f7845(_0x228840['error'](0x1f4,_0x4f2c9e[_0x89f2('0x15')]));});});};exports['UnpauseUser']=function(_0x205669){var _0x55e015=this;return new Promise(function(_0xd1c96e,_0x532006){var _0x1d592b;return db[_0x89f2('0x18')]['findOne']({'where':_0x205669['options'][_0x89f2('0x1a')]||{},'attributes':['id','name']})[_0x89f2('0x20')](function(_0x359c41){if(_0x359c41){_0x1d592b=_0x359c41;if(_0x205669[_0x89f2('0x30')][_0x89f2('0x36')]&&!utils['isValidChannel'](_0x205669[_0x89f2('0x30')]['channel'])){throw new Error(_0x89f2('0x37'));}switch(_0x205669['body'][_0x89f2('0x36')]){case _0x89f2('0x38'):return db[_0x89f2('0x27')][_0x89f2('0x34')]({'paused':0x0},{'where':{'UserId':_0x1d592b['id']}})['then'](function(){return db[_0x89f2('0x28')][_0x89f2('0x22')]({'where':{'channel':_0x205669['body'][_0x89f2('0x36')],'membername':_0x1d592b['name'],'type':_0x89f2('0x29'),'exitAt':null}});})[_0x89f2('0x20')](function(_0x177efb){if(_0x177efb){return _0x177efb[_0x89f2('0x34')]({'exitAt':moment()[_0x89f2('0x2a')](_0x89f2('0x2f')),'data2':_0x89f2('0x39')},{'individualHooks':!![]});}return null;})[_0x89f2('0x20')](function(){return _0x1d592b[_0x89f2('0x34')]({'voicePause':![]});});case _0x89f2('0x3a'):case _0x89f2('0x3b'):case'fax':case _0x89f2('0x3c'):case _0x89f2('0x3d'):return db['MemberReport']['find']({'where':{'channel':_0x205669[_0x89f2('0x30')][_0x89f2('0x36')],'membername':_0x1d592b['name'],'type':_0x89f2('0x29'),'exitAt':null}})['then'](function(_0x45cc9c){return _0x45cc9c[_0x89f2('0x34')]({'exitAt':moment()[_0x89f2('0x2a')](_0x89f2('0x2f')),'data2':'system'},{'individualHooks':!![]});})[_0x89f2('0x20')](function(){var _0x5f0db4={};_[_0x89f2('0x3e')](_0x5f0db4,util[_0x89f2('0x2a')]('%s%s',_0x205669[_0x89f2('0x30')]['channel'],'Pause'),![]);return _0x1d592b[_0x89f2('0x34')](_0x5f0db4);});default:return db[_0x89f2('0x27')]['update']({'paused':0x0},{'where':{'UserId':_0x1d592b['id']}})[_0x89f2('0x20')](function(){return db[_0x89f2('0x28')][_0x89f2('0x3f')]({'where':{'membername':_0x1d592b[_0x89f2('0x25')],'type':'PAUSE','exitAt':null}});})['then'](function(_0xb7a70f){var _0x333bec=[];_0xb7a70f['forEach'](function(_0x267494){_0x333bec[_0x89f2('0x40')](_0x267494['update']({'exitAt':moment()['format'](_0x89f2('0x2f')),'data2':'system'}));});return BPromise['all'](_0x333bec);})['then'](function(){return _0x1d592b[_0x89f2('0x34')]({'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});});}}})[_0x89f2('0x20')](function(_0x9cccea){_0xd1c96e(_0x9cccea);})[_0x89f2('0x16')](function(_0x14e50d){_0x532006(_0x55e015[_0x89f2('0x12')](0x1f4,_0x14e50d['message']));});});};exports['LogoutUser']=function(_0x50c850){var _0x25d70f=this;return new Promise(function(_0x1be6d9,_0x4b4ee7){var _0x28efc3;return db[_0x89f2('0x18')]['findOne']({'where':_0x50c850[_0x89f2('0x19')][_0x89f2('0x1a')]||{},'attributes':['id',_0x89f2('0x25')]})[_0x89f2('0x20')](function(_0x49cb6e){if(_0x49cb6e){_0x28efc3=_0x49cb6e;return db[_0x89f2('0x27')][_0x89f2('0x41')]({'where':{'membername':_0x28efc3['name']},'individualHooks':!![]});}})[_0x89f2('0x20')](function(){if(_0x28efc3){return db[_0x89f2('0x28')][_0x89f2('0x3f')]({'where':{'membername':_0x28efc3['name'],'exitAt':null}});}})['then'](function(_0x366faa){if(_0x28efc3){if(_0x366faa&&_0x366faa['length']){var _0x583d76=[];_0x366faa[_0x89f2('0x2d')](function(_0x599662){_0x583d76['push'](_0x599662['update']({'exitAt':moment()[_0x89f2('0x2a')](_0x89f2('0x2f'))}));});return BPromise[_0x89f2('0x42')](_0x583d76);}}})[_0x89f2('0x20')](function(){if(_0x28efc3){return _0x28efc3['update']({'online':![],'lastLoginAt':null,'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0x89f2('0x20')](function(_0x4444e5){_0x1be6d9(_0x4444e5);})['catch'](function(_0x1c121d){_0x4b4ee7(_0x25d70f['error'](0x1f4,_0x1c121d['message']));});});};exports[_0x89f2('0x43')]=function(_0x52cc38){var _0x164f30=this;return new Promise(function(_0x4fbb3b,_0xddf910){var _0x7e1af3;var _0x57c80e;var _0x4f52ef;var _0x200d76;return db[_0x89f2('0x18')]['findOne']({'where':_0x52cc38[_0x89f2('0x19')][_0x89f2('0x1a')]||{},'attributes':['id',_0x89f2('0x25'),_0x89f2('0x32'),_0x89f2('0x44'),'phone',_0x89f2('0x26'),'loginInPause','interface']})['then'](function(_0x113889){if(_0x113889){_0x7e1af3=_0x113889;_0x200d76=_0x52cc38[_0x89f2('0x30')][_0x89f2('0x45')]?_0x52cc38[_0x89f2('0x30')][_0x89f2('0x45')]:util[_0x89f2('0x2a')](_0x89f2('0x46'),_0x7e1af3['name']);return db['User'][_0x89f2('0x47')]({'where':{'role':_0x89f2('0x48'),'online':!![],'id':{'$ne':_0x7e1af3['id']}}});}})[_0x89f2('0x20')](function(_0xdf6eda){if(_0xdf6eda){_0x4f52ef=_0xdf6eda;return require('../../config/license/util')[_0x89f2('0x49')]();}})[_0x89f2('0x20')](function(_0x1649a2){if(_0x1649a2&&_0x1649a2[_0x89f2('0x4a')]&&_0x4f52ef>=_0x1649a2['agents']){throw new Error('Maximum\x20login\x20has\x20been\x20reached');}})[_0x89f2('0x20')](function(){if(_0x7e1af3){return db[_0x89f2('0x27')][_0x89f2('0x41')]({'where':{'membername':_0x7e1af3[_0x89f2('0x25')]},'individualHooks':!![]});}})[_0x89f2('0x20')](function(){if(_0x7e1af3){return _0x7e1af3[_0x89f2('0x4b')]({'raw':!![]});}})[_0x89f2('0x20')](function(_0x1f4743){if(_0x7e1af3){if(_0x1f4743){var _0x25dc36=[];_0x57c80e=_0x1f4743;if(_0x57c80e[_0x89f2('0x2b')]){for(var _0x3d4b5f=0x0;_0x3d4b5f<_0x57c80e[_0x89f2('0x2b')];_0x3d4b5f++){_0x25dc36[_0x89f2('0x40')](db[_0x89f2('0x27')][_0x89f2('0x4c')]({'membername':_0x7e1af3[_0x89f2('0x25')],'UserId':_0x7e1af3['id'],'queue_name':_0x57c80e[_0x3d4b5f]['name'],'VoiceQueueId':_0x57c80e[_0x3d4b5f]['id'],'interface':_0x200d76,'paused':_0x7e1af3['loginInPause']||![],'penalty':_0x57c80e[_0x3d4b5f][_0x89f2('0x4d')]||0x0}));}return BPromise[_0x89f2('0x42')](_0x25dc36);}}}})[_0x89f2('0x20')](function(){if(_0x7e1af3){return db[_0x89f2('0x28')][_0x89f2('0x3f')]({'where':{'membername':_0x7e1af3[_0x89f2('0x25')],'exitAt':null}});}})[_0x89f2('0x20')](function(_0xaf4f4a){if(_0x7e1af3){if(_0xaf4f4a&&_0xaf4f4a[_0x89f2('0x2b')]){var _0x24fe86=[];_0xaf4f4a['forEach'](function(_0x2f306f){_0x24fe86[_0x89f2('0x40')](_0x2f306f['update']({'exitAt':moment()['format'](_0x89f2('0x2f'))}));});return BPromise['all'](_0x24fe86);}}})[_0x89f2('0x20')](function(_0x517b13){if(_0x7e1af3){var _0x933baa=[];var _0x11ca51=[];var _0x42c4bb=utils[_0x89f2('0x2c')]();var _0x35707c={'membername':_0x7e1af3[_0x89f2('0x25')],'interface':_0x200d76,'enterAt':moment()[_0x89f2('0x2a')]('YYYY-MM-DD\x20HH:mm:ss'),'role':_0x7e1af3[_0x89f2('0x32')],'internal':_0x7e1af3['internal']};_0x42c4bb[_0x89f2('0x2d')](function(_0x254a06){_0x933baa[_0x89f2('0x40')](_[_0x89f2('0x4e')]({'channel':_0x254a06,'type':'LOGIN','data1':'callysquare','data2':_0x200d76},_0x35707c));if(_0x7e1af3[_0x89f2('0x4f')]){_0x11ca51[_0x89f2('0x40')](_[_0x89f2('0x4e')]({'channel':_0x254a06,'type':'PAUSE','data1':_0x89f2('0x50')},_0x35707c));}});return db[_0x89f2('0x28')]['bulkCreate'](_0x933baa[_0x89f2('0x51')](_0x11ca51),{'individualHooks':!![]});}})[_0x89f2('0x20')](function(){if(_0x7e1af3){return _0x7e1af3[_0x89f2('0x34')]({'online':!![],'lastLoginAt':moment()[_0x89f2('0x2a')](_0x89f2('0x2f')),'voicePause':_0x7e1af3[_0x89f2('0x4f')]||![],'mailPause':_0x7e1af3['loginInPause']||![],'chatPause':_0x7e1af3[_0x89f2('0x4f')]||![],'faxPause':_0x7e1af3['loginInPause']||![],'smsPause':_0x7e1af3[_0x89f2('0x4f')]||![],'openchannelPause':_0x7e1af3[_0x89f2('0x4f')]||![],'pauseType':_0x7e1af3[_0x89f2('0x4f')]?_0x89f2('0x50'):null,'lastPauseAt':moment()[_0x89f2('0x2a')](_0x89f2('0x2f')),'interface':_0x200d76});}})[_0x89f2('0x20')](function(_0xf20992){_0x4fbb3b(_0xf20992);})[_0x89f2('0x16')](function(_0x2e0818){_0xddf910(_0x164f30[_0x89f2('0x12')](0x1f4,_0x2e0818[_0x89f2('0x15')]));});});};