Built motion from commit c0cdc9d8.|2.6.28
[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 _0x37f2=['util','../../components/parsers/qs','../../config/logger','api','jayson/promise','request','then','info','Network,\x20%s,\x20%s','Network,\x20%s,\x20%s,\x20%s','error','code','message','request\x20sent','result','catch','status','json','undefined','count','offset','limit','set','update','destroy','end','stack','name','send','index','Network','rawAttributes','fieldName','type','key','model','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','filter','where','merge','map','VIRTUAL','options','rows','show','includeAll','include','create','body','findAll','toUpperCase','STUN','readFileSync','root','utf8','writeFileSync','/etc/asterisk/stun_xcally.conf','Reload','res_stun_monitor.so','TURN','server/files/templates/turn_multiple.ejs','render','/etc/asterisk/turn_xcally.conf','join','server/files/templates/network.ejs','format','/etc/asterisk/sip_xcally_%s.conf','toLowerCase','chan_sip.so','find','params','server/files/templates/stun_multiple.ejs'];(function(_0x725b8e,_0xc22b48){var _0x43ac45=function(_0x46bb4a){while(--_0x46bb4a){_0x725b8e['push'](_0x725b8e['shift']());}};_0x43ac45(++_0xc22b48);}(_0x37f2,0x1ec));var _0x237f=function(_0xb48347,_0x2a3fd9){_0xb48347=_0xb48347-0x0;var _0x1c1a99=_0x37f2[_0xb48347];return _0x1c1a99;};'use strict';var util=require(_0x237f('0x0'));var path=require('path');var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var qs=require(_0x237f('0x1'));var logger=require(_0x237f('0x2'))(_0x237f('0x3'));var config=require('../../config/environment');var db=require('../../mysqldb')['db'];var jayson=require(_0x237f('0x4'));var client=jayson['client']['http']({'port':0x232a});function respondWithRpcPromise(_0x1b9011,_0x322879,_0x119069,_0x12f9e9){return new Promise(function(_0x42e0c2,_0x270f75){var _0x2614be=_0x12f9e9||client;return _0x2614be[_0x237f('0x5')](_0x1b9011,_0x119069)[_0x237f('0x6')](function(_0x2a6482){logger[_0x237f('0x7')](_0x237f('0x8'),_0x322879,'request\x20sent');logger['debug'](_0x237f('0x9'),_0x322879,'request\x20sent',JSON['stringify'](_0x2a6482));if(_0x2a6482['error']){if(_0x2a6482[_0x237f('0xa')][_0x237f('0xb')]===0x1f4){logger[_0x237f('0xa')]('Network,\x20%s,\x20%s',_0x322879,_0x2a6482[_0x237f('0xa')][_0x237f('0xc')]);return _0x270f75(_0x2a6482['error']['message']);}logger['error'](_0x237f('0x8'),_0x322879,_0x2a6482[_0x237f('0xa')][_0x237f('0xc')]);return _0x42e0c2(_0x2a6482[_0x237f('0xa')][_0x237f('0xc')]);}else{logger[_0x237f('0x7')](_0x237f('0x8'),_0x322879,_0x237f('0xd'));_0x42e0c2(_0x2a6482[_0x237f('0xe')][_0x237f('0xc')]);}})[_0x237f('0xf')](function(_0x49f920){logger[_0x237f('0xa')](_0x237f('0x8'),_0x322879,_0x49f920);_0x270f75(_0x49f920);});});}function respondWithResult(_0x2a11e0,_0x7af897){_0x7af897=_0x7af897||0xc8;return function(_0x4f59bd){if(_0x4f59bd){return _0x2a11e0[_0x237f('0x10')](_0x7af897)[_0x237f('0x11')](_0x4f59bd);}};}function respondWithFilteredResult(_0x5db588,_0x20c62c){return function(_0x232b17){if(_0x232b17){var _0x400c56=typeof _0x20c62c['offset']===_0x237f('0x12')&&typeof _0x20c62c['limit']===_0x237f('0x12');var _0x19b70c=_0x232b17[_0x237f('0x13')];var _0x4110b0=_0x400c56?0x0:_0x20c62c[_0x237f('0x14')];var _0x3b7903=_0x400c56?_0x232b17[_0x237f('0x13')]:_0x20c62c['offset']+_0x20c62c[_0x237f('0x15')];var _0x3660b8;if(_0x3b7903>=_0x19b70c){_0x3b7903=_0x19b70c;_0x3660b8=0xc8;}else{_0x3660b8=0xce;}_0x5db588[_0x237f('0x10')](_0x3660b8);return _0x5db588[_0x237f('0x16')]('Content-Range',_0x4110b0+'-'+_0x3b7903+'/'+_0x19b70c)[_0x237f('0x11')](_0x232b17);}return null;};}function saveUpdates(_0x388556){return function(_0x5f4d4e){if(_0x5f4d4e){return _0x5f4d4e[_0x237f('0x17')](_0x388556)[_0x237f('0x6')](function(_0x2727ba){return _0x2727ba;});}return null;};}function removeEntity(_0x224bf9){return function(_0x1ff058){if(_0x1ff058){return _0x1ff058[_0x237f('0x18')]()[_0x237f('0x6')](function(){_0x224bf9[_0x237f('0x10')](0xcc)[_0x237f('0x19')]();});}};}function handleEntityNotFound(_0x5bc3c8){return function(_0x2decae){if(!_0x2decae){_0x5bc3c8['sendStatus'](0x194);}return _0x2decae;};}function handleError(_0x71f4c1,_0x46e9d2){_0x46e9d2=_0x46e9d2||0x1f4;return function(_0xcb963a){logger[_0x237f('0xa')](_0xcb963a[_0x237f('0x1a')]);if(_0xcb963a[_0x237f('0x1b')]){delete _0xcb963a[_0x237f('0x1b')];}_0x71f4c1['status'](_0x46e9d2)[_0x237f('0x1c')](_0xcb963a);};}exports[_0x237f('0x1d')]=function(_0xf7f8b2,_0x1366aa){var _0x4fe715={},_0x5360cd={},_0x23834b={'count':0x0,'rows':[]};var _0x3eb4e4=_['map'](db[_0x237f('0x1e')][_0x237f('0x1f')],function(_0x2c5860){return{'name':_0x2c5860[_0x237f('0x20')],'type':_0x2c5860[_0x237f('0x21')][_0x237f('0x22')]};});_0x5360cd[_0x237f('0x23')]=_['map'](_0x3eb4e4,_0x237f('0x1b'));_0x5360cd[_0x237f('0x24')]=_[_0x237f('0x25')](_0xf7f8b2[_0x237f('0x24')]);_0x5360cd[_0x237f('0x26')]=_[_0x237f('0x27')](_0x5360cd[_0x237f('0x23')],_0x5360cd[_0x237f('0x24')]);_0x4fe715['attributes']=_[_0x237f('0x27')](_0x5360cd[_0x237f('0x23')],qs[_0x237f('0x28')](_0xf7f8b2[_0x237f('0x24')][_0x237f('0x28')]));_0x4fe715[_0x237f('0x29')]=_0x4fe715[_0x237f('0x29')][_0x237f('0x2a')]?_0x4fe715['attributes']:_0x5360cd[_0x237f('0x23')];if(!_0xf7f8b2['query'][_0x237f('0x2b')](_0x237f('0x2c'))){_0x4fe715['limit']=qs[_0x237f('0x15')](_0xf7f8b2[_0x237f('0x24')]['limit']);_0x4fe715[_0x237f('0x14')]=qs[_0x237f('0x14')](_0xf7f8b2[_0x237f('0x24')]['offset']);}_0x4fe715[_0x237f('0x2d')]=qs[_0x237f('0x2e')](_0xf7f8b2['query'][_0x237f('0x2e')]);_0x4fe715['where']=qs[_0x237f('0x26')](_['pick'](_0xf7f8b2[_0x237f('0x24')],_0x5360cd[_0x237f('0x26')]),_0x3eb4e4);if(_0xf7f8b2[_0x237f('0x24')][_0x237f('0x2f')]){_0x4fe715[_0x237f('0x30')]=_[_0x237f('0x31')](_0x4fe715['where'],{'$or':_[_0x237f('0x32')](_0x3eb4e4,function(_0x4eb11e){if(_0x4eb11e[_0x237f('0x21')]!==_0x237f('0x33')){var _0x551ae5={};_0x551ae5[_0x4eb11e[_0x237f('0x1b')]]={'$like':'%'+_0xf7f8b2[_0x237f('0x24')][_0x237f('0x2f')]+'%'};return _0x551ae5;}})});}_0x4fe715=_[_0x237f('0x31')]({},_0x4fe715,_0xf7f8b2[_0x237f('0x34')]);var _0x369ad9={'where':_0x4fe715[_0x237f('0x30')]};return db['Network'][_0x237f('0x13')](_0x369ad9)[_0x237f('0x6')](function(_0x33eba4){_0x23834b[_0x237f('0x13')]=_0x33eba4;if(_0xf7f8b2[_0x237f('0x24')]['includeAll']){_0x4fe715['include']=[{'all':!![]}];}return db['Network']['findAll'](_0x4fe715);})[_0x237f('0x6')](function(_0x162625){_0x23834b[_0x237f('0x35')]=_0x162625;return _0x23834b;})[_0x237f('0x6')](respondWithFilteredResult(_0x1366aa,_0x4fe715))[_0x237f('0xf')](handleError(_0x1366aa,null));};exports[_0x237f('0x36')]=function(_0x436509,_0x6a6c7a){var _0x5b6134={'raw':!![],'where':{'id':_0x436509['params']['id']}},_0x47ade3={};_0x47ade3['model']=_[_0x237f('0x25')](db[_0x237f('0x1e')][_0x237f('0x1f')]);_0x47ade3[_0x237f('0x24')]=_[_0x237f('0x25')](_0x436509[_0x237f('0x24')]);_0x47ade3[_0x237f('0x26')]=_[_0x237f('0x27')](_0x47ade3[_0x237f('0x23')],_0x47ade3[_0x237f('0x24')]);_0x5b6134[_0x237f('0x29')]=_[_0x237f('0x27')](_0x47ade3['model'],qs['fields'](_0x436509[_0x237f('0x24')][_0x237f('0x28')]));_0x5b6134[_0x237f('0x29')]=_0x5b6134['attributes'][_0x237f('0x2a')]?_0x5b6134['attributes']:_0x47ade3[_0x237f('0x23')];if(_0x436509[_0x237f('0x24')][_0x237f('0x37')]){_0x5b6134[_0x237f('0x38')]=[{'all':!![]}];}_0x5b6134=_[_0x237f('0x31')]({},_0x5b6134,_0x436509[_0x237f('0x34')]);return db[_0x237f('0x1e')]['find'](_0x5b6134)['then'](handleEntityNotFound(_0x6a6c7a,null))['then'](respondWithResult(_0x6a6c7a,null))[_0x237f('0xf')](handleError(_0x6a6c7a,null));};exports['create']=function(_0x3bc3de,_0x30cc12){var _0x30469c;return db[_0x237f('0x1e')][_0x237f('0x39')](_0x3bc3de[_0x237f('0x3a')],{'raw':!![]})[_0x237f('0x6')](function(_0x1919a2){_0x30469c=_0x1919a2;return db['Network'][_0x237f('0x3b')]({'where':{'type':_0x3bc3de[_0x237f('0x3a')]['type']},'raw':!![]});})[_0x237f('0x6')](function(_0x513448){if(_0x30469c[_0x237f('0x21')][_0x237f('0x3c')]()==_0x237f('0x3d')){var _0x5f07f7=fs[_0x237f('0x3e')](path['join'](config[_0x237f('0x3f')],'server/files/templates/stun_multiple.ejs'),_0x237f('0x40'));var _0x49afd6=ejs['render'](_0x5f07f7,{'networks':_0x513448||[]});fs[_0x237f('0x41')](_0x237f('0x42'),_0x49afd6);return respondWithRpcPromise(_0x237f('0x43'),_0x237f('0x39'),{'module':_0x237f('0x44')});}else if(_0x30469c['type'][_0x237f('0x3c')]()==_0x237f('0x45')){var _0x36f328=fs[_0x237f('0x3e')](path['join'](config[_0x237f('0x3f')],_0x237f('0x46')),_0x237f('0x40'));var _0x2015d2=ejs[_0x237f('0x47')](_0x36f328,{'networks':_0x513448||[]});fs['writeFileSync'](_0x237f('0x48'),_0x2015d2);return respondWithRpcPromise(_0x237f('0x43'),_0x237f('0x39'),{'module':_0x237f('0x44')});}else{var _0xbc7ce9=fs[_0x237f('0x3e')](path[_0x237f('0x49')](config[_0x237f('0x3f')],_0x237f('0x4a')),_0x237f('0x40'));var _0x55944b=ejs[_0x237f('0x47')](_0xbc7ce9,{'networks':_0x513448||[]});fs[_0x237f('0x41')](util[_0x237f('0x4b')](_0x237f('0x4c'),_0x3bc3de[_0x237f('0x3a')][_0x237f('0x21')][_0x237f('0x4d')]()),_0x55944b);return respondWithRpcPromise('Reload',_0x237f('0x39'),{'module':_0x237f('0x4e')});}})['then'](function(){return _0x30469c;})[_0x237f('0x6')](respondWithResult(_0x30cc12,0xc9))[_0x237f('0xf')](handleError(_0x30cc12,null));};exports[_0x237f('0x17')]=function(_0x59b454,_0x2ce5ce){var _0x13f063;return db[_0x237f('0x1e')][_0x237f('0x4f')]({'where':{'id':_0x59b454[_0x237f('0x50')]['id']}})[_0x237f('0x6')](handleEntityNotFound(_0x2ce5ce,null))[_0x237f('0x6')](saveUpdates(_0x59b454['body'],null))[_0x237f('0x6')](function(_0x10ce11){if(_0x10ce11){_0x13f063=_0x10ce11;return db['Network'][_0x237f('0x3b')]({'where':{'type':_0x13f063[_0x237f('0x21')]},'raw':!![]});}return null;})[_0x237f('0x6')](function(_0x45ea6f){if(_0x13f063[_0x237f('0x21')][_0x237f('0x3c')]()==_0x237f('0x3d')){var _0x3e22d2=fs[_0x237f('0x3e')](path[_0x237f('0x49')](config[_0x237f('0x3f')],_0x237f('0x51')),'utf8');var _0x1a0ccf=ejs['render'](_0x3e22d2,{'networks':_0x45ea6f||[]});fs['writeFileSync'](_0x237f('0x42'),_0x1a0ccf);return respondWithRpcPromise(_0x237f('0x43'),_0x237f('0x17'),{'module':_0x237f('0x44')});}else if(_0x13f063['type'][_0x237f('0x3c')]()==_0x237f('0x45')){var _0x3f2fdf=fs['readFileSync'](path['join'](config[_0x237f('0x3f')],_0x237f('0x46')),_0x237f('0x40'));var _0x2a3304=ejs['render'](_0x3f2fdf,{'networks':_0x45ea6f||[]});fs[_0x237f('0x41')](_0x237f('0x48'),_0x2a3304);return respondWithRpcPromise(_0x237f('0x43'),_0x237f('0x17'),{'module':'res_stun_monitor.so'});}else{var _0x223e05=fs[_0x237f('0x3e')](path['join'](config['root'],_0x237f('0x4a')),_0x237f('0x40'));var _0x31bc73=ejs[_0x237f('0x47')](_0x223e05,{'networks':_0x45ea6f||[]});fs[_0x237f('0x41')](util[_0x237f('0x4b')](_0x237f('0x4c'),_0x59b454[_0x237f('0x3a')][_0x237f('0x21')][_0x237f('0x4d')]()),_0x31bc73);return respondWithRpcPromise('Reload',_0x237f('0x17'),{'module':_0x237f('0x4e')});}})[_0x237f('0x6')](function(_0x37989a){if(_0x37989a){return _0x13f063;}return null;})['then'](respondWithResult(_0x2ce5ce,null))[_0x237f('0xf')](handleError(_0x2ce5ce,null));};exports[_0x237f('0x18')]=function(_0x2817f4,_0x125214){var _0x2a3196;return db[_0x237f('0x1e')][_0x237f('0x4f')]({'where':{'id':_0x2817f4['params']['id']}})[_0x237f('0x6')](function(_0x115d0c){_0x2a3196=_0x115d0c;return _0x115d0c;})[_0x237f('0x6')](handleEntityNotFound(_0x125214,null))[_0x237f('0x6')](removeEntity(_0x125214,null))[_0x237f('0x6')](function(){if(_0x2a3196){return db[_0x237f('0x1e')][_0x237f('0x3b')]({'where':{'type':_0x2a3196[_0x237f('0x21')]},'raw':!![]});}})[_0x237f('0x6')](function(_0x24a540){if(_0x2a3196[_0x237f('0x21')]['toUpperCase']()=='STUN'){var _0x1efadd=fs[_0x237f('0x3e')](path[_0x237f('0x49')](config[_0x237f('0x3f')],'server/files/templates/stun_multiple.ejs'),_0x237f('0x40'));var _0x1475e6=ejs[_0x237f('0x47')](_0x1efadd,{'networks':_0x24a540||[]});fs['writeFileSync']('/etc/asterisk/stun_xcally.conf',_0x1475e6);return respondWithRpcPromise('Reload',_0x237f('0x18'),{'module':'res_stun_monitor.so'});}else if(_0x2a3196[_0x237f('0x21')][_0x237f('0x3c')]()==_0x237f('0x45')){var _0x5affba=fs[_0x237f('0x3e')](path[_0x237f('0x49')](config['root'],_0x237f('0x46')),_0x237f('0x40'));var _0x99a48c=ejs['render'](_0x5affba,{'networks':_0x24a540||[]});fs[_0x237f('0x41')](_0x237f('0x48'),_0x99a48c);return respondWithRpcPromise(_0x237f('0x43'),_0x237f('0x18'),{'module':'res_stun_monitor.so'});}else{var _0x151bdd=fs[_0x237f('0x3e')](path[_0x237f('0x49')](config['root'],'server/files/templates/network.ejs'),'utf8');var _0x1ef6c6=ejs[_0x237f('0x47')](_0x151bdd,{'networks':_0x24a540||[]});fs[_0x237f('0x41')](util['format'](_0x237f('0x4c'),_0x2a3196['type'][_0x237f('0x4d')]()),_0x1ef6c6);return respondWithRpcPromise(_0x237f('0x43'),_0x237f('0x18'),{'module':_0x237f('0x4e')});}})[_0x237f('0xf')](handleError(_0x125214,null));};