0ee149216df7b4ea65f406f272b680e4c9603f5c
[motion2.git] / server / services / ami / acw / index.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 _0xb4c7=['custom:agentConnectPreview','syncAgentConnectPreview','milliseconds','seconds','prototype','updateAcw','timer','uniqueid','then','info','[%s][UPDATE][ACWTIME:%s]','catch','error','finally','startAcw','interface','queue','ACW','mandatoryDisposition','mandatoryDispositionPause','name','resolve','voicePause','[%s][INPAUSE][AGENT:%s][QUEUE:%s]','[%s][START][AGENT:%s][QUEUE:%s]','attended','destlinkedid','savePause','acwTimeout','format','YYYY-MM-DD\x20HH:mm:ss','[ACW][START]\x20%s','message','stopAcw','saveUnpause','update','[%s][UPDATE][ERRORS:%s]','[ACW][STOP]\x20Interface\x20%s\x20doesn\x27t\x20have\x20timer','[ACW][TIMERS]','[ACW][STOP]\x20%s','[ACW][syncAgentConnect]\x20%s','[ACW][syncAgentConnectPreview]\x20%s','isUndefined','linkedid','[%s][HANGUP:LINKEDID]','[ACW][syncHangupRequest]\x20%s','[%s][HANGUPATTENDEDTRANSFER:UNIQUEID]','[ACW][syncHangup]\x20%s','isNil','transfereeuniqueid','secondtransfereruniqueid','transfertargetuniqueid','origtransfereruniqueid','[ACW][syncAttendedTransfer]\x20%s','transfereruniqueid','stringify','[%s][BLINDTRANSFER:LINKEDID]','transfererlinkedid','[ACW][syncBlindTransfer]\x20%s','autopause','all','[%s][AUTOPAUSE][AGENT:%s][QUEUE:%]','Auto-Pause','[ACW][syncAgentRingNoAnswer]\x20%s','exports','lodash','moment','../ami','../../../config/logger','acw','../rpc/voiceAgentReport','voiceQueues','agents','timers','channels','syncAgentConnect','bind','syncHangupRequest','hangup','syncHangup','agentringnoanswer','syncAgentRingNoAnswer','attendedtransfer','syncAttendedTransfer','syncBlindTransfer'];(function(_0x18546c,_0x4daf94){var _0x46ef43=function(_0x559377){while(--_0x559377){_0x18546c['push'](_0x18546c['shift']());}};_0x46ef43(++_0x4daf94);}(_0xb4c7,0x1ea));var _0x7b4c=function(_0x2f208f,_0x54978c){_0x2f208f=_0x2f208f-0x0;var _0x4d3a12=_0xb4c7[_0x2f208f];return _0x4d3a12;};'use strict';var _=require(_0x7b4c('0x0'));var moment=require(_0x7b4c('0x1'));var ami=require(_0x7b4c('0x2'));var logger=require(_0x7b4c('0x3'))(_0x7b4c('0x4'));var RpcUser=require('../rpc/user');var RpcVoiceAgentReport=require(_0x7b4c('0x5'));function Acw(_0x149d55){this[_0x7b4c('0x6')]=_0x149d55[_0x7b4c('0x6')];this[_0x7b4c('0x7')]=_0x149d55['agents'];this[_0x7b4c('0x8')]=_0x149d55[_0x7b4c('0x8')];this[_0x7b4c('0x9')]={};ami['on']('agentconnect',this[_0x7b4c('0xa')][_0x7b4c('0xb')](this));ami['on']('hanguprequest',this[_0x7b4c('0xc')][_0x7b4c('0xb')](this));ami['on'](_0x7b4c('0xd'),this[_0x7b4c('0xe')][_0x7b4c('0xb')](this));ami['on'](_0x7b4c('0xf'),this[_0x7b4c('0x10')][_0x7b4c('0xb')](this));ami['on'](_0x7b4c('0x11'),this[_0x7b4c('0x12')]['bind'](this));ami['on']('blindtransfer',this[_0x7b4c('0x13')][_0x7b4c('0xb')](this));ami['on'](_0x7b4c('0x14'),this[_0x7b4c('0x15')][_0x7b4c('0xb')](this));}function getDiff(_0x341d36){var _0x3ea0da=moment()[_0x7b4c('0x16')](0x0);var _0x25b90b=moment(_0x341d36)[_0x7b4c('0x16')](0x0);return _0x3ea0da['diff'](_0x25b90b,_0x7b4c('0x17'));}Acw[_0x7b4c('0x18')][_0x7b4c('0x19')]=function(_0x2a0e2f,_0xa0a812){var _0x2471b3=this;var _0x1642e1=getDiff(_0x2471b3[_0x7b4c('0x8')][_0x2a0e2f]['lastPauseAt']);clearTimeout(_0x2471b3[_0x7b4c('0x8')][_0x2a0e2f][_0x7b4c('0x1a')]);_0x2471b3[_0x7b4c('0x8')][_0x2a0e2f][_0x7b4c('0x1a')]=![];return RpcVoiceAgentReport['update']({'acwtime':_0x1642e1},_0x2471b3[_0x7b4c('0x8')][_0x2a0e2f][_0x7b4c('0x1b')],_0x2a0e2f)[_0x7b4c('0x1c')](function(_0x380b79){logger[_0x7b4c('0x1d')](_0x7b4c('0x1e'),_0xa0a812,_0x1642e1,_0x380b79);})[_0x7b4c('0x1f')](function(_0x445281){logger[_0x7b4c('0x20')]('[%s][UPDATE][ERRORS:%s]',_0xa0a812,_0x445281);})[_0x7b4c('0x21')](function(){delete _0x2471b3[_0x7b4c('0x8')][_0x2a0e2f];});};Acw[_0x7b4c('0x18')][_0x7b4c('0x22')]=function(_0x148a79){try{var _0x168ac9=this;var _0x176704=this[_0x7b4c('0x9')][_0x148a79][_0x7b4c('0x23')];var _0x2c267c=this['channels'][_0x148a79][_0x7b4c('0x24')];var _0x4350c9=this[_0x7b4c('0x6')][_0x2c267c];var _0x3157b8=![];var _0x3124e7=_0x7b4c('0x25');if(_0x4350c9[_0x7b4c('0x26')]&&_0x4350c9[_0x7b4c('0x27')][_0x7b4c('0x28')]){_0x3157b8=!![];_0x3124e7=_0x4350c9[_0x7b4c('0x27')][_0x7b4c('0x28')];}if(!_0x168ac9[_0x7b4c('0x7')][_0x176704]){return Promise[_0x7b4c('0x29')]();}return RpcUser['getAgent'](_0x168ac9[_0x7b4c('0x7')][_0x176704]['id'])[_0x7b4c('0x1c')](function(_0x1fe0f5){if(!_0x1fe0f5||_0x1fe0f5[_0x7b4c('0x2a')]){logger[_0x7b4c('0x1d')](_0x7b4c('0x2b'),_0x148a79,_0x176704,_0x2c267c);return;}logger[_0x7b4c('0x1d')](_0x7b4c('0x2c'),_0x148a79,_0x176704,_0x2c267c);var _0x3e4a46=_0x168ac9[_0x7b4c('0x9')][_0x148a79][_0x7b4c('0x2d')]&&_0x168ac9[_0x7b4c('0x9')][_0x148a79][_0x7b4c('0x2e')]?_0x168ac9[_0x7b4c('0x9')][_0x148a79][_0x7b4c('0x2e')]:_0x148a79;return RpcUser[_0x7b4c('0x2f')](_0x168ac9[_0x7b4c('0x7')][_0x176704],_0x3124e7,_0x3e4a46);})[_0x7b4c('0x1c')](function(_0x9d3d31){if(_0x3157b8||!_0x9d3d31){return;}_0x168ac9['timers'][_0x176704]={};logger['info']('[%s][TIMER:%s][AGENT:%s][QUEUE:%s]',_0x148a79,_0x168ac9[_0x7b4c('0x6')][_0x2c267c][_0x7b4c('0x30')],_0x176704,_0x2c267c);_0x168ac9[_0x7b4c('0x8')][_0x176704][_0x7b4c('0x1a')]=setTimeout(_0x168ac9['stopAcw'][_0x7b4c('0xb')](_0x168ac9,_0x176704),_0x168ac9[_0x7b4c('0x6')][_0x2c267c]['acwTimeout']*0x3e8);_0x168ac9[_0x7b4c('0x8')][_0x176704]['lastPauseAt']=moment()[_0x7b4c('0x31')](_0x7b4c('0x32'));_0x168ac9[_0x7b4c('0x8')][_0x176704]['uniqueid']=_0x148a79;_0x168ac9[_0x7b4c('0x8')][_0x176704][_0x7b4c('0x24')]=_0x2c267c;_0x168ac9['timers'][_0x176704][_0x7b4c('0x19')]=_0x168ac9[_0x7b4c('0x19')][_0x7b4c('0xb')](_0x168ac9,_0x176704,_0x148a79);})[_0x7b4c('0x1f')](function(){logger['error']('[%s][TIMER:%s][AGENT:%s][QUEUE:%s]',_0x148a79,_0x168ac9[_0x7b4c('0x6')][_0x2c267c][_0x7b4c('0x30')],_0x176704,_0x2c267c);})['finally'](function(){delete _0x168ac9[_0x7b4c('0x9')][_0x148a79];});}catch(_0x4a451f){logger['error'](_0x7b4c('0x33'),_0x4a451f[_0x7b4c('0x34')]);}};Acw[_0x7b4c('0x18')][_0x7b4c('0x35')]=function(_0x5ba56a){try{var _0x81ddf4=this;if(_0x81ddf4[_0x7b4c('0x8')][_0x5ba56a]){_0x81ddf4[_0x7b4c('0x8')][_0x5ba56a][_0x7b4c('0x1a')]=![];var _0x346e0c=_0x81ddf4[_0x7b4c('0x8')][_0x5ba56a][_0x7b4c('0x1b')];var _0x4c23cc=_0x81ddf4['timers'][_0x5ba56a][_0x7b4c('0x24')];var _0x37f136=_0x81ddf4[_0x7b4c('0x6')][_0x4c23cc][_0x7b4c('0x30')];logger[_0x7b4c('0x1d')]('[%s][TIMER][STOP][AGENT:%s]',_0x346e0c,_0x5ba56a);RpcUser[_0x7b4c('0x36')](this[_0x7b4c('0x7')][_0x5ba56a]);RpcVoiceAgentReport[_0x7b4c('0x37')]({'acwtime':_0x37f136,'agentacw':!![]},_0x346e0c,_0x5ba56a)[_0x7b4c('0x1c')](function(_0x22f71b){logger[_0x7b4c('0x1d')](_0x7b4c('0x1e'),_0x346e0c,_0x37f136,_0x22f71b);})[_0x7b4c('0x1f')](function(_0x3d8126){logger['error'](_0x7b4c('0x38'),_0x346e0c,_0x3d8126);});}else{logger['error'](_0x7b4c('0x39'),_0x5ba56a);if(_0x81ddf4['timers']){logger[_0x7b4c('0x1d')](_0x7b4c('0x3a'),JSON['stringify'](_0x81ddf4['timers']));}}}catch(_0x354578){logger[_0x7b4c('0x20')](_0x7b4c('0x3b'),_0x354578[_0x7b4c('0x34')]);}};Acw['prototype'][_0x7b4c('0xa')]=function(_0x2ed731){try{if(this[_0x7b4c('0x6')][_0x2ed731['queue']]&&(this[_0x7b4c('0x6')][_0x2ed731['queue']]['acw']||this[_0x7b4c('0x6')][_0x2ed731['queue']][_0x7b4c('0x26')])){if(this[_0x7b4c('0x7')][_0x2ed731[_0x7b4c('0x23')]]){this[_0x7b4c('0x9')][_0x2ed731[_0x7b4c('0x1b')]]={'queue':_0x2ed731[_0x7b4c('0x24')],'interface':_0x2ed731[_0x7b4c('0x23')],'destlinkedid':_0x2ed731[_0x7b4c('0x2e')]};}}}catch(_0x3f3d46){logger['error'](_0x7b4c('0x3c'),_0x3f3d46['message']);}};Acw['prototype'][_0x7b4c('0x15')]=function(_0x1f1653){try{if(this['voiceQueues'][_0x1f1653['queue']]&&(this[_0x7b4c('0x6')][_0x1f1653[_0x7b4c('0x24')]][_0x7b4c('0x4')]||this[_0x7b4c('0x6')][_0x1f1653[_0x7b4c('0x24')]][_0x7b4c('0x26')])){if(this[_0x7b4c('0x7')][_0x1f1653[_0x7b4c('0x23')]]){this['channels'][_0x1f1653[_0x7b4c('0x1b')]]={'queue':_0x1f1653[_0x7b4c('0x24')],'interface':_0x1f1653[_0x7b4c('0x23')]};}}}catch(_0x387efd){logger[_0x7b4c('0x20')](_0x7b4c('0x3d'),_0x387efd[_0x7b4c('0x34')]);}};Acw[_0x7b4c('0x18')][_0x7b4c('0xc')]=function(_0x5a1262){try{if(this['channels'][_0x5a1262[_0x7b4c('0x1b')]]&&_[_0x7b4c('0x3e')](this[_0x7b4c('0x9')][_0x5a1262['uniqueid']][_0x7b4c('0x2d')])){logger[_0x7b4c('0x1d')]('[%s][HANGUP:UNIQUEID]',_0x5a1262[_0x7b4c('0x1b')]);this['startAcw'](_0x5a1262[_0x7b4c('0x1b')]);}else if(this['channels'][_0x5a1262[_0x7b4c('0x3f')]]&&_['isUndefined'](this[_0x7b4c('0x9')][_0x5a1262[_0x7b4c('0x3f')]]['attended'])){logger['info'](_0x7b4c('0x40'),_0x5a1262[_0x7b4c('0x3f')]);this[_0x7b4c('0x22')](_0x5a1262['linkedid']);}}catch(_0x56233b){logger[_0x7b4c('0x20')](_0x7b4c('0x41'),_0x56233b['message']);}};Acw['prototype'][_0x7b4c('0xe')]=function(_0x2a1442){try{if(this[_0x7b4c('0x9')][_0x2a1442['uniqueid']]&&this[_0x7b4c('0x9')][_0x2a1442[_0x7b4c('0x1b')]]['attended']){logger['info'](_0x7b4c('0x42'),_0x2a1442['uniqueid']);this['startAcw'](_0x2a1442[_0x7b4c('0x1b')]);}else if(this['channels'][_0x2a1442['linkedid']]&&this[_0x7b4c('0x9')][_0x2a1442[_0x7b4c('0x3f')]][_0x7b4c('0x2d')]){logger[_0x7b4c('0x1d')]('[%s][HANGUPATTENDEDTRANSFER:LINKEDID]',_0x2a1442['linkedid']);this[_0x7b4c('0x22')](_0x2a1442[_0x7b4c('0x3f')]);}}catch(_0x30a7e3){logger['error'](_0x7b4c('0x43'),_0x30a7e3[_0x7b4c('0x34')]);}};Acw[_0x7b4c('0x18')]['syncAttendedTransfer']=function(_0x451675){try{if(!_[_0x7b4c('0x44')](this['channels'][_0x451675[_0x7b4c('0x45')]])){this[_0x7b4c('0x22')](_0x451675[_0x7b4c('0x45')]);if(this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x46')]]){this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x46')]]['attended']=!![];this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x47')]]=this['channels'][_0x451675[_0x7b4c('0x46')]];delete this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x46')]];}}else if(!_[_0x7b4c('0x44')](this['channels'][_0x451675[_0x7b4c('0x47')]])){this['startAcw'](_0x451675['transfertargetuniqueid']);if(this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x48')]]){this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x48')]][_0x7b4c('0x2d')]=!![];this['channels'][_0x451675['transfereeuniqueid']]=this[_0x7b4c('0x9')][_0x451675[_0x7b4c('0x48')]];delete this[_0x7b4c('0x9')][_0x451675['origtransfereruniqueid']];}}}catch(_0xade53c){logger[_0x7b4c('0x20')](_0x7b4c('0x49'),_0xade53c['message']);}};Acw[_0x7b4c('0x18')][_0x7b4c('0x13')]=function(_0xf6f913){try{if(this['channels'][_0xf6f913[_0x7b4c('0x4a')]]){logger[_0x7b4c('0x1d')]('[%s][BLINDTRANSFER:UNIQUEID]',JSON[_0x7b4c('0x4b')](_0xf6f913['transfereruniqueid']));this[_0x7b4c('0x22')](_0xf6f913[_0x7b4c('0x4a')]);}else if(this[_0x7b4c('0x9')][_0xf6f913['transfererlinkedid']]){logger['info'](_0x7b4c('0x4c'),JSON[_0x7b4c('0x4b')](_0xf6f913[_0x7b4c('0x4d')]));this['startAcw'](_0xf6f913[_0x7b4c('0x4d')]);}}catch(_0x49b068){logger[_0x7b4c('0x20')](_0x7b4c('0x4e'),_0x49b068[_0x7b4c('0x34')]);}};Acw[_0x7b4c('0x18')][_0x7b4c('0x10')]=function(_0x57c52b){try{if(this[_0x7b4c('0x6')][_0x57c52b[_0x7b4c('0x24')]]&&this['voiceQueues'][_0x57c52b[_0x7b4c('0x24')]][_0x7b4c('0x4f')]===_0x7b4c('0x50')){if(this[_0x7b4c('0x7')][_0x57c52b[_0x7b4c('0x23')]]){logger[_0x7b4c('0x1d')](_0x7b4c('0x51'),_0x57c52b[_0x7b4c('0x1b')],_0x57c52b['interface'],_0x57c52b[_0x7b4c('0x24')]);return RpcUser[_0x7b4c('0x2f')](this[_0x7b4c('0x7')][_0x57c52b[_0x7b4c('0x23')]],_0x7b4c('0x52'),_0x57c52b[_0x7b4c('0x1b')]);}}}catch(_0x2f3ba9){logger[_0x7b4c('0x20')](_0x7b4c('0x53'),_0x2f3ba9[_0x7b4c('0x34')]);}};module[_0x7b4c('0x54')]=Acw;