Built motion from commit (unavailable).|2.6.0
[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 _0x59b7=['loginInPause','count','../../config/license/util','getLicense','agents','Maximum\x20login\x20has\x20been\x20reached','upsert','merge','LOGIN','callysquare','LOGININPAUSE','lodash','util','moment','bluebird','randomstring','ioredis','../../mysqldb','../../config/utils','../../config/logger','rpc','../../config/environment','client','defaults','redis','./user.socket','register','request','then','info','User,\x20%s,\x20%s','User,\x20%s,\x20%s,\x20%s','request\x20sent','stringify','message','error','catch','GetUser','options','raw','where','attributes','limit','include','map','model','debug','User','ShowUser','PauseUser','name','role','internal','interface','UserVoiceQueueRt','update','findAll','PAUSE','format','updateAttributes','forEach','push','isNil','SIP/%s','YYYY-MM-DD\x20HH:mm:ss','body','uniqueid','MemberReport','bulkCreate','type','DEFAULT\x20PAUSE','UnpauseUser','findOne','isValidChannel','channel','Unknown\x20channel','find','system','mail','chat','fax','sms','openchannel','set','%s%s','Pause','all','LogoutUser','destroy','length','LoginUser','mobile','phone'];(function(_0x5c13f0,_0x3722cd){var _0x5e28a0=function(_0x1eac23){while(--_0x1eac23){_0x5c13f0['push'](_0x5c13f0['shift']());}};_0x5e28a0(++_0x3722cd);}(_0x59b7,0xc5));var _0x759b=function(_0x569564,_0x50dd26){_0x569564=_0x569564-0x0;var _0x20871f=_0x59b7[_0x569564];return _0x20871f;};'use strict';var _=require(_0x759b('0x0'));var util=require(_0x759b('0x1'));var moment=require(_0x759b('0x2'));var BPromise=require(_0x759b('0x3'));var rs=require(_0x759b('0x4'));var fs=require('fs');var Redis=require(_0x759b('0x5'));var db=require(_0x759b('0x6'))['db'];var utils=require(_0x759b('0x7'));var logger=require(_0x759b('0x8'))(_0x759b('0x9'));var config=require(_0x759b('0xa'));var jayson=require('jayson/promise');var client=jayson[_0x759b('0xb')]['http']({'port':0x232a});config['redis']=_[_0x759b('0xc')](config[_0x759b('0xd')],{'host':'localhost','port':0x18eb});var socket=require('socket.io-emitter')(new Redis(config['redis']));require(_0x759b('0xe'))[_0x759b('0xf')](socket);function respondWithRpcPromise(_0x2870e4,_0x48fb98,_0x15f330){return new BPromise(function(_0x5781d9,_0x9f8042){return client[_0x759b('0x10')](_0x2870e4,_0x15f330)[_0x759b('0x11')](function(_0x3950f0){logger[_0x759b('0x12')](_0x759b('0x13'),_0x48fb98,'request\x20sent');logger['debug'](_0x759b('0x14'),_0x48fb98,_0x759b('0x15'),JSON[_0x759b('0x16')](_0x3950f0));if(_0x3950f0['error']){if(_0x3950f0['error']['code']===0x1f4){logger['error'](_0x759b('0x13'),_0x48fb98,_0x3950f0['error'][_0x759b('0x17')]);return _0x9f8042(_0x3950f0['error'][_0x759b('0x17')]);}logger[_0x759b('0x18')]('User,\x20%s,\x20%s',_0x48fb98,_0x3950f0['error'][_0x759b('0x17')]);return _0x5781d9(_0x3950f0[_0x759b('0x18')]['message']);}else{logger['info'](_0x759b('0x13'),_0x48fb98,_0x759b('0x15'));_0x5781d9(_0x3950f0['result'][_0x759b('0x17')]);}})[_0x759b('0x19')](function(_0x183416){logger['error'](_0x759b('0x13'),_0x48fb98,_0x183416);_0x9f8042(_0x183416);});});}exports[_0x759b('0x1a')]=function(_0x9536ab){var _0x63634e=this;return new Promise(function(_0x1eb739,_0x53cd4e){return db['User']['findAll']({'raw':_0x9536ab['options']?_0x9536ab[_0x759b('0x1b')][_0x759b('0x1c')]===undefined?!![]:![]:!![],'where':_0x9536ab[_0x759b('0x1b')]?_0x9536ab['options'][_0x759b('0x1d')]||null:null,'attributes':_0x9536ab[_0x759b('0x1b')]?_0x9536ab[_0x759b('0x1b')][_0x759b('0x1e')]||null:null,'limit':_0x9536ab[_0x759b('0x1b')]?_0x9536ab[_0x759b('0x1b')][_0x759b('0x1f')]||null:null,'include':_0x9536ab['options']?_0x9536ab[_0x759b('0x1b')][_0x759b('0x20')]?_[_0x759b('0x21')](_0x9536ab['options'][_0x759b('0x20')],function(_0x50ad09){return{'model':db[_0x50ad09[_0x759b('0x22')]],'as':_0x50ad09['as'],'attributes':_0x50ad09[_0x759b('0x1e')],'include':_0x50ad09[_0x759b('0x20')]?_[_0x759b('0x21')](_0x50ad09['include'],function(_0x5ae91f){return{'model':db[_0x5ae91f['model']],'as':_0x5ae91f['as'],'attributes':_0x5ae91f[_0x759b('0x1e')],'include':_0x5ae91f[_0x759b('0x20')]?_[_0x759b('0x21')](_0x5ae91f['include'],function(_0x2b6747){return{'model':db[_0x2b6747[_0x759b('0x22')]],'as':_0x2b6747['as'],'attributes':_0x2b6747[_0x759b('0x1e')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x2ed0b0){logger['info']('GetUser',_0x9536ab);logger[_0x759b('0x23')]('GetUser',_0x9536ab,JSON[_0x759b('0x16')](_0x2ed0b0));_0x1eb739(_0x2ed0b0);})[_0x759b('0x19')](function(_0x1d649a){logger[_0x759b('0x18')](_0x759b('0x1a'),_0x1d649a[_0x759b('0x17')],_0x9536ab);_0x53cd4e(_0x63634e[_0x759b('0x18')](0x1f4,_0x1d649a[_0x759b('0x17')]));});});};exports['ShowUser']=function(_0x5a0c07){var _0x549a1e=this;return new Promise(function(_0x5c2867,_0x43a62b){return db[_0x759b('0x24')]['find']({'raw':_0x5a0c07[_0x759b('0x1b')]?_0x5a0c07[_0x759b('0x1b')][_0x759b('0x1c')]===undefined?!![]:![]:!![],'where':_0x5a0c07['options']?_0x5a0c07[_0x759b('0x1b')][_0x759b('0x1d')]||null:null,'attributes':_0x5a0c07['options']?_0x5a0c07[_0x759b('0x1b')][_0x759b('0x1e')]||null:null,'include':_0x5a0c07[_0x759b('0x1b')]?_0x5a0c07['options'][_0x759b('0x20')]?_[_0x759b('0x21')](_0x5a0c07['options'][_0x759b('0x20')],function(_0x2a3be9){return{'model':db[_0x2a3be9['model']],'as':_0x2a3be9['as'],'attributes':_0x2a3be9[_0x759b('0x1e')],'include':_0x2a3be9['include']?_[_0x759b('0x21')](_0x2a3be9['include'],function(_0x479f84){return{'model':db[_0x479f84[_0x759b('0x22')]],'as':_0x479f84['as'],'attributes':_0x479f84[_0x759b('0x1e')],'include':_0x479f84[_0x759b('0x20')]?_['map'](_0x479f84[_0x759b('0x20')],function(_0x19ad87){return{'model':db[_0x19ad87[_0x759b('0x22')]],'as':_0x19ad87['as'],'attributes':_0x19ad87[_0x759b('0x1e')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x4b56a0){logger['info']('ShowUser',_0x5a0c07);logger['debug'](_0x759b('0x25'),_0x5a0c07,JSON[_0x759b('0x16')](_0x4b56a0));_0x5c2867(_0x4b56a0);})[_0x759b('0x19')](function(_0x21d18a){logger[_0x759b('0x18')](_0x759b('0x25'),_0x21d18a[_0x759b('0x17')],_0x5a0c07);_0x43a62b(_0x549a1e['error'](0x1f4,_0x21d18a[_0x759b('0x17')]));});});};exports[_0x759b('0x26')]=function(_0x4f5e6d){var _0x48ebaf=this;return new Promise(function(_0x2a072f,_0x5d3307){var _0xb5cea8;return db[_0x759b('0x24')]['find']({'where':_0x4f5e6d[_0x759b('0x1b')]['where']||{},'attributes':['id',_0x759b('0x27'),_0x759b('0x28'),_0x759b('0x29'),_0x759b('0x2a')]})[_0x759b('0x11')](function(_0x2cc3ea){if(_0x2cc3ea){_0xb5cea8=_0x2cc3ea;return db[_0x759b('0x2b')][_0x759b('0x2c')]({'paused':0x1},{'where':{'UserId':_0xb5cea8['id']},'individualHooks':!![]});}})[_0x759b('0x11')](function(){if(_0xb5cea8){return db['MemberReport'][_0x759b('0x2d')]({'where':{'membername':_0xb5cea8[_0x759b('0x27')],'type':_0x759b('0x2e'),'exitAt':null}});}})['then'](function(_0x15574b){if(_0xb5cea8){if(_0x15574b&&_0x15574b['length']){var _0x5bb71b=moment()[_0x759b('0x2f')]('YYYY-MM-DD\x20HH:mm:ss');for(var _0x1521f1=0x0,_0x1a20b0=_0x15574b['length'];_0x1521f1<_0x1a20b0;_0x1521f1++){_0x15574b[_0x1521f1][_0x759b('0x30')]({'exitAt':_0x5bb71b});}}}})[_0x759b('0x11')](function(){if(_0xb5cea8){var _0x264879=[];var _0x5e459d=utils['getChannels']();_0x5e459d[_0x759b('0x31')](function(_0x340985){_0x264879[_0x759b('0x32')]({'channel':_0x340985,'membername':_0xb5cea8[_0x759b('0x27')],'interface':_[_0x759b('0x33')](_0xb5cea8[_0x759b('0x2a')])?util[_0x759b('0x2f')](_0x759b('0x34'),_0xb5cea8[_0x759b('0x27')]):_0xb5cea8[_0x759b('0x2a')],'type':_0x759b('0x2e'),'enterAt':moment()[_0x759b('0x2f')](_0x759b('0x35')),'data1':_0x4f5e6d['body']['type']||'DEFAULT\x20PAUSE','role':_0xb5cea8[_0x759b('0x28')],'internal':_0xb5cea8[_0x759b('0x29')],'uniqueid':_0x4f5e6d[_0x759b('0x36')][_0x759b('0x37')]||null});});return db[_0x759b('0x38')][_0x759b('0x39')](_0x264879,{'individualHooks':!![]});}})[_0x759b('0x11')](function(){if(_0xb5cea8){return _0xb5cea8[_0x759b('0x2c')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x4f5e6d[_0x759b('0x36')][_0x759b('0x3a')]||_0x759b('0x3b'),'lastPauseAt':moment()[_0x759b('0x2f')](_0x759b('0x35'))});}})[_0x759b('0x11')](function(_0x5c1f67){_0x2a072f(_0x5c1f67);})[_0x759b('0x19')](function(_0x18aae3){_0x5d3307(_0x48ebaf[_0x759b('0x18')](0x1f4,_0x18aae3[_0x759b('0x17')]));});});};exports[_0x759b('0x3c')]=function(_0x829d91){var _0x10cd39=this;return new Promise(function(_0x51ca41,_0x10daa9){var _0x2986a7;return db[_0x759b('0x24')][_0x759b('0x3d')]({'where':_0x829d91[_0x759b('0x1b')][_0x759b('0x1d')]||{},'attributes':['id',_0x759b('0x27')]})[_0x759b('0x11')](function(_0xd6d028){if(_0xd6d028){_0x2986a7=_0xd6d028;if(_0x829d91[_0x759b('0x36')]['channel']&&!utils[_0x759b('0x3e')](_0x829d91[_0x759b('0x36')][_0x759b('0x3f')])){throw new Error(_0x759b('0x40'));}switch(_0x829d91[_0x759b('0x36')][_0x759b('0x3f')]){case'voice':return db[_0x759b('0x2b')][_0x759b('0x2c')]({'paused':0x0},{'where':{'UserId':_0x2986a7['id']}})[_0x759b('0x11')](function(){return db['MemberReport'][_0x759b('0x41')]({'where':{'channel':_0x829d91[_0x759b('0x36')][_0x759b('0x3f')],'membername':_0x2986a7[_0x759b('0x27')],'type':'PAUSE','exitAt':null}});})[_0x759b('0x11')](function(_0x2cb952){if(_0x2cb952){return _0x2cb952[_0x759b('0x2c')]({'exitAt':moment()[_0x759b('0x2f')](_0x759b('0x35')),'data2':_0x759b('0x42')},{'individualHooks':!![]});}return null;})[_0x759b('0x11')](function(){return _0x2986a7['update']({'voicePause':![]});});case _0x759b('0x43'):case _0x759b('0x44'):case _0x759b('0x45'):case _0x759b('0x46'):case _0x759b('0x47'):case'whatsapp':return db[_0x759b('0x38')][_0x759b('0x41')]({'where':{'channel':_0x829d91[_0x759b('0x36')]['channel'],'membername':_0x2986a7['name'],'type':_0x759b('0x2e'),'exitAt':null}})[_0x759b('0x11')](function(_0x12e000){return _0x12e000[_0x759b('0x2c')]({'exitAt':moment()[_0x759b('0x2f')](_0x759b('0x35')),'data2':_0x759b('0x42')},{'individualHooks':!![]});})[_0x759b('0x11')](function(){var _0x35f57d={};_[_0x759b('0x48')](_0x35f57d,util[_0x759b('0x2f')](_0x759b('0x49'),_0x829d91[_0x759b('0x36')]['channel'],_0x759b('0x4a')),![]);return _0x2986a7[_0x759b('0x2c')](_0x35f57d);});default:return db['UserVoiceQueueRt']['update']({'paused':0x0},{'where':{'UserId':_0x2986a7['id']}})[_0x759b('0x11')](function(){return db[_0x759b('0x38')][_0x759b('0x2d')]({'where':{'membername':_0x2986a7[_0x759b('0x27')],'type':'PAUSE','exitAt':null}});})[_0x759b('0x11')](function(_0x5d6a61){var _0x282580=[];_0x5d6a61[_0x759b('0x31')](function(_0x51d009){_0x282580[_0x759b('0x32')](_0x51d009[_0x759b('0x2c')]({'exitAt':moment()[_0x759b('0x2f')](_0x759b('0x35')),'data2':_0x759b('0x42')}));});return BPromise[_0x759b('0x4b')](_0x282580);})['then'](function(){return _0x2986a7[_0x759b('0x2c')]({'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![],'whatsappPause':![]});});}}})['then'](function(_0x2f89e5){_0x51ca41(_0x2f89e5);})['catch'](function(_0x230676){_0x10daa9(_0x10cd39[_0x759b('0x18')](0x1f4,_0x230676['message']));});});};exports[_0x759b('0x4c')]=function(_0x29eceb){var _0x43c7f0=this;return new Promise(function(_0x2b492d,_0x5e4951){var _0x4f3307;return db['User']['findOne']({'where':_0x29eceb[_0x759b('0x1b')]['where']||{},'attributes':['id',_0x759b('0x27')]})['then'](function(_0x2d9813){if(_0x2d9813){_0x4f3307=_0x2d9813;return db[_0x759b('0x2b')][_0x759b('0x4d')]({'where':{'membername':_0x4f3307[_0x759b('0x27')]},'individualHooks':!![]});}})[_0x759b('0x11')](function(){if(_0x4f3307){return db['MemberReport'][_0x759b('0x2d')]({'where':{'membername':_0x4f3307[_0x759b('0x27')],'exitAt':null}});}})[_0x759b('0x11')](function(_0x21ce72){if(_0x4f3307){if(_0x21ce72&&_0x21ce72[_0x759b('0x4e')]){var _0x1f6fa9=[];_0x21ce72['forEach'](function(_0x80a2c0){_0x1f6fa9[_0x759b('0x32')](_0x80a2c0[_0x759b('0x2c')]({'exitAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss')}));});return BPromise[_0x759b('0x4b')](_0x1f6fa9);}}})[_0x759b('0x11')](function(){if(_0x4f3307){return _0x4f3307[_0x759b('0x2c')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})['then'](function(_0x36a044){_0x2b492d(_0x36a044);})[_0x759b('0x19')](function(_0x47e137){_0x5e4951(_0x43c7f0[_0x759b('0x18')](0x1f4,_0x47e137[_0x759b('0x17')]));});});};exports[_0x759b('0x4f')]=function(_0x341054){var _0x4415cf=this;return new Promise(function(_0x153079,_0x1f3c1f){var _0x2818af;var _0x637414;var _0x26ca3e;var _0x2a6b02;return db[_0x759b('0x24')][_0x759b('0x3d')]({'where':_0x341054['options'][_0x759b('0x1d')]||{},'attributes':['id',_0x759b('0x27'),_0x759b('0x28'),_0x759b('0x50'),_0x759b('0x51'),_0x759b('0x29'),_0x759b('0x52'),_0x759b('0x2a')]})[_0x759b('0x11')](function(_0x47c520){if(_0x47c520){_0x2818af=_0x47c520;_0x2a6b02=_0x341054[_0x759b('0x36')][_0x759b('0x2a')]?_0x341054[_0x759b('0x36')][_0x759b('0x2a')]:util['format'](_0x759b('0x34'),_0x2818af[_0x759b('0x27')]);return db[_0x759b('0x24')][_0x759b('0x53')]({'where':{'role':'agent','online':!![],'id':{'$ne':_0x2818af['id']}}});}})[_0x759b('0x11')](function(_0x14f1f0){if(_0x14f1f0){_0x26ca3e=_0x14f1f0;return require(_0x759b('0x54'))[_0x759b('0x55')]();}})[_0x759b('0x11')](function(_0xd6c511){if(_0xd6c511&&_0xd6c511[_0x759b('0x56')]&&_0x26ca3e>=_0xd6c511['agents']){throw new Error(_0x759b('0x57'));}})[_0x759b('0x11')](function(){if(_0x2818af){return db[_0x759b('0x2b')][_0x759b('0x4d')]({'where':{'membername':_0x2818af[_0x759b('0x27')]},'individualHooks':!![]});}})['then'](function(){if(_0x2818af){return _0x2818af['getVoiceQueues']({'raw':!![]});}})['then'](function(_0x437192){if(_0x2818af){if(_0x437192){var _0x3ae87a=[];_0x637414=_0x437192;if(_0x637414[_0x759b('0x4e')]){for(var _0x3a463a=0x0;_0x3a463a<_0x637414['length'];_0x3a463a++){_0x3ae87a[_0x759b('0x32')](db[_0x759b('0x2b')][_0x759b('0x58')]({'membername':_0x2818af['name'],'UserId':_0x2818af['id'],'queue_name':_0x637414[_0x3a463a][_0x759b('0x27')],'VoiceQueueId':_0x637414[_0x3a463a]['id'],'interface':_0x2a6b02,'paused':_0x2818af['loginInPause']||![],'penalty':_0x637414[_0x3a463a]['UserVoiceQueue.penalty']||0x0}));}return BPromise[_0x759b('0x4b')](_0x3ae87a);}}}})[_0x759b('0x11')](function(){if(_0x2818af){return db[_0x759b('0x38')][_0x759b('0x2d')]({'where':{'membername':_0x2818af[_0x759b('0x27')],'exitAt':null}});}})[_0x759b('0x11')](function(_0x54a2d2){if(_0x2818af){if(_0x54a2d2&&_0x54a2d2[_0x759b('0x4e')]){var _0x1a1bb9=[];_0x54a2d2[_0x759b('0x31')](function(_0xa50413){_0x1a1bb9['push'](_0xa50413['update']({'exitAt':moment()['format'](_0x759b('0x35'))}));});return BPromise['all'](_0x1a1bb9);}}})[_0x759b('0x11')](function(_0x2a2dbb){if(_0x2818af){var _0x33779b=[];var _0x58b4f9=[];var _0x58948f=utils['getChannels']();var _0x96cc6f={'membername':_0x2818af[_0x759b('0x27')],'interface':_0x2a6b02,'enterAt':moment()['format'](_0x759b('0x35')),'role':_0x2818af[_0x759b('0x28')],'internal':_0x2818af[_0x759b('0x29')]};_0x58948f[_0x759b('0x31')](function(_0x2fe3d6){_0x33779b['push'](_[_0x759b('0x59')]({'channel':_0x2fe3d6,'type':_0x759b('0x5a'),'data1':_0x759b('0x5b'),'data2':_0x2a6b02},_0x96cc6f));if(_0x2818af[_0x759b('0x52')]){_0x58b4f9[_0x759b('0x32')](_[_0x759b('0x59')]({'channel':_0x2fe3d6,'type':_0x759b('0x2e'),'data1':_0x759b('0x5c')},_0x96cc6f));}});return db[_0x759b('0x38')][_0x759b('0x39')](_0x33779b['concat'](_0x58b4f9),{'individualHooks':!![]});}})[_0x759b('0x11')](function(){if(_0x2818af){return _0x2818af[_0x759b('0x2c')]({'online':!![],'lastLoginAt':moment()[_0x759b('0x2f')](_0x759b('0x35')),'voicePause':_0x2818af['loginInPause']||![],'mailPause':_0x2818af['loginInPause']||![],'chatPause':_0x2818af[_0x759b('0x52')]||![],'faxPause':_0x2818af[_0x759b('0x52')]||![],'smsPause':_0x2818af[_0x759b('0x52')]||![],'openchannelPause':_0x2818af[_0x759b('0x52')]||![],'pauseType':_0x2818af[_0x759b('0x52')]?'LOGININPAUSE':null,'lastPauseAt':moment()[_0x759b('0x2f')](_0x759b('0x35')),'interface':_0x2a6b02});}})[_0x759b('0x11')](function(_0x23d9c3){_0x153079(_0x23d9c3);})[_0x759b('0x19')](function(_0x31e034){_0x1f3c1f(_0x4415cf[_0x759b('0x18')](0x1f4,_0x31e034[_0x759b('0x17')]));});});};