15f032c6548f7f5b3b090331065242ac900385e1
[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 _0x11b9=['[%s][calls][varset]\x20variable:sipcallid\x20value:%s','newexten','[%s][calls][newexten]','[%s][calls][newexten]\x20application:%s\x20appdata:%s','application','appdata','toLowerCase','set','cdr','split','substring','lastIndexOf','gotoif','lodash','util','FAILED','NO\x20ANSWER','BUSY','blinds','uniqueid','transfereeuniqueid','info','format','channel','queue','membername','calleridnum','connectedlinenum','lastevent','getUserByInternal','UserId','request','CreateVoiceCallReport','newchannel','debug','[%s][calls][newchannel]','linkedid','[%s][calls][newchannel]\x20context:%s\x20exten:%s','context','exten','accountcode','DOCUMENTATION','error','newstate','[%s][calls][newstate]\x20context:%s\x20channel:%s','isNil','systemanswertime','now','channelstate','destinationchannel','answertime','disposition','toNumber','[%s][calls][newstate]','hangup','[%s][calls][hangup]','callerid','calleridname','duration','diffTime','endtime','starttime','billableseconds','attendedtransfer','localtwolinkedid','[%s][calls][attendedtransfer]','origtransfereruniqueid','transfereechannel','[%s][calls][attendedtransfer]\x20localtwolinkedid:%s\x20destinationchannel:%s','secondtransfererchannel','userfield','_attended','transfertargetchannel','origtransfererchannel','[%s][calls][musiconholdstart]','[%s][calls][musiconholdstart]\x20musiconholdstartAt:%s','musiconholdstartAt','[%s][calls][musiconholdstop]','mohtime','[%s][calls][musiconholdstop]\x20mohtime:%s','varset','[%s][calls][varset]','xmd-originatecalleridnum','[%s][calls][varset]\x20variable:xmd-originatecalleridnum\x20value:%s','value','source','[%s][calls][varset]\x20variable:xmd-phone\x20value:%s','destination','\x22\x20<','variable','xmd-callerid-preview','xmd-callerid','[%s][calls][varset]\x20variable:%s\x20value:%s','xmd-queue','xmcs-queue','lastapplication','lastdata','xmd-contactid','[%s][calls][varset]\x20variable:xmd-contactid\x20value:%s','[%s][calls][varset]\x20variable:xmd-cdrtype\x20value:%s','type','outboundrouteid','[%s][calls][varset]\x20variable:outboundrouteid\x20value:%s','routeid','sipcallid'];(function(_0x2acf2b,_0x266acd){var _0x273fa6=function(_0x360cab){while(--_0x360cab){_0x2acf2b['push'](_0x2acf2b['shift']());}};_0x273fa6(++_0x266acd);}(_0x11b9,0x76));var _0x911b=function(_0xbb20ec,_0x19cec5){_0xbb20ec=_0xbb20ec-0x0;var _0x5b33fa=_0x11b9[_0xbb20ec];return _0x5b33fa;};'use strict';var _=require(_0x911b('0x0'));var util=require(_0x911b('0x1'));var utils=require('./utils');var transfers=require('./transfers.controller');var dispositions=['NO\x20ANSWER',_0x911b('0x2'),_0x911b('0x2'),_0x911b('0x3'),'NO\x20ANSWER',_0x911b('0x3'),'ANSWERED',_0x911b('0x4'),'FAILED','FAILED',_0x911b('0x2')];var channels={};function writeDB(_0x7fe34f){var _0x25f2a9;if(transfers[_0x911b('0x5')][_0x7fe34f[_0x911b('0x6')]]){_0x25f2a9=_0x7fe34f['uniqueid'];_0x7fe34f[_0x911b('0x6')]=transfers[_0x911b('0x5')][_0x7fe34f[_0x911b('0x6')]][_0x911b('0x7')];}utils[_0x911b('0x8')](util[_0x911b('0x9')]('[%s][calls][writeDB]\x20channel:%s\x20queue:%s\x20agent:%s\x20interface:%s\x20calleridnum:%s\x20connectedlinenum:%s\x20lastevent:%s',_0x7fe34f[_0x911b('0x6')],_0x7fe34f[_0x911b('0xa')],_0x7fe34f[_0x911b('0xb')],_0x7fe34f[_0x911b('0xc')],_0x7fe34f['interface'],_0x7fe34f[_0x911b('0xd')],_0x7fe34f[_0x911b('0xe')],_0x7fe34f[_0x911b('0xf')]));if(_0x7fe34f['accountcode']){utils[_0x911b('0x10')](_0x7fe34f['accountcode'])['then'](function(_0x3e4a87){if(_0x3e4a87){_0x7fe34f[_0x911b('0x11')]=_0x3e4a87['id'];}utils[_0x911b('0x12')](_0x911b('0x13'),_0x7fe34f);});}else{utils[_0x911b('0x12')](_0x911b('0x13'),_0x7fe34f);}if(channels[_0x7fe34f[_0x911b('0x6')]]){delete channels[_0x7fe34f[_0x911b('0x6')]];}else if(_0x25f2a9&&channels[_0x25f2a9]){delete transfers[_0x911b('0x5')][_0x25f2a9];delete channels[_0x25f2a9];}}exports[_0x911b('0x14')]=function(_0x3bc314){try{utils[_0x911b('0x15')](util[_0x911b('0x9')](_0x911b('0x16'),_0x3bc314['uniqueid']),_0x3bc314);if(_0x3bc314[_0x911b('0x6')]==_0x3bc314[_0x911b('0x17')]&&_0x3bc314[_0x911b('0xa')]!='OutgoingSpoolFailed'){utils[_0x911b('0x8')](util['format'](_0x911b('0x18'),_0x3bc314[_0x911b('0x6')],_0x3bc314[_0x911b('0x19')],_0x3bc314[_0x911b('0x1a')]));channels[_0x3bc314['uniqueid']]={'uniqueid':_0x3bc314[_0x911b('0x6')],'accountcode':_0x3bc314[_0x911b('0x1b')],'source':_0x3bc314[_0x911b('0xd')],'destination':_0x3bc314[_0x911b('0x1a')],'destinationcontext':_0x3bc314['context'],'channel':_0x3bc314[_0x911b('0xa')],'starttime':utils['now'](),'answertime':null,'systemanswertime':null,'mohtime':0x0,'disposition':_0x911b('0x3'),'amaflags':_0x911b('0x1c')};}}catch(_0x2aaac9){utils[_0x911b('0x1d')](util[_0x911b('0x9')]('[%s][calls][newchannel]',_0x3bc314[_0x911b('0x6')]),_0x2aaac9);}};exports[_0x911b('0x1e')]=function(_0x1544ff){try{utils[_0x911b('0x15')](util['format']('[%s][calls][newstate]',_0x1544ff[_0x911b('0x6')]),_0x1544ff);if(!_['isNil'](channels[_0x1544ff[_0x911b('0x6')]])&&_0x1544ff['channelstate']==0x6){utils[_0x911b('0x8')](util[_0x911b('0x9')](_0x911b('0x1f'),_0x1544ff[_0x911b('0x6')],_0x1544ff['context'],_0x1544ff[_0x911b('0xa')]));if(_[_0x911b('0x20')](channels[_0x1544ff[_0x911b('0x6')]][_0x911b('0x21')])){channels[_0x1544ff[_0x911b('0x6')]][_0x911b('0x21')]=utils[_0x911b('0x22')]();}}else if(!_[_0x911b('0x20')](channels[_0x1544ff[_0x911b('0x17')]])&&_0x1544ff[_0x911b('0x23')]==0x6&&_0x1544ff['context']!='transfer'){utils['info'](util[_0x911b('0x9')]('[%s][calls][newstate]\x20linkedid:%s\x20context:%s\x20channel:%s',_0x1544ff['uniqueid'],_0x1544ff[_0x911b('0x17')],_0x1544ff[_0x911b('0x19')],_0x1544ff[_0x911b('0xa')]));channels[_0x1544ff[_0x911b('0x17')]][_0x911b('0x1b')]=_0x1544ff[_0x911b('0x1b')];channels[_0x1544ff[_0x911b('0x17')]][_0x911b('0x24')]=_0x1544ff[_0x911b('0xa')];if(_['isNil'](channels[_0x1544ff['linkedid']][_0x911b('0x25')])){channels[_0x1544ff['linkedid']][_0x911b('0x25')]=utils[_0x911b('0x22')]();}channels[_0x1544ff[_0x911b('0x17')]][_0x911b('0x26')]=dispositions[_[_0x911b('0x27')](_0x1544ff[_0x911b('0x23')])]||'ANSWERED';}}catch(_0xe462a){utils['error'](util[_0x911b('0x9')](_0x911b('0x28'),_0x1544ff['uniqueid']),_0xe462a);}};exports[_0x911b('0x29')]=function(_0x1943b3){try{if(!_[_0x911b('0x20')](channels[_0x1943b3[_0x911b('0x6')]])){utils[_0x911b('0x15')](util['format'](_0x911b('0x2a'),_0x1943b3['uniqueid']),_0x1943b3);if(_[_0x911b('0x20')](channels[_0x1943b3[_0x911b('0x6')]][_0x911b('0x2b')])){channels[_0x1943b3[_0x911b('0x6')]][_0x911b('0x2b')]=_0x1943b3[_0x911b('0x2c')]+'\x20<'+_0x1943b3[_0x911b('0xd')]+'>';}utils[_0x911b('0x8')](util[_0x911b('0x9')]('[%s][calls][hangup]\x20callerid:%s\x20reason:%s',_0x1943b3['uniqueid'],channels[_0x1943b3[_0x911b('0x6')]]['callerid'],_0x1943b3['cause-txt']));channels[_0x1943b3[_0x911b('0x6')]]['endtime']=utils['now']();channels[_0x1943b3[_0x911b('0x6')]][_0x911b('0x2d')]=utils[_0x911b('0x2e')](channels[_0x1943b3['uniqueid']][_0x911b('0x2f')],channels[_0x1943b3['uniqueid']][_0x911b('0x30')]);channels[_0x1943b3[_0x911b('0x6')]][_0x911b('0x31')]=channels[_0x1943b3[_0x911b('0x6')]]['answertime']?utils['diffTime'](channels[_0x1943b3[_0x911b('0x6')]][_0x911b('0x2f')],channels[_0x1943b3[_0x911b('0x6')]][_0x911b('0x25')]):0x0;writeDB(channels[_0x1943b3[_0x911b('0x6')]]);}}catch(_0x1e5cb8){utils[_0x911b('0x1d')](util[_0x911b('0x9')](_0x911b('0x2a'),_0x1943b3[_0x911b('0x6')]),_0x1e5cb8);}};exports[_0x911b('0x32')]=function(_0x3ee302){try{if(!_[_0x911b('0x20')](channels[_0x3ee302[_0x911b('0x33')]])){utils[_0x911b('0x15')](util[_0x911b('0x9')](_0x911b('0x34'),_0x3ee302[_0x911b('0x35')]),_0x3ee302);if(channels[_0x3ee302[_0x911b('0x33')]][_0x911b('0xa')]==_0x3ee302[_0x911b('0x36')]){utils[_0x911b('0x8')](util['format'](_0x911b('0x37'),_0x3ee302[_0x911b('0x35')],_0x3ee302[_0x911b('0x33')],_0x3ee302[_0x911b('0x38')]));channels[_0x3ee302['localtwolinkedid']]['destinationchannel']=_0x3ee302['secondtransfererchannel'];channels[_0x3ee302['localtwolinkedid']][_0x911b('0x39')]=_0x911b('0x3a');}else if(channels[_0x3ee302[_0x911b('0x33')]]['channel']=_0x3ee302[_0x911b('0x3b')]){utils[_0x911b('0x8')](util['format'](_0x911b('0x37'),_0x3ee302['origtransfereruniqueid'],_0x3ee302[_0x911b('0x33')],_0x3ee302[_0x911b('0x3c')]));channels[_0x3ee302['localtwolinkedid']][_0x911b('0x24')]=_0x3ee302[_0x911b('0x3c')];channels[_0x3ee302[_0x911b('0x33')]][_0x911b('0x39')]=_0x911b('0x3a');}}}catch(_0x4f861a){utils[_0x911b('0x1d')](util['format'](_0x911b('0x34'),_0x3ee302['origtransfereruniqueid']),_0x4f861a);}};exports['musiconholdstart']=function(_0x1ae285){try{if(!_[_0x911b('0x20')](channels[_0x1ae285[_0x911b('0x6')]])){utils[_0x911b('0x15')](util[_0x911b('0x9')](_0x911b('0x3d'),_0x1ae285['uniqueid']),_0x1ae285);channels[_0x1ae285[_0x911b('0x6')]]['musiconholdstartAt']=utils[_0x911b('0x22')]();utils[_0x911b('0x8')](util['format'](_0x911b('0x3e'),_0x1ae285['uniqueid'],channels[_0x1ae285[_0x911b('0x6')]][_0x911b('0x3f')]));}}catch(_0x4d85ec){utils['error'](util['format'](_0x911b('0x3d'),_0x1ae285[_0x911b('0x6')]),_0x4d85ec);}};exports['musiconholdstop']=function(_0x242dce){try{if(!_['isNil'](channels[_0x242dce['uniqueid']])){utils[_0x911b('0x15')](util['format'](_0x911b('0x40'),_0x242dce[_0x911b('0x6')]),_0x242dce);if(!_['isNil'](channels[_0x242dce['uniqueid']][_0x911b('0x3f')])){channels[_0x242dce[_0x911b('0x6')]][_0x911b('0x41')]+=utils[_0x911b('0x2e')](utils[_0x911b('0x22')](),channels[_0x242dce[_0x911b('0x6')]][_0x911b('0x3f')]);utils['info'](util[_0x911b('0x9')](_0x911b('0x42'),_0x242dce[_0x911b('0x6')],channels[_0x242dce[_0x911b('0x6')]][_0x911b('0x41')]));delete channels[_0x242dce[_0x911b('0x6')]][_0x911b('0x3f')];}}}catch(_0x4ce83d){utils['error'](util[_0x911b('0x9')](_0x911b('0x40'),_0x242dce[_0x911b('0x6')]),_0x4ce83d);}};exports[_0x911b('0x43')]=function(_0x5ca530){try{if(!_[_0x911b('0x20')](channels[_0x5ca530[_0x911b('0x6')]])){utils['debug'](util[_0x911b('0x9')](_0x911b('0x44'),_0x5ca530[_0x911b('0x6')]),_0x5ca530);if(!_[_0x911b('0x20')](_0x5ca530['variable'][_0x911b('0x45')])){utils['info'](util[_0x911b('0x9')](_0x911b('0x46'),_0x5ca530[_0x911b('0x6')],_0x5ca530[_0x911b('0x47')]));channels[_0x5ca530['uniqueid']][_0x911b('0x48')]=_0x5ca530[_0x911b('0x47')];}if(!_[_0x911b('0x20')](_0x5ca530['variable']['xmd-phone'])){utils['info'](util[_0x911b('0x9')](_0x911b('0x49'),_0x5ca530['uniqueid'],_0x5ca530['value']));channels[_0x5ca530[_0x911b('0x6')]][_0x911b('0x4a')]=_0x5ca530[_0x911b('0x47')];channels[_0x5ca530['uniqueid']][_0x911b('0x2b')]='\x22'+_0x5ca530['value']+_0x911b('0x4b')+_0x5ca530[_0x911b('0x47')]+'>';}if(!_[_0x911b('0x20')](_0x5ca530[_0x911b('0x4c')][_0x911b('0x4d')])||!_[_0x911b('0x20')](_0x5ca530['variable'][_0x911b('0x4e')])){utils[_0x911b('0x8')](util['format'](_0x911b('0x4f'),_0x5ca530[_0x911b('0x6')],!_['isNil'](_0x5ca530['variable']['xmd-callerid-preview'])?_0x911b('0x4d'):_0x911b('0x4e'),_0x5ca530[_0x911b('0x47')]));channels[_0x5ca530[_0x911b('0x6')]][_0x911b('0x2b')]=_0x5ca530[_0x911b('0x47')];}if(!_[_0x911b('0x20')](_0x5ca530['variable'][_0x911b('0x50')])||!_['isNil'](_0x5ca530[_0x911b('0x4c')][_0x911b('0x51')])){utils[_0x911b('0x8')](util[_0x911b('0x9')]('[%s][calls][varset]\x20variable:%s\x20value:%s',_0x5ca530[_0x911b('0x6')],!_[_0x911b('0x20')](_0x5ca530[_0x911b('0x4c')]['xmd-queue'])?_0x911b('0x50'):'xmcs-queue',_0x5ca530[_0x911b('0x47')]));channels[_0x5ca530['uniqueid']][_0x911b('0x52')]='Queue';channels[_0x5ca530[_0x911b('0x6')]][_0x911b('0x53')]=_0x5ca530[_0x911b('0x47')];channels[_0x5ca530[_0x911b('0x6')]][_0x911b('0xb')]=!![];}if(!_[_0x911b('0x20')](_0x5ca530['variable'][_0x911b('0x54')])){utils['info'](util[_0x911b('0x9')](_0x911b('0x55'),_0x5ca530[_0x911b('0x6')],_0x5ca530['value']));channels[_0x5ca530[_0x911b('0x6')]]['ContactId']=_0x5ca530[_0x911b('0x47')];}if(!_['isNil'](_0x5ca530[_0x911b('0x4c')]['xmd-cdrtype'])){utils['info'](util[_0x911b('0x9')](_0x911b('0x56'),_0x5ca530['uniqueid'],_0x5ca530[_0x911b('0x47')]));channels[_0x5ca530[_0x911b('0x6')]][_0x911b('0x57')]=_0x5ca530[_0x911b('0x47')];}if(!_[_0x911b('0x20')](_0x5ca530[_0x911b('0x4c')][_0x911b('0x58')])){utils[_0x911b('0x8')](util['format'](_0x911b('0x59'),_0x5ca530['uniqueid'],_0x5ca530[_0x911b('0x47')]));channels[_0x5ca530[_0x911b('0x6')]][_0x911b('0x5a')]=_0x5ca530['value'];}if(!_[_0x911b('0x20')](_0x5ca530[_0x911b('0x4c')][_0x911b('0x5b')])){utils['info'](util['format'](_0x911b('0x5c'),_0x5ca530[_0x911b('0x6')],_0x5ca530[_0x911b('0x47')]));channels[_0x5ca530['uniqueid']]['sipcallid']=_0x5ca530[_0x911b('0x47')];}}}catch(_0x468acc){utils['error'](util['format']('[%s][calls][varset]',_0x5ca530[_0x911b('0x6')]),_0x468acc);}};exports[_0x911b('0x5d')]=function(_0x24aa2e){try{if(!_[_0x911b('0x20')](channels[_0x24aa2e[_0x911b('0x6')]])){utils[_0x911b('0x15')](util['format'](_0x911b('0x5e'),_0x24aa2e[_0x911b('0x6')]),_0x24aa2e);utils[_0x911b('0x8')](util[_0x911b('0x9')](_0x911b('0x5f'),_0x24aa2e[_0x911b('0x6')],_0x24aa2e[_0x911b('0x60')],_0x24aa2e[_0x911b('0x61')]));if(_0x24aa2e[_0x911b('0x60')][_0x911b('0x62')]()==_0x911b('0x63')&&_['startsWith'](_0x24aa2e['appdata'][_0x911b('0x62')](),_0x911b('0x64'))){var _0x17608b=_0x24aa2e[_0x911b('0x61')][_0x911b('0x65')]('=');var _0x417a23=_0x17608b[0x0][_0x911b('0x66')](_0x17608b[0x0]['lastIndexOf']('(')+0x1,_0x17608b[0x0][_0x911b('0x67')](')'));var _0x4b5b73=_0x17608b[0x1];channels[_0x24aa2e['uniqueid']][_0x417a23]=_0x4b5b73;}if(!_['includes']([_0x911b('0x29'),'noop','execif',_0x911b('0x63'),_0x911b('0x68')],_0x24aa2e['application']['toLowerCase']())){channels[_0x24aa2e[_0x911b('0x6')]][_0x911b('0x52')]=_0x24aa2e[_0x911b('0x60')];channels[_0x24aa2e[_0x911b('0x6')]][_0x911b('0x53')]=_0x24aa2e['appdata'];}}}catch(_0x57dbb2){utils[_0x911b('0x1d')](util[_0x911b('0x9')](_0x911b('0x5e'),_0x24aa2e[_0x911b('0x6')]),_0x57dbb2);}};