583df73bdde3778e30edfe1a1665750f4e9468f7
[motion2.git] / server / services / xdr / calls.controller.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 _0x85c2=['musiconholdstart','[%s][calls][musiconholdstart]','musiconholdstartAt','[%s][calls][musiconholdstart]\x20musiconholdstartAt:%s','musiconholdstop','[%s][calls][musiconholdstop]','mohtime','[%s][calls][musiconholdstop]\x20mohtime:%s','varset','variable','[%s][calls][varset]\x20variable:xmd-originatecalleridnum\x20value:%s','value','source','xmd-phone','[%s][calls][varset]\x20variable:xmd-phone\x20value:%s','destination','xmd-callerid-preview','xmd-callerid','[%s][calls][varset]\x20variable:%s\x20value:%s','xmd-queue','xmcs-queue','lastapplication','xmd-contactid','[%s][calls][varset]\x20variable:xmd-contactid\x20value:%s','[%s][calls][varset]\x20variable:xmd-cdrtype\x20value:%s','[%s][calls][varset]\x20variable:outboundrouteid\x20value:%s','routeid','sipcallid','[%s][calls][varset]\x20variable:sipcallid\x20value:%s','[%s][calls][varset]','[%s][calls][newexten]\x20application:%s\x20appdata:%s','application','appdata','toLowerCase','set','startsWith','cdr','substring','lastIndexOf','includes','hangup','noop','execif','gotoif','[%s][calls][newexten]','lodash','util','./utils','FAILED','NO\x20ANSWER','ANSWERED','BUSY','info','format','[%s][calls][writeDB]\x20channel:%s\x20queue:%s\x20agent:%s\x20interface:%s\x20calleridnum:%s\x20connectedlinenum:%s\x20lastevent:%s','uniqueid','channel','queue','membername','interface','connectedlinenum','lastevent','accountcode','UserId','request','debug','linkedid','OutgoingSpoolFailed','[%s][calls][newchannel]\x20context:%s\x20exten:%s','exten','calleridnum','context','now','DOCUMENTATION','error','[%s][calls][newchannel]','newstate','[%s][calls][newstate]','channelstate','[%s][calls][newstate]\x20context:%s\x20channel:%s','isNil','systemanswertime','transfer','answertime','toNumber','[%s][calls][hangup]','callerid','calleridname','cause-txt','endtime','diffTime','billableseconds','attendedtransfer','localtwolinkedid','origtransfereruniqueid','transfereechannel','secondtransfererchannel','destinationchannel','userfield','transfertargetchannel','[%s][calls][attendedtransfer]\x20localtwolinkedid:%s\x20destinationchannel:%s','origtransfererchannel','_attended','[%s][calls][attendedtransfer]'];(function(_0x516ffb,_0x5b5b42){var _0x319667=function(_0x250098){while(--_0x250098){_0x516ffb['push'](_0x516ffb['shift']());}};_0x319667(++_0x5b5b42);}(_0x85c2,0x95));var _0x285c=function(_0x266618,_0x18f637){_0x266618=_0x266618-0x0;var _0x54dbf5=_0x85c2[_0x266618];return _0x54dbf5;};'use strict';var _=require(_0x285c('0x0'));var util=require(_0x285c('0x1'));var utils=require(_0x285c('0x2'));var dispositions=['NO\x20ANSWER',_0x285c('0x3'),'FAILED','NO\x20ANSWER',_0x285c('0x4'),'NO\x20ANSWER',_0x285c('0x5'),_0x285c('0x6'),_0x285c('0x3'),_0x285c('0x3'),_0x285c('0x3')];var channels={};function writeDB(_0x387026){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x9'),_0x387026[_0x285c('0xa')],_0x387026[_0x285c('0xb')],_0x387026[_0x285c('0xc')],_0x387026[_0x285c('0xd')],_0x387026[_0x285c('0xe')],_0x387026['calleridnum'],_0x387026[_0x285c('0xf')],_0x387026[_0x285c('0x10')]));if(_0x387026['accountcode']){utils['getUserByInternal'](_0x387026[_0x285c('0x11')])['then'](function(_0x146fb4){if(_0x146fb4){_0x387026[_0x285c('0x12')]=_0x146fb4['id'];}utils['request']('CreateVoiceCallReport',_0x387026);});}else{utils[_0x285c('0x13')]('CreateVoiceCallReport',_0x387026);}if(channels[_0x387026[_0x285c('0xa')]]){delete channels[_0x387026[_0x285c('0xa')]];}}exports['newchannel']=function(_0x1780f2){try{utils[_0x285c('0x14')](util['format']('[%s][calls][newchannel]',_0x1780f2[_0x285c('0xa')]),_0x1780f2);if(_0x1780f2[_0x285c('0xa')]==_0x1780f2[_0x285c('0x15')]&&_0x1780f2['channel']!=_0x285c('0x16')){utils['info'](util[_0x285c('0x8')](_0x285c('0x17'),_0x1780f2[_0x285c('0xa')],_0x1780f2['context'],_0x1780f2[_0x285c('0x18')]));channels[_0x1780f2[_0x285c('0xa')]]={'uniqueid':_0x1780f2['uniqueid'],'accountcode':_0x1780f2[_0x285c('0x11')],'source':_0x1780f2[_0x285c('0x19')],'destination':_0x1780f2[_0x285c('0x18')],'destinationcontext':_0x1780f2[_0x285c('0x1a')],'channel':_0x1780f2[_0x285c('0xb')],'starttime':utils[_0x285c('0x1b')](),'answertime':null,'systemanswertime':null,'mohtime':0x0,'disposition':_0x285c('0x4'),'amaflags':_0x285c('0x1c')};}}catch(_0x190147){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x1e'),_0x1780f2['uniqueid']),_0x190147);}};exports[_0x285c('0x1f')]=function(_0x3e1db4){try{utils[_0x285c('0x14')](util['format'](_0x285c('0x20'),_0x3e1db4[_0x285c('0xa')]),_0x3e1db4);if(!_['isNil'](channels[_0x3e1db4[_0x285c('0xa')]])&&_0x3e1db4[_0x285c('0x21')]==0x6){utils['info'](util[_0x285c('0x8')](_0x285c('0x22'),_0x3e1db4[_0x285c('0xa')],_0x3e1db4[_0x285c('0x1a')],_0x3e1db4[_0x285c('0xb')]));if(_[_0x285c('0x23')](channels[_0x3e1db4[_0x285c('0xa')]][_0x285c('0x24')])){channels[_0x3e1db4[_0x285c('0xa')]][_0x285c('0x24')]=utils[_0x285c('0x1b')]();}}else if(!_[_0x285c('0x23')](channels[_0x3e1db4[_0x285c('0x15')]])&&_0x3e1db4[_0x285c('0x21')]==0x6&&_0x3e1db4['context']!=_0x285c('0x25')){utils[_0x285c('0x7')](util[_0x285c('0x8')]('[%s][calls][newstate]\x20linkedid:%s\x20context:%s\x20channel:%s',_0x3e1db4[_0x285c('0xa')],_0x3e1db4[_0x285c('0x15')],_0x3e1db4[_0x285c('0x1a')],_0x3e1db4[_0x285c('0xb')]));channels[_0x3e1db4[_0x285c('0x15')]][_0x285c('0x11')]=_0x3e1db4['accountcode'];channels[_0x3e1db4[_0x285c('0x15')]]['destinationchannel']=_0x3e1db4['channel'];if(_[_0x285c('0x23')](channels[_0x3e1db4[_0x285c('0x15')]][_0x285c('0x26')])){channels[_0x3e1db4[_0x285c('0x15')]][_0x285c('0x26')]=utils[_0x285c('0x1b')]();}channels[_0x3e1db4['linkedid']]['disposition']=dispositions[_[_0x285c('0x27')](_0x3e1db4['channelstate'])]||_0x285c('0x5');}}catch(_0x1fac18){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x20'),_0x3e1db4[_0x285c('0xa')]),_0x1fac18);}};exports['hangup']=function(_0x59a546){try{if(!_[_0x285c('0x23')](channels[_0x59a546['uniqueid']])){utils[_0x285c('0x14')](util[_0x285c('0x8')](_0x285c('0x28'),_0x59a546[_0x285c('0xa')]),_0x59a546);if(_[_0x285c('0x23')](channels[_0x59a546[_0x285c('0xa')]]['callerid'])){channels[_0x59a546[_0x285c('0xa')]][_0x285c('0x29')]=_0x59a546[_0x285c('0x2a')]+'\x20<'+_0x59a546['calleridnum']+'>';}utils[_0x285c('0x7')](util[_0x285c('0x8')]('[%s][calls][hangup]\x20callerid:%s\x20reason:%s',_0x59a546[_0x285c('0xa')],channels[_0x59a546[_0x285c('0xa')]]['callerid'],_0x59a546[_0x285c('0x2b')]));channels[_0x59a546['uniqueid']][_0x285c('0x2c')]=utils[_0x285c('0x1b')]();channels[_0x59a546['uniqueid']]['duration']=utils[_0x285c('0x2d')](channels[_0x59a546[_0x285c('0xa')]][_0x285c('0x2c')],channels[_0x59a546[_0x285c('0xa')]]['starttime']);channels[_0x59a546[_0x285c('0xa')]][_0x285c('0x2e')]=channels[_0x59a546['uniqueid']]['answertime']?utils[_0x285c('0x2d')](channels[_0x59a546[_0x285c('0xa')]][_0x285c('0x2c')],channels[_0x59a546[_0x285c('0xa')]]['answertime']):0x0;writeDB(channels[_0x59a546[_0x285c('0xa')]]);}}catch(_0x4b859d){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x28'),_0x59a546[_0x285c('0xa')]),_0x4b859d);}};exports[_0x285c('0x2f')]=function(_0x249613){try{if(!_[_0x285c('0x23')](channels[_0x249613[_0x285c('0x30')]])){utils['debug'](util['format']('[%s][calls][attendedtransfer]',_0x249613[_0x285c('0x31')]),_0x249613);if(channels[_0x249613[_0x285c('0x30')]][_0x285c('0xb')]==_0x249613[_0x285c('0x32')]){utils[_0x285c('0x7')](util['format']('[%s][calls][attendedtransfer]\x20localtwolinkedid:%s\x20destinationchannel:%s',_0x249613[_0x285c('0x31')],_0x249613[_0x285c('0x30')],_0x249613[_0x285c('0x33')]));channels[_0x249613[_0x285c('0x30')]][_0x285c('0x34')]=_0x249613[_0x285c('0x33')];channels[_0x249613['localtwolinkedid']][_0x285c('0x35')]='_attended';}else if(channels[_0x249613[_0x285c('0x30')]][_0x285c('0xb')]=_0x249613[_0x285c('0x36')]){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x37'),_0x249613[_0x285c('0x31')],_0x249613[_0x285c('0x30')],_0x249613[_0x285c('0x38')]));channels[_0x249613['localtwolinkedid']][_0x285c('0x34')]=_0x249613[_0x285c('0x38')];channels[_0x249613['localtwolinkedid']][_0x285c('0x35')]=_0x285c('0x39');}}}catch(_0x300bac){utils['error'](util[_0x285c('0x8')](_0x285c('0x3a'),_0x249613['origtransfereruniqueid']),_0x300bac);}};exports[_0x285c('0x3b')]=function(_0x2c19e4){try{if(!_[_0x285c('0x23')](channels[_0x2c19e4[_0x285c('0xa')]])){utils['debug'](util['format'](_0x285c('0x3c'),_0x2c19e4[_0x285c('0xa')]),_0x2c19e4);channels[_0x2c19e4['uniqueid']][_0x285c('0x3d')]=utils[_0x285c('0x1b')]();utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x3e'),_0x2c19e4['uniqueid'],channels[_0x2c19e4[_0x285c('0xa')]][_0x285c('0x3d')]));}}catch(_0x4ef1ca){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x3c'),_0x2c19e4[_0x285c('0xa')]),_0x4ef1ca);}};exports[_0x285c('0x3f')]=function(_0x369734){try{if(!_[_0x285c('0x23')](channels[_0x369734['uniqueid']])){utils[_0x285c('0x14')](util[_0x285c('0x8')](_0x285c('0x40'),_0x369734[_0x285c('0xa')]),_0x369734);if(!_[_0x285c('0x23')](channels[_0x369734[_0x285c('0xa')]]['musiconholdstartAt'])){channels[_0x369734[_0x285c('0xa')]][_0x285c('0x41')]+=utils[_0x285c('0x2d')](utils[_0x285c('0x1b')](),channels[_0x369734[_0x285c('0xa')]][_0x285c('0x3d')]);utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x42'),_0x369734['uniqueid'],channels[_0x369734[_0x285c('0xa')]][_0x285c('0x41')]));delete channels[_0x369734[_0x285c('0xa')]][_0x285c('0x3d')];}}}catch(_0x2edcd5){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x40'),_0x369734[_0x285c('0xa')]),_0x2edcd5);}};exports[_0x285c('0x43')]=function(_0x585ff6){try{if(!_[_0x285c('0x23')](channels[_0x585ff6[_0x285c('0xa')]])){utils[_0x285c('0x14')](util[_0x285c('0x8')]('[%s][calls][varset]',_0x585ff6[_0x285c('0xa')]),_0x585ff6);if(!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')]['xmd-originatecalleridnum'])){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x45'),_0x585ff6[_0x285c('0xa')],_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6[_0x285c('0xa')]][_0x285c('0x47')]=_0x585ff6['value'];}if(!_[_0x285c('0x23')](_0x585ff6['variable'][_0x285c('0x48')])){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x49'),_0x585ff6[_0x285c('0xa')],_0x585ff6['value']));channels[_0x585ff6[_0x285c('0xa')]][_0x285c('0x4a')]=_0x585ff6[_0x285c('0x46')];channels[_0x585ff6[_0x285c('0xa')]]['callerid']='\x22'+_0x585ff6['value']+'\x22\x20<'+_0x585ff6[_0x285c('0x46')]+'>';}if(!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')][_0x285c('0x4b')])||!_['isNil'](_0x585ff6[_0x285c('0x44')][_0x285c('0x4c')])){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x4d'),_0x585ff6[_0x285c('0xa')],!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')]['xmd-callerid-preview'])?_0x285c('0x4b'):_0x285c('0x4c'),_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6['uniqueid']][_0x285c('0x29')]=_0x585ff6[_0x285c('0x46')];}if(!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')][_0x285c('0x4e')])||!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')][_0x285c('0x4f')])){utils[_0x285c('0x7')](util[_0x285c('0x8')]('[%s][calls][varset]\x20variable:%s\x20value:%s',_0x585ff6[_0x285c('0xa')],!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')][_0x285c('0x4e')])?_0x285c('0x4e'):_0x285c('0x4f'),_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6['uniqueid']][_0x285c('0x50')]='Queue';channels[_0x585ff6['uniqueid']]['lastdata']=_0x585ff6[_0x285c('0x46')];channels[_0x585ff6['uniqueid']][_0x285c('0xc')]=!![];}if(!_[_0x285c('0x23')](_0x585ff6['variable'][_0x285c('0x51')])){utils[_0x285c('0x7')](util['format'](_0x285c('0x52'),_0x585ff6['uniqueid'],_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6['uniqueid']]['ContactId']=_0x585ff6[_0x285c('0x46')];}if(!_[_0x285c('0x23')](_0x585ff6[_0x285c('0x44')]['xmd-cdrtype'])){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x53'),_0x585ff6[_0x285c('0xa')],_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6[_0x285c('0xa')]]['type']=_0x585ff6[_0x285c('0x46')];}if(!_['isNil'](_0x585ff6['variable']['outboundrouteid'])){utils['info'](util[_0x285c('0x8')](_0x285c('0x54'),_0x585ff6['uniqueid'],_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6[_0x285c('0xa')]][_0x285c('0x55')]=_0x585ff6[_0x285c('0x46')];}if(!_['isNil'](_0x585ff6[_0x285c('0x44')][_0x285c('0x56')])){utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x57'),_0x585ff6[_0x285c('0xa')],_0x585ff6[_0x285c('0x46')]));channels[_0x585ff6[_0x285c('0xa')]][_0x285c('0x56')]=_0x585ff6[_0x285c('0x46')];}}}catch(_0x4f64a6){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x58'),_0x585ff6[_0x285c('0xa')]),_0x4f64a6);}};exports['newexten']=function(_0x110715){try{if(!_[_0x285c('0x23')](channels[_0x110715['uniqueid']])){utils[_0x285c('0x14')](util[_0x285c('0x8')]('[%s][calls][newexten]',_0x110715[_0x285c('0xa')]),_0x110715);utils[_0x285c('0x7')](util[_0x285c('0x8')](_0x285c('0x59'),_0x110715['uniqueid'],_0x110715[_0x285c('0x5a')],_0x110715[_0x285c('0x5b')]));if(_0x110715[_0x285c('0x5a')][_0x285c('0x5c')]()==_0x285c('0x5d')&&_[_0x285c('0x5e')](_0x110715[_0x285c('0x5b')]['toLowerCase'](),_0x285c('0x5f'))){var _0x826aa6=_0x110715[_0x285c('0x5b')]['split']('=');var _0x380680=_0x826aa6[0x0][_0x285c('0x60')](_0x826aa6[0x0][_0x285c('0x61')]('(')+0x1,_0x826aa6[0x0][_0x285c('0x61')](')'));var _0x577710=_0x826aa6[0x1];channels[_0x110715[_0x285c('0xa')]][_0x380680]=_0x577710;}if(!_[_0x285c('0x62')]([_0x285c('0x63'),_0x285c('0x64'),_0x285c('0x65'),_0x285c('0x5d'),_0x285c('0x66')],_0x110715[_0x285c('0x5a')]['toLowerCase']())){channels[_0x110715[_0x285c('0xa')]][_0x285c('0x50')]=_0x110715[_0x285c('0x5a')];channels[_0x110715[_0x285c('0xa')]]['lastdata']=_0x110715[_0x285c('0x5b')];}}}catch(_0x503502){utils[_0x285c('0x1d')](util[_0x285c('0x8')](_0x285c('0x67'),_0x110715['uniqueid']),_0x503502);}};