Built motion from commit 4fa9d4a7.|2.6.31
[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 _0xe647=['findAll','PAUSE','length','YYYY-MM-DD\x20HH:mm:ss','updateAttributes','getChannels','forEach','push','isNil','interface','format','type','body','uniqueid','DEFAULT\x20PAUSE','UnpauseUser','resolve','isValidChannel','Channel\x20parameter\x20%s\x20is\x20invalid','channel','online','voicePause','mailPause','chatPause','faxPause','openchannelPause','smsPause','whatsappPause','lastPauseAt','User\x20with\x20id\x20%s\x20not\x20found','system','LogoutUser','all','LoginUser','loginInPause','count','agent','getLicense','Maximum\x20login\x20has\x20been\x20reached','UserVoiceQueue.penalty','LOGIN','callysquare','LOGININPAUSE','bulkCreate','concat','lodash','util','bluebird','ioredis','../../mysqldb','../../config/utils','../../config/logger','../../config/environment','redis','defaults','localhost','socket.io-emitter','./user.socket','GetUser','User','options','raw','where','attributes','limit','include','map','model','debug','stringify','catch','message','error','ShowUser','find','info','PauseUser','name','role','internal','then','params','UserVoiceQueueRt','update','MemberReport'];(function(_0x3bceea,_0x58af75){var _0x30a8d3=function(_0x1550b5){while(--_0x1550b5){_0x3bceea['push'](_0x3bceea['shift']());}};_0x30a8d3(++_0x58af75);}(_0xe647,0x12c));var _0x7e64=function(_0x56928f,_0x5ed25f){_0x56928f=_0x56928f-0x0;var _0x28d5b8=_0xe647[_0x56928f];return _0x28d5b8;};'use strict';var _=require(_0x7e64('0x0'));var util=require(_0x7e64('0x1'));var moment=require('moment');var BPromise=require(_0x7e64('0x2'));var Redis=require(_0x7e64('0x3'));var db=require(_0x7e64('0x4'))['db'];var utils=require(_0x7e64('0x5'));var logger=require(_0x7e64('0x6'))('rpc');var config=require(_0x7e64('0x7'));config[_0x7e64('0x8')]=_[_0x7e64('0x9')](config[_0x7e64('0x8')],{'host':_0x7e64('0xa'),'port':0x18eb});var socket=require(_0x7e64('0xb'))(new Redis(config['redis']));require(_0x7e64('0xc'))['register'](socket);exports[_0x7e64('0xd')]=function(_0x368c26){var _0x3109b5=this;return new Promise(function(_0x40dd45,_0x45235b){return db[_0x7e64('0xe')]['findAll']({'raw':_0x368c26[_0x7e64('0xf')]?_0x368c26[_0x7e64('0xf')][_0x7e64('0x10')]===undefined?!![]:![]:!![],'where':_0x368c26[_0x7e64('0xf')]?_0x368c26[_0x7e64('0xf')][_0x7e64('0x11')]||null:null,'attributes':_0x368c26[_0x7e64('0xf')]?_0x368c26[_0x7e64('0xf')][_0x7e64('0x12')]||null:null,'limit':_0x368c26[_0x7e64('0xf')]?_0x368c26[_0x7e64('0xf')][_0x7e64('0x13')]||null:null,'include':_0x368c26['options']?_0x368c26[_0x7e64('0xf')][_0x7e64('0x14')]?_[_0x7e64('0x15')](_0x368c26[_0x7e64('0xf')][_0x7e64('0x14')],function(_0x5f2035){return{'model':db[_0x5f2035[_0x7e64('0x16')]],'as':_0x5f2035['as'],'attributes':_0x5f2035[_0x7e64('0x12')],'include':_0x5f2035[_0x7e64('0x14')]?_['map'](_0x5f2035[_0x7e64('0x14')],function(_0x7c02e5){return{'model':db[_0x7c02e5[_0x7e64('0x16')]],'as':_0x7c02e5['as'],'attributes':_0x7c02e5['attributes'],'include':_0x7c02e5[_0x7e64('0x14')]?_[_0x7e64('0x15')](_0x7c02e5['include'],function(_0x4a788a){return{'model':db[_0x4a788a[_0x7e64('0x16')]],'as':_0x4a788a['as'],'attributes':_0x4a788a['attributes']};}):[]};}):[]};}):[]:[]})['then'](function(_0x5b28fb){logger['info'](_0x7e64('0xd'),_0x368c26);logger[_0x7e64('0x17')](_0x7e64('0xd'),_0x368c26,JSON[_0x7e64('0x18')](_0x5b28fb));_0x40dd45(_0x5b28fb);})[_0x7e64('0x19')](function(_0x16e544){logger['error'](_0x7e64('0xd'),_0x16e544[_0x7e64('0x1a')],_0x368c26);_0x45235b(_0x3109b5[_0x7e64('0x1b')](0x1f4,_0x16e544['message']));});});};exports[_0x7e64('0x1c')]=function(_0x1fedf1){var _0xc620a5=this;return new Promise(function(_0x475311,_0x952b99){return db[_0x7e64('0xe')][_0x7e64('0x1d')]({'raw':_0x1fedf1[_0x7e64('0xf')]?_0x1fedf1[_0x7e64('0xf')][_0x7e64('0x10')]===undefined?!![]:![]:!![],'where':_0x1fedf1[_0x7e64('0xf')]?_0x1fedf1['options'][_0x7e64('0x11')]||null:null,'attributes':_0x1fedf1[_0x7e64('0xf')]?_0x1fedf1[_0x7e64('0xf')][_0x7e64('0x12')]||null:null,'include':_0x1fedf1[_0x7e64('0xf')]?_0x1fedf1[_0x7e64('0xf')][_0x7e64('0x14')]?_[_0x7e64('0x15')](_0x1fedf1[_0x7e64('0xf')]['include'],function(_0x1c9b9c){return{'model':db[_0x1c9b9c[_0x7e64('0x16')]],'as':_0x1c9b9c['as'],'attributes':_0x1c9b9c[_0x7e64('0x12')],'include':_0x1c9b9c[_0x7e64('0x14')]?_[_0x7e64('0x15')](_0x1c9b9c[_0x7e64('0x14')],function(_0x965273){return{'model':db[_0x965273['model']],'as':_0x965273['as'],'attributes':_0x965273[_0x7e64('0x12')],'include':_0x965273[_0x7e64('0x14')]?_['map'](_0x965273['include'],function(_0x5dba7c){return{'model':db[_0x5dba7c[_0x7e64('0x16')]],'as':_0x5dba7c['as'],'attributes':_0x5dba7c[_0x7e64('0x12')]};}):[]};}):[]};}):[]:[]})['then'](function(_0x594cab){logger[_0x7e64('0x1e')]('ShowUser',_0x1fedf1);logger[_0x7e64('0x17')](_0x7e64('0x1c'),_0x1fedf1,JSON['stringify'](_0x594cab));_0x475311(_0x594cab);})[_0x7e64('0x19')](function(_0x703fcc){logger['error'](_0x7e64('0x1c'),_0x703fcc[_0x7e64('0x1a')],_0x1fedf1);_0x952b99(_0xc620a5[_0x7e64('0x1b')](0x1f4,_0x703fcc[_0x7e64('0x1a')]));});});};exports[_0x7e64('0x1f')]=function(_0x433097){var _0x2a6738;return db[_0x7e64('0xe')][_0x7e64('0x1d')]({'where':_0x433097['options'][_0x7e64('0x11')]||{},'attributes':['id',_0x7e64('0x20'),_0x7e64('0x21'),_0x7e64('0x22'),'interface']})[_0x7e64('0x23')](function(_0x36998a){if(!_0x36998a){throw new Error(util['format']('User\x20with\x20id\x20%s\x20not\x20found',_0x433097[_0x7e64('0x24')]['id']));}_0x2a6738=_0x36998a;return db[_0x7e64('0x25')][_0x7e64('0x26')]({'paused':0x1},{'where':{'UserId':_0x2a6738['id']},'individualHooks':!![]});})[_0x7e64('0x23')](function(){return db[_0x7e64('0x27')][_0x7e64('0x28')]({'where':{'membername':_0x2a6738[_0x7e64('0x20')],'type':_0x7e64('0x29'),'exitAt':null}});})[_0x7e64('0x23')](function(_0x32278e){if(_0x32278e&&_0x32278e[_0x7e64('0x2a')]){var _0x4c71b1=moment()['format'](_0x7e64('0x2b'));for(var _0x5386ab=0x0,_0x4e345b=_0x32278e[_0x7e64('0x2a')];_0x5386ab<_0x4e345b;_0x5386ab++){_0x32278e[_0x5386ab][_0x7e64('0x2c')]({'exitAt':_0x4c71b1});}}})[_0x7e64('0x23')](function(){var _0x51d99c=[];var _0x5cdc72=utils[_0x7e64('0x2d')]();_0x5cdc72[_0x7e64('0x2e')](function(_0x1cd40a){_0x51d99c[_0x7e64('0x2f')]({'channel':_0x1cd40a,'membername':_0x2a6738[_0x7e64('0x20')],'interface':_[_0x7e64('0x30')](_0x2a6738[_0x7e64('0x31')])?util[_0x7e64('0x32')]('SIP/%s',_0x2a6738[_0x7e64('0x20')]):_0x2a6738[_0x7e64('0x31')],'type':_0x7e64('0x29'),'enterAt':moment()[_0x7e64('0x32')]('YYYY-MM-DD\x20HH:mm:ss'),'data1':_0x433097['body'][_0x7e64('0x33')]||'DEFAULT\x20PAUSE','role':_0x2a6738['role'],'internal':_0x2a6738[_0x7e64('0x22')],'uniqueid':_0x433097[_0x7e64('0x34')][_0x7e64('0x35')]||null});});return db[_0x7e64('0x27')]['bulkCreate'](_0x51d99c,{'individualHooks':!![]});})['then'](function(){return _0x2a6738[_0x7e64('0x26')]({'voicePause':!![],'mailPause':!![],'chatPause':!![],'faxPause':!![],'smsPause':!![],'openchannelPause':!![],'whatsappPause':!![],'pauseType':_0x433097[_0x7e64('0x34')]['type']||_0x7e64('0x36'),'lastPauseAt':moment()['format'](_0x7e64('0x2b'))});})['catch'](function(_0x395616){throw _0x395616;});};exports[_0x7e64('0x37')]=function(_0x3b6356){return Promise[_0x7e64('0x38')]()['then'](function(){if(_0x3b6356[_0x7e64('0x34')]['channel']&&!utils[_0x7e64('0x39')](_0x3b6356[_0x7e64('0x34')]['channel'])){throw new Error(util[_0x7e64('0x32')](_0x7e64('0x3a'),_0x3b6356['body'][_0x7e64('0x3b')]));}return db['User']['findOne']({'where':_0x3b6356[_0x7e64('0xf')][_0x7e64('0x11')]||{},'attributes':['id',_0x7e64('0x20'),_0x7e64('0x21'),_0x7e64('0x22'),_0x7e64('0x31'),_0x7e64('0x3c'),_0x7e64('0x3d'),_0x7e64('0x3e'),_0x7e64('0x3f'),_0x7e64('0x40'),_0x7e64('0x41'),_0x7e64('0x42'),_0x7e64('0x43'),'pauseType',_0x7e64('0x44')]});})[_0x7e64('0x23')](function(_0x26e3dd){if(!_0x26e3dd){throw new Error(util[_0x7e64('0x32')](_0x7e64('0x45'),_0x3b6356[_0x7e64('0x24')]['id']));}return _0x26e3dd['unpause'](_0x3b6356['body']['channel'],_0x7e64('0x46'));})[_0x7e64('0x19')](function(_0x5ecc65){throw _0x5ecc65;});};exports[_0x7e64('0x47')]=function(_0x12a569){var _0x56b154=this;return new Promise(function(_0x57f938,_0x4bb9ab){var _0x40e669;return db[_0x7e64('0xe')]['findOne']({'where':_0x12a569[_0x7e64('0xf')][_0x7e64('0x11')]||{},'attributes':['id','name']})[_0x7e64('0x23')](function(_0x5f1f5a){if(_0x5f1f5a){_0x40e669=_0x5f1f5a;return db[_0x7e64('0x25')]['destroy']({'where':{'membername':_0x40e669[_0x7e64('0x20')]},'individualHooks':!![]});}})[_0x7e64('0x23')](function(){if(_0x40e669){return db['MemberReport'][_0x7e64('0x28')]({'where':{'membername':_0x40e669[_0x7e64('0x20')],'exitAt':null}});}})[_0x7e64('0x23')](function(_0x38e885){if(_0x40e669){if(_0x38e885&&_0x38e885['length']){var _0x242295=[];_0x38e885[_0x7e64('0x2e')](function(_0x302512){_0x242295['push'](_0x302512[_0x7e64('0x26')]({'exitAt':moment()[_0x7e64('0x32')](_0x7e64('0x2b'))}));});return BPromise[_0x7e64('0x48')](_0x242295);}}})[_0x7e64('0x23')](function(){if(_0x40e669){return _0x40e669[_0x7e64('0x26')]({'online':![],'voicePause':![],'mailPause':![],'chatPause':![],'faxPause':![],'smsPause':![],'openchannelPause':![]});}})[_0x7e64('0x23')](function(_0xd4e97b){_0x57f938(_0xd4e97b);})[_0x7e64('0x19')](function(_0x274a15){_0x4bb9ab(_0x56b154[_0x7e64('0x1b')](0x1f4,_0x274a15[_0x7e64('0x1a')]));});});};exports[_0x7e64('0x49')]=function(_0x9a9705){var _0x32307f=this;return new Promise(function(_0x4f4e41,_0xc8fb38){var _0x5823d5;var _0x1b7c18;var _0x54404d;var _0x34f319;return db[_0x7e64('0xe')]['findOne']({'where':_0x9a9705['options']['where']||{},'attributes':['id',_0x7e64('0x20'),_0x7e64('0x21'),'mobile','phone',_0x7e64('0x22'),_0x7e64('0x4a'),_0x7e64('0x31')]})[_0x7e64('0x23')](function(_0xc2e9da){if(_0xc2e9da){_0x5823d5=_0xc2e9da;_0x34f319=_0x9a9705[_0x7e64('0x34')][_0x7e64('0x31')]?_0x9a9705[_0x7e64('0x34')][_0x7e64('0x31')]:util['format']('SIP/%s',_0x5823d5[_0x7e64('0x20')]);return db[_0x7e64('0xe')][_0x7e64('0x4b')]({'where':{'role':_0x7e64('0x4c'),'online':!![],'id':{'$ne':_0x5823d5['id']}}});}})[_0x7e64('0x23')](function(_0xcf018f){if(_0xcf018f){_0x54404d=_0xcf018f;return require('../../config/license/util')[_0x7e64('0x4d')]();}})['then'](function(_0x186a3f){if(_0x186a3f&&_0x186a3f['agents']&&_0x54404d>=_0x186a3f['agents']){throw new Error(_0x7e64('0x4e'));}})['then'](function(){if(_0x5823d5){return db['UserVoiceQueueRt']['destroy']({'where':{'membername':_0x5823d5[_0x7e64('0x20')]},'individualHooks':!![]});}})[_0x7e64('0x23')](function(){if(_0x5823d5){return _0x5823d5['getVoiceQueues']({'raw':!![]});}})[_0x7e64('0x23')](function(_0x1a6d98){if(_0x5823d5){if(_0x1a6d98){var _0x5aee75=[];_0x1b7c18=_0x1a6d98;if(_0x1b7c18[_0x7e64('0x2a')]){for(var _0x21e201=0x0;_0x21e201<_0x1b7c18[_0x7e64('0x2a')];_0x21e201++){_0x5aee75['push'](db[_0x7e64('0x25')]['upsert']({'membername':_0x5823d5[_0x7e64('0x20')],'UserId':_0x5823d5['id'],'queue_name':_0x1b7c18[_0x21e201][_0x7e64('0x20')],'VoiceQueueId':_0x1b7c18[_0x21e201]['id'],'interface':_0x34f319,'paused':_0x5823d5[_0x7e64('0x4a')]||![],'penalty':_0x1b7c18[_0x21e201][_0x7e64('0x4f')]||0x0}));}return BPromise['all'](_0x5aee75);}}}})['then'](function(){if(_0x5823d5){return db[_0x7e64('0x27')][_0x7e64('0x28')]({'where':{'membername':_0x5823d5[_0x7e64('0x20')],'exitAt':null}});}})[_0x7e64('0x23')](function(_0x3b8134){if(_0x5823d5){if(_0x3b8134&&_0x3b8134[_0x7e64('0x2a')]){var _0xfac38a=[];_0x3b8134[_0x7e64('0x2e')](function(_0x3e4417){_0xfac38a['push'](_0x3e4417[_0x7e64('0x26')]({'exitAt':moment()['format'](_0x7e64('0x2b'))}));});return BPromise[_0x7e64('0x48')](_0xfac38a);}}})[_0x7e64('0x23')](function(){if(_0x5823d5){var _0xcffdcd=[];var _0x4cabf6=[];var _0x350047=utils[_0x7e64('0x2d')]();var _0x562b5d={'membername':_0x5823d5[_0x7e64('0x20')],'interface':_0x34f319,'enterAt':moment()['format'](_0x7e64('0x2b')),'role':_0x5823d5['role'],'internal':_0x5823d5['internal']};_0x350047[_0x7e64('0x2e')](function(_0x5df6cc){_0xcffdcd[_0x7e64('0x2f')](_['merge']({'channel':_0x5df6cc,'type':_0x7e64('0x50'),'data1':_0x7e64('0x51'),'data2':_0x34f319},_0x562b5d));if(_0x5823d5['loginInPause']){_0x4cabf6[_0x7e64('0x2f')](_['merge']({'channel':_0x5df6cc,'type':_0x7e64('0x29'),'data1':_0x7e64('0x52')},_0x562b5d));}});return db[_0x7e64('0x27')][_0x7e64('0x53')](_0xcffdcd[_0x7e64('0x54')](_0x4cabf6),{'individualHooks':!![]});}})['then'](function(){if(_0x5823d5){return _0x5823d5[_0x7e64('0x26')]({'online':!![],'lastLoginAt':moment()[_0x7e64('0x32')](_0x7e64('0x2b')),'voicePause':_0x5823d5[_0x7e64('0x4a')]||![],'mailPause':_0x5823d5['loginInPause']||![],'chatPause':_0x5823d5['loginInPause']||![],'faxPause':_0x5823d5[_0x7e64('0x4a')]||![],'smsPause':_0x5823d5[_0x7e64('0x4a')]||![],'openchannelPause':_0x5823d5['loginInPause']||![],'pauseType':_0x5823d5['loginInPause']?_0x7e64('0x52'):null,'lastPauseAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'interface':_0x34f319});}})[_0x7e64('0x23')](function(_0x4b3df6){_0x4f4e41(_0x4b3df6);})['catch'](function(_0x5878e4){_0xc8fb38(_0x32307f['error'](0x1f4,_0x5878e4[_0x7e64('0x1a')]));});});};