Built motion from commit ffdca78c.|2.6.24
[motion2.git] / server / api / network / network.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 _0xf507=['res_stun_monitor.so','toUpperCase','TURN','join','utf8','server/files/templates/network.ejs','/etc/asterisk/sip_xcally_%s.conf','chan_sip.so','update','params','STUN','server/files/templates/turn_multiple.ejs','/etc/asterisk/turn_xcally.conf','toLowerCase','format','path','ejs','lodash','../../components/parsers/qs','../../config/environment','jayson/promise','client','http','then','info','request\x20sent','debug','Network,\x20%s,\x20%s,\x20%s','stringify','error','message','Network,\x20%s,\x20%s','result','status','json','limit','undefined','count','offset','set','Content-Range','destroy','end','sendStatus','stack','name','send','index','map','Network','rawAttributes','fieldName','type','key','query','filters','intersection','model','fields','attributes','length','hasOwnProperty','nolimit','sort','where','pick','VIRTUAL','filter','merge','options','includeAll','include','findAll','rows','show','keys','find','catch','create','body','readFileSync','root','server/files/templates/stun_multiple.ejs','render','writeFileSync','/etc/asterisk/stun_xcally.conf','Reload'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xf507,0xbd));var _0x7f50=function(_0x827d61,_0x127d23){_0x827d61=_0x827d61-0x0;var _0x3d2398=_0xf507[_0x827d61];return _0x3d2398;};'use strict';var util=require('util');var path=require(_0x7f50('0x0'));var ejs=require(_0x7f50('0x1'));var fs=require('fs');var _=require(_0x7f50('0x2'));var qs=require(_0x7f50('0x3'));var logger=require('../../config/logger')('api');var config=require(_0x7f50('0x4'));var db=require('../../mysqldb')['db'];var jayson=require(_0x7f50('0x5'));var client=jayson[_0x7f50('0x6')][_0x7f50('0x7')]({'port':0x232a});function respondWithRpcPromise(_0x6d929f,_0x416eac,_0x266631,_0xd62dc9){return new Promise(function(_0x15a443,_0x3aca22){var _0x20c697=_0xd62dc9||client;return _0x20c697['request'](_0x6d929f,_0x266631)[_0x7f50('0x8')](function(_0x3d6efe){logger[_0x7f50('0x9')]('Network,\x20%s,\x20%s',_0x416eac,_0x7f50('0xa'));logger[_0x7f50('0xb')](_0x7f50('0xc'),_0x416eac,_0x7f50('0xa'),JSON[_0x7f50('0xd')](_0x3d6efe));if(_0x3d6efe[_0x7f50('0xe')]){if(_0x3d6efe['error']['code']===0x1f4){logger[_0x7f50('0xe')]('Network,\x20%s,\x20%s',_0x416eac,_0x3d6efe[_0x7f50('0xe')]['message']);return _0x3aca22(_0x3d6efe['error'][_0x7f50('0xf')]);}logger[_0x7f50('0xe')](_0x7f50('0x10'),_0x416eac,_0x3d6efe[_0x7f50('0xe')][_0x7f50('0xf')]);return _0x15a443(_0x3d6efe['error'][_0x7f50('0xf')]);}else{logger[_0x7f50('0x9')](_0x7f50('0x10'),_0x416eac,_0x7f50('0xa'));_0x15a443(_0x3d6efe[_0x7f50('0x11')][_0x7f50('0xf')]);}})['catch'](function(_0x557013){logger['error'](_0x7f50('0x10'),_0x416eac,_0x557013);_0x3aca22(_0x557013);});});}function respondWithResult(_0x576752,_0x5afa2b){_0x5afa2b=_0x5afa2b||0xc8;return function(_0x21407d){if(_0x21407d){return _0x576752[_0x7f50('0x12')](_0x5afa2b)[_0x7f50('0x13')](_0x21407d);}};}function respondWithFilteredResult(_0x3c7aa4,_0x803ce8){return function(_0x237a8a){if(_0x237a8a){var _0xd19962=typeof _0x803ce8['offset']==='undefined'&&typeof _0x803ce8[_0x7f50('0x14')]===_0x7f50('0x15');var _0x5dcd7c=_0x237a8a[_0x7f50('0x16')];var _0x4b62d8=_0xd19962?0x0:_0x803ce8[_0x7f50('0x17')];var _0x3617b0=_0xd19962?_0x237a8a[_0x7f50('0x16')]:_0x803ce8[_0x7f50('0x17')]+_0x803ce8['limit'];var _0x43eb51;if(_0x3617b0>=_0x5dcd7c){_0x3617b0=_0x5dcd7c;_0x43eb51=0xc8;}else{_0x43eb51=0xce;}_0x3c7aa4[_0x7f50('0x12')](_0x43eb51);return _0x3c7aa4[_0x7f50('0x18')](_0x7f50('0x19'),_0x4b62d8+'-'+_0x3617b0+'/'+_0x5dcd7c)[_0x7f50('0x13')](_0x237a8a);}return null;};}function saveUpdates(_0x521208){return function(_0x5a42d2){if(_0x5a42d2){return _0x5a42d2['update'](_0x521208)[_0x7f50('0x8')](function(_0xf0d02b){return _0xf0d02b;});}return null;};}function removeEntity(_0x223fe2){return function(_0x41887d){if(_0x41887d){return _0x41887d[_0x7f50('0x1a')]()[_0x7f50('0x8')](function(){_0x223fe2[_0x7f50('0x12')](0xcc)[_0x7f50('0x1b')]();});}};}function handleEntityNotFound(_0xe219b7){return function(_0x380b02){if(!_0x380b02){_0xe219b7[_0x7f50('0x1c')](0x194);}return _0x380b02;};}function handleError(_0x80a4ef,_0x1df061){_0x1df061=_0x1df061||0x1f4;return function(_0x1de369){logger[_0x7f50('0xe')](_0x1de369[_0x7f50('0x1d')]);if(_0x1de369[_0x7f50('0x1e')]){delete _0x1de369[_0x7f50('0x1e')];}_0x80a4ef[_0x7f50('0x12')](_0x1df061)[_0x7f50('0x1f')](_0x1de369);};}exports[_0x7f50('0x20')]=function(_0x36b64e,_0x3ae435){var _0x43051f={},_0xd41b81={},_0x2b9d81={'count':0x0,'rows':[]};var _0x31cb30=_[_0x7f50('0x21')](db[_0x7f50('0x22')][_0x7f50('0x23')],function(_0x3cb7bb){return{'name':_0x3cb7bb[_0x7f50('0x24')],'type':_0x3cb7bb[_0x7f50('0x25')][_0x7f50('0x26')]};});_0xd41b81['model']=_['map'](_0x31cb30,_0x7f50('0x1e'));_0xd41b81[_0x7f50('0x27')]=_['keys'](_0x36b64e[_0x7f50('0x27')]);_0xd41b81[_0x7f50('0x28')]=_['intersection'](_0xd41b81['model'],_0xd41b81[_0x7f50('0x27')]);_0x43051f['attributes']=_[_0x7f50('0x29')](_0xd41b81[_0x7f50('0x2a')],qs[_0x7f50('0x2b')](_0x36b64e[_0x7f50('0x27')][_0x7f50('0x2b')]));_0x43051f['attributes']=_0x43051f[_0x7f50('0x2c')][_0x7f50('0x2d')]?_0x43051f[_0x7f50('0x2c')]:_0xd41b81[_0x7f50('0x2a')];if(!_0x36b64e[_0x7f50('0x27')][_0x7f50('0x2e')](_0x7f50('0x2f'))){_0x43051f[_0x7f50('0x14')]=qs[_0x7f50('0x14')](_0x36b64e[_0x7f50('0x27')][_0x7f50('0x14')]);_0x43051f[_0x7f50('0x17')]=qs[_0x7f50('0x17')](_0x36b64e[_0x7f50('0x27')][_0x7f50('0x17')]);}_0x43051f['order']=qs[_0x7f50('0x30')](_0x36b64e[_0x7f50('0x27')][_0x7f50('0x30')]);_0x43051f[_0x7f50('0x31')]=qs[_0x7f50('0x28')](_[_0x7f50('0x32')](_0x36b64e[_0x7f50('0x27')],_0xd41b81[_0x7f50('0x28')]),_0x31cb30);if(_0x36b64e[_0x7f50('0x27')]['filter']){_0x43051f['where']=_['merge'](_0x43051f[_0x7f50('0x31')],{'$or':_[_0x7f50('0x21')](_0x31cb30,function(_0xd90d18){if(_0xd90d18[_0x7f50('0x25')]!==_0x7f50('0x33')){var _0x349d62={};_0x349d62[_0xd90d18[_0x7f50('0x1e')]]={'$like':'%'+_0x36b64e[_0x7f50('0x27')][_0x7f50('0x34')]+'%'};return _0x349d62;}})});}_0x43051f=_[_0x7f50('0x35')]({},_0x43051f,_0x36b64e[_0x7f50('0x36')]);var _0x4eca6a={'where':_0x43051f[_0x7f50('0x31')]};return db[_0x7f50('0x22')][_0x7f50('0x16')](_0x4eca6a)[_0x7f50('0x8')](function(_0x5e7576){_0x2b9d81[_0x7f50('0x16')]=_0x5e7576;if(_0x36b64e[_0x7f50('0x27')][_0x7f50('0x37')]){_0x43051f[_0x7f50('0x38')]=[{'all':!![]}];}return db['Network'][_0x7f50('0x39')](_0x43051f);})[_0x7f50('0x8')](function(_0x8b3ff7){_0x2b9d81[_0x7f50('0x3a')]=_0x8b3ff7;return _0x2b9d81;})[_0x7f50('0x8')](respondWithFilteredResult(_0x3ae435,_0x43051f))['catch'](handleError(_0x3ae435,null));};exports[_0x7f50('0x3b')]=function(_0x2c06e4,_0x6f3aa4){var _0x359555={'raw':!![],'where':{'id':_0x2c06e4['params']['id']}},_0x24daaa={};_0x24daaa[_0x7f50('0x2a')]=_[_0x7f50('0x3c')](db[_0x7f50('0x22')][_0x7f50('0x23')]);_0x24daaa[_0x7f50('0x27')]=_[_0x7f50('0x3c')](_0x2c06e4[_0x7f50('0x27')]);_0x24daaa[_0x7f50('0x28')]=_['intersection'](_0x24daaa[_0x7f50('0x2a')],_0x24daaa[_0x7f50('0x27')]);_0x359555[_0x7f50('0x2c')]=_[_0x7f50('0x29')](_0x24daaa[_0x7f50('0x2a')],qs[_0x7f50('0x2b')](_0x2c06e4[_0x7f50('0x27')]['fields']));_0x359555[_0x7f50('0x2c')]=_0x359555['attributes'][_0x7f50('0x2d')]?_0x359555[_0x7f50('0x2c')]:_0x24daaa[_0x7f50('0x2a')];if(_0x2c06e4[_0x7f50('0x27')][_0x7f50('0x37')]){_0x359555[_0x7f50('0x38')]=[{'all':!![]}];}_0x359555=_[_0x7f50('0x35')]({},_0x359555,_0x2c06e4[_0x7f50('0x36')]);return db[_0x7f50('0x22')][_0x7f50('0x3d')](_0x359555)[_0x7f50('0x8')](handleEntityNotFound(_0x6f3aa4,null))[_0x7f50('0x8')](respondWithResult(_0x6f3aa4,null))[_0x7f50('0x3e')](handleError(_0x6f3aa4,null));};exports[_0x7f50('0x3f')]=function(_0x54b288,_0x43486e){var _0x5dda74;return db['Network'][_0x7f50('0x3f')](_0x54b288[_0x7f50('0x40')],{'raw':!![]})[_0x7f50('0x8')](function(_0x2141e4){_0x5dda74=_0x2141e4;return db[_0x7f50('0x22')][_0x7f50('0x39')]({'where':{'type':_0x54b288[_0x7f50('0x40')][_0x7f50('0x25')]},'raw':!![]});})[_0x7f50('0x8')](function(_0x53eef0){if(_0x5dda74[_0x7f50('0x25')]['toUpperCase']()=='STUN'){var _0x5f0405=fs[_0x7f50('0x41')](path['join'](config[_0x7f50('0x42')],_0x7f50('0x43')),'utf8');var _0xe59a49=ejs[_0x7f50('0x44')](_0x5f0405,{'networks':_0x53eef0||[]});fs[_0x7f50('0x45')](_0x7f50('0x46'),_0xe59a49);return respondWithRpcPromise(_0x7f50('0x47'),'create',{'module':_0x7f50('0x48')});}else if(_0x5dda74[_0x7f50('0x25')][_0x7f50('0x49')]()==_0x7f50('0x4a')){var _0x58d518=fs['readFileSync'](path[_0x7f50('0x4b')](config[_0x7f50('0x42')],'server/files/templates/turn_multiple.ejs'),_0x7f50('0x4c'));var _0x397dbc=ejs[_0x7f50('0x44')](_0x58d518,{'networks':_0x53eef0||[]});fs[_0x7f50('0x45')]('/etc/asterisk/turn_xcally.conf',_0x397dbc);return respondWithRpcPromise(_0x7f50('0x47'),_0x7f50('0x3f'),{'module':_0x7f50('0x48')});}else{var _0x3a5437=fs[_0x7f50('0x41')](path[_0x7f50('0x4b')](config['root'],_0x7f50('0x4d')),_0x7f50('0x4c'));var _0x246acd=ejs[_0x7f50('0x44')](_0x3a5437,{'networks':_0x53eef0||[]});fs[_0x7f50('0x45')](util['format'](_0x7f50('0x4e'),_0x54b288[_0x7f50('0x40')][_0x7f50('0x25')]['toLowerCase']()),_0x246acd);return respondWithRpcPromise(_0x7f50('0x47'),_0x7f50('0x3f'),{'module':_0x7f50('0x4f')});}})[_0x7f50('0x8')](function(){return _0x5dda74;})[_0x7f50('0x8')](respondWithResult(_0x43486e,0xc9))[_0x7f50('0x3e')](handleError(_0x43486e,null));};exports[_0x7f50('0x50')]=function(_0x1abee7,_0x41496a){var _0xa70055;return db[_0x7f50('0x22')][_0x7f50('0x3d')]({'where':{'id':_0x1abee7[_0x7f50('0x51')]['id']}})[_0x7f50('0x8')](handleEntityNotFound(_0x41496a,null))[_0x7f50('0x8')](saveUpdates(_0x1abee7[_0x7f50('0x40')],null))['then'](function(_0x3cc4d4){if(_0x3cc4d4){_0xa70055=_0x3cc4d4;return db['Network'][_0x7f50('0x39')]({'where':{'type':_0xa70055[_0x7f50('0x25')]},'raw':!![]});}return null;})[_0x7f50('0x8')](function(_0x28ecff){if(_0xa70055['type']['toUpperCase']()==_0x7f50('0x52')){var _0x2a24b2=fs[_0x7f50('0x41')](path[_0x7f50('0x4b')](config[_0x7f50('0x42')],_0x7f50('0x43')),_0x7f50('0x4c'));var _0x1db964=ejs[_0x7f50('0x44')](_0x2a24b2,{'networks':_0x28ecff||[]});fs[_0x7f50('0x45')](_0x7f50('0x46'),_0x1db964);return respondWithRpcPromise(_0x7f50('0x47'),_0x7f50('0x50'),{'module':'res_stun_monitor.so'});}else if(_0xa70055[_0x7f50('0x25')][_0x7f50('0x49')]()=='TURN'){var _0x361b9b=fs[_0x7f50('0x41')](path[_0x7f50('0x4b')](config[_0x7f50('0x42')],_0x7f50('0x53')),'utf8');var _0x1f7eef=ejs['render'](_0x361b9b,{'networks':_0x28ecff||[]});fs[_0x7f50('0x45')](_0x7f50('0x54'),_0x1f7eef);return respondWithRpcPromise(_0x7f50('0x47'),'update',{'module':'res_stun_monitor.so'});}else{var _0x1ed09a=fs[_0x7f50('0x41')](path[_0x7f50('0x4b')](config[_0x7f50('0x42')],'server/files/templates/network.ejs'),_0x7f50('0x4c'));var _0x15a67d=ejs[_0x7f50('0x44')](_0x1ed09a,{'networks':_0x28ecff||[]});fs[_0x7f50('0x45')](util['format']('/etc/asterisk/sip_xcally_%s.conf',_0x1abee7[_0x7f50('0x40')]['type'][_0x7f50('0x55')]()),_0x15a67d);return respondWithRpcPromise(_0x7f50('0x47'),_0x7f50('0x50'),{'module':_0x7f50('0x4f')});}})[_0x7f50('0x8')](function(_0x472fa2){if(_0x472fa2){return _0xa70055;}return null;})[_0x7f50('0x8')](respondWithResult(_0x41496a,null))[_0x7f50('0x3e')](handleError(_0x41496a,null));};exports['destroy']=function(_0x304b53,_0x30ec79){var _0x1117a0;return db[_0x7f50('0x22')][_0x7f50('0x3d')]({'where':{'id':_0x304b53[_0x7f50('0x51')]['id']}})['then'](function(_0x26e80e){_0x1117a0=_0x26e80e;return _0x26e80e;})[_0x7f50('0x8')](handleEntityNotFound(_0x30ec79,null))['then'](removeEntity(_0x30ec79,null))[_0x7f50('0x8')](function(){if(_0x1117a0){return db[_0x7f50('0x22')][_0x7f50('0x39')]({'where':{'type':_0x1117a0['type']},'raw':!![]});}})[_0x7f50('0x8')](function(_0x4ba56e){if(_0x1117a0[_0x7f50('0x25')][_0x7f50('0x49')]()==_0x7f50('0x52')){var _0xb17fa5=fs[_0x7f50('0x41')](path['join'](config['root'],_0x7f50('0x43')),_0x7f50('0x4c'));var _0x1d1525=ejs[_0x7f50('0x44')](_0xb17fa5,{'networks':_0x4ba56e||[]});fs[_0x7f50('0x45')](_0x7f50('0x46'),_0x1d1525);return respondWithRpcPromise(_0x7f50('0x47'),_0x7f50('0x1a'),{'module':'res_stun_monitor.so'});}else if(_0x1117a0[_0x7f50('0x25')][_0x7f50('0x49')]()==_0x7f50('0x4a')){var _0x4c1424=fs[_0x7f50('0x41')](path['join'](config[_0x7f50('0x42')],_0x7f50('0x53')),_0x7f50('0x4c'));var _0xc6f6d9=ejs[_0x7f50('0x44')](_0x4c1424,{'networks':_0x4ba56e||[]});fs[_0x7f50('0x45')](_0x7f50('0x54'),_0xc6f6d9);return respondWithRpcPromise(_0x7f50('0x47'),'destroy',{'module':_0x7f50('0x48')});}else{var _0x24ddd7=fs[_0x7f50('0x41')](path[_0x7f50('0x4b')](config[_0x7f50('0x42')],_0x7f50('0x4d')),_0x7f50('0x4c'));var _0x37fcb1=ejs[_0x7f50('0x44')](_0x24ddd7,{'networks':_0x4ba56e||[]});fs['writeFileSync'](util[_0x7f50('0x56')](_0x7f50('0x4e'),_0x1117a0[_0x7f50('0x25')][_0x7f50('0x55')]()),_0x37fcb1);return respondWithRpcPromise(_0x7f50('0x47'),_0x7f50('0x1a'),{'module':_0x7f50('0x4f')});}})[_0x7f50('0x3e')](handleError(_0x30ec79,null));};