e741a0c7033c84e1fc293504c0dc85a893ba6a5b
[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 _0xad7d=['request','then','info','Network,\x20%s,\x20%s','request\x20sent','debug','Network,\x20%s,\x20%s,\x20%s','error','message','result','catch','status','json','offset','undefined','count','limit','set','Content-Range','update','destroy','end','sendStatus','name','send','index','map','Network','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','intersection','fields','length','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','include','findAll','rows','show','params','includeAll','find','create','body','toUpperCase','STUN','readFileSync','join','server/files/templates/stun_multiple.ejs','utf8','render','writeFileSync','/etc/asterisk/stun_xcally.conf','Reload','res_stun_monitor.so','TURN','root','server/files/templates/turn_multiple.ejs','/etc/asterisk/turn_xcally.conf','server/files/templates/network.ejs','format','/etc/asterisk/sip_xcally_%s.conf','toLowerCase','chan_sip.so','util','path','ejs','lodash','../../components/parsers/qs','../../config/logger','api','../../config/environment','../../mysqldb','jayson/promise','client','http'];(function(_0x1e65f5,_0x53d9f8){var _0x437ccd=function(_0x126b1f){while(--_0x126b1f){_0x1e65f5['push'](_0x1e65f5['shift']());}};_0x437ccd(++_0x53d9f8);}(_0xad7d,0x1b6));var _0xdad7=function(_0xdbc4af,_0x4e2eb2){_0xdbc4af=_0xdbc4af-0x0;var _0x196ead=_0xad7d[_0xdbc4af];return _0x196ead;};'use strict';var util=require(_0xdad7('0x0'));var path=require(_0xdad7('0x1'));var ejs=require(_0xdad7('0x2'));var fs=require('fs');var _=require(_0xdad7('0x3'));var qs=require(_0xdad7('0x4'));var logger=require(_0xdad7('0x5'))(_0xdad7('0x6'));var config=require(_0xdad7('0x7'));var db=require(_0xdad7('0x8'))['db'];var jayson=require(_0xdad7('0x9'));var client=jayson[_0xdad7('0xa')][_0xdad7('0xb')]({'port':0x232a});function respondWithRpcPromise(_0x12f0d9,_0x4bcb96,_0x28c8c8,_0x391b10){return new Promise(function(_0x35abab,_0x245cfc){var _0x28a9a3=_0x391b10||client;return _0x28a9a3[_0xdad7('0xc')](_0x12f0d9,_0x28c8c8)[_0xdad7('0xd')](function(_0x46a9ad){logger[_0xdad7('0xe')](_0xdad7('0xf'),_0x4bcb96,_0xdad7('0x10'));logger[_0xdad7('0x11')](_0xdad7('0x12'),_0x4bcb96,_0xdad7('0x10'),JSON['stringify'](_0x46a9ad));if(_0x46a9ad[_0xdad7('0x13')]){if(_0x46a9ad['error']['code']===0x1f4){logger['error'](_0xdad7('0xf'),_0x4bcb96,_0x46a9ad[_0xdad7('0x13')]['message']);return _0x245cfc(_0x46a9ad['error'][_0xdad7('0x14')]);}logger['error'](_0xdad7('0xf'),_0x4bcb96,_0x46a9ad[_0xdad7('0x13')][_0xdad7('0x14')]);return _0x35abab(_0x46a9ad[_0xdad7('0x13')][_0xdad7('0x14')]);}else{logger['info'](_0xdad7('0xf'),_0x4bcb96,'request\x20sent');_0x35abab(_0x46a9ad[_0xdad7('0x15')][_0xdad7('0x14')]);}})[_0xdad7('0x16')](function(_0x350fc8){logger[_0xdad7('0x13')](_0xdad7('0xf'),_0x4bcb96,_0x350fc8);_0x245cfc(_0x350fc8);});});}function respondWithResult(_0x54236c,_0x345966){_0x345966=_0x345966||0xc8;return function(_0x125c15){if(_0x125c15){return _0x54236c[_0xdad7('0x17')](_0x345966)[_0xdad7('0x18')](_0x125c15);}};}function respondWithFilteredResult(_0x5a892b,_0x5e3364){return function(_0x3809d3){if(_0x3809d3){var _0x5d5576=typeof _0x5e3364[_0xdad7('0x19')]===_0xdad7('0x1a')&&typeof _0x5e3364['limit']===_0xdad7('0x1a');var _0x313da6=_0x3809d3[_0xdad7('0x1b')];var _0x1b3fd2=_0x5d5576?0x0:_0x5e3364[_0xdad7('0x19')];var _0x3acdc1=_0x5d5576?_0x3809d3[_0xdad7('0x1b')]:_0x5e3364[_0xdad7('0x19')]+_0x5e3364[_0xdad7('0x1c')];var _0x27381a;if(_0x3acdc1>=_0x313da6){_0x3acdc1=_0x313da6;_0x27381a=0xc8;}else{_0x27381a=0xce;}_0x5a892b[_0xdad7('0x17')](_0x27381a);return _0x5a892b[_0xdad7('0x1d')](_0xdad7('0x1e'),_0x1b3fd2+'-'+_0x3acdc1+'/'+_0x313da6)['json'](_0x3809d3);}return null;};}function saveUpdates(_0x200878){return function(_0x8457bd){if(_0x8457bd){return _0x8457bd[_0xdad7('0x1f')](_0x200878)[_0xdad7('0xd')](function(_0x5bb83f){return _0x5bb83f;});}return null;};}function removeEntity(_0x4bb6da){return function(_0x187ee0){if(_0x187ee0){return _0x187ee0[_0xdad7('0x20')]()[_0xdad7('0xd')](function(){_0x4bb6da['status'](0xcc)[_0xdad7('0x21')]();});}};}function handleEntityNotFound(_0x364b46){return function(_0x34edfc){if(!_0x34edfc){_0x364b46[_0xdad7('0x22')](0x194);}return _0x34edfc;};}function handleError(_0x46c845,_0xb22734){_0xb22734=_0xb22734||0x1f4;return function(_0x42ffa5){logger[_0xdad7('0x13')](_0x42ffa5['stack']);if(_0x42ffa5[_0xdad7('0x23')]){delete _0x42ffa5[_0xdad7('0x23')];}_0x46c845['status'](_0xb22734)[_0xdad7('0x24')](_0x42ffa5);};}exports[_0xdad7('0x25')]=function(_0x431c2b,_0x1353fb){var _0x2e28fe={},_0x3e4e41={},_0x12367c={'count':0x0,'rows':[]};var _0x122e74=_[_0xdad7('0x26')](db[_0xdad7('0x27')][_0xdad7('0x28')],function(_0x55fc2e){return{'name':_0x55fc2e[_0xdad7('0x29')],'type':_0x55fc2e[_0xdad7('0x2a')][_0xdad7('0x2b')]};});_0x3e4e41[_0xdad7('0x2c')]=_[_0xdad7('0x26')](_0x122e74,'name');_0x3e4e41[_0xdad7('0x2d')]=_[_0xdad7('0x2e')](_0x431c2b[_0xdad7('0x2d')]);_0x3e4e41[_0xdad7('0x2f')]=_['intersection'](_0x3e4e41[_0xdad7('0x2c')],_0x3e4e41[_0xdad7('0x2d')]);_0x2e28fe[_0xdad7('0x30')]=_[_0xdad7('0x31')](_0x3e4e41[_0xdad7('0x2c')],qs[_0xdad7('0x32')](_0x431c2b[_0xdad7('0x2d')][_0xdad7('0x32')]));_0x2e28fe[_0xdad7('0x30')]=_0x2e28fe[_0xdad7('0x30')][_0xdad7('0x33')]?_0x2e28fe['attributes']:_0x3e4e41['model'];if(!_0x431c2b[_0xdad7('0x2d')]['hasOwnProperty'](_0xdad7('0x34'))){_0x2e28fe['limit']=qs[_0xdad7('0x1c')](_0x431c2b[_0xdad7('0x2d')]['limit']);_0x2e28fe[_0xdad7('0x19')]=qs[_0xdad7('0x19')](_0x431c2b[_0xdad7('0x2d')]['offset']);}_0x2e28fe[_0xdad7('0x35')]=qs[_0xdad7('0x36')](_0x431c2b['query']['sort']);_0x2e28fe[_0xdad7('0x37')]=qs[_0xdad7('0x2f')](_[_0xdad7('0x38')](_0x431c2b[_0xdad7('0x2d')],_0x3e4e41[_0xdad7('0x2f')]),_0x122e74);if(_0x431c2b[_0xdad7('0x2d')][_0xdad7('0x39')]){_0x2e28fe[_0xdad7('0x37')]=_[_0xdad7('0x3a')](_0x2e28fe[_0xdad7('0x37')],{'$or':_['map'](_0x122e74,function(_0x5d2231){if(_0x5d2231['type']!==_0xdad7('0x3b')){var _0x521156={};_0x521156[_0x5d2231[_0xdad7('0x23')]]={'$like':'%'+_0x431c2b[_0xdad7('0x2d')][_0xdad7('0x39')]+'%'};return _0x521156;}})});}_0x2e28fe=_['merge']({},_0x2e28fe,_0x431c2b[_0xdad7('0x3c')]);var _0x2c54d3={'where':_0x2e28fe['where']};return db[_0xdad7('0x27')]['count'](_0x2c54d3)['then'](function(_0x51c1b9){_0x12367c[_0xdad7('0x1b')]=_0x51c1b9;if(_0x431c2b['query']['includeAll']){_0x2e28fe[_0xdad7('0x3d')]=[{'all':!![]}];}return db[_0xdad7('0x27')][_0xdad7('0x3e')](_0x2e28fe);})[_0xdad7('0xd')](function(_0x1688dd){_0x12367c[_0xdad7('0x3f')]=_0x1688dd;return _0x12367c;})[_0xdad7('0xd')](respondWithFilteredResult(_0x1353fb,_0x2e28fe))['catch'](handleError(_0x1353fb,null));};exports[_0xdad7('0x40')]=function(_0x3c7bec,_0x49c11b){var _0x5baff0={'raw':!![],'where':{'id':_0x3c7bec[_0xdad7('0x41')]['id']}},_0x5cc01c={};_0x5cc01c['model']=_[_0xdad7('0x2e')](db[_0xdad7('0x27')][_0xdad7('0x28')]);_0x5cc01c[_0xdad7('0x2d')]=_[_0xdad7('0x2e')](_0x3c7bec['query']);_0x5cc01c[_0xdad7('0x2f')]=_[_0xdad7('0x31')](_0x5cc01c[_0xdad7('0x2c')],_0x5cc01c[_0xdad7('0x2d')]);_0x5baff0['attributes']=_[_0xdad7('0x31')](_0x5cc01c[_0xdad7('0x2c')],qs[_0xdad7('0x32')](_0x3c7bec[_0xdad7('0x2d')][_0xdad7('0x32')]));_0x5baff0[_0xdad7('0x30')]=_0x5baff0[_0xdad7('0x30')][_0xdad7('0x33')]?_0x5baff0[_0xdad7('0x30')]:_0x5cc01c[_0xdad7('0x2c')];if(_0x3c7bec['query'][_0xdad7('0x42')]){_0x5baff0[_0xdad7('0x3d')]=[{'all':!![]}];}_0x5baff0=_[_0xdad7('0x3a')]({},_0x5baff0,_0x3c7bec[_0xdad7('0x3c')]);return db[_0xdad7('0x27')][_0xdad7('0x43')](_0x5baff0)[_0xdad7('0xd')](handleEntityNotFound(_0x49c11b,null))['then'](respondWithResult(_0x49c11b,null))['catch'](handleError(_0x49c11b,null));};exports['create']=function(_0x4ba826,_0x5902c1){var _0x457bc4;return db[_0xdad7('0x27')][_0xdad7('0x44')](_0x4ba826['body'],{'raw':!![]})[_0xdad7('0xd')](function(_0x784965){_0x457bc4=_0x784965;return db[_0xdad7('0x27')][_0xdad7('0x3e')]({'where':{'type':_0x4ba826[_0xdad7('0x45')]['type']},'raw':!![]});})['then'](function(_0x114fa1){if(_0x457bc4[_0xdad7('0x2a')][_0xdad7('0x46')]()==_0xdad7('0x47')){var _0x1487db=fs[_0xdad7('0x48')](path[_0xdad7('0x49')](config['root'],_0xdad7('0x4a')),_0xdad7('0x4b'));var _0x1dffe3=ejs[_0xdad7('0x4c')](_0x1487db,{'networks':_0x114fa1||[]});fs[_0xdad7('0x4d')](_0xdad7('0x4e'),_0x1dffe3);return respondWithRpcPromise(_0xdad7('0x4f'),_0xdad7('0x44'),{'module':_0xdad7('0x50')});}else if(_0x457bc4[_0xdad7('0x2a')]['toUpperCase']()==_0xdad7('0x51')){var _0x5727e1=fs[_0xdad7('0x48')](path[_0xdad7('0x49')](config[_0xdad7('0x52')],_0xdad7('0x53')),_0xdad7('0x4b'));var _0x48292d=ejs[_0xdad7('0x4c')](_0x5727e1,{'networks':_0x114fa1||[]});fs['writeFileSync'](_0xdad7('0x54'),_0x48292d);return respondWithRpcPromise('Reload',_0xdad7('0x44'),{'module':_0xdad7('0x50')});}else{var _0x2249ad=fs[_0xdad7('0x48')](path[_0xdad7('0x49')](config[_0xdad7('0x52')],_0xdad7('0x55')),_0xdad7('0x4b'));var _0x9531c3=ejs[_0xdad7('0x4c')](_0x2249ad,{'networks':_0x114fa1||[]});fs[_0xdad7('0x4d')](util[_0xdad7('0x56')](_0xdad7('0x57'),_0x4ba826['body'][_0xdad7('0x2a')][_0xdad7('0x58')]()),_0x9531c3);return respondWithRpcPromise(_0xdad7('0x4f'),_0xdad7('0x44'),{'module':_0xdad7('0x59')});}})['then'](function(){return _0x457bc4;})[_0xdad7('0xd')](respondWithResult(_0x5902c1,0xc9))[_0xdad7('0x16')](handleError(_0x5902c1,null));};exports[_0xdad7('0x1f')]=function(_0x49d9fe,_0x410a1c){var _0x1dedf0;return db[_0xdad7('0x27')][_0xdad7('0x43')]({'where':{'id':_0x49d9fe[_0xdad7('0x41')]['id']}})[_0xdad7('0xd')](handleEntityNotFound(_0x410a1c,null))[_0xdad7('0xd')](saveUpdates(_0x49d9fe[_0xdad7('0x45')],null))[_0xdad7('0xd')](function(_0x3b2803){if(_0x3b2803){_0x1dedf0=_0x3b2803;return db[_0xdad7('0x27')][_0xdad7('0x3e')]({'where':{'type':_0x1dedf0['type']},'raw':!![]});}return null;})[_0xdad7('0xd')](function(_0x5454c0){if(_0x1dedf0[_0xdad7('0x2a')][_0xdad7('0x46')]()==_0xdad7('0x47')){var _0x30e1a7=fs[_0xdad7('0x48')](path['join'](config[_0xdad7('0x52')],'server/files/templates/stun_multiple.ejs'),_0xdad7('0x4b'));var _0x115209=ejs[_0xdad7('0x4c')](_0x30e1a7,{'networks':_0x5454c0||[]});fs[_0xdad7('0x4d')](_0xdad7('0x4e'),_0x115209);return respondWithRpcPromise(_0xdad7('0x4f'),_0xdad7('0x1f'),{'module':'res_stun_monitor.so'});}else if(_0x1dedf0[_0xdad7('0x2a')][_0xdad7('0x46')]()==_0xdad7('0x51')){var _0x48726c=fs[_0xdad7('0x48')](path[_0xdad7('0x49')](config[_0xdad7('0x52')],'server/files/templates/turn_multiple.ejs'),_0xdad7('0x4b'));var _0x5d7589=ejs[_0xdad7('0x4c')](_0x48726c,{'networks':_0x5454c0||[]});fs['writeFileSync']('/etc/asterisk/turn_xcally.conf',_0x5d7589);return respondWithRpcPromise('Reload',_0xdad7('0x1f'),{'module':_0xdad7('0x50')});}else{var _0x51587b=fs[_0xdad7('0x48')](path[_0xdad7('0x49')](config['root'],_0xdad7('0x55')),_0xdad7('0x4b'));var _0x27cea4=ejs[_0xdad7('0x4c')](_0x51587b,{'networks':_0x5454c0||[]});fs['writeFileSync'](util[_0xdad7('0x56')](_0xdad7('0x57'),_0x49d9fe[_0xdad7('0x45')][_0xdad7('0x2a')][_0xdad7('0x58')]()),_0x27cea4);return respondWithRpcPromise(_0xdad7('0x4f'),_0xdad7('0x1f'),{'module':_0xdad7('0x59')});}})[_0xdad7('0xd')](function(_0x20641a){if(_0x20641a){return _0x1dedf0;}return null;})['then'](respondWithResult(_0x410a1c,null))['catch'](handleError(_0x410a1c,null));};exports[_0xdad7('0x20')]=function(_0x5a26f3,_0x55baf8){var _0xaf7f5d;return db[_0xdad7('0x27')][_0xdad7('0x43')]({'where':{'id':_0x5a26f3[_0xdad7('0x41')]['id']}})[_0xdad7('0xd')](function(_0x36172a){_0xaf7f5d=_0x36172a;return _0x36172a;})['then'](handleEntityNotFound(_0x55baf8,null))[_0xdad7('0xd')](removeEntity(_0x55baf8,null))[_0xdad7('0xd')](function(){if(_0xaf7f5d){return db[_0xdad7('0x27')][_0xdad7('0x3e')]({'where':{'type':_0xaf7f5d[_0xdad7('0x2a')]},'raw':!![]});}})[_0xdad7('0xd')](function(_0x24526b){if(_0xaf7f5d[_0xdad7('0x2a')][_0xdad7('0x46')]()=='STUN'){var _0x2022a1=fs[_0xdad7('0x48')](path[_0xdad7('0x49')](config[_0xdad7('0x52')],_0xdad7('0x4a')),'utf8');var _0x1c2946=ejs[_0xdad7('0x4c')](_0x2022a1,{'networks':_0x24526b||[]});fs[_0xdad7('0x4d')](_0xdad7('0x4e'),_0x1c2946);return respondWithRpcPromise(_0xdad7('0x4f'),_0xdad7('0x20'),{'module':'res_stun_monitor.so'});}else if(_0xaf7f5d[_0xdad7('0x2a')][_0xdad7('0x46')]()==_0xdad7('0x51')){var _0x5242a3=fs[_0xdad7('0x48')](path['join'](config[_0xdad7('0x52')],_0xdad7('0x53')),_0xdad7('0x4b'));var _0x299ca9=ejs[_0xdad7('0x4c')](_0x5242a3,{'networks':_0x24526b||[]});fs[_0xdad7('0x4d')](_0xdad7('0x54'),_0x299ca9);return respondWithRpcPromise(_0xdad7('0x4f'),_0xdad7('0x20'),{'module':'res_stun_monitor.so'});}else{var _0x5615ec=fs['readFileSync'](path[_0xdad7('0x49')](config[_0xdad7('0x52')],_0xdad7('0x55')),_0xdad7('0x4b'));var _0xb7d07=ejs['render'](_0x5615ec,{'networks':_0x24526b||[]});fs['writeFileSync'](util[_0xdad7('0x56')]('/etc/asterisk/sip_xcally_%s.conf',_0xaf7f5d['type'][_0xdad7('0x58')]()),_0xb7d07);return respondWithRpcPromise(_0xdad7('0x4f'),'destroy',{'module':'chan_sip.so'});}})[_0xdad7('0x16')](handleError(_0x55baf8,null));};