45b31d7253ed949f8f1796aac76aa7f7e0f27cfb
[motion2.git] / server / services / ami / report / userReport.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 _0xfcf8=['../../../config/environment','ami','../ami','redis','socket.io-emitter','trunks','agents','telephones','registryentry','syncRegistry','bind','syncDeviceState','peerstatus','syncPeerStatus','then','actionSipShowRegistry','catch','error','[userReport][sipshowregistry]','inspect','resolve','actionDeviceStateList','[userReport][devicestatelist]','actionSipPeerStatus','[userReport][sippeerstatus]','prototype','syncRealtimeShow','isNotNull','isUndefined','emit','[userReport][registry]\x20registry:%s\x20state:%s','debug','[userReport][registry]','stateRegistry','merge','omit','username','state','[userReport][registry]\x20sending\x20trunk:save\x20event:','format','name','info','[userReport][devicestatechange]\x20device:%s\x20state:%s','device','local','toLowerCase','stateTime','toNumber','updateStatusRealtime','[userReport][devicestatechange]\x20sending\x20telephone:save\x20event:','telephone:%s','telephone:save','[userReport][devicestatechange]\x20sending\x20trunk:save\x20event:','trunk:save','Local','indexOf','find','mobile','accountcode','[userReport][devicestatechange]','[userReport][peerstatus]\x20peer:%s\x20address:%s\x20status:%s','peer','address','[userReport][peerstatus]','registered','status','[userReport][peerstatus]\x20sending\x20trunk:save\x20event:','trunk:%s','exports','lodash','moment','util','ioredis','bluebird'];(function(_0x5e17e3,_0x18a1d8){var _0x4d2325=function(_0x4dcf5c){while(--_0x4dcf5c){_0x5e17e3['push'](_0x5e17e3['shift']());}};_0x4d2325(++_0x18a1d8);}(_0xfcf8,0x8f));var _0x8fcf=function(_0x5608a3,_0x5b33c5){_0x5608a3=_0x5608a3-0x0;var _0x1ed50a=_0xfcf8[_0x5608a3];return _0x1ed50a;};'use strict';var _=require(_0x8fcf('0x0'));var moment=require(_0x8fcf('0x1'));var util=require(_0x8fcf('0x2'));var Redis=require(_0x8fcf('0x3'));var BPromise=require(_0x8fcf('0x4'));var config=require(_0x8fcf('0x5'));var logger=require('../../../config/logger')(_0x8fcf('0x6'));var ami=require(_0x8fcf('0x7'));config['redis']=_['defaults'](config[_0x8fcf('0x8')],{'host':'localhost','port':0x18eb});var io=require(_0x8fcf('0x9'))(new Redis(config[_0x8fcf('0x8')]));function UserReport(_0x1d05d4){this[_0x8fcf('0xa')]=_0x1d05d4[_0x8fcf('0xa')];this[_0x8fcf('0xb')]=_0x1d05d4[_0x8fcf('0xb')];this['telephones']=_0x1d05d4[_0x8fcf('0xc')];ami['on'](_0x8fcf('0xd'),this[_0x8fcf('0xe')][_0x8fcf('0xf')](this));ami['on']('registry',this[_0x8fcf('0xe')][_0x8fcf('0xf')](this));ami['on']('devicestatechange',this[_0x8fcf('0x10')]['bind'](this));ami['on'](_0x8fcf('0x11'),this[_0x8fcf('0x12')][_0x8fcf('0xf')](this));return this['syncRealtimeShow']();}function showSipRegistrations(){Promise['resolve']()[_0x8fcf('0x13')](ami[_0x8fcf('0x14')]())[_0x8fcf('0x15')](function(_0x41db9b){logger[_0x8fcf('0x16')](_0x8fcf('0x17'),util[_0x8fcf('0x18')](_0x41db9b,{'showHidden':![],'depth':null}));});}function listDeviceStates(){Promise[_0x8fcf('0x19')]()[_0x8fcf('0x13')](ami[_0x8fcf('0x1a')]())[_0x8fcf('0x15')](function(_0x4e5064){logger[_0x8fcf('0x16')](_0x8fcf('0x1b'),util[_0x8fcf('0x18')](_0x4e5064,{'showHidden':![],'depth':null}));});}function showPeersStatus(){Promise['resolve']()['then'](ami[_0x8fcf('0x1c')]())[_0x8fcf('0x15')](function(_0x13008a){logger[_0x8fcf('0x16')](_0x8fcf('0x1d'),util['inspect'](_0x13008a,{'showHidden':![],'depth':null}));});}UserReport[_0x8fcf('0x1e')][_0x8fcf('0x1f')]=function(){setInterval(function(){BPromise[_0x8fcf('0x19')]()[_0x8fcf('0x13')](showSipRegistrations())[_0x8fcf('0x13')](listDeviceStates())[_0x8fcf('0x13')](showPeersStatus());},0x32c8);};UserReport[_0x8fcf('0x1e')][_0x8fcf('0x20')]=function(_0x1673f6){return _0x1673f6!==null&&!_[_0x8fcf('0x21')](_0x1673f6);};UserReport[_0x8fcf('0x1e')][_0x8fcf('0x22')]=function(_0x2842a8,_0x338662,_0x255b35){io['to'](_0x2842a8)['emit'](_0x338662,_0x255b35);};UserReport[_0x8fcf('0x1e')][_0x8fcf('0xe')]=function(_0x4ac8fc){try{if(this[_0x8fcf('0x20')](_0x4ac8fc)){logger['info'](_0x8fcf('0x23'),_0x4ac8fc['username'],_0x4ac8fc['state']);logger[_0x8fcf('0x24')](_0x8fcf('0x25'),util['inspect'](_0x4ac8fc,{'showHidden':![],'depth':null}));var _0x4c3235=_['find'](this['trunks'],{'registry':_0x4ac8fc['username']});if(_0x4c3235){_0x4ac8fc[_0x8fcf('0x26')]=_0x4ac8fc['state'];_[_0x8fcf('0x27')](_0x4c3235,_[_0x8fcf('0x28')](_0x4ac8fc,['event','actionid',_0x8fcf('0x29'),_0x8fcf('0x2a')]));logger[_0x8fcf('0x24')](_0x8fcf('0x2b'),util[_0x8fcf('0x18')](_0x4c3235,{'showHidden':![],'depth':null}));this['emit'](util[_0x8fcf('0x2c')]('trunk:%s',_0x4c3235[_0x8fcf('0x2d')]),'trunk:save',_0x4c3235);}}}catch(_0x2df8ef){logger['error']('[userReport][registry]',util[_0x8fcf('0x18')](_0x2df8ef,{'showHidden':![],'depth':null}));}};UserReport[_0x8fcf('0x1e')]['syncDeviceState']=function(_0xf950ae){try{if(this[_0x8fcf('0x20')](_0xf950ae)){logger[_0x8fcf('0x2e')](_0x8fcf('0x2f'),_0xf950ae['device'],_0xf950ae['state']);logger[_0x8fcf('0x24')]('[userReport][devicestatechange]',util[_0x8fcf('0x18')](_0xf950ae,{'showHidden':![],'depth':null}));if(this[_0x8fcf('0xb')][_0xf950ae[_0x8fcf('0x30')]]){if(!this[_0x8fcf('0xb')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x31')]||_0xf950ae['state'][_0x8fcf('0x32')]()=='inuse'){this[_0x8fcf('0xb')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x31')]=![];if(this['agents'][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2a')]!==_0xf950ae[_0x8fcf('0x2a')][_0x8fcf('0x32')]()){this[_0x8fcf('0xb')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x33')]=_[_0x8fcf('0x34')](moment()['format']('x'));}this[_0x8fcf('0xb')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2a')]=_0xf950ae['state'][_0x8fcf('0x32')]();this[_0x8fcf('0xb')][_0xf950ae['device']][_0x8fcf('0x35')](_0xf950ae);}}else if(this['telephones'][_0xf950ae[_0x8fcf('0x30')]]){if(this[_0x8fcf('0xc')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2a')]!==_0xf950ae[_0x8fcf('0x2a')]['toLowerCase']()){this[_0x8fcf('0xc')][_0xf950ae['device']]['stateTime']=_[_0x8fcf('0x34')](moment()['format']('x'));}this[_0x8fcf('0xc')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2a')]=_0xf950ae['state'][_0x8fcf('0x32')]();logger['debug'](_0x8fcf('0x36'),util[_0x8fcf('0x18')](this[_0x8fcf('0xc')][_0xf950ae[_0x8fcf('0x30')]],{'showHidden':![],'depth':null}));this[_0x8fcf('0x22')](util[_0x8fcf('0x2c')](_0x8fcf('0x37'),this[_0x8fcf('0xc')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2d')]),_0x8fcf('0x38'),this[_0x8fcf('0xc')][_0xf950ae[_0x8fcf('0x30')]]);}else if(this[_0x8fcf('0xa')][_0xf950ae[_0x8fcf('0x30')]]){if(this[_0x8fcf('0xa')][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2a')]!==_0xf950ae[_0x8fcf('0x2a')][_0x8fcf('0x32')]()){this[_0x8fcf('0xa')][_0xf950ae[_0x8fcf('0x30')]]['stateTime']=_['toNumber'](moment()['format']('x'));}this[_0x8fcf('0xa')][_0xf950ae['device']][_0x8fcf('0x2a')]=_0xf950ae['state'][_0x8fcf('0x32')]();logger[_0x8fcf('0x24')](_0x8fcf('0x39'),util['inspect'](this[_0x8fcf('0xa')][_0xf950ae[_0x8fcf('0x30')]],{'showHidden':![],'depth':null}));this[_0x8fcf('0x22')](util[_0x8fcf('0x2c')]('trunk:%s',this['trunks'][_0xf950ae[_0x8fcf('0x30')]][_0x8fcf('0x2d')]),_0x8fcf('0x3a'),this[_0x8fcf('0xa')][_0xf950ae[_0x8fcf('0x30')]]);}else if(_['startsWith'](_0xf950ae[_0x8fcf('0x30')],_0x8fcf('0x3b'))){var _0x533be6=_0xf950ae[_0x8fcf('0x30')];var _0x1488cd=_0x533be6['substring'](_0x533be6[_0x8fcf('0x3c')]('/')+0x1,_0x533be6[_0x8fcf('0x3c')]('@'));var _0x2808bd=_[_0x8fcf('0x3d')](this['agents'],function(_0x22d2e3){return _0x22d2e3[_0x8fcf('0x3e')]==_0x1488cd||_0x22d2e3['phone']==_0x1488cd||_0x22d2e3[_0x8fcf('0x3f')]==_0x1488cd;});if(_0x2808bd){if(_0x2808bd[_0x8fcf('0x2a')]!==_0xf950ae[_0x8fcf('0x2a')]['toLowerCase']()){_0x2808bd[_0x8fcf('0x33')]=_[_0x8fcf('0x34')](moment()['format']('x'));}_0x2808bd[_0x8fcf('0x2a')]=_0xf950ae['state'][_0x8fcf('0x32')]();_0x2808bd[_0x8fcf('0x31')]=!![];_0x2808bd['updateStatusRealtime'](_0xf950ae);}}}}catch(_0x4007a3){logger[_0x8fcf('0x16')](_0x8fcf('0x40'),util[_0x8fcf('0x18')](_0x4007a3,{'showHidden':![],'depth':null}));}};UserReport[_0x8fcf('0x1e')][_0x8fcf('0x12')]=function(_0x5ceb47){try{if(this[_0x8fcf('0x20')](_0x5ceb47)&&this[_0x8fcf('0x20')](_0x5ceb47[_0x8fcf('0x11')])){logger[_0x8fcf('0x2e')](_0x8fcf('0x41'),_0x5ceb47[_0x8fcf('0x42')],_0x5ceb47[_0x8fcf('0x43')],_0x5ceb47[_0x8fcf('0x11')]);logger[_0x8fcf('0x24')](_0x8fcf('0x44'),util['inspect'](_0x5ceb47,{'showHidden':![],'depth':null}));if(this[_0x8fcf('0xb')][_0x5ceb47['peer']]){if(!this[_0x8fcf('0xb')][_0x5ceb47['peer']][_0x8fcf('0x31')]||_0x5ceb47['peerstatus'][_0x8fcf('0x32')]()==_0x8fcf('0x45')){this[_0x8fcf('0xb')][_0x5ceb47['peer']][_0x8fcf('0x31')]=![];this['agents'][_0x5ceb47[_0x8fcf('0x42')]][_0x8fcf('0x46')]=_0x5ceb47[_0x8fcf('0x11')][_0x8fcf('0x32')]();this[_0x8fcf('0xb')][_0x5ceb47['peer']]['address']=_0x5ceb47['address'];this[_0x8fcf('0xb')][_0x5ceb47[_0x8fcf('0x42')]][_0x8fcf('0x35')](_0x5ceb47);}}else if(this['telephones'][_0x5ceb47[_0x8fcf('0x42')]]){this[_0x8fcf('0xc')][_0x5ceb47[_0x8fcf('0x42')]]['status']=_0x5ceb47['peerstatus']['toLowerCase']();this[_0x8fcf('0xc')][_0x5ceb47[_0x8fcf('0x42')]][_0x8fcf('0x43')]=_0x5ceb47[_0x8fcf('0x43')];logger['debug']('[userReport][peerstatus]\x20sending\x20telephone:save\x20event:',util[_0x8fcf('0x18')](this[_0x8fcf('0xc')][_0x5ceb47[_0x8fcf('0x42')]],{'showHidden':![],'depth':null}));this[_0x8fcf('0x22')](util[_0x8fcf('0x2c')](_0x8fcf('0x37'),this['telephones'][_0x5ceb47[_0x8fcf('0x42')]][_0x8fcf('0x2d')]),'telephone:save',this['telephones'][_0x5ceb47[_0x8fcf('0x42')]]);}else if(this[_0x8fcf('0xa')][_0x5ceb47[_0x8fcf('0x42')]]){this[_0x8fcf('0xa')][_0x5ceb47[_0x8fcf('0x42')]]['status']=_0x5ceb47[_0x8fcf('0x11')][_0x8fcf('0x32')]();this[_0x8fcf('0xa')][_0x5ceb47[_0x8fcf('0x42')]]['address']=_0x5ceb47[_0x8fcf('0x43')];logger['debug'](_0x8fcf('0x47'),util['inspect'](this[_0x8fcf('0xa')][_0x5ceb47['peer']],{'showHidden':![],'depth':null}));this[_0x8fcf('0x22')](util['format'](_0x8fcf('0x48'),this[_0x8fcf('0xa')][_0x5ceb47['peer']]['name']),_0x8fcf('0x3a'),this[_0x8fcf('0xa')][_0x5ceb47[_0x8fcf('0x42')]]);}}}catch(_0x2acdac){logger[_0x8fcf('0x16')]('[userReport][peerstatus]',util['inspect'](_0x2acdac,{'showHidden':![],'depth':null}));}};module[_0x8fcf('0x49')]=UserReport;