0aa883bbae1db4c546c06ce4ddf5c24c252fe101
[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 _0x5617=['appType','custom','answer','$[${XCALLY-MOTION-ANSWER}=0]?Answer','$[${XCALLY-MOTION-PROGRESS}=0]?Progress','XCALLY-MOTION-PROGRESS=1','splice',',${EXTEN},','last','Hangup','destroy','bulkCreate','error','rewrite','lodash','util','models','Interval','findAll','intervals','VoiceExtension','IntervalId','uniq','map','then','filter','groupBy','exten','playback','hasOwnProperty','priority','extension','type','CDR(type)=%s','context','inbound','extensions','Set','CDR(routeid)=','VoiceExtensionId','outbound','push','CDR(destination)=','cutdigits','${EXTEN:','${EXTEN}','$[${LEN(${CDR(prefix)})}\x20>\x200]?Goto(','):Goto(','CDR(prefix)=noprefix','recordingFormat','_XCALLY_MIXMONITOR_UNIQUEID=${UNIQUEID}','wav','XCALLY-MOTION-PROGRESS=0','toLowerCase','updatedAt','interval','*,*,*,*','tag','CDR(tag)=','length','callerID','ExecIf','$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):NoOp(No\x20number\x20available)','indexOf','app'];(function(_0x1cec22,_0x34766e){var _0x547137=function(_0x3bcf6b){while(--_0x3bcf6b){_0x1cec22['push'](_0x1cec22['shift']());}};_0x547137(++_0x34766e);}(_0x5617,0x194));var _0x7561=function(_0x48401f,_0x2457e2){_0x48401f=_0x48401f-0x0;var _0x5dc63f=_0x5617[_0x48401f];return _0x5dc63f;};'use strict';var _=require(_0x7561('0x0'));var util=require(_0x7561('0x1'));function rewrite(_0x1c03f3,_0x3cd253){var _0x444f39=this;return _0x1c03f3[_0x7561('0x2')][_0x7561('0x3')][_0x7561('0x4')]({'raw':!![]})['then'](function(_0x49f7cd){_0x444f39[_0x7561('0x5')]=_0x49f7cd;return _0x1c03f3[_0x7561('0x2')][_0x7561('0x6')][_0x7561('0x4')]({'where':{'IntervalId':_0x3cd253[_0x7561('0x7')]},'raw':!![]});})['then'](function(_0x2f2bac){return _0x1c03f3['models']['VoiceExtension'][_0x7561('0x4')]({'where':{'exten':{'$in':_[_0x7561('0x8')](_[_0x7561('0x9')](_0x2f2bac,'exten'))}},'order':[{'raw':'CAST(priority\x20AS\x20UNSIGNED)'}],'raw':!![]});})[_0x7561('0xa')](function(_0x5f3c86){var _0x567f26=_[_0x7561('0xb')](_0x5f3c86,{'priority':'1'});var _0x31ea04=_[_0x7561('0xb')](_0x5f3c86,{'isApp':0x1});var _0x5c803a=_[_0x7561('0xc')](_0x31ea04,_0x7561('0xd'));var _0x3507cb=['queue',_0x7561('0xe')];var _0x23a8e0=[];var _0xe446b7=[];for(var _0x4886bf in _0x5c803a){if(_0x5c803a[_0x7561('0xf')](_0x4886bf)){_0x444f39[_0x7561('0x10')]=0x2;_0x444f39['extensions']=[];_0x444f39[_0x7561('0x11')]=_['find'](_0x567f26,{'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')]});_0x444f39['extensions']['push']({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':'Set','appdata':util['format'](_0x7561('0x13'),_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')]),'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39['priority']++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0]['VoiceExtensionId']});if(_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')]===_0x7561('0x15')){_0x444f39[_0x7561('0x16')]['push']({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':_0x7561('0x18')+_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')],'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});}else if(_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')]===_0x7561('0x1a')){if(_0x444f39[_0x7561('0x11')]){_0x444f39[_0x7561('0x16')][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':_0x7561('0x1c')+(_0x444f39[_0x7561('0x11')][_0x7561('0x1d')]?_0x7561('0x1e')+_0x444f39[_0x7561('0x11')][_0x7561('0x1d')]+'}':_0x7561('0x1f')),'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});}_0x444f39[_0x7561('0x16')][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':'Set','appdata':_0x7561('0x18')+_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')],'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0]['VoiceExtensionId']});var _0x9c108a=_0x444f39[_0x7561('0x10')]++;_0x444f39[_0x7561('0x16')][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':'ExecIf','appdata':_0x7561('0x20')+(_0x9c108a+0x2)+_0x7561('0x21')+(_0x9c108a+0x1)+')','context':_0x5c803a[_0x4886bf][0x0]['context'],'exten':_0x5c803a[_0x4886bf][0x0]['exten'],'priority':_0x9c108a,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0]['VoiceExtensionId']});if(_0x444f39[_0x7561('0x11')]){_0x444f39['extensions']['push']({'type':_0x5c803a[_0x4886bf][0x0]['type'],'app':'Set','appdata':_0x444f39[_0x7561('0x11')][_0x7561('0x1d')]?'CDR(prefix)=${EXTEN:0:'+_0x444f39[_0x7561('0x11')][_0x7561('0x1d')]+'}':_0x7561('0x22'),'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});}_0x444f39[_0x7561('0x16')][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':'outboundrouteid='+_0x5c803a[_0x4886bf][0x0]['VoiceExtensionId'],'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});if(_0x444f39['extension']&&_0x444f39[_0x7561('0x11')][_0x7561('0xf')](_0x7561('0x23'))&&_0x444f39[_0x7561('0x11')][_0x7561('0x23')]!=='none'){_0x444f39['extensions'][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':_0x7561('0x24'),'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0]['VoiceExtensionId']});_0x444f39['extensions'][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0]['type'],'app':_0x7561('0x17'),'appdata':'_XCALLY_MIXMONITOR_FORMAT='+_0x444f39[_0x7561('0x11')][_0x7561('0x23')]||_0x7561('0x25'),'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});}}_0x444f39[_0x7561('0x16')]['push']({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':'XCALLY-MOTION-ANSWER=0','context':_0x5c803a[_0x4886bf][0x0]['context'],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});_0x444f39[_0x7561('0x16')]['push']({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':_0x7561('0x26'),'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39[_0x7561('0x10')]++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0]['VoiceExtensionId']});_0x444f39['extensions'][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':'MONITOR_OPTIONS=ab','context':_0x5c803a[_0x4886bf][0x0]['context'],'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'priority':_0x444f39['priority']++,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});for(var _0x1b475a=0x0,_0x42bef1=0x0;_0x1b475a<_0x5c803a[_0x4886bf]['length'];_0x1b475a+=0x1,_0x42bef1=0x0){var _0x25064b=_0x5c803a[_0x4886bf][_0x1b475a];if(_0x25064b[_0x7561('0x12')]){_0x25064b[_0x7561('0x12')]=_0x25064b[_0x7561('0x12')][_0x7561('0x27')]();}var _0x48d492=[];var _0x3ebecf=[];delete _0x25064b['id'];delete _0x25064b['createdAt'];delete _0x25064b[_0x7561('0x28')];var _0x2179b0=_0x25064b[_0x7561('0x29')]!==_0x7561('0x2a')?[_0x25064b[_0x7561('0x29')]]:_0x25064b[_0x7561('0x7')]?_[_0x7561('0x9')](_[_0x7561('0xb')](_0x444f39[_0x7561('0x5')],{'IntervalId':_0x25064b[_0x7561('0x7')]}),_0x7561('0x29')):[];if(_0x25064b[_0x7561('0xf')](_0x7561('0x2b'))){if(_0x25064b['tag']&&_0x25064b[_0x7561('0x2b')]!=='--'){_0x48d492['push']({'type':_0x25064b[_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':_0x7561('0x2c')+_0x25064b[_0x7561('0x2b')],'context':_0x25064b[_0x7561('0x14')],'exten':_0x25064b[_0x7561('0xd')],'priority':_0x2179b0[_0x7561('0x2d')]+_0x444f39[_0x7561('0x10')]+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});}}if(_0x25064b[_0x7561('0x12')]==='outbound'&&_0x25064b['hasOwnProperty'](_0x7561('0x2e'))){if(_0x25064b['callerID']){_0x48d492[_0x7561('0x1b')]({'type':_0x25064b[_0x7561('0x12')],'app':_0x7561('0x2f'),'appdata':'$[${LEN(${PREFIX-CALLERIDALL})}\x20>\x200]?Set(CALLERID(all)=${PREFIX-CALLERIDALL}):Set(CALLERID(all)='+_0x25064b[_0x7561('0x2e')]+')','context':_0x25064b[_0x7561('0x14')],'exten':_0x25064b[_0x7561('0xd')],'priority':_0x2179b0[_0x7561('0x2d')]+_0x444f39[_0x7561('0x10')]+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});}else{_0x48d492['push']({'type':_0x25064b[_0x7561('0x12')],'app':'ExecIf','appdata':_0x7561('0x30'),'context':_0x25064b['context'],'exten':_0x25064b['exten'],'priority':_0x2179b0['length']+_0x444f39[_0x7561('0x10')]+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});}}if(_0x25064b['hasOwnProperty']('answer')){if(_0x3507cb[_0x7561('0x31')](_0x25064b[_0x7561('0x32')][_0x7561('0x27')]())>=0x0&&_0x25064b[_0x7561('0x33')]!==_0x7561('0x34')){if(_0x25064b[_0x7561('0x35')]){_0x48d492[_0x7561('0x1b')]({'type':_0x25064b['type'],'app':_0x7561('0x2f'),'appdata':_0x7561('0x36'),'context':_0x25064b[_0x7561('0x14')],'exten':_0x25064b['exten'],'priority':_0x2179b0[_0x7561('0x2d')]+_0x444f39['priority']+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});_0x48d492['push']({'type':_0x25064b[_0x7561('0x12')],'app':'Set','appdata':'XCALLY-MOTION-ANSWER=1','context':_0x25064b[_0x7561('0x14')],'exten':_0x25064b[_0x7561('0xd')],'priority':_0x2179b0[_0x7561('0x2d')]+_0x444f39['priority']+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});}else{_0x48d492[_0x7561('0x1b')]({'type':_0x25064b[_0x7561('0x12')],'app':_0x7561('0x2f'),'appdata':_0x7561('0x37'),'context':_0x25064b[_0x7561('0x14')],'exten':_0x25064b[_0x7561('0xd')],'priority':_0x2179b0['length']+_0x444f39[_0x7561('0x10')]+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});_0x48d492[_0x7561('0x1b')]({'type':_0x25064b[_0x7561('0x12')],'app':_0x7561('0x17'),'appdata':_0x7561('0x38'),'context':_0x25064b[_0x7561('0x14')],'exten':_0x25064b['exten'],'priority':_0x2179b0[_0x7561('0x2d')]+_0x444f39['priority']+_0x42bef1++,'VoiceExtensionId':_0x25064b[_0x7561('0x19')]});}}}for(var _0x37abd4=0x0;_0x37abd4<_0x2179b0[_0x7561('0x2d')];_0x37abd4+=0x1){var _0xb40945=_0x444f39[_0x7561('0x10')]+_0x2179b0['length'];var _0x32bf6b=_0x37abd4===_0x2179b0[_0x7561('0x2d')]-0x1?_0x444f39['priority']+_0x48d492[_0x7561('0x2d')]+_0x3ebecf[_0x7561('0x2d')]+0x2:_0x444f39[_0x7561('0x10')]+_0x37abd4+0x1;_0x48d492[_0x7561('0x39')](_0x37abd4,0x0,{'type':_0x25064b['type'],'app':'GotoIfTime','appdata':_0x2179b0[_0x37abd4]+'?'+_0x25064b['context']+_0x7561('0x3a')+_0xb40945+':'+_0x25064b[_0x7561('0x14')]+_0x7561('0x3a')+_0x32bf6b,'exten':_0x25064b[_0x7561('0xd')],'context':_0x25064b[_0x7561('0x14')],'priority':_0x444f39['priority']+_0x37abd4,'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});}_0x25064b[_0x7561('0x10')]=_0x48d492[_0x7561('0x2d')]?_[_0x7561('0x3b')](_0x48d492)[_0x7561('0x10')]+0x1:_0x444f39[_0x7561('0x10')];_0x444f39[_0x7561('0x10')]=(_0x3ebecf[_0x7561('0x2d')]?_['last'](_0x3ebecf)[_0x7561('0x10')]:_0x25064b[_0x7561('0x10')])+0x1;_0x444f39[_0x7561('0x16')]=_['concat'](_0x444f39[_0x7561('0x16')],_0x48d492,[_0x25064b],_0x3ebecf);}_0x444f39[_0x7561('0x16')][_0x7561('0x1b')]({'type':_0x5c803a[_0x4886bf][0x0][_0x7561('0x12')],'app':_0x7561('0x3c'),'exten':_0x5c803a[_0x4886bf][0x0][_0x7561('0xd')],'context':_0x5c803a[_0x4886bf][0x0][_0x7561('0x14')],'priority':_0x444f39[_0x7561('0x10')],'VoiceExtensionId':_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]});_0x23a8e0['push'](_0x5c803a[_0x4886bf][0x0][_0x7561('0x19')]);_0xe446b7=_['concat'](_0xe446b7,_0x444f39[_0x7561('0x16')]);}}return _0x1c03f3['transaction'](function(_0x2e5497){return _0x1c03f3[_0x7561('0x2')]['VoiceExtension'][_0x7561('0x3d')]({'where':{'VoiceExtensionId':{'$in':_0x23a8e0}},'transaction':_0x2e5497})[_0x7561('0xa')](function(_0x2328b8){return _0x1c03f3[_0x7561('0x2')][_0x7561('0x6')][_0x7561('0x3e')](_['sortBy'](_0xe446b7,_0x7561('0x10')),{'transaction':_0x2e5497});});});})['catch'](function(_0x42de15){console[_0x7561('0x3f')](_0x42de15);});}exports[_0x7561('0x40')]=rewrite;