1c64119c72f38bb596ab393018d915439f44cf93
[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 _0x6338=['xmd-callerid-preview','xmd-callerid','xmcs-queue','[%s][calls][varset]\x20variable:%s\x20value:%s','xmd-queue','lastapplication','Queue','lastdata','queue','[%s][calls][varset]\x20variable:xmd-contactid\x20value:%s','ContactId','xmd-cdrtype','type','outboundrouteid','[%s][calls][varset]\x20variable:outboundrouteid\x20value:%s','sipcallid','[%s][calls][varset]\x20variable:sipcallid\x20value:%s','[%s][calls][varset]','newexten','[%s][calls][newexten]','[%s][calls][newexten]\x20application:%s\x20appdata:%s','application','appdata','toLowerCase','set','startsWith','split','substring','lastIndexOf','includes','execif','gotoif','lodash','util','./utils','NO\x20ANSWER','FAILED','ANSWERED','info','format','[%s][calls][writeDB]\x20channel:%s\x20queue:%s\x20agent:%s\x20interface:%s\x20calleridnum:%s\x20connectedlinenum:%s\x20lastevent:%s','uniqueid','channel','connectedlinenum','lastevent','getUserByInternal','then','UserId','request','CreateVoiceCallReport','newchannel','[%s][calls][newchannel]','linkedid','OutgoingSpoolFailed','context','exten','accountcode','now','error','newstate','debug','isNil','channelstate','[%s][calls][newstate]\x20context:%s\x20channel:%s','systemanswertime','[%s][calls][newstate]\x20linkedid:%s\x20context:%s\x20channel:%s','answertime','disposition','toNumber','hangup','[%s][calls][hangup]','callerid','calleridnum','[%s][calls][hangup]\x20callerid:%s\x20reason:%s','cause-txt','endtime','duration','diffTime','starttime','billableseconds','attendedtransfer','[%s][calls][attendedtransfer]','origtransfereruniqueid','transfereechannel','[%s][calls][attendedtransfer]\x20localtwolinkedid:%s\x20destinationchannel:%s','localtwolinkedid','secondtransfererchannel','destinationchannel','userfield','_attended','origtransfererchannel','musiconholdstartAt','[%s][calls][musiconholdstart]\x20musiconholdstartAt:%s','[%s][calls][musiconholdstart]','musiconholdstop','[%s][calls][musiconholdstop]','[%s][calls][musiconholdstop]\x20mohtime:%s','mohtime','varset','variable','xmd-originatecalleridnum','[%s][calls][varset]\x20variable:xmd-originatecalleridnum\x20value:%s','value','source','[%s][calls][varset]\x20variable:xmd-phone\x20value:%s','destination'];(function(_0x362b52,_0x262808){var _0x267f13=function(_0x40beba){while(--_0x40beba){_0x362b52['push'](_0x362b52['shift']());}};_0x267f13(++_0x262808);}(_0x6338,0x15e));var _0x8633=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x6338[_0x3dd15e];return _0x231fd0;};'use strict';var _=require(_0x8633('0x0'));var util=require(_0x8633('0x1'));var utils=require(_0x8633('0x2'));var dispositions=[_0x8633('0x3'),'FAILED',_0x8633('0x4'),'NO\x20ANSWER',_0x8633('0x3'),_0x8633('0x3'),_0x8633('0x5'),'BUSY',_0x8633('0x4'),_0x8633('0x4'),_0x8633('0x4')];var channels={};function writeDB(_0x21994d){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x8'),_0x21994d[_0x8633('0x9')],_0x21994d[_0x8633('0xa')],_0x21994d['queue'],_0x21994d['membername'],_0x21994d['interface'],_0x21994d['calleridnum'],_0x21994d[_0x8633('0xb')],_0x21994d[_0x8633('0xc')]));if(_0x21994d['accountcode']){utils[_0x8633('0xd')](_0x21994d['accountcode'])[_0x8633('0xe')](function(_0x26dc85){if(_0x26dc85){_0x21994d[_0x8633('0xf')]=_0x26dc85['id'];}utils[_0x8633('0x10')](_0x8633('0x11'),_0x21994d);});}else{utils[_0x8633('0x10')](_0x8633('0x11'),_0x21994d);}if(channels[_0x21994d[_0x8633('0x9')]]){delete channels[_0x21994d[_0x8633('0x9')]];}}exports[_0x8633('0x12')]=function(_0x2bf764){try{utils['debug'](util[_0x8633('0x7')](_0x8633('0x13'),_0x2bf764[_0x8633('0x9')]),_0x2bf764);if(_0x2bf764['uniqueid']==_0x2bf764[_0x8633('0x14')]&&_0x2bf764[_0x8633('0xa')]!=_0x8633('0x15')){utils[_0x8633('0x6')](util['format']('[%s][calls][newchannel]\x20context:%s\x20exten:%s',_0x2bf764['uniqueid'],_0x2bf764[_0x8633('0x16')],_0x2bf764[_0x8633('0x17')]));channels[_0x2bf764[_0x8633('0x9')]]={'uniqueid':_0x2bf764[_0x8633('0x9')],'accountcode':_0x2bf764[_0x8633('0x18')],'source':_0x2bf764['calleridnum'],'destination':_0x2bf764[_0x8633('0x17')],'destinationcontext':_0x2bf764['context'],'channel':_0x2bf764[_0x8633('0xa')],'starttime':utils[_0x8633('0x19')](),'answertime':null,'systemanswertime':null,'mohtime':0x0,'disposition':_0x8633('0x3'),'amaflags':'DOCUMENTATION'};}}catch(_0x3afcf9){utils[_0x8633('0x1a')](util[_0x8633('0x7')](_0x8633('0x13'),_0x2bf764[_0x8633('0x9')]),_0x3afcf9);}};exports[_0x8633('0x1b')]=function(_0xb8471b){try{utils[_0x8633('0x1c')](util[_0x8633('0x7')]('[%s][calls][newstate]',_0xb8471b[_0x8633('0x9')]),_0xb8471b);if(!_[_0x8633('0x1d')](channels[_0xb8471b['uniqueid']])&&_0xb8471b[_0x8633('0x1e')]==0x6){utils[_0x8633('0x6')](util['format'](_0x8633('0x1f'),_0xb8471b['uniqueid'],_0xb8471b[_0x8633('0x16')],_0xb8471b['channel']));if(_['isNil'](channels[_0xb8471b[_0x8633('0x9')]][_0x8633('0x20')])){channels[_0xb8471b[_0x8633('0x9')]][_0x8633('0x20')]=utils[_0x8633('0x19')]();}}else if(!_['isNil'](channels[_0xb8471b['linkedid']])&&_0xb8471b[_0x8633('0x1e')]==0x6&&_0xb8471b[_0x8633('0x16')]!='transfer'){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x21'),_0xb8471b['uniqueid'],_0xb8471b[_0x8633('0x14')],_0xb8471b[_0x8633('0x16')],_0xb8471b[_0x8633('0xa')]));channels[_0xb8471b[_0x8633('0x14')]][_0x8633('0x18')]=_0xb8471b['accountcode'];channels[_0xb8471b[_0x8633('0x14')]]['destinationchannel']=_0xb8471b[_0x8633('0xa')];if(_['isNil'](channels[_0xb8471b[_0x8633('0x14')]][_0x8633('0x22')])){channels[_0xb8471b[_0x8633('0x14')]][_0x8633('0x22')]=utils[_0x8633('0x19')]();}channels[_0xb8471b[_0x8633('0x14')]][_0x8633('0x23')]=dispositions[_[_0x8633('0x24')](_0xb8471b['channelstate'])]||_0x8633('0x5');}}catch(_0x2bc3d2){utils[_0x8633('0x1a')](util[_0x8633('0x7')]('[%s][calls][newstate]',_0xb8471b[_0x8633('0x9')]),_0x2bc3d2);}};exports[_0x8633('0x25')]=function(_0x159c96){try{if(!_['isNil'](channels[_0x159c96[_0x8633('0x9')]])){utils[_0x8633('0x1c')](util['format'](_0x8633('0x26'),_0x159c96['uniqueid']),_0x159c96);if(_[_0x8633('0x1d')](channels[_0x159c96['uniqueid']]['callerid'])){channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x27')]=_0x159c96['calleridname']+'\x20<'+_0x159c96[_0x8633('0x28')]+'>';}utils['info'](util[_0x8633('0x7')](_0x8633('0x29'),_0x159c96[_0x8633('0x9')],channels[_0x159c96[_0x8633('0x9')]]['callerid'],_0x159c96[_0x8633('0x2a')]));channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x2b')]=utils[_0x8633('0x19')]();channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x2c')]=utils[_0x8633('0x2d')](channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x2b')],channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x2e')]);channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x2f')]=channels[_0x159c96[_0x8633('0x9')]]['answertime']?utils[_0x8633('0x2d')](channels[_0x159c96[_0x8633('0x9')]]['endtime'],channels[_0x159c96[_0x8633('0x9')]][_0x8633('0x22')]):0x0;writeDB(channels[_0x159c96[_0x8633('0x9')]]);}}catch(_0x1b7bda){utils[_0x8633('0x1a')](util[_0x8633('0x7')](_0x8633('0x26'),_0x159c96[_0x8633('0x9')]),_0x1b7bda);}};exports[_0x8633('0x30')]=function(_0x45f948){try{if(!_['isNil'](channels[_0x45f948['localtwolinkedid']])){utils[_0x8633('0x1c')](util[_0x8633('0x7')](_0x8633('0x31'),_0x45f948[_0x8633('0x32')]),_0x45f948);if(channels[_0x45f948['localtwolinkedid']][_0x8633('0xa')]==_0x45f948[_0x8633('0x33')]){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x34'),_0x45f948[_0x8633('0x32')],_0x45f948[_0x8633('0x35')],_0x45f948[_0x8633('0x36')]));channels[_0x45f948['localtwolinkedid']][_0x8633('0x37')]=_0x45f948['secondtransfererchannel'];channels[_0x45f948[_0x8633('0x35')]][_0x8633('0x38')]=_0x8633('0x39');}else if(channels[_0x45f948['localtwolinkedid']]['channel']=_0x45f948['transfertargetchannel']){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x34'),_0x45f948[_0x8633('0x32')],_0x45f948['localtwolinkedid'],_0x45f948[_0x8633('0x3a')]));channels[_0x45f948[_0x8633('0x35')]]['destinationchannel']=_0x45f948['origtransfererchannel'];channels[_0x45f948['localtwolinkedid']][_0x8633('0x38')]=_0x8633('0x39');}}}catch(_0x282cf6){utils[_0x8633('0x1a')](util['format'](_0x8633('0x31'),_0x45f948[_0x8633('0x32')]),_0x282cf6);}};exports['musiconholdstart']=function(_0x45a506){try{if(!_[_0x8633('0x1d')](channels[_0x45a506[_0x8633('0x9')]])){utils[_0x8633('0x1c')](util['format']('[%s][calls][musiconholdstart]',_0x45a506['uniqueid']),_0x45a506);channels[_0x45a506[_0x8633('0x9')]][_0x8633('0x3b')]=utils['now']();utils['info'](util[_0x8633('0x7')](_0x8633('0x3c'),_0x45a506[_0x8633('0x9')],channels[_0x45a506[_0x8633('0x9')]][_0x8633('0x3b')]));}}catch(_0x234442){utils['error'](util[_0x8633('0x7')](_0x8633('0x3d'),_0x45a506[_0x8633('0x9')]),_0x234442);}};exports[_0x8633('0x3e')]=function(_0x8c38b7){try{if(!_[_0x8633('0x1d')](channels[_0x8c38b7[_0x8633('0x9')]])){utils[_0x8633('0x1c')](util[_0x8633('0x7')](_0x8633('0x3f'),_0x8c38b7['uniqueid']),_0x8c38b7);if(!_[_0x8633('0x1d')](channels[_0x8c38b7[_0x8633('0x9')]][_0x8633('0x3b')])){channels[_0x8c38b7[_0x8633('0x9')]]['mohtime']+=utils[_0x8633('0x2d')](utils[_0x8633('0x19')](),channels[_0x8c38b7[_0x8633('0x9')]]['musiconholdstartAt']);utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x40'),_0x8c38b7[_0x8633('0x9')],channels[_0x8c38b7[_0x8633('0x9')]][_0x8633('0x41')]));delete channels[_0x8c38b7[_0x8633('0x9')]][_0x8633('0x3b')];}}}catch(_0x4b6b7a){utils[_0x8633('0x1a')](util[_0x8633('0x7')](_0x8633('0x3f'),_0x8c38b7[_0x8633('0x9')]),_0x4b6b7a);}};exports[_0x8633('0x42')]=function(_0xb24d1e){try{if(!_[_0x8633('0x1d')](channels[_0xb24d1e['uniqueid']])){utils[_0x8633('0x1c')](util[_0x8633('0x7')]('[%s][calls][varset]',_0xb24d1e[_0x8633('0x9')]),_0xb24d1e);if(!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')][_0x8633('0x44')])){utils['info'](util[_0x8633('0x7')](_0x8633('0x45'),_0xb24d1e['uniqueid'],_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e['uniqueid']][_0x8633('0x47')]=_0xb24d1e[_0x8633('0x46')];}if(!_['isNil'](_0xb24d1e[_0x8633('0x43')]['xmd-phone'])){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x48'),_0xb24d1e[_0x8633('0x9')],_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e['uniqueid']][_0x8633('0x49')]=_0xb24d1e[_0x8633('0x46')];channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x27')]='\x22'+_0xb24d1e['value']+'\x22\x20<'+_0xb24d1e[_0x8633('0x46')]+'>';}if(!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')][_0x8633('0x4a')])||!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')][_0x8633('0x4b')])){utils[_0x8633('0x6')](util[_0x8633('0x7')]('[%s][calls][varset]\x20variable:%s\x20value:%s',_0xb24d1e[_0x8633('0x9')],!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')]['xmd-callerid-preview'])?_0x8633('0x4a'):_0x8633('0x4b'),_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x27')]=_0xb24d1e[_0x8633('0x46')];}if(!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')]['xmd-queue'])||!_[_0x8633('0x1d')](_0xb24d1e['variable'][_0x8633('0x4c')])){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x4d'),_0xb24d1e[_0x8633('0x9')],!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')][_0x8633('0x4e')])?'xmd-queue':'xmcs-queue',_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x4f')]=_0x8633('0x50');channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x51')]=_0xb24d1e[_0x8633('0x46')];channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x52')]=!![];}if(!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')]['xmd-contactid'])){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x53'),_0xb24d1e[_0x8633('0x9')],_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x54')]=_0xb24d1e[_0x8633('0x46')];}if(!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')][_0x8633('0x55')])){utils[_0x8633('0x6')](util['format']('[%s][calls][varset]\x20variable:xmd-cdrtype\x20value:%s',_0xb24d1e['uniqueid'],_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x56')]=_0xb24d1e[_0x8633('0x46')];}if(!_[_0x8633('0x1d')](_0xb24d1e[_0x8633('0x43')][_0x8633('0x57')])){utils['info'](util[_0x8633('0x7')](_0x8633('0x58'),_0xb24d1e[_0x8633('0x9')],_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e[_0x8633('0x9')]]['routeid']=_0xb24d1e[_0x8633('0x46')];}if(!_[_0x8633('0x1d')](_0xb24d1e['variable'][_0x8633('0x59')])){utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x5a'),_0xb24d1e[_0x8633('0x9')],_0xb24d1e[_0x8633('0x46')]));channels[_0xb24d1e[_0x8633('0x9')]][_0x8633('0x59')]=_0xb24d1e[_0x8633('0x46')];}}}catch(_0x82b8f8){utils[_0x8633('0x1a')](util[_0x8633('0x7')](_0x8633('0x5b'),_0xb24d1e[_0x8633('0x9')]),_0x82b8f8);}};exports[_0x8633('0x5c')]=function(_0x4abe17){try{if(!_['isNil'](channels[_0x4abe17['uniqueid']])){utils[_0x8633('0x1c')](util[_0x8633('0x7')](_0x8633('0x5d'),_0x4abe17[_0x8633('0x9')]),_0x4abe17);utils[_0x8633('0x6')](util[_0x8633('0x7')](_0x8633('0x5e'),_0x4abe17[_0x8633('0x9')],_0x4abe17[_0x8633('0x5f')],_0x4abe17[_0x8633('0x60')]));if(_0x4abe17[_0x8633('0x5f')][_0x8633('0x61')]()==_0x8633('0x62')&&_[_0x8633('0x63')](_0x4abe17[_0x8633('0x60')][_0x8633('0x61')](),'cdr')){var _0xa25b05=_0x4abe17['appdata'][_0x8633('0x64')]('=');var _0x2c4e69=_0xa25b05[0x0][_0x8633('0x65')](_0xa25b05[0x0][_0x8633('0x66')]('(')+0x1,_0xa25b05[0x0][_0x8633('0x66')](')'));var _0x4e77c7=_0xa25b05[0x1];channels[_0x4abe17[_0x8633('0x9')]][_0x2c4e69]=_0x4e77c7;}if(!_[_0x8633('0x67')](['hangup','noop',_0x8633('0x68'),_0x8633('0x62'),_0x8633('0x69')],_0x4abe17['application'][_0x8633('0x61')]())){channels[_0x4abe17[_0x8633('0x9')]][_0x8633('0x4f')]=_0x4abe17[_0x8633('0x5f')];channels[_0x4abe17['uniqueid']][_0x8633('0x51')]=_0x4abe17['appdata'];}}}catch(_0x2f75c6){utils[_0x8633('0x1a')](util[_0x8633('0x7')](_0x8633('0x5d'),_0x4abe17[_0x8633('0x9')]),_0x2f75c6);}};