73cc5fff5f76b25f0bee808e4c6366812cd1ce43
[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 _0xbb97=['tag','CDR(tag)=','outbound','callerID','$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):Set(CALLERID(all)=','length','$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):NoOp(No\x20number\x20available)','answer','indexOf','appType','$[${XCALLY-MOTION-ANSWER}=0]?Answer','XCALLY-MOTION-ANSWER=1','$[${XCALLY-MOTION-PROGRESS}=0]?Progress','splice','GotoIfTime',',${EXTEN},','concat','Hangup','destroy','bulkCreate','sortBy','error','lodash','models','Interval','findAll','then','intervals','VoiceExtension','uniq','map','exten','CAST(priority\x20AS\x20UNSIGNED)','filter','groupBy','queue','playback','priority','extensions','extension','find','type','format','context','inbound','push','Set','CDR(routeid)=','VoiceExtensionId','CDR(destination)=','cutdigits','${EXTEN:','${EXTEN}','ExecIf','):Goto(','CDR(prefix)=${EXTEN:0:','CDR(prefix)=noprefix','outboundrouteid=','hasOwnProperty','recordingFormat','none','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','_XCALLY_MIXMONITOR_FORMAT=','wav','XCALLY-MOTION-ANSWER=0','XCALLY-MOTION-PROGRESS=0','MONITOR_OPTIONS=ab','toLowerCase','createdAt','updatedAt','*,*,*,*','interval','IntervalId'];(function(_0x2dafbe,_0x5d04de){var _0x58a45a=function(_0x50ac96){while(--_0x50ac96){_0x2dafbe['push'](_0x2dafbe['shift']());}};_0x58a45a(++_0x5d04de);}(_0xbb97,0xf1));var _0x7bb9=function(_0x5c67af,_0x2a1e67){_0x5c67af=_0x5c67af-0x0;var _0x1314a1=_0xbb97[_0x5c67af];return _0x1314a1;};'use strict';var _=require(_0x7bb9('0x0'));var util=require('util');function rewrite(_0x2b6e45,_0x4f231b){var _0x128d3a=this;return _0x2b6e45[_0x7bb9('0x1')][_0x7bb9('0x2')][_0x7bb9('0x3')]({'raw':!![]})[_0x7bb9('0x4')](function(_0x2aa2ce){_0x128d3a[_0x7bb9('0x5')]=_0x2aa2ce;return _0x2b6e45[_0x7bb9('0x1')][_0x7bb9('0x6')][_0x7bb9('0x3')]({'where':{'IntervalId':_0x4f231b['IntervalId']},'raw':!![]});})['then'](function(_0x103477){return _0x2b6e45[_0x7bb9('0x1')][_0x7bb9('0x6')][_0x7bb9('0x3')]({'where':{'exten':{'$in':_[_0x7bb9('0x7')](_[_0x7bb9('0x8')](_0x103477,_0x7bb9('0x9')))}},'order':[{'raw':_0x7bb9('0xa')}],'raw':!![]});})['then'](function(_0x43aed1){var _0x5cf97a=_['filter'](_0x43aed1,{'priority':'1'});var _0x535672=_[_0x7bb9('0xb')](_0x43aed1,{'isApp':0x1});var _0x10bfef=_[_0x7bb9('0xc')](_0x535672,_0x7bb9('0x9'));var _0x4b4817=[_0x7bb9('0xd'),_0x7bb9('0xe')];var _0xd6d0e7=[];var _0x278577=[];for(var _0x3670c0 in _0x10bfef){if(_0x10bfef['hasOwnProperty'](_0x3670c0)){_0x128d3a[_0x7bb9('0xf')]=0x2;_0x128d3a[_0x7bb9('0x10')]=[];_0x128d3a[_0x7bb9('0x11')]=_[_0x7bb9('0x12')](_0x5cf97a,{'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')]});_0x128d3a[_0x7bb9('0x10')]['push']({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':'Set','appdata':util[_0x7bb9('0x14')]('CDR(type)=%s',_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')]),'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a[_0x7bb9('0xf')]++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0]['VoiceExtensionId']});if(_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')]===_0x7bb9('0x16')){_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0]['type'],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x19')+_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')],'context':_0x10bfef[_0x3670c0][0x0]['context'],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a['priority']++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});}else if(_0x10bfef[_0x3670c0][0x0]['type']==='outbound'){if(_0x128d3a[_0x7bb9('0x11')]){_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x1b')+(_0x128d3a[_0x7bb9('0x11')][_0x7bb9('0x1c')]?_0x7bb9('0x1d')+_0x128d3a[_0x7bb9('0x11')]['cutdigits']+'}':_0x7bb9('0x1e')),'context':_0x10bfef[_0x3670c0][0x0]['context'],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a['priority']++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});}_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x19')+_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')],'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a[_0x7bb9('0xf')]++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0]['VoiceExtensionId']});var _0x2b847d=_0x128d3a[_0x7bb9('0xf')]++;_0x128d3a['extensions'][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x1f'),'appdata':'$[${LEN(${CDR(prefix)})}\x20>\x200]?Goto('+(_0x2b847d+0x2)+_0x7bb9('0x20')+(_0x2b847d+0x1)+')','context':_0x10bfef[_0x3670c0][0x0]['context'],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x2b847d,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});if(_0x128d3a['extension']){_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x128d3a[_0x7bb9('0x11')][_0x7bb9('0x1c')]?_0x7bb9('0x21')+_0x128d3a['extension']['cutdigits']+'}':_0x7bb9('0x22'),'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0]['exten'],'priority':_0x128d3a[_0x7bb9('0xf')]++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0]['VoiceExtensionId']});}_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':'Set','appdata':_0x7bb9('0x23')+_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')],'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0]['exten'],'priority':_0x128d3a['priority']++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});if(_0x128d3a['extension']&&_0x128d3a[_0x7bb9('0x11')][_0x7bb9('0x24')](_0x7bb9('0x25'))&&_0x128d3a[_0x7bb9('0x11')]['recordingFormat']!==_0x7bb9('0x26')){_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0]['type'],'app':'Set','appdata':_0x7bb9('0x27'),'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0]['exten'],'priority':_0x128d3a['priority']++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x28')+_0x128d3a[_0x7bb9('0x11')][_0x7bb9('0x25')]||_0x7bb9('0x29'),'context':_0x10bfef[_0x3670c0][0x0]['context'],'exten':_0x10bfef[_0x3670c0][0x0]['exten'],'priority':_0x128d3a[_0x7bb9('0xf')]++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});}}_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':'Set','appdata':_0x7bb9('0x2a'),'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a[_0x7bb9('0xf')]++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0]['VoiceExtensionId']});_0x128d3a[_0x7bb9('0x10')]['push']({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x2b'),'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a[_0x7bb9('0xf')]++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x2c'),'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'priority':_0x128d3a['priority']++,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});for(var _0x5efd57=0x0,_0x1f7d52=0x0;_0x5efd57<_0x10bfef[_0x3670c0]['length'];_0x5efd57+=0x1,_0x1f7d52=0x0){var _0x917685=_0x10bfef[_0x3670c0][_0x5efd57];if(_0x917685[_0x7bb9('0x13')]){_0x917685[_0x7bb9('0x13')]=_0x917685[_0x7bb9('0x13')][_0x7bb9('0x2d')]();}var _0x275fdb=[];var _0x212ad1=[];delete _0x917685['id'];delete _0x917685[_0x7bb9('0x2e')];delete _0x917685[_0x7bb9('0x2f')];var _0x42de5f=_0x917685['interval']!==_0x7bb9('0x30')?[_0x917685[_0x7bb9('0x31')]]:_0x917685['IntervalId']?_[_0x7bb9('0x8')](_[_0x7bb9('0xb')](_0x128d3a[_0x7bb9('0x5')],{'IntervalId':_0x917685[_0x7bb9('0x32')]}),_0x7bb9('0x31')):[];if(_0x917685[_0x7bb9('0x24')](_0x7bb9('0x33'))){if(_0x917685['tag']&&_0x917685[_0x7bb9('0x33')]!=='--'){_0x275fdb[_0x7bb9('0x17')]({'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x34')+_0x917685[_0x7bb9('0x33')],'context':_0x917685[_0x7bb9('0x15')],'exten':_0x917685[_0x7bb9('0x9')],'priority':_0x42de5f['length']+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685[_0x7bb9('0x1a')]});}}if(_0x917685[_0x7bb9('0x13')]===_0x7bb9('0x35')&&_0x917685[_0x7bb9('0x24')]('callerID')){if(_0x917685[_0x7bb9('0x36')]){_0x275fdb[_0x7bb9('0x17')]({'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x1f'),'appdata':_0x7bb9('0x37')+_0x917685[_0x7bb9('0x36')]+')','context':_0x917685[_0x7bb9('0x15')],'exten':_0x917685[_0x7bb9('0x9')],'priority':_0x42de5f[_0x7bb9('0x38')]+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685[_0x7bb9('0x1a')]});}else{_0x275fdb[_0x7bb9('0x17')]({'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x1f'),'appdata':_0x7bb9('0x39'),'context':_0x917685[_0x7bb9('0x15')],'exten':_0x917685[_0x7bb9('0x9')],'priority':_0x42de5f[_0x7bb9('0x38')]+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685['VoiceExtensionId']});}}if(_0x917685[_0x7bb9('0x24')](_0x7bb9('0x3a'))){if(_0x4b4817[_0x7bb9('0x3b')](_0x917685['app']['toLowerCase']())>=0x0&&_0x917685[_0x7bb9('0x3c')]!=='custom'){if(_0x917685[_0x7bb9('0x3a')]){_0x275fdb['push']({'type':_0x917685['type'],'app':'ExecIf','appdata':_0x7bb9('0x3d'),'context':_0x917685[_0x7bb9('0x15')],'exten':_0x917685[_0x7bb9('0x9')],'priority':_0x42de5f[_0x7bb9('0x38')]+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685[_0x7bb9('0x1a')]});_0x275fdb['push']({'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':_0x7bb9('0x3e'),'context':_0x917685[_0x7bb9('0x15')],'exten':_0x917685['exten'],'priority':_0x42de5f[_0x7bb9('0x38')]+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685[_0x7bb9('0x1a')]});}else{_0x275fdb[_0x7bb9('0x17')]({'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x1f'),'appdata':_0x7bb9('0x3f'),'context':_0x917685[_0x7bb9('0x15')],'exten':_0x917685['exten'],'priority':_0x42de5f[_0x7bb9('0x38')]+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685[_0x7bb9('0x1a')]});_0x275fdb[_0x7bb9('0x17')]({'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x18'),'appdata':'XCALLY-MOTION-PROGRESS=1','context':_0x917685['context'],'exten':_0x917685['exten'],'priority':_0x42de5f[_0x7bb9('0x38')]+_0x128d3a[_0x7bb9('0xf')]+_0x1f7d52++,'VoiceExtensionId':_0x917685['VoiceExtensionId']});}}}for(var _0x5c9bc6=0x0;_0x5c9bc6<_0x42de5f[_0x7bb9('0x38')];_0x5c9bc6+=0x1){var _0x181767=_0x128d3a[_0x7bb9('0xf')]+_0x42de5f[_0x7bb9('0x38')];var _0x471cbf=_0x5c9bc6===_0x42de5f[_0x7bb9('0x38')]-0x1?_0x128d3a[_0x7bb9('0xf')]+_0x275fdb[_0x7bb9('0x38')]+_0x212ad1['length']+0x2:_0x128d3a[_0x7bb9('0xf')]+_0x5c9bc6+0x1;_0x275fdb[_0x7bb9('0x40')](_0x5c9bc6,0x0,{'type':_0x917685[_0x7bb9('0x13')],'app':_0x7bb9('0x41'),'appdata':_0x42de5f[_0x5c9bc6]+'?'+_0x917685[_0x7bb9('0x15')]+',${EXTEN},'+_0x181767+':'+_0x917685[_0x7bb9('0x15')]+_0x7bb9('0x42')+_0x471cbf,'exten':_0x917685['exten'],'context':_0x917685[_0x7bb9('0x15')],'priority':_0x128d3a['priority']+_0x5c9bc6,'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0]['VoiceExtensionId']});}_0x917685['priority']=_0x275fdb[_0x7bb9('0x38')]?_['last'](_0x275fdb)['priority']+0x1:_0x128d3a['priority'];_0x128d3a[_0x7bb9('0xf')]=(_0x212ad1[_0x7bb9('0x38')]?_['last'](_0x212ad1)[_0x7bb9('0xf')]:_0x917685[_0x7bb9('0xf')])+0x1;_0x128d3a[_0x7bb9('0x10')]=_[_0x7bb9('0x43')](_0x128d3a[_0x7bb9('0x10')],_0x275fdb,[_0x917685],_0x212ad1);}_0x128d3a[_0x7bb9('0x10')][_0x7bb9('0x17')]({'type':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x13')],'app':_0x7bb9('0x44'),'exten':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x9')],'context':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x15')],'priority':_0x128d3a[_0x7bb9('0xf')],'VoiceExtensionId':_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]});_0xd6d0e7[_0x7bb9('0x17')](_0x10bfef[_0x3670c0][0x0][_0x7bb9('0x1a')]);_0x278577=_['concat'](_0x278577,_0x128d3a['extensions']);}}return _0x2b6e45['transaction'](function(_0x191424){return _0x2b6e45['models'][_0x7bb9('0x6')][_0x7bb9('0x45')]({'where':{'VoiceExtensionId':{'$in':_0xd6d0e7}},'transaction':_0x191424})[_0x7bb9('0x4')](function(_0x10edcf){return _0x2b6e45['models']['VoiceExtension'][_0x7bb9('0x46')](_[_0x7bb9('0x47')](_0x278577,_0x7bb9('0xf')),{'transaction':_0x191424});});});})['catch'](function(_0x2f2ec5){console[_0x7bb9('0x48')](_0x2f2ec5);});}exports['rewrite']=rewrite;