Built motion from commit d1eab355.|2.6.28
[motion2.git] / server / components / extensions / rewrite.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 _0x76e6=['none','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=','wav','XCALLY-MOTION-PROGRESS=0','MONITOR_OPTIONS=ab','length','toLowerCase','createdAt','updatedAt','interval','*,*,*,*','tag','CDR(tag)=','ExecIf','$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):Set(CALLERID(all)=','callerID','$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):NoOp(No\x20number\x20available)','answer','app','appType','$[${XCALLY-MOTION-ANSWER}=0]?Answer','XCALLY-MOTION-ANSWER=1','XCALLY-MOTION-PROGRESS=1','GotoIfTime',',${EXTEN},','last','Hangup','concat','transaction','destroy','bulkCreate','sortBy','catch','error','lodash','util','models','Interval','findAll','intervals','VoiceExtension','IntervalId','then','uniq','map','exten','CAST(priority\x20AS\x20UNSIGNED)','filter','groupBy','playback','hasOwnProperty','priority','extensions','type','CDR(type)=%s','context','VoiceExtensionId','inbound','CDR(routeid)=','push','Set','extension','cutdigits','${EXTEN:','${EXTEN}','$[${LEN(${CDR(prefix)})}\x20>\x200]?Goto(','):Goto(','outboundrouteid=','recordingFormat'];(function(_0x1be72d,_0x8ab5df){var _0x5691f2=function(_0x5ceaa5){while(--_0x5ceaa5){_0x1be72d['push'](_0x1be72d['shift']());}};_0x5691f2(++_0x8ab5df);}(_0x76e6,0x69));var _0x676e=function(_0xa464c9,_0x7c86d8){_0xa464c9=_0xa464c9-0x0;var _0x3d73c4=_0x76e6[_0xa464c9];return _0x3d73c4;};'use strict';var _=require(_0x676e('0x0'));var util=require(_0x676e('0x1'));function rewrite(_0x2cda0f,_0x4feeef){var _0x4023b4=this;return _0x2cda0f[_0x676e('0x2')][_0x676e('0x3')][_0x676e('0x4')]({'raw':!![]})['then'](function(_0x4e40b3){_0x4023b4[_0x676e('0x5')]=_0x4e40b3;return _0x2cda0f[_0x676e('0x2')][_0x676e('0x6')][_0x676e('0x4')]({'where':{'IntervalId':_0x4feeef[_0x676e('0x7')]},'raw':!![]});})[_0x676e('0x8')](function(_0x217084){return _0x2cda0f[_0x676e('0x2')]['VoiceExtension']['findAll']({'where':{'exten':{'$in':_[_0x676e('0x9')](_[_0x676e('0xa')](_0x217084,_0x676e('0xb')))}},'order':[{'raw':_0x676e('0xc')}],'raw':!![]});})['then'](function(_0x50e45a){var _0x2027af=_[_0x676e('0xd')](_0x50e45a,{'priority':'1'});var _0x5002f8=_[_0x676e('0xd')](_0x50e45a,{'isApp':0x1});var _0x3384ec=_[_0x676e('0xe')](_0x5002f8,'exten');var _0x4b1c0f=['queue',_0x676e('0xf')];var _0x56ca80=[];var _0x248e55=[];for(var _0x280c7a in _0x3384ec){if(_0x3384ec[_0x676e('0x10')](_0x280c7a)){_0x4023b4[_0x676e('0x11')]=0x2;_0x4023b4[_0x676e('0x12')]=[];_0x4023b4['extension']=_['find'](_0x2027af,{'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')]});_0x4023b4[_0x676e('0x12')]['push']({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':'Set','appdata':util['format'](_0x676e('0x14'),_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')]),'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});if(_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')]===_0x676e('0x17')){_0x4023b4['extensions']['push']({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':'Set','appdata':_0x676e('0x18')+_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')],'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0]['exten'],'priority':_0x4023b4['priority']++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});}else if(_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')]==='outbound'){if(_0x4023b4['extension']){_0x4023b4['extensions'][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0]['type'],'app':_0x676e('0x1a'),'appdata':'CDR(destination)='+(_0x4023b4[_0x676e('0x1b')][_0x676e('0x1c')]?_0x676e('0x1d')+_0x4023b4[_0x676e('0x1b')][_0x676e('0x1c')]+'}':_0x676e('0x1e')),'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});}_0x4023b4['extensions'][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':_0x676e('0x1a'),'appdata':_0x676e('0x18')+_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')],'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0]['exten'],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});var _0x5ef4f7=_0x4023b4[_0x676e('0x11')]++;_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':'ExecIf','appdata':_0x676e('0x1f')+(_0x5ef4f7+0x2)+_0x676e('0x20')+(_0x5ef4f7+0x1)+')','context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0]['exten'],'priority':_0x5ef4f7,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});if(_0x4023b4[_0x676e('0x1b')]){_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0]['type'],'app':_0x676e('0x1a'),'appdata':_0x4023b4[_0x676e('0x1b')][_0x676e('0x1c')]?'CDR(prefix)=${EXTEN:0:'+_0x4023b4[_0x676e('0x1b')][_0x676e('0x1c')]+'}':'CDR(prefix)=noprefix','context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0]['exten'],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});}_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0]['type'],'app':_0x676e('0x1a'),'appdata':_0x676e('0x21')+_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')],'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});if(_0x4023b4[_0x676e('0x1b')]&&_0x4023b4[_0x676e('0x1b')][_0x676e('0x10')](_0x676e('0x22'))&&_0x4023b4[_0x676e('0x1b')][_0x676e('0x22')]!==_0x676e('0x23')){_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0]['type'],'app':_0x676e('0x1a'),'appdata':_0x676e('0x24'),'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0]['exten'],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':'Set','appdata':_0x676e('0x25')+_0x4023b4[_0x676e('0x1b')]['recordingFormat']||_0x676e('0x26'),'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4['priority']++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});}}_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':_0x676e('0x1a'),'appdata':'XCALLY-MOTION-ANSWER=0','context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4['priority']++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0]['VoiceExtensionId']});_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':_0x676e('0x1a'),'appdata':_0x676e('0x27'),'context':_0x3384ec[_0x280c7a][0x0][_0x676e('0x15')],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4[_0x676e('0x11')]++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0]['VoiceExtensionId']});_0x4023b4[_0x676e('0x12')]['push']({'type':_0x3384ec[_0x280c7a][0x0][_0x676e('0x13')],'app':_0x676e('0x1a'),'appdata':_0x676e('0x28'),'context':_0x3384ec[_0x280c7a][0x0]['context'],'exten':_0x3384ec[_0x280c7a][0x0][_0x676e('0xb')],'priority':_0x4023b4['priority']++,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});for(var _0x3cae1f=0x0,_0xf1cabf=0x0;_0x3cae1f<_0x3384ec[_0x280c7a][_0x676e('0x29')];_0x3cae1f+=0x1,_0xf1cabf=0x0){var _0x1031d9=_0x3384ec[_0x280c7a][_0x3cae1f];if(_0x1031d9['type']){_0x1031d9[_0x676e('0x13')]=_0x1031d9[_0x676e('0x13')][_0x676e('0x2a')]();}var _0x56a6ef=[];var _0x509e6a=[];delete _0x1031d9['id'];delete _0x1031d9[_0x676e('0x2b')];delete _0x1031d9[_0x676e('0x2c')];var _0x5c66b1=_0x1031d9[_0x676e('0x2d')]!==_0x676e('0x2e')?[_0x1031d9[_0x676e('0x2d')]]:_0x1031d9[_0x676e('0x7')]?_[_0x676e('0xa')](_[_0x676e('0xd')](_0x4023b4[_0x676e('0x5')],{'IntervalId':_0x1031d9['IntervalId']}),_0x676e('0x2d')):[];if(_0x1031d9[_0x676e('0x10')]('tag')){if(_0x1031d9[_0x676e('0x2f')]&&_0x1031d9[_0x676e('0x2f')]!=='--'){_0x56a6ef[_0x676e('0x19')]({'type':_0x1031d9[_0x676e('0x13')],'app':_0x676e('0x1a'),'appdata':_0x676e('0x30')+_0x1031d9[_0x676e('0x2f')],'context':_0x1031d9[_0x676e('0x15')],'exten':_0x1031d9[_0x676e('0xb')],'priority':_0x5c66b1['length']+_0x4023b4[_0x676e('0x11')]+_0xf1cabf++,'VoiceExtensionId':_0x1031d9[_0x676e('0x16')]});}}if(_0x1031d9[_0x676e('0x13')]==='outbound'&&_0x1031d9[_0x676e('0x10')]('callerID')){if(_0x1031d9['callerID']){_0x56a6ef[_0x676e('0x19')]({'type':_0x1031d9[_0x676e('0x13')],'app':_0x676e('0x31'),'appdata':_0x676e('0x32')+_0x1031d9[_0x676e('0x33')]+')','context':_0x1031d9[_0x676e('0x15')],'exten':_0x1031d9['exten'],'priority':_0x5c66b1[_0x676e('0x29')]+_0x4023b4[_0x676e('0x11')]+_0xf1cabf++,'VoiceExtensionId':_0x1031d9[_0x676e('0x16')]});}else{_0x56a6ef[_0x676e('0x19')]({'type':_0x1031d9[_0x676e('0x13')],'app':'ExecIf','appdata':_0x676e('0x34'),'context':_0x1031d9['context'],'exten':_0x1031d9['exten'],'priority':_0x5c66b1['length']+_0x4023b4[_0x676e('0x11')]+_0xf1cabf++,'VoiceExtensionId':_0x1031d9[_0x676e('0x16')]});}}if(_0x1031d9[_0x676e('0x10')](_0x676e('0x35'))){if(_0x4b1c0f['indexOf'](_0x1031d9[_0x676e('0x36')][_0x676e('0x2a')]())>=0x0&&_0x1031d9[_0x676e('0x37')]!=='custom'){if(_0x1031d9[_0x676e('0x35')]){_0x56a6ef[_0x676e('0x19')]({'type':_0x1031d9[_0x676e('0x13')],'app':_0x676e('0x31'),'appdata':_0x676e('0x38'),'context':_0x1031d9[_0x676e('0x15')],'exten':_0x1031d9[_0x676e('0xb')],'priority':_0x5c66b1['length']+_0x4023b4[_0x676e('0x11')]+_0xf1cabf++,'VoiceExtensionId':_0x1031d9['VoiceExtensionId']});_0x56a6ef[_0x676e('0x19')]({'type':_0x1031d9['type'],'app':'Set','appdata':_0x676e('0x39'),'context':_0x1031d9['context'],'exten':_0x1031d9[_0x676e('0xb')],'priority':_0x5c66b1[_0x676e('0x29')]+_0x4023b4[_0x676e('0x11')]+_0xf1cabf++,'VoiceExtensionId':_0x1031d9[_0x676e('0x16')]});}else{_0x56a6ef['push']({'type':_0x1031d9[_0x676e('0x13')],'app':_0x676e('0x31'),'appdata':'$[${XCALLY-MOTION-PROGRESS}=0]?Progress','context':_0x1031d9['context'],'exten':_0x1031d9[_0x676e('0xb')],'priority':_0x5c66b1[_0x676e('0x29')]+_0x4023b4[_0x676e('0x11')]+_0xf1cabf++,'VoiceExtensionId':_0x1031d9[_0x676e('0x16')]});_0x56a6ef[_0x676e('0x19')]({'type':_0x1031d9['type'],'app':'Set','appdata':_0x676e('0x3a'),'context':_0x1031d9['context'],'exten':_0x1031d9[_0x676e('0xb')],'priority':_0x5c66b1[_0x676e('0x29')]+_0x4023b4['priority']+_0xf1cabf++,'VoiceExtensionId':_0x1031d9[_0x676e('0x16')]});}}}for(var _0x5b1dcd=0x0;_0x5b1dcd<_0x5c66b1[_0x676e('0x29')];_0x5b1dcd+=0x1){var _0x4d2bbb=_0x4023b4[_0x676e('0x11')]+_0x5c66b1[_0x676e('0x29')];var _0x54b4fa=_0x5b1dcd===_0x5c66b1[_0x676e('0x29')]-0x1?_0x4023b4[_0x676e('0x11')]+_0x56a6ef[_0x676e('0x29')]+_0x509e6a[_0x676e('0x29')]+0x2:_0x4023b4[_0x676e('0x11')]+_0x5b1dcd+0x1;_0x56a6ef['splice'](_0x5b1dcd,0x0,{'type':_0x1031d9[_0x676e('0x13')],'app':_0x676e('0x3b'),'appdata':_0x5c66b1[_0x5b1dcd]+'?'+_0x1031d9['context']+_0x676e('0x3c')+_0x4d2bbb+':'+_0x1031d9[_0x676e('0x15')]+_0x676e('0x3c')+_0x54b4fa,'exten':_0x1031d9[_0x676e('0xb')],'context':_0x1031d9[_0x676e('0x15')],'priority':_0x4023b4['priority']+_0x5b1dcd,'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});}_0x1031d9[_0x676e('0x11')]=_0x56a6ef[_0x676e('0x29')]?_[_0x676e('0x3d')](_0x56a6ef)[_0x676e('0x11')]+0x1:_0x4023b4[_0x676e('0x11')];_0x4023b4[_0x676e('0x11')]=(_0x509e6a['length']?_[_0x676e('0x3d')](_0x509e6a)['priority']:_0x1031d9['priority'])+0x1;_0x4023b4[_0x676e('0x12')]=_['concat'](_0x4023b4[_0x676e('0x12')],_0x56a6ef,[_0x1031d9],_0x509e6a);}_0x4023b4[_0x676e('0x12')][_0x676e('0x19')]({'type':_0x3384ec[_0x280c7a][0x0]['type'],'app':_0x676e('0x3e'),'exten':_0x3384ec[_0x280c7a][0x0]['exten'],'context':_0x3384ec[_0x280c7a][0x0]['context'],'priority':_0x4023b4[_0x676e('0x11')],'VoiceExtensionId':_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]});_0x56ca80[_0x676e('0x19')](_0x3384ec[_0x280c7a][0x0][_0x676e('0x16')]);_0x248e55=_[_0x676e('0x3f')](_0x248e55,_0x4023b4['extensions']);}}return _0x2cda0f[_0x676e('0x40')](function(_0x381bb9){return _0x2cda0f[_0x676e('0x2')]['VoiceExtension'][_0x676e('0x41')]({'where':{'VoiceExtensionId':{'$in':_0x56ca80}},'transaction':_0x381bb9})[_0x676e('0x8')](function(_0x4643b4){return _0x2cda0f[_0x676e('0x2')][_0x676e('0x6')][_0x676e('0x42')](_[_0x676e('0x43')](_0x248e55,_0x676e('0x11')),{'transaction':_0x381bb9});});});})[_0x676e('0x44')](function(_0x231fd5){console[_0x676e('0x45')](_0x231fd5);});}exports['rewrite']=rewrite;