8db36b1408f9ff2780bb8f7af019869d798f0448
[motion2.git] / server / services / ami / report / voiceDialReport.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 _0x81b6=['privilege','info','membername','[%s][voiceDialReport][dialbegin]\x20sending\x20voice_outbound_channel:save\x20event:','voice:outbound','debug','error','[%s][voiceDialReport][dialend]\x20event:','dialstatus','ANSWER','connectedlinenum','connectedlinename','lastevent','[%s][voiceDialReport][dialend]\x20agent:%s\x20lastevent:connect','[%s][voiceDialReport][dialend]\x20sending\x20dialconnect\x20event:','custom:dialconnect','[%s][voiceDialReport][dialend]\x20sending\x20voice_outbound_channel:save\x20event:','[%s][voiceDialReport][varset]\x20event:','addVariable','keys','variable','value','voice','[%s][voiceDialReport][varset]\x20outboundrouteid:%s\x20variable:%s\x20value:%s','[%s][voiceDialReport][varset]\x20sending\x20voice_outbound_channel:save\x20event:','voice_outbound_channel:save','[%s][voiceDialReport][varset]','rtpaudioqos','rtpaudioqosjitter','rtpaudioqosjitterbridged','rtpaudioqosloss','rtpaudioqosrtt','rtpaudioqosrttbridged','sipcallid','sipuri','bridgepeer','bridgepvtcallid','membercalls','memberdynamic','memberinterface','memberpenalty','memberrealtime','isNil','[%s][voiceDialReport][hangup]\x20reason:%s\x20lasteevent:complete','cause-txt','duration','endtime','diff','starttime','seconds','complete','total','answertime','holdtime','billableseconds','sumHoldTime','sumDuration','[%s][voiceDialReport][hangup]\x20sending\x20voiceDialReport\x20event:','custom:voiceDialReport','custom:dialend','[%s][voiceDialReport][hangup]\x20sending\x20voice_outbound:save\x20event:','voice_outbound:save','[DEBUG]\x20Emit\x20voice_outbound_channel:remove\x20event','stringify','[%s][voiceDialReport][hangup]\x20sending\x20voice_outbound_channel:remove\x20event:','voice_outbound_channel:remove','deleteChannelAfterTimeout','linkedid','destuniqueid','[%s][voiceDialReport][hangup]','syncNewExten','[%s][voiceDialReport][newexten]\x20event:','application','toLowerCase','set','applicationdata','startsWith','match','exports','lodash','moment','../../../config/environment','../../../config/logger','../ami','redis','defaults','localhost','outboundChannels','outbound','applications','variables','dialbegin','syncDialBegin','bind','dialend','syncDialEnd','syncVarSet','hangup','syncHangup','prototype','isNotNull','isUndefined','emit','[%s][voiceDialReport][dialbegin]\x20event:','inspect','uniqueid','merge','channels','format','YYYY-MM-DD\x20HH:mm:ss','called','channel','lastIndexOf','substring','omit','event'];(function(_0x17d9af,_0x4b29c6){var _0x4853d5=function(_0x4c4642){while(--_0x4c4642){_0x17d9af['push'](_0x17d9af['shift']());}};_0x4853d5(++_0x4b29c6);}(_0x81b6,0x137));var _0x681b=function(_0x99a0fd,_0x188d72){_0x99a0fd=_0x99a0fd-0x0;var _0xfb2e82=_0x81b6[_0x99a0fd];return _0xfb2e82;};'use strict';var _=require(_0x681b('0x0'));var moment=require(_0x681b('0x1'));var util=require('util');var Redis=require('ioredis');var config=require(_0x681b('0x2'));var logger=require(_0x681b('0x3'))('ami');var ami=require(_0x681b('0x4'));config[_0x681b('0x5')]=_[_0x681b('0x6')](config[_0x681b('0x5')],{'host':_0x681b('0x7'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config['redis']));function VoiceDialReport(_0x24fec8){this['channels']=_0x24fec8[_0x681b('0x8')];this[_0x681b('0x9')]=_0x24fec8['outbound'];this[_0x681b('0xa')]={};this[_0x681b('0xb')]={};ami['on'](_0x681b('0xc'),this[_0x681b('0xd')][_0x681b('0xe')](this));ami['on'](_0x681b('0xf'),this[_0x681b('0x10')][_0x681b('0xe')](this));ami['on']('varset',this[_0x681b('0x11')][_0x681b('0xe')](this));ami['on'](_0x681b('0x12'),this[_0x681b('0x13')][_0x681b('0xe')](this));}VoiceDialReport[_0x681b('0x14')][_0x681b('0x15')]=function(_0x5a8ae0){return _0x5a8ae0!==null&&!_[_0x681b('0x16')](_0x5a8ae0);};VoiceDialReport['prototype'][_0x681b('0x17')]=function(_0x5bcd17,_0x238123,_0x1aba52){io['to'](_0x5bcd17)[_0x681b('0x17')](_0x238123,_0x1aba52);};VoiceDialReport['prototype'][_0x681b('0xd')]=function(_0x100318){try{if(this['isNotNull'](_0x100318)){logger['debug'](_0x681b('0x18'),_0x100318['uniqueid'],util[_0x681b('0x19')](_0x100318,{'showHidden':![],'depth':null}));if(this['channels'][_0x100318[_0x681b('0x1a')]]){_[_0x681b('0x1b')](this[_0x681b('0x1c')][_0x100318[_0x681b('0x1a')]],{'starttime':moment()[_0x681b('0x1d')](_0x681b('0x1e')),'lastevent':_0x681b('0x1f'),'interface':_0x100318[_0x681b('0x20')]['substring'](0x0,_0x100318[_0x681b('0x20')][_0x681b('0x21')]('-')),'membername':_0x100318['channel'][_0x681b('0x22')](_0x100318[_0x681b('0x20')]['lastIndexOf']('/')+0x1,_0x100318[_0x681b('0x20')][_0x681b('0x21')]('-'))},_[_0x681b('0x23')](_0x100318,[_0x681b('0x24'),_0x681b('0x25')]));logger[_0x681b('0x26')]('[%s][voiceDialReport][dialbegin]\x20agent:%s\x20interface:%s\x20lastevent:called',_0x100318[_0x681b('0x1a')],this[_0x681b('0x1c')][_0x100318[_0x681b('0x1a')]][_0x681b('0x27')],this[_0x681b('0x1c')][_0x100318[_0x681b('0x1a')]]['interface']);logger['debug'](_0x681b('0x28'),_0x100318[_0x681b('0x1a')],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x100318[_0x681b('0x1a')]],{'showHidden':![],'depth':null}));if(this[_0x681b('0xa')][_0x100318['uniqueid']])_[_0x681b('0x1b')](this[_0x681b('0x1c')][_0x100318[_0x681b('0x1a')]],this['applications'][_0x100318[_0x681b('0x1a')]]);this[_0x681b('0x17')](_0x681b('0x29'),'voice_outbound_channel:save',this[_0x681b('0x1c')][_0x100318[_0x681b('0x1a')]]);logger[_0x681b('0x2a')]('[%s][voiceDialReport][dialbegin]\x20sending\x20dialbegin\x20event:',_0x100318['uniqueid'],util['inspect'](this['channels'][_0x100318['uniqueid']],{'showHidden':![],'depth':null}));ami[_0x681b('0x17')]('custom:dialbegin',this['channels'][_0x100318[_0x681b('0x1a')]]);}}}catch(_0x4b57e2){logger[_0x681b('0x2b')]('[%s][voiceDialReport][dialbegin]',_0x100318[_0x681b('0x1a')],util[_0x681b('0x19')](_0x4b57e2,{'showHidden':![],'depth':null}));}};VoiceDialReport['prototype']['syncDialEnd']=function(_0x2b3452){try{if(this[_0x681b('0x15')](_0x2b3452)){logger['debug'](_0x681b('0x2c'),_0x2b3452['uniqueid'],util['inspect'](_0x2b3452,{'showHidden':![],'depth':null}));if(this['channels'][_0x2b3452[_0x681b('0x1a')]]){this['channels'][_0x2b3452[_0x681b('0x1a')]][_0x681b('0x2d')]=_0x2b3452[_0x681b('0x2d')];if(_0x2b3452[_0x681b('0x2d')]===_0x681b('0x2e')){this[_0x681b('0x1c')][_0x2b3452[_0x681b('0x1a')]][_0x681b('0x2f')]=_0x2b3452[_0x681b('0x2f')];this[_0x681b('0x1c')][_0x2b3452[_0x681b('0x1a')]]['connectedlinename']=_0x2b3452[_0x681b('0x30')];this[_0x681b('0x1c')][_0x2b3452['uniqueid']]['answertime']=moment()[_0x681b('0x1d')](_0x681b('0x1e'));this[_0x681b('0x1c')][_0x2b3452[_0x681b('0x1a')]][_0x681b('0x31')]='connect';logger[_0x681b('0x26')](_0x681b('0x32'),_0x2b3452[_0x681b('0x1a')],_0x2b3452[_0x681b('0x20')][_0x681b('0x22')](_0x2b3452[_0x681b('0x20')][_0x681b('0x21')]('/')+0x1,_0x2b3452[_0x681b('0x20')]['lastIndexOf']('-')));logger['debug'](_0x681b('0x33'),_0x2b3452[_0x681b('0x1a')],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x2b3452['uniqueid']],{'showHidden':![],'depth':null}));ami[_0x681b('0x17')](_0x681b('0x34'),this[_0x681b('0x1c')][_0x2b3452['uniqueid']]);}logger['debug'](_0x681b('0x35'),_0x2b3452[_0x681b('0x1a')],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x2b3452[_0x681b('0x1a')]],{'showHidden':![],'depth':null}));this[_0x681b('0x17')](_0x681b('0x29'),'voice_outbound_channel:save',this[_0x681b('0x1c')][_0x2b3452[_0x681b('0x1a')]]);}}}catch(_0x3ce3f2){logger['error']('[%s][voiceDialReport][dialend]',_0x2b3452[_0x681b('0x1a')],util[_0x681b('0x19')](_0x3ce3f2,{'showHidden':![],'depth':null}));}};VoiceDialReport[_0x681b('0x14')][_0x681b('0x11')]=function(_0x2b0414){try{if(this[_0x681b('0x15')](_0x2b0414)){logger[_0x681b('0x2a')](_0x681b('0x36'),_0x2b0414[_0x681b('0x1a')],util['inspect'](_0x2b0414,{'showHidden':![],'depth':null}));this[_0x681b('0x37')](_0x2b0414['uniqueid'],_[_0x681b('0x38')](_0x2b0414[_0x681b('0x39')])[0x0],_0x2b0414[_0x681b('0x3a')]);if(this[_0x681b('0x15')](_0x2b0414['variable'])&&this[_0x681b('0x15')](_0x2b0414['variable']['outboundrouteid'])){if(_[_0x681b('0x16')](this['channels'][_0x2b0414[_0x681b('0x1a')]])){this[_0x681b('0x1c')][_0x2b0414[_0x681b('0x1a')]]={'routeId':_0x2b0414[_0x681b('0x3a')],'variables':this[_0x681b('0xb')][_0x2b0414[_0x681b('0x1a')]],'motionChannel':_0x681b('0x3b')};logger[_0x681b('0x26')](_0x681b('0x3c'),_0x2b0414['uniqueid'],_0x2b0414[_0x681b('0x39')]['outboundrouteid'],_[_0x681b('0x38')](_0x2b0414[_0x681b('0x39')])[0x0],_0x2b0414['value']);logger['debug'](_0x681b('0x3d'),_0x2b0414[_0x681b('0x1a')],util[_0x681b('0x19')](this['channels'][_0x2b0414['uniqueid']],{'showHidden':![],'depth':null}));this['emit'](_0x681b('0x29'),_0x681b('0x3e'),this[_0x681b('0x1c')][_0x2b0414[_0x681b('0x1a')]]);}}}}catch(_0x346209){logger[_0x681b('0x2b')](_0x681b('0x3f'),_0x2b0414[_0x681b('0x1a')],util['inspect'](_0x346209,{'showHidden':![],'depth':null}));}};VoiceDialReport['prototype'][_0x681b('0x37')]=function(_0x2e3133,_0x31ddd0,_0x4844e2){var _0x827a2b=[_0x681b('0x40'),'rtpaudioqosbridged',_0x681b('0x41'),_0x681b('0x42'),_0x681b('0x43'),'rtpaudioqoslossbridged',_0x681b('0x44'),_0x681b('0x45'),_0x681b('0x46'),_0x681b('0x47'),_0x681b('0x48'),_0x681b('0x49'),_0x681b('0x4a'),_0x681b('0x4b'),_0x681b('0x4c'),'memberlastcall',_0x681b('0x27'),_0x681b('0x4d'),_0x681b('0x4e')];if(!_[_0x681b('0x4f')](_0x31ddd0)&&!_['includes'](_0x827a2b,_0x31ddd0)){if(_[_0x681b('0x16')](this['variables'][_0x2e3133])){this[_0x681b('0xb')][_0x2e3133]={};}this['variables'][_0x2e3133][_0x31ddd0]=_0x4844e2;}};VoiceDialReport[_0x681b('0x14')][_0x681b('0x13')]=function(_0x10ad24){try{if(this[_0x681b('0x15')](_0x10ad24)){logger[_0x681b('0x2a')]('[%s][voiceDialReport][hangup]\x20event:',_0x10ad24['uniqueid'],util[_0x681b('0x19')](_0x10ad24,{'showHidden':![],'depth':null}));if(this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]]){logger['info'](_0x681b('0x50'),_0x10ad24[_0x681b('0x1a')],_0x10ad24[_0x681b('0x51')]);this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]]['endtime']=moment()[_0x681b('0x1d')](_0x681b('0x1e'));this[_0x681b('0x1c')][_0x10ad24['uniqueid']][_0x681b('0x52')]=moment(this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x53')])[_0x681b('0x54')](this['channels'][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x55')],_0x681b('0x56'));this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x31')]=_0x681b('0x57');this[_0x681b('0x9')][_0x681b('0x58')]+=0x1;if(this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x59')]){this['outbound']['answered']+=0x1;this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x5a')]=moment(this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]]['answertime'])[_0x681b('0x54')](this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x55')],'seconds');this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x5b')]=moment(this['channels'][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x53')])[_0x681b('0x54')](this['channels'][_0x10ad24['uniqueid']][_0x681b('0x59')],_0x681b('0x56'));this[_0x681b('0x9')][_0x681b('0x5c')]+=this[_0x681b('0x1c')][_0x10ad24['uniqueid']]['holdtime']||0x0;this[_0x681b('0x9')]['sumBillable']+=this['channels'][_0x10ad24[_0x681b('0x1a')]]['billableseconds'];}this[_0x681b('0x9')][_0x681b('0x5d')]+=this['channels'][_0x10ad24[_0x681b('0x1a')]][_0x681b('0x52')];logger['debug'](_0x681b('0x5e'),_0x10ad24[_0x681b('0x1a')],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]],{'showHidden':![],'depth':null}));ami['emit'](_0x681b('0x5f'),this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]]);logger[_0x681b('0x2a')]('[%s][voiceDialReport][hangup]\x20sending\x20dialend\x20event:',_0x10ad24['uniqueid'],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]],{'showHidden':![],'depth':null}));ami[_0x681b('0x17')](_0x681b('0x60'),this[_0x681b('0x1c')][_0x10ad24['uniqueid']]);logger[_0x681b('0x2a')](_0x681b('0x61'),_0x10ad24[_0x681b('0x1a')],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]],{'showHidden':![],'depth':null}));this['emit'](_0x681b('0x29'),_0x681b('0x62'),this[_0x681b('0x9')]);logger[_0x681b('0x26')](_0x681b('0x63'),JSON[_0x681b('0x64')](this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x1a')]]));logger[_0x681b('0x2a')](_0x681b('0x65'),_0x10ad24[_0x681b('0x1a')],util[_0x681b('0x19')](this[_0x681b('0x1c')][_0x10ad24['uniqueid']],{'showHidden':![],'depth':null}));this[_0x681b('0x17')](_0x681b('0x29'),_0x681b('0x66'),this['channels'][_0x10ad24[_0x681b('0x1a')]]);this[_0x681b('0x67')](_0x10ad24[_0x681b('0x1a')]);}else if(this[_0x681b('0x1c')][_0x10ad24['linkedid']]){this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x68')]][_0x681b('0x1a')]=this[_0x681b('0x1c')][_0x10ad24[_0x681b('0x68')]][_0x681b('0x69')];}if(this['variables'][_0x10ad24[_0x681b('0x1a')]]){delete this[_0x681b('0xb')][_0x10ad24[_0x681b('0x1a')]];}}}catch(_0x4cbc6b){logger[_0x681b('0x2b')](_0x681b('0x6a'),_0x10ad24[_0x681b('0x1a')],util[_0x681b('0x19')](_0x4cbc6b,{'showHidden':![],'depth':null}));}};VoiceDialReport['prototype'][_0x681b('0x6b')]=function(_0x3ae2b2){try{if(this[_0x681b('0x15')](_0x3ae2b2)){logger[_0x681b('0x2a')](_0x681b('0x6c'),_0x3ae2b2[_0x681b('0x1a')],util[_0x681b('0x19')](_0x3ae2b2,{'showHidden':![],'depth':null}));if(_0x3ae2b2[_0x681b('0x6d')][_0x681b('0x6e')]()===_0x681b('0x6f')){_0x3ae2b2[_0x681b('0x70')]=(_0x3ae2b2['appdata']||_0x3ae2b2[_0x681b('0x70')])['toLowerCase']();if(_[_0x681b('0x71')](_0x3ae2b2['applicationdata'],'cdr(')){if(_['isUndefined'](this[_0x681b('0xa')][_0x3ae2b2[_0x681b('0x1a')]]))this[_0x681b('0xa')][_0x3ae2b2[_0x681b('0x1a')]]={};logger[_0x681b('0x26')]('[%s][voiceDialReport][newexten]\x20application:%s\x20appdata:%s',_0x3ae2b2['uniqueid'],_0x3ae2b2[_0x681b('0x6d')],_0x3ae2b2[_0x681b('0x70')]);var _0x387533=_0x3ae2b2[_0x681b('0x70')]['split']('=');var _0x2f3382=_0x387533[0x0][_0x681b('0x72')](/cdr\((.*)\)/);if(_0x2f3382)this[_0x681b('0xa')][_0x3ae2b2[_0x681b('0x1a')]][_0x2f3382[0x1]]=_0x387533[0x1];}}}}catch(_0x565b12){logger[_0x681b('0x2b')]('[%s][voiceDialReport][newexten]',_0x3ae2b2['uniqueid'],util['inspect'](_0x565b12,{'showHidden':![],'depth':null}));}};VoiceDialReport[_0x681b('0x14')][_0x681b('0x67')]=function(_0x31214a,_0x2d05ad=0x2710){if(this[_0x681b('0x1c')][_0x31214a]){setTimeout(()=>{delete this[_0x681b('0x1c')][_0x31214a];},_0x2d05ad);}};module[_0x681b('0x73')]=VoiceDialReport;