a90b74bdbc5ab620e22e4d982369f72f1027300a
[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 _0x57b7=['indexOf','appType','custom','XCALLY-MOTION-ANSWER=1','XCALLY-MOTION-PROGRESS=1','splice',',${EXTEN},','last','concat','Hangup','transaction','destroy','bulkCreate','sortBy','error','rewrite','lodash','util','models','Interval','findAll','intervals','VoiceExtension','IntervalId','then','exten','filter','groupBy','playback','hasOwnProperty','priority','extension','find','Set','format','type','context','VoiceExtensionId','push','CDR(routeid)=','extensions','CDR(destination)=','cutdigits','${EXTEN:','ExecIf','CDR(prefix)=${EXTEN:0:','outboundrouteid=','recordingFormat','none','_XCALLY_MIXMONITOR_FORMAT=','wav','XCALLY-MOTION-ANSWER=0','XCALLY-MOTION-PROGRESS=0','length','toLowerCase','createdAt','updatedAt','interval','*,*,*,*','map','tag','CDR(tag)=','outbound','$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):Set(CALLERID(all)=','answer'];(function(_0xd27ded,_0x1164e2){var _0x5ef850=function(_0x582e7d){while(--_0x582e7d){_0xd27ded['push'](_0xd27ded['shift']());}};_0x5ef850(++_0x1164e2);}(_0x57b7,0x196));var _0x757b=function(_0x28b528,_0xa62109){_0x28b528=_0x28b528-0x0;var _0x5cde4f=_0x57b7[_0x28b528];return _0x5cde4f;};'use strict';var _=require(_0x757b('0x0'));var util=require(_0x757b('0x1'));function rewrite(_0x3df85f,_0x511a96){var _0x4704c2=this;return _0x3df85f[_0x757b('0x2')][_0x757b('0x3')][_0x757b('0x4')]({'raw':!![]})['then'](function(_0x2411cf){_0x4704c2[_0x757b('0x5')]=_0x2411cf;return _0x3df85f[_0x757b('0x2')][_0x757b('0x6')][_0x757b('0x4')]({'where':{'IntervalId':_0x511a96[_0x757b('0x7')]},'raw':!![]});})[_0x757b('0x8')](function(_0x3dfeff){return _0x3df85f[_0x757b('0x2')][_0x757b('0x6')][_0x757b('0x4')]({'where':{'exten':{'$in':_['uniq'](_['map'](_0x3dfeff,_0x757b('0x9')))}},'order':[{'raw':'CAST(priority\x20AS\x20UNSIGNED)'}],'raw':!![]});})[_0x757b('0x8')](function(_0x6d2f1){var _0x198ae5=_[_0x757b('0xa')](_0x6d2f1,{'priority':'1'});var _0x527b6d=_[_0x757b('0xa')](_0x6d2f1,{'isApp':0x1});var _0x4f5250=_[_0x757b('0xb')](_0x527b6d,'exten');var _0x243890=['queue',_0x757b('0xc')];var _0x33daea=[];var _0x2ad0c9=[];for(var _0x2251a8 in _0x4f5250){if(_0x4f5250[_0x757b('0xd')](_0x2251a8)){_0x4704c2[_0x757b('0xe')]=0x2;_0x4704c2['extensions']=[];_0x4704c2[_0x757b('0xf')]=_[_0x757b('0x10')](_0x198ae5,{'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')]});_0x4704c2['extensions']['push']({'type':_0x4f5250[_0x2251a8][0x0]['type'],'app':_0x757b('0x11'),'appdata':util[_0x757b('0x12')]('CDR(type)=%s',_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')]),'context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0]['exten'],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});if(_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')]==='inbound'){_0x4704c2['extensions'][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0]['type'],'app':_0x757b('0x11'),'appdata':_0x757b('0x17')+_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')],'context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});}else if(_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')]==='outbound'){if(_0x4704c2['extension']){_0x4704c2[_0x757b('0x18')][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0]['type'],'app':_0x757b('0x11'),'appdata':_0x757b('0x19')+(_0x4704c2['extension'][_0x757b('0x1a')]?_0x757b('0x1b')+_0x4704c2[_0x757b('0xf')][_0x757b('0x1a')]+'}':'${EXTEN}'),'context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2['priority']++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0]['VoiceExtensionId']});}_0x4704c2[_0x757b('0x18')][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':'Set','appdata':_0x757b('0x17')+_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')],'context':_0x4f5250[_0x2251a8][0x0]['context'],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0]['VoiceExtensionId']});var _0x3a0e4d=_0x4704c2[_0x757b('0xe')]++;_0x4704c2['extensions'][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x1c'),'appdata':'$[${LEN(${CDR(prefix)})}\x20>\x200]?Goto('+(_0x3a0e4d+0x2)+'):Goto('+(_0x3a0e4d+0x1)+')','context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x3a0e4d,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});if(_0x4704c2[_0x757b('0xf')]){_0x4704c2['extensions'][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':_0x4704c2[_0x757b('0xf')][_0x757b('0x1a')]?_0x757b('0x1d')+_0x4704c2[_0x757b('0xf')][_0x757b('0x1a')]+'}':'CDR(prefix)=noprefix','context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});}_0x4704c2[_0x757b('0x18')]['push']({'type':_0x4f5250[_0x2251a8][0x0]['type'],'app':'Set','appdata':_0x757b('0x1e')+_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')],'context':_0x4f5250[_0x2251a8][0x0]['context'],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2['priority']++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});if(_0x4704c2[_0x757b('0xf')]&&_0x4704c2[_0x757b('0xf')]['hasOwnProperty'](_0x757b('0x1f'))&&_0x4704c2[_0x757b('0xf')]['recordingFormat']!==_0x757b('0x20')){_0x4704c2[_0x757b('0x18')]['push']({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':'_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});_0x4704c2[_0x757b('0x18')]['push']({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':_0x757b('0x21')+_0x4704c2[_0x757b('0xf')]['recordingFormat']||_0x757b('0x22'),'context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0]['exten'],'priority':_0x4704c2['priority']++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});}}_0x4704c2['extensions']['push']({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':_0x757b('0x23'),'context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0]['exten'],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0]['VoiceExtensionId']});_0x4704c2[_0x757b('0x18')][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':_0x757b('0x24'),'context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0][_0x757b('0x9')],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0]['VoiceExtensionId']});_0x4704c2[_0x757b('0x18')]['push']({'type':_0x4f5250[_0x2251a8][0x0][_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':'MONITOR_OPTIONS=ab','context':_0x4f5250[_0x2251a8][0x0][_0x757b('0x14')],'exten':_0x4f5250[_0x2251a8][0x0]['exten'],'priority':_0x4704c2[_0x757b('0xe')]++,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});for(var _0x3bed07=0x0,_0x4393e8=0x0;_0x3bed07<_0x4f5250[_0x2251a8][_0x757b('0x25')];_0x3bed07+=0x1,_0x4393e8=0x0){var _0x1e53d5=_0x4f5250[_0x2251a8][_0x3bed07];if(_0x1e53d5[_0x757b('0x13')]){_0x1e53d5[_0x757b('0x13')]=_0x1e53d5[_0x757b('0x13')][_0x757b('0x26')]();}var _0xd9a74f=[];var _0x6fbd4=[];delete _0x1e53d5['id'];delete _0x1e53d5[_0x757b('0x27')];delete _0x1e53d5[_0x757b('0x28')];var _0x4e7b1f=_0x1e53d5[_0x757b('0x29')]!==_0x757b('0x2a')?[_0x1e53d5[_0x757b('0x29')]]:_0x1e53d5[_0x757b('0x7')]?_[_0x757b('0x2b')](_['filter'](_0x4704c2[_0x757b('0x5')],{'IntervalId':_0x1e53d5['IntervalId']}),_0x757b('0x29')):[];if(_0x1e53d5[_0x757b('0xd')]('tag')){if(_0x1e53d5[_0x757b('0x2c')]&&_0x1e53d5[_0x757b('0x2c')]!=='--'){_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':'Set','appdata':_0x757b('0x2d')+_0x1e53d5[_0x757b('0x2c')],'context':_0x1e53d5[_0x757b('0x14')],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f[_0x757b('0x25')]+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5[_0x757b('0x15')]});}}if(_0x1e53d5[_0x757b('0x13')]===_0x757b('0x2e')&&_0x1e53d5[_0x757b('0xd')]('callerID')){if(_0x1e53d5['callerID']){_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':_0x757b('0x1c'),'appdata':_0x757b('0x2f')+_0x1e53d5['callerID']+')','context':_0x1e53d5[_0x757b('0x14')],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f[_0x757b('0x25')]+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5[_0x757b('0x15')]});}else{_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':_0x757b('0x1c'),'appdata':'$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):NoOp(No\x20number\x20available)','context':_0x1e53d5['context'],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f[_0x757b('0x25')]+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5[_0x757b('0x15')]});}}if(_0x1e53d5[_0x757b('0xd')](_0x757b('0x30'))){if(_0x243890[_0x757b('0x31')](_0x1e53d5['app'][_0x757b('0x26')]())>=0x0&&_0x1e53d5[_0x757b('0x32')]!==_0x757b('0x33')){if(_0x1e53d5[_0x757b('0x30')]){_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':_0x757b('0x1c'),'appdata':'$[${XCALLY-MOTION-ANSWER}=0]?Answer','context':_0x1e53d5['context'],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f[_0x757b('0x25')]+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5[_0x757b('0x15')]});_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':_0x757b('0x34'),'context':_0x1e53d5[_0x757b('0x14')],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f[_0x757b('0x25')]+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5[_0x757b('0x15')]});}else{_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':'ExecIf','appdata':'$[${XCALLY-MOTION-PROGRESS}=0]?Progress','context':_0x1e53d5['context'],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f[_0x757b('0x25')]+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5[_0x757b('0x15')]});_0xd9a74f[_0x757b('0x16')]({'type':_0x1e53d5[_0x757b('0x13')],'app':_0x757b('0x11'),'appdata':_0x757b('0x35'),'context':_0x1e53d5[_0x757b('0x14')],'exten':_0x1e53d5[_0x757b('0x9')],'priority':_0x4e7b1f['length']+_0x4704c2[_0x757b('0xe')]+_0x4393e8++,'VoiceExtensionId':_0x1e53d5['VoiceExtensionId']});}}}for(var _0x5e673d=0x0;_0x5e673d<_0x4e7b1f['length'];_0x5e673d+=0x1){var _0x5206c6=_0x4704c2[_0x757b('0xe')]+_0x4e7b1f[_0x757b('0x25')];var _0x120565=_0x5e673d===_0x4e7b1f['length']-0x1?_0x4704c2['priority']+_0xd9a74f['length']+_0x6fbd4[_0x757b('0x25')]+0x2:_0x4704c2['priority']+_0x5e673d+0x1;_0xd9a74f[_0x757b('0x36')](_0x5e673d,0x0,{'type':_0x1e53d5[_0x757b('0x13')],'app':'GotoIfTime','appdata':_0x4e7b1f[_0x5e673d]+'?'+_0x1e53d5[_0x757b('0x14')]+_0x757b('0x37')+_0x5206c6+':'+_0x1e53d5[_0x757b('0x14')]+_0x757b('0x37')+_0x120565,'exten':_0x1e53d5[_0x757b('0x9')],'context':_0x1e53d5[_0x757b('0x14')],'priority':_0x4704c2[_0x757b('0xe')]+_0x5e673d,'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});}_0x1e53d5['priority']=_0xd9a74f['length']?_[_0x757b('0x38')](_0xd9a74f)['priority']+0x1:_0x4704c2['priority'];_0x4704c2[_0x757b('0xe')]=(_0x6fbd4[_0x757b('0x25')]?_['last'](_0x6fbd4)[_0x757b('0xe')]:_0x1e53d5[_0x757b('0xe')])+0x1;_0x4704c2[_0x757b('0x18')]=_[_0x757b('0x39')](_0x4704c2['extensions'],_0xd9a74f,[_0x1e53d5],_0x6fbd4);}_0x4704c2[_0x757b('0x18')][_0x757b('0x16')]({'type':_0x4f5250[_0x2251a8][0x0]['type'],'app':_0x757b('0x3a'),'exten':_0x4f5250[_0x2251a8][0x0]['exten'],'context':_0x4f5250[_0x2251a8][0x0]['context'],'priority':_0x4704c2[_0x757b('0xe')],'VoiceExtensionId':_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]});_0x33daea[_0x757b('0x16')](_0x4f5250[_0x2251a8][0x0][_0x757b('0x15')]);_0x2ad0c9=_[_0x757b('0x39')](_0x2ad0c9,_0x4704c2['extensions']);}}return _0x3df85f[_0x757b('0x3b')](function(_0x52719c){return _0x3df85f[_0x757b('0x2')][_0x757b('0x6')][_0x757b('0x3c')]({'where':{'VoiceExtensionId':{'$in':_0x33daea}},'transaction':_0x52719c})[_0x757b('0x8')](function(_0x5407b2){return _0x3df85f['models'][_0x757b('0x6')][_0x757b('0x3d')](_[_0x757b('0x3e')](_0x2ad0c9,'priority'),{'transaction':_0x52719c});});});})['catch'](function(_0x1768bf){console[_0x757b('0x3f')](_0x1768bf);});}exports[_0x757b('0x40')]=rewrite;