70c7fbceb03894da7653a4d25d856c5d9d08edbc
[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 _0x7e9a=['moment','bluebird','ioredis','../../config/utils','../../config/logger','rpc','../../config/environment','redis','defaults','localhost','socket.io-emitter','./user.socket','register','GetUser','User','findAll','options','raw','where','limit','include','map','attributes','model','then','debug','stringify','catch','error','message','ShowUser','find','PauseUser','name','role','interface','User\x20with\x20id\x20%s\x20not\x20found','params','UserVoiceQueueRt','update','MemberReport','YYYY-MM-DD\x20HH:mm:ss','updateAttributes','getChannels','forEach','push','isNil','format','SIP/%s','body','type','DEFAULT\x20PAUSE','internal','uniqueid','bulkCreate','UnpauseUser','resolve','channel','Channel\x20parameter\x20%s\x20is\x20invalid','findOne','mailPause','chatPause','faxPause','openchannelPause','smsPause','whatsappPause','pauseType','lastPauseAt','system','LogoutUser','destroy','length','all','mobile','phone','../../config/license/util','getLicense','agents','getVoiceQueues','upsert','loginInPause','UserVoiceQueue.penalty','LOGIN','LOGININPAUSE','lodash','util'];(function(_0x1e3671,_0x9ae4fc){var _0x48d5f5=function(_0xfab713){while(--_0xfab713){_0x1e3671['push'](_0x1e3671['shift']());}};_0x48d5f5(++_0x9ae4fc);}(_0x7e9a,0x1ac));var _0xa7e9=function(_0x1fceea,_0x151680){_0x1fceea=_0x1fceea-0x0;var _0x400527=_0x7e9a[_0x1fceea];return _0x400527;};'use strict';var _=require(_0xa7e9('0x0'));var util=require(_0xa7e9('0x1'));var moment=require(_0xa7e9('0x2'));var BPromise=require(_0xa7e9('0x3'));var Redis=require(_0xa7e9('0x4'));var db=require('../../mysqldb')['db'];var utils=require(_0xa7e9('0x5'));var logger=require(_0xa7e9('0x6'))(_0xa7e9('0x7'));var config=require(_0xa7e9('0x8'));config[_0xa7e9('0x9')]=_[_0xa7e9('0xa')](config[_0xa7e9('0x9')],{'host':_0xa7e9('0xb'),'port':0x18eb});var socket=require(_0xa7e9('0xc'))(new Redis(config[_0xa7e9('0x9')]));require(_0xa7e9('0xd'))[_0xa7e9('0xe')](socket);exports[_0xa7e9('0xf')]=function(_0x2ff188){var _0x2b8d03=this;return new Promise(function(_0x41e0d4,_0x390009){return db[_0xa7e9('0x10')][_0xa7e9('0x11')]({'raw':_0x2ff188['options']?_0x2ff188[_0xa7e9('0x12')][_0xa7e9('0x13')]===undefined?!![]:![]:!![],'where':_0x2ff188[_0xa7e9('0x12')]?_0x2ff188[_0xa7e9('0x12')][_0xa7e9('0x14')]||null:null,'attributes':_0x2ff188['options']?_0x2ff188[_0xa7e9('0x12')]['attributes']||null:null,'limit':_0x2ff188['options']?_0x2ff188[_0xa7e9('0x12')][_0xa7e9('0x15')]||null:null,'include':_0x2ff188['options']?_0x2ff188[_0xa7e9('0x12')][_0xa7e9('0x16')]?_[_0xa7e9('0x17')](_0x2ff188[_0xa7e9('0x12')][_0xa7e9('0x16')],function(_0x7c7453){return{'model':db[_0x7c7453['model']],'as':_0x7c7453['as'],'attributes':_0x7c7453[_0xa7e9('0x18')],'include':_0x7c7453[_0xa7e9('0x16')]?_['map'](_0x7c7453[_0xa7e9('0x16')],function(_0x340771){return{'model':db[_0x340771[_0xa7e9('0x19')]],'as':_0x340771['as'],'attributes':_0x340771[_0xa7e9('0x18')],'include':_0x340771['include']?_[_0xa7e9('0x17')](_0x340771['include'],function(_0x29001d){return{'model':db[_0x29001d[_0xa7e9('0x19')]],'as':_0x29001d['as'],'attributes':_0x29001d['attributes']};}):[]};}):[]};}):[]:[]})[_0xa7e9('0x1a')](function(_0x10765d){logger['info']('GetUser',_0x2ff188);logger[_0xa7e9('0x1b')](_0xa7e9('0xf'),_0x2ff188,JSON[_0xa7e9('0x1c')](_0x10765d));_0x41e0d4(_0x10765d);})[_0xa7e9('0x1d')](function(_0x3e5a7a){logger[_0xa7e9('0x1e')](_0xa7e9('0xf'),_0x3e5a7a[_0xa7e9('0x1f')],_0x2ff188);_0x390009(_0x2b8d03['error'](0x1f4,_0x3e5a7a[_0xa7e9('0x1f')]));});});};exports[_0xa7e9('0x20')]=function(_0x57acfe){var _0x5ab3db=this;return new Promise(function(_0x5f20c0,_0x4891c3){return db[_0xa7e9('0x10')][_0xa7e9('0x21')]({'raw':_0x57acfe[_0xa7e9('0x12')]?_0x57acfe[_0xa7e9('0x12')][_0xa7e9('0x13')]===undefined?!![]:![]:!![],'where':_0x57acfe[_0xa7e9('0x12')]?_0x57acfe[_0xa7e9('0x12')][_0xa7e9('0x14')]||null:null,'attributes':_0x57acfe[_0xa7e9('0x12')]?_0x57acfe[_0xa7e9('0x12')][_0xa7e9('0x18')]||null:null,'include':_0x57acfe[_0xa7e9('0x12')]?_0x57acfe['options'][_0xa7e9('0x16')]?_[_0xa7e9('0x17')](_0x57acfe['options'][_0xa7e9('0x16')],function(_0x1697b7){return{'model':db[_0x1697b7[_0xa7e9('0x19')]],'as':_0x1697b7['as'],'attributes':_0x1697b7[_0xa7e9('0x18')],'include':_0x1697b7[_0xa7e9('0x16')]?_[_0xa7e9('0x17')](_0x1697b7[_0xa7e9('0x16')],function(_0x33532f){return{'model':db[_0x33532f[_0xa7e9('0x19')]],'as':_0x33532f['as'],'attributes':_0x33532f[_0xa7e9('0x18')],'include':_0x33532f[_0xa7e9('0x16')]?_[_0xa7e9('0x17')](_0x33532f[_0xa7e9('0x16')],function(_0x516c73){return{'model':db[_0x516c73[_0xa7e9('0x19')]],'as':_0x516c73['as'],'attributes':_0x516c73[_0xa7e9('0x18')]};}):[]};}):[]};}):[]:[]})[_0xa7e9('0x1a')](function(_0x1a6963){logger['info'](_0xa7e9('0x20'),_0x57acfe);logger[_0xa7e9('0x1b')](_0xa7e9('0x20'),_0x57acfe,JSON['stringify'](_0x1a6963));_0x5f20c0(_0x1a6963);})['catch'](function(_0x2f630f){logger[_0xa7e9('0x1e')](_0xa7e9('0x20'),_0x2f630f[_0xa7e9('0x1f')],_0x57acfe);_0x4891c3(_0x5ab3db[_0xa7e9('0x1e')](0x1f4,_0x2f630f[_0xa7e9('0x1f')]));});});};exports[_0xa7e9('0x22')]=function(_0xed36b4){var _0x5d89d7;return db[_0xa7e9('0x10')][_0xa7e9('0x21')]({'where':_0xed36b4[_0xa7e9('0x12')][_0xa7e9('0x14')]||{},'attributes':['id',_0xa7e9('0x23'),_0xa7e9('0x24'),'internal',_0xa7e9('0x25')]})[_0xa7e9('0x1a')](function(_0x2d6214){if(!_0x2d6214){throw new Error(util['format'](_0xa7e9('0x26'),_0xed36b4[_0xa7e9('0x27')]['id']));}_0x5d89d7=_0x2d6214;return db[_0xa7e9('0x28')][_0xa7e9('0x29')]({'paused':0x1},{'where':{'UserId':_0x5d89d7['id']},'individualHooks':!![]});})['then'](function(){return db[_0xa7e9('0x2a')]['findAll']({'where':{'membername':_0x5d89d7['name'],'type':'PAUSE','exitAt':null}});})[_0xa7e9('0x1a')](function(_0x1cd94a){if(_0x1cd94a&&_0x1cd94a['length']){var _0x18f635=moment()['format'](_0xa7e9('0x2b'));for(var _0x4a376c=0x0,_0x2d45eb=_0x1cd94a['length'];_0x4a376c<_0x2d45eb;_0x4a376c++){_0x1cd94a[_0x4a376c][_0xa7e9('0x2c')]({'exitAt':_0x18f635});}}})[_0xa7e9('0x1a')](function(){var _0x5755f7=[];var _0x36a4f2=utils[_0xa7e9('0x2d')]();_0x36a4f2[_0xa7e9('0x2e')](function(_0x5ecea0){_0x5755f7[_0xa7e9('0x2f')]({'channel':_0x5ecea0,'membername':_0x5d89d7['name'],'interface':_[_0xa7e9('0x30')](_0x5d89d7[_0xa7e9('0x25')])?util[_0xa7e9('0x31')](_0xa7e9('0x32'),_0x5d89d7[_0xa7e9('0x23')]):_0x5d89d7[_0xa7e9('0x25')],'type':'PAUSE','enterAt':moment()[_0xa7e9('0x31')](_0xa7e9('0x2b')),'data1':_0xed36b4[_0xa7e9('0x33')][_0xa7e9('0x34')]||_0xa7e9('0x35'),'role':_0x5d89d7[_0xa7e9('0x24')],'internal':_0x5d89d7[_0xa7e9('0x36')],'uniqueid':_0xed36b4[_0xa7e9('0x33')][_0xa7e9('0x37')]||null});});return db[_0xa7e9('0x2a')][_0xa7e9('0x38')](_0x5755f7,{'individualHooks':!![]});})['then'](function(){return _0x5d89d7[_0xa7e9('0x29')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0xed36b4[_0xa7e9('0x33')][_0xa7e9('0x34')]||_0xa7e9('0x35'),'lastPauseAt':moment()[_0xa7e9('0x31')](_0xa7e9('0x2b'))});})['catch'](function(_0x1df9ed){throw _0x1df9ed;});};exports[_0xa7e9('0x39')]=function(_0x54164e){return Promise[_0xa7e9('0x3a')]()[_0xa7e9('0x1a')](function(){if(_0x54164e['body'][_0xa7e9('0x3b')]&&!utils['isValidChannel'](_0x54164e[_0xa7e9('0x33')]['channel'])){throw new Error(util[_0xa7e9('0x31')](_0xa7e9('0x3c'),_0x54164e[_0xa7e9('0x33')]['channel']));}return db[_0xa7e9('0x10')][_0xa7e9('0x3d')]({'where':_0x54164e[_0xa7e9('0x12')][_0xa7e9('0x14')]||{},'attributes':['id',_0xa7e9('0x23'),_0xa7e9('0x24'),_0xa7e9('0x36'),_0xa7e9('0x25'),'online','voicePause',_0xa7e9('0x3e'),_0xa7e9('0x3f'),_0xa7e9('0x40'),_0xa7e9('0x41'),_0xa7e9('0x42'),_0xa7e9('0x43'),_0xa7e9('0x44'),_0xa7e9('0x45')]});})['then'](function(_0x4764c0){if(!_0x4764c0){throw new Error(util['format']('User\x20with\x20id\x20%s\x20not\x20found',_0x54164e[_0xa7e9('0x27')]['id']));}return _0x4764c0['unpause'](_0x54164e['body']['channel'],_0xa7e9('0x46'));})[_0xa7e9('0x1d')](function(_0xf06e77){throw _0xf06e77;});};exports[_0xa7e9('0x47')]=function(_0xd5f0da){var _0x3f93f9=this;return new Promise(function(_0x13772f,_0x1df8cb){var _0x4dca17;return db[_0xa7e9('0x10')][_0xa7e9('0x3d')]({'where':_0xd5f0da['options']['where']||{},'attributes':['id',_0xa7e9('0x23')]})[_0xa7e9('0x1a')](function(_0x3431d9){if(_0x3431d9){_0x4dca17=_0x3431d9;return db[_0xa7e9('0x28')][_0xa7e9('0x48')]({'where':{'membername':_0x4dca17[_0xa7e9('0x23')]},'individualHooks':!![]});}})[_0xa7e9('0x1a')](function(){if(_0x4dca17){return db[_0xa7e9('0x2a')]['findAll']({'where':{'membername':_0x4dca17[_0xa7e9('0x23')],'exitAt':null}});}})['then'](function(_0x5e9eda){if(_0x4dca17){if(_0x5e9eda&&_0x5e9eda[_0xa7e9('0x49')]){var _0x9aeecf=[];_0x5e9eda['forEach'](function(_0x5f188b){_0x9aeecf['push'](_0x5f188b[_0xa7e9('0x29')]({'exitAt':moment()[_0xa7e9('0x31')](_0xa7e9('0x2b'))}));});return BPromise[_0xa7e9('0x4a')](_0x9aeecf);}}})[_0xa7e9('0x1a')](function(){if(_0x4dca17){return _0x4dca17[_0xa7e9('0x29')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0xa7e9('0x1a')](function(_0x35c34b){_0x13772f(_0x35c34b);})[_0xa7e9('0x1d')](function(_0x2cc1ea){_0x1df8cb(_0x3f93f9[_0xa7e9('0x1e')](0x1f4,_0x2cc1ea[_0xa7e9('0x1f')]));});});};exports['LoginUser']=function(_0x3d8433){var _0x2bce8c=this;return new Promise(function(_0x52de9e,_0x62c7c6){var _0x2f7301;var _0x4e327d;var _0x584521;var _0x3fc989;return db[_0xa7e9('0x10')][_0xa7e9('0x3d')]({'where':_0x3d8433['options']['where']||{},'attributes':['id','name',_0xa7e9('0x24'),_0xa7e9('0x4b'),_0xa7e9('0x4c'),'internal','loginInPause',_0xa7e9('0x25')]})[_0xa7e9('0x1a')](function(_0x139dad){if(_0x139dad){_0x2f7301=_0x139dad;_0x3fc989=_0x3d8433['body']['interface']?_0x3d8433[_0xa7e9('0x33')][_0xa7e9('0x25')]:util[_0xa7e9('0x31')](_0xa7e9('0x32'),_0x2f7301['name']);return db[_0xa7e9('0x10')]['count']({'where':{'role':'agent','online':!![],'id':{'$ne':_0x2f7301['id']}}});}})[_0xa7e9('0x1a')](function(_0x1548f7){if(_0x1548f7){_0x584521=_0x1548f7;return require(_0xa7e9('0x4d'))[_0xa7e9('0x4e')]();}})[_0xa7e9('0x1a')](function(_0x2a5e42){if(_0x2a5e42&&_0x2a5e42[_0xa7e9('0x4f')]&&_0x584521>=_0x2a5e42['agents']){throw new Error('Maximum\x20login\x20has\x20been\x20reached');}})[_0xa7e9('0x1a')](function(){if(_0x2f7301){return db[_0xa7e9('0x28')][_0xa7e9('0x48')]({'where':{'membername':_0x2f7301[_0xa7e9('0x23')]},'individualHooks':!![]});}})[_0xa7e9('0x1a')](function(){if(_0x2f7301){return _0x2f7301[_0xa7e9('0x50')]({'raw':!![]});}})['then'](function(_0x37e867){if(_0x2f7301){if(_0x37e867){var _0x214fdd=[];_0x4e327d=_0x37e867;if(_0x4e327d[_0xa7e9('0x49')]){for(var _0x4d5efd=0x0;_0x4d5efd<_0x4e327d[_0xa7e9('0x49')];_0x4d5efd++){_0x214fdd['push'](db[_0xa7e9('0x28')][_0xa7e9('0x51')]({'membername':_0x2f7301[_0xa7e9('0x23')],'UserId':_0x2f7301['id'],'queue_name':_0x4e327d[_0x4d5efd]['name'],'VoiceQueueId':_0x4e327d[_0x4d5efd]['id'],'interface':_0x3fc989,'paused':_0x2f7301[_0xa7e9('0x52')]||![],'penalty':_0x4e327d[_0x4d5efd][_0xa7e9('0x53')]||0x0}));}return BPromise['all'](_0x214fdd);}}}})['then'](function(){if(_0x2f7301){return db[_0xa7e9('0x2a')][_0xa7e9('0x11')]({'where':{'membername':_0x2f7301[_0xa7e9('0x23')],'exitAt':null}});}})['then'](function(_0x43618d){if(_0x2f7301){if(_0x43618d&&_0x43618d[_0xa7e9('0x49')]){var _0x4c8067=[];_0x43618d[_0xa7e9('0x2e')](function(_0xaeb608){_0x4c8067[_0xa7e9('0x2f')](_0xaeb608[_0xa7e9('0x29')]({'exitAt':moment()[_0xa7e9('0x31')](_0xa7e9('0x2b'))}));});return BPromise[_0xa7e9('0x4a')](_0x4c8067);}}})[_0xa7e9('0x1a')](function(){if(_0x2f7301){var _0x23f5a3=[];var _0x3abde1=[];var _0x596984=utils[_0xa7e9('0x2d')]();var _0x5bd293={'membername':_0x2f7301[_0xa7e9('0x23')],'interface':_0x3fc989,'enterAt':moment()[_0xa7e9('0x31')]('YYYY-MM-DD\x20HH:mm:ss'),'role':_0x2f7301[_0xa7e9('0x24')],'internal':_0x2f7301['internal']};_0x596984[_0xa7e9('0x2e')](function(_0x40c533){_0x23f5a3['push'](_['merge']({'channel':_0x40c533,'type':_0xa7e9('0x54'),'data1':'callysquare','data2':_0x3fc989},_0x5bd293));if(_0x2f7301[_0xa7e9('0x52')]){_0x3abde1[_0xa7e9('0x2f')](_['merge']({'channel':_0x40c533,'type':'PAUSE','data1':_0xa7e9('0x55')},_0x5bd293));}});return db['MemberReport']['bulkCreate'](_0x23f5a3['concat'](_0x3abde1),{'individualHooks':!![]});}})[_0xa7e9('0x1a')](function(){if(_0x2f7301){return _0x2f7301[_0xa7e9('0x29')]({'online':!![],'lastLoginAt':moment()[_0xa7e9('0x31')](_0xa7e9('0x2b')),'voicePause':_0x2f7301[_0xa7e9('0x52')]||![],'mailPause':_0x2f7301[_0xa7e9('0x52')]||![],'chatPause':_0x2f7301['loginInPause']||![],'faxPause':_0x2f7301['loginInPause']||![],'smsPause':_0x2f7301['loginInPause']||![],'openchannelPause':_0x2f7301['loginInPause']||![],'pauseType':_0x2f7301['loginInPause']?_0xa7e9('0x55'):null,'lastPauseAt':moment()[_0xa7e9('0x31')](_0xa7e9('0x2b')),'interface':_0x3fc989});}})[_0xa7e9('0x1a')](function(_0xf2566e){_0x52de9e(_0xf2566e);})[_0xa7e9('0x1d')](function(_0x3ed950){_0x62c7c6(_0x2bce8c[_0xa7e9('0x1e')](0x1f4,_0x3ed950[_0xa7e9('0x1f')]));});});};