ce04196464c68b55816887cde10cd09ace7d4292
[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 _0xf5f5=['findAll','rows','include','find','create','transaction','body','readFileSync','join','writeFileSync','/etc/asterisk/stun_xcally.conf','root','server/files/templates/turn_multiple.ejs','utf8','/etc/asterisk/turn_xcally.conf','res_stun_monitor.so','server/files/templates/network.ejs','chan_sip.so','update','sequelize','params','toUpperCase','server/files/templates/stun_multiple.ejs','Reload','TURN','render','/etc/asterisk/sip_xcally_%s.conf','STUN','destroy','format','toLowerCase','eml-format','rimraf','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','to-csv','lodash','squel','crypto','jsforce','querystring','ioredis','../../components/parsers/advancedSearch','api','../../config/utils','../../config/environment','jayson/promise','client','http','test','Sequelize','ValidationError','info','debug','request\x20sent','stringify','error','code','Network,\x20%s,\x20%s','message','catch','status','end','count','offset','limit','set','Content-Range','json','apply','save','then','sendStatus','stack','send','index','rawAttributes','fieldName','type','key','name','query','keys','filters','model','attributes','intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','map','VIRTUAL','options','includeAll','Network'];(function(_0x5d5bde,_0x3d4be0){var _0x38a9d3=function(_0x5c3cc5){while(--_0x5c3cc5){_0x5d5bde['push'](_0x5d5bde['shift']());}};_0x38a9d3(++_0x3d4be0);}(_0xf5f5,0xf5));var _0x5f5f=function(_0x529eee,_0x311ed0){_0x529eee=_0x529eee-0x0;var _0x5edb8a=_0xf5f5[_0x529eee];return _0x5edb8a;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x5f5f('0x0'));var rimraf=require(_0x5f5f('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x5f5f('0x2'));var rp=require(_0x5f5f('0x3'));var moment=require('moment');var BPromise=require(_0x5f5f('0x4'));var Mustache=require(_0x5f5f('0x5'));var util=require(_0x5f5f('0x6'));var path=require(_0x5f5f('0x7'));var sox=require(_0x5f5f('0x8'));var csv=require(_0x5f5f('0x9'));var ejs=require('ejs');var fs=require('fs');var _=require(_0x5f5f('0xa'));var squel=require(_0x5f5f('0xb'));var crypto=require(_0x5f5f('0xc'));var jsforce=require(_0x5f5f('0xd'));var deskjs=require('desk.js');var toCsv=require(_0x5f5f('0x9'));var querystring=require(_0x5f5f('0xe'));var Papa=require('papaparse');var Redis=require(_0x5f5f('0xf'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x5f5f('0x10'));var hardwareService=require('../../config/license/hardware');var logger=require('../../config/logger')(_0x5f5f('0x11'));var utils=require(_0x5f5f('0x12'));var config=require(_0x5f5f('0x13'));var db=require('../../mysqldb')['db'];var jayson=require(_0x5f5f('0x14'));var client=jayson[_0x5f5f('0x15')][_0x5f5f('0x16')]({'port':0x232a});function checkPasswordPattern(_0x1abf9d,_0x10ffd7,_0x248773){if(_0x248773){var _0x1ed2f2=new RegExp(/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[?!@#\$%\^&\*~\-_=+[{\]\}])(?=.{8,})/);if(_0x1ed2f2[_0x5f5f('0x17')](_0x10ffd7)){return _0x1abf9d;}else{throw new db[(_0x5f5f('0x18'))][(_0x5f5f('0x19'))]('The\x20password\x20must\x20be\x20at\x20least\x208\x20characters\x20long\x20and\x20have\x201\x20lowercase\x20character,\x201\x20uppercase\x20character,\x201\x20number\x20and\x201\x20special\x20character\x20~!@#$%^&-_=+[{]}.');}}else{return _0x1abf9d;}}function respondWithRpcPromise(_0x4048ee,_0x5d12a9,_0x46cef4,_0x26e093){return new BPromise(function(_0x1ccd83,_0x422727){var _0x2a2403=_0x26e093||client;return _0x2a2403['request'](_0x4048ee,_0x46cef4)['then'](function(_0x5bbfd3){logger[_0x5f5f('0x1a')]('Network,\x20%s,\x20%s',_0x5d12a9,'request\x20sent');logger[_0x5f5f('0x1b')]('Network,\x20%s,\x20%s,\x20%s',_0x5d12a9,_0x5f5f('0x1c'),JSON[_0x5f5f('0x1d')](_0x5bbfd3));if(_0x5bbfd3['error']){if(_0x5bbfd3[_0x5f5f('0x1e')][_0x5f5f('0x1f')]===0x1f4){logger[_0x5f5f('0x1e')](_0x5f5f('0x20'),_0x5d12a9,_0x5bbfd3[_0x5f5f('0x1e')]['message']);return _0x422727(_0x5bbfd3[_0x5f5f('0x1e')]['message']);}logger[_0x5f5f('0x1e')](_0x5f5f('0x20'),_0x5d12a9,_0x5bbfd3[_0x5f5f('0x1e')]['message']);return _0x1ccd83(_0x5bbfd3['error'][_0x5f5f('0x21')]);}else{logger[_0x5f5f('0x1a')](_0x5f5f('0x20'),_0x5d12a9,'request\x20sent');_0x1ccd83(_0x5bbfd3['result']['message']);}})[_0x5f5f('0x22')](function(_0x384386){logger[_0x5f5f('0x1e')](_0x5f5f('0x20'),_0x5d12a9,_0x384386);_0x422727(_0x384386);});});}function respondWithStatusCode(_0x30ca36,_0x51f6b9){_0x51f6b9=_0x51f6b9||0xcc;return function(_0x2c5f0c){if(_0x2c5f0c){return _0x30ca36['sendStatus'](_0x51f6b9);}return _0x30ca36[_0x5f5f('0x23')](_0x51f6b9)[_0x5f5f('0x24')]();};}function respondWithResult(_0x3c5f37,_0x3c1b01){_0x3c1b01=_0x3c1b01||0xc8;return function(_0x22925f){if(_0x22925f){return _0x3c5f37[_0x5f5f('0x23')](_0x3c1b01)['json'](_0x22925f);}};}function respondWithFilteredResult(_0x5ece53,_0x4253e2){return function(_0x447120){if(_0x447120){var _0x93300d=_0x447120[_0x5f5f('0x25')],_0x131e47=_0x4253e2[_0x5f5f('0x26')],_0x30c6ee=_0x4253e2['offset']+_0x4253e2[_0x5f5f('0x27')],_0x28b6f8;if(_0x30c6ee>=_0x93300d){_0x30c6ee=_0x93300d;_0x28b6f8=0xc8;}else{_0x28b6f8=0xce;}_0x5ece53['status'](_0x28b6f8);return _0x5ece53[_0x5f5f('0x28')](_0x5f5f('0x29'),_0x131e47+'-'+_0x30c6ee+'/'+_0x93300d)[_0x5f5f('0x2a')](_0x447120);}return null;};}function patchUpdates(_0x48e049){return function(_0x179f8a){try{jsonpatch[_0x5f5f('0x2b')](_0x179f8a,_0x48e049,!![]);}catch(_0x321ce3){return BPromise['reject'](_0x321ce3);}return _0x179f8a[_0x5f5f('0x2c')]();};}function saveUpdates(_0x5b172b,_0x410131){return function(_0x481f4e){if(_0x481f4e){return _0x481f4e['update'](_0x5b172b)[_0x5f5f('0x2d')](function(_0x1bb8f5){return _0x1bb8f5;});}return null;};}function removeEntity(_0x27ad30,_0x3c8abe){return function(_0x23ff76){if(_0x23ff76){return _0x23ff76['destroy']()[_0x5f5f('0x2d')](function(){_0x27ad30['status'](0xcc)[_0x5f5f('0x24')]();});}};}function handleEntityNotFound(_0x209f77,_0x50762d){return function(_0x364a96){if(!_0x364a96){_0x209f77[_0x5f5f('0x2e')](0x194);}return _0x364a96;};}function handleError(_0x43fa96,_0x41dd1b){_0x41dd1b=_0x41dd1b||0x1f4;return function(_0x96d76c){logger[_0x5f5f('0x1e')](_0x96d76c[_0x5f5f('0x2f')]);if(_0x96d76c['name']){delete _0x96d76c['name'];}_0x43fa96[_0x5f5f('0x23')](_0x41dd1b)[_0x5f5f('0x30')](_0x96d76c);};}exports[_0x5f5f('0x31')]=function(_0x29c98c,_0x1bf8e3){var _0x237ea7={},_0x163d5f={},_0x4b0a83={'count':0x0,'rows':[]};var _0x5ca21c=_['map'](db['Network'][_0x5f5f('0x32')],function(_0x7d463b){return{'name':_0x7d463b[_0x5f5f('0x33')],'type':_0x7d463b[_0x5f5f('0x34')][_0x5f5f('0x35')]};});_0x163d5f['model']=_['map'](_0x5ca21c,_0x5f5f('0x36'));_0x163d5f[_0x5f5f('0x37')]=_[_0x5f5f('0x38')](_0x29c98c['query']);_0x163d5f[_0x5f5f('0x39')]=_['intersection'](_0x163d5f[_0x5f5f('0x3a')],_0x163d5f[_0x5f5f('0x37')]);_0x237ea7[_0x5f5f('0x3b')]=_[_0x5f5f('0x3c')](_0x163d5f[_0x5f5f('0x3a')],qs[_0x5f5f('0x3d')](_0x29c98c[_0x5f5f('0x37')][_0x5f5f('0x3d')]));_0x237ea7['attributes']=_0x237ea7[_0x5f5f('0x3b')][_0x5f5f('0x3e')]?_0x237ea7[_0x5f5f('0x3b')]:_0x163d5f[_0x5f5f('0x3a')];if(!_0x29c98c[_0x5f5f('0x37')][_0x5f5f('0x3f')](_0x5f5f('0x40'))){_0x237ea7['limit']=qs[_0x5f5f('0x27')](_0x29c98c[_0x5f5f('0x37')]['limit']);_0x237ea7[_0x5f5f('0x26')]=qs[_0x5f5f('0x26')](_0x29c98c[_0x5f5f('0x37')][_0x5f5f('0x26')]);}_0x237ea7[_0x5f5f('0x41')]=qs[_0x5f5f('0x42')](_0x29c98c[_0x5f5f('0x37')][_0x5f5f('0x42')]);_0x237ea7[_0x5f5f('0x43')]=qs[_0x5f5f('0x39')](_[_0x5f5f('0x44')](_0x29c98c['query'],_0x163d5f[_0x5f5f('0x39')]),_0x5ca21c);if(_0x29c98c[_0x5f5f('0x37')][_0x5f5f('0x45')]){_0x237ea7[_0x5f5f('0x43')]=_[_0x5f5f('0x46')](_0x237ea7['where'],{'$or':_[_0x5f5f('0x47')](_0x5ca21c,function(_0x3b15e9){if(_0x3b15e9[_0x5f5f('0x34')]!==_0x5f5f('0x48')){var _0x5bb77b={};_0x5bb77b[_0x3b15e9['name']]={'$like':'%'+_0x29c98c[_0x5f5f('0x37')][_0x5f5f('0x45')]+'%'};return _0x5bb77b;}})});}_0x237ea7=_[_0x5f5f('0x46')]({},_0x237ea7,_0x29c98c[_0x5f5f('0x49')]);var _0x157757={'where':_0x237ea7[_0x5f5f('0x43')]};return db['Network'][_0x5f5f('0x25')](_0x157757)[_0x5f5f('0x2d')](function(_0x1ce03d){_0x4b0a83['count']=_0x1ce03d;if(_0x29c98c['query'][_0x5f5f('0x4a')]){_0x237ea7['include']=[{'all':!![]}];}return db[_0x5f5f('0x4b')][_0x5f5f('0x4c')](_0x237ea7);})[_0x5f5f('0x2d')](function(_0xa56bd3){_0x4b0a83[_0x5f5f('0x4d')]=_0xa56bd3;return _0x4b0a83;})[_0x5f5f('0x2d')](respondWithFilteredResult(_0x1bf8e3,_0x237ea7))[_0x5f5f('0x22')](handleError(_0x1bf8e3,null));};exports['show']=function(_0x9d0d89,_0x1b1046){var _0x3e5d80={'raw':!![],'where':{'id':_0x9d0d89['params']['id']}},_0x50ae00={};_0x50ae00[_0x5f5f('0x3a')]=_['keys'](db[_0x5f5f('0x4b')][_0x5f5f('0x32')]);_0x50ae00[_0x5f5f('0x37')]=_[_0x5f5f('0x38')](_0x9d0d89[_0x5f5f('0x37')]);_0x50ae00['filters']=_[_0x5f5f('0x3c')](_0x50ae00['model'],_0x50ae00['query']);_0x3e5d80[_0x5f5f('0x3b')]=_[_0x5f5f('0x3c')](_0x50ae00['model'],qs['fields'](_0x9d0d89['query'][_0x5f5f('0x3d')]));_0x3e5d80[_0x5f5f('0x3b')]=_0x3e5d80[_0x5f5f('0x3b')][_0x5f5f('0x3e')]?_0x3e5d80[_0x5f5f('0x3b')]:_0x50ae00[_0x5f5f('0x3a')];if(_0x9d0d89[_0x5f5f('0x37')][_0x5f5f('0x4a')]){_0x3e5d80[_0x5f5f('0x4e')]=[{'all':!![]}];}_0x3e5d80=_[_0x5f5f('0x46')]({},_0x3e5d80,_0x9d0d89[_0x5f5f('0x49')]);return db[_0x5f5f('0x4b')][_0x5f5f('0x4f')](_0x3e5d80)['then'](handleEntityNotFound(_0x1b1046,null))[_0x5f5f('0x2d')](respondWithResult(_0x1b1046,null))['catch'](handleError(_0x1b1046,null));};exports[_0x5f5f('0x50')]=function(_0x4f86f2,_0x5d5983,_0x2e07cf){var _0x316e24;return db['sequelize'][_0x5f5f('0x51')](function(_0x4817c5){return db['Network'][_0x5f5f('0x50')](_0x4f86f2[_0x5f5f('0x52')],{'raw':!![]})['then'](function(_0x158496){_0x316e24=_0x158496;return db[_0x5f5f('0x4b')][_0x5f5f('0x4c')]({'where':{'type':_0x4f86f2[_0x5f5f('0x52')][_0x5f5f('0x34')]},'raw':!![]});})[_0x5f5f('0x2d')](function(_0x3995d1){if(_0x316e24[_0x5f5f('0x34')]['toUpperCase']()=='STUN'){var _0x31feaa=fs[_0x5f5f('0x53')](path[_0x5f5f('0x54')](config['root'],'server/files/templates/stun_multiple.ejs'),'utf8');var _0x503f30=ejs['render'](_0x31feaa,{'networks':_0x3995d1||[]});fs[_0x5f5f('0x55')](_0x5f5f('0x56'),_0x503f30);return respondWithRpcPromise('Reload',_0x5f5f('0x50'),{'module':'res_stun_monitor.so'});}else if(_0x316e24['type']['toUpperCase']()=='TURN'){var _0x228f57=fs['readFileSync'](path[_0x5f5f('0x54')](config[_0x5f5f('0x57')],_0x5f5f('0x58')),_0x5f5f('0x59'));var _0x45e46e=ejs['render'](_0x228f57,{'networks':_0x3995d1||[]});fs[_0x5f5f('0x55')](_0x5f5f('0x5a'),_0x45e46e);return respondWithRpcPromise('Reload',_0x5f5f('0x50'),{'module':_0x5f5f('0x5b')});}else{var _0x4937bf=fs[_0x5f5f('0x53')](path[_0x5f5f('0x54')](config[_0x5f5f('0x57')],_0x5f5f('0x5c')),_0x5f5f('0x59'));var _0x2bc820=ejs['render'](_0x4937bf,{'networks':_0x3995d1||[]});fs['writeFileSync'](util['format']('/etc/asterisk/sip_xcally_%s.conf',_0x4f86f2[_0x5f5f('0x52')][_0x5f5f('0x34')]['toLowerCase']()),_0x2bc820);return respondWithRpcPromise('Reload','create',{'module':_0x5f5f('0x5d')});}return null;})[_0x5f5f('0x2d')](function(){return _0x316e24;});})[_0x5f5f('0x2d')](respondWithResult(_0x5d5983,0xc9))['catch'](handleError(_0x5d5983,null));};exports[_0x5f5f('0x5e')]=function(_0x20f2cc,_0x26f7fd,_0x13e869){var _0x206f19;return db[_0x5f5f('0x5f')][_0x5f5f('0x51')](function(_0x24bbb6){return db[_0x5f5f('0x4b')][_0x5f5f('0x4f')]({'where':{'id':_0x20f2cc[_0x5f5f('0x60')]['id']}})['then'](handleEntityNotFound(_0x26f7fd,null))[_0x5f5f('0x2d')](saveUpdates(_0x20f2cc[_0x5f5f('0x52')],null))[_0x5f5f('0x2d')](function(_0x322369){if(_0x322369){_0x206f19=_0x322369;return db[_0x5f5f('0x4b')]['findAll']({'where':{'type':_0x206f19[_0x5f5f('0x34')]},'raw':!![]});}return null;})[_0x5f5f('0x2d')](function(_0x42a6bd){if(_0x206f19[_0x5f5f('0x34')][_0x5f5f('0x61')]()=='STUN'){var _0x5c7748=fs[_0x5f5f('0x53')](path['join'](config[_0x5f5f('0x57')],_0x5f5f('0x62')),_0x5f5f('0x59'));var _0x16c6ff=ejs['render'](_0x5c7748,{'networks':_0x42a6bd||[]});fs[_0x5f5f('0x55')](_0x5f5f('0x56'),_0x16c6ff);return respondWithRpcPromise(_0x5f5f('0x63'),_0x5f5f('0x5e'),{'module':_0x5f5f('0x5b')});}else if(_0x206f19['type'][_0x5f5f('0x61')]()==_0x5f5f('0x64')){var _0x48364e=fs[_0x5f5f('0x53')](path[_0x5f5f('0x54')](config[_0x5f5f('0x57')],_0x5f5f('0x58')),'utf8');var _0x1b042e=ejs[_0x5f5f('0x65')](_0x48364e,{'networks':_0x42a6bd||[]});fs['writeFileSync'](_0x5f5f('0x5a'),_0x1b042e);return respondWithRpcPromise('Reload',_0x5f5f('0x5e'),{'module':_0x5f5f('0x5b')});}else{var _0x452764=fs[_0x5f5f('0x53')](path['join'](config[_0x5f5f('0x57')],_0x5f5f('0x5c')),_0x5f5f('0x59'));var _0x549a44=ejs[_0x5f5f('0x65')](_0x452764,{'networks':_0x42a6bd||[]});fs[_0x5f5f('0x55')](util['format'](_0x5f5f('0x66'),_0x20f2cc[_0x5f5f('0x52')]['type']['toLowerCase']()),_0x549a44);return respondWithRpcPromise(_0x5f5f('0x63'),_0x5f5f('0x5e'),{'module':_0x5f5f('0x5d')});}return null;})['then'](function(_0x45b56e){if(_0x45b56e){return _0x206f19;}return null;});})['then'](respondWithResult(_0x26f7fd,null))['catch'](handleError(_0x26f7fd,null));};exports['destroy']=function(_0x6b0b3a,_0x53f3d2,_0xf91bd5){var _0x532c98;return db[_0x5f5f('0x4b')][_0x5f5f('0x4f')]({'where':{'id':_0x6b0b3a['params']['id']}})['then'](function(_0x4318f8){_0x532c98=_0x4318f8;return _0x4318f8;})[_0x5f5f('0x2d')](handleEntityNotFound(_0x53f3d2,null))['then'](removeEntity(_0x53f3d2,null))[_0x5f5f('0x2d')](function(){if(_0x532c98){return db[_0x5f5f('0x4b')][_0x5f5f('0x4c')]({'where':{'type':_0x532c98[_0x5f5f('0x34')]},'raw':!![]});}})[_0x5f5f('0x2d')](function(_0xb2839d){if(_0x532c98[_0x5f5f('0x34')][_0x5f5f('0x61')]()==_0x5f5f('0x67')){var _0x160d25=fs[_0x5f5f('0x53')](path['join'](config[_0x5f5f('0x57')],_0x5f5f('0x62')),_0x5f5f('0x59'));var _0x440890=ejs['render'](_0x160d25,{'networks':_0xb2839d||[]});fs[_0x5f5f('0x55')](_0x5f5f('0x56'),_0x440890);return respondWithRpcPromise('Reload',_0x5f5f('0x68'),{'module':'res_stun_monitor.so'});}else if(_0x532c98[_0x5f5f('0x34')][_0x5f5f('0x61')]()==_0x5f5f('0x64')){var _0x25671c=fs[_0x5f5f('0x53')](path['join'](config[_0x5f5f('0x57')],_0x5f5f('0x58')),'utf8');var _0x1946a4=ejs[_0x5f5f('0x65')](_0x25671c,{'networks':_0xb2839d||[]});fs[_0x5f5f('0x55')]('/etc/asterisk/turn_xcally.conf',_0x1946a4);return respondWithRpcPromise(_0x5f5f('0x63'),_0x5f5f('0x68'),{'module':_0x5f5f('0x5b')});}else{var _0x5f1784=fs[_0x5f5f('0x53')](path[_0x5f5f('0x54')](config[_0x5f5f('0x57')],_0x5f5f('0x5c')),'utf8');var _0x2312fa=ejs[_0x5f5f('0x65')](_0x5f1784,{'networks':_0xb2839d||[]});fs['writeFileSync'](util[_0x5f5f('0x69')]('/etc/asterisk/sip_xcally_%s.conf',_0x6b0b3a['body']['type'][_0x5f5f('0x6a')]()),_0x2312fa);return respondWithRpcPromise(_0x5f5f('0x63'),_0x5f5f('0x68'),{'module':'chan_sip.so'});}return null;})['catch'](handleError(_0x53f3d2,null));};