feat: es6+mods

This commit is contained in:
Andres 2023-10-06 22:11:40 +02:00
parent 773b64ea30
commit 19c92ed244
19 changed files with 179 additions and 245 deletions

View file

@ -1,12 +1,17 @@
const express = require("express");
import express from "express";
const router = express.Router();
const auth = require("../services/auth");
const network = require("../services/network");
import { isAuthorized } from "../services/auth.js";
import {
getNetworksData,
createNetworkAdditionalData,
updateNetworkAdditionalData,
deleteNetworkAdditionalData,
} from "../services/network.js";
const api = require("../utils/controller-api");
const constants = require("../utils/constants");
const getZTAddress = require("../utils/zt-address");
import { api } from "../utils/controller-api.js";
import { defaultRules } from "../utils/constants.js";
import { getZTAddress } from "../utils/zt-address.js";
let ZT_ADDRESS = null;
getZTAddress().then(function (address) {
@ -14,18 +19,18 @@ getZTAddress().then(function (address) {
});
// get all networks
router.get("/", auth.isAuthorized, async function (req, res) {
router.get("/", isAuthorized, async function (req, res) {
api.get("controller/network").then(async function (controllerRes) {
const nwids = controllerRes.data;
const data = await network.getNetworksData(nwids);
const data = await getNetworksData(nwids);
res.send(data);
});
});
// get network
router.get("/:nwid", auth.isAuthorized, async function (req, res) {
router.get("/:nwid", isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
const data = await network.getNetworksData([nwid]);
const data = await getNetworksData([nwid]);
if (data[0]) {
res.send(data[0]);
} else {
@ -34,49 +39,49 @@ router.get("/:nwid", auth.isAuthorized, async function (req, res) {
});
// create new network
router.post("/", auth.isAuthorized, async function (req, res) {
router.post("/", isAuthorized, async function (req, res) {
let reqData = req.body;
if (reqData.config) {
const config = reqData.config;
delete reqData.config;
reqData = config;
reqData.rules = JSON.parse(constants.defaultRules);
reqData.rules = JSON.parse(defaultRules);
} else {
res.status(400).send({ error: "Bad request" });
}
api
.post("controller/network/" + ZT_ADDRESS + "______", reqData)
.then(async function (controllerRes) {
await network.createNetworkAdditionalData(controllerRes.data.id);
const data = await network.getNetworksData([controllerRes.data.id]);
await createNetworkAdditionalData(controllerRes.data.id);
const data = await getNetworksData([controllerRes.data.id]);
res.send(data[0]);
});
});
// update network
router.post("/:nwid", auth.isAuthorized, async function (req, res) {
router.post("/:nwid", isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
network.updateNetworkAdditionalData(nwid, req.body);
updateNetworkAdditionalData(nwid, req.body);
if (req.body.config) {
api
.post("controller/network/" + nwid, req.body.config)
.then(async function () {
const data = await network.getNetworksData([nwid]);
const data = await getNetworksData([nwid]);
res.send(data[0]);
})
.catch(function (err) {
res.status(500).send({ error: err.message });
});
} else {
const data = await network.getNetworksData([nwid]);
const data = await getNetworksData([nwid]);
res.send(data[0]);
}
});
// delete network
router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
router.delete("/:nwid", isAuthorized, async function (req, res) {
const nwid = req.params.nwid;
network.deleteNetworkAdditionalData(nwid);
deleteNetworkAdditionalData(nwid);
api
.delete("controller/network/" + nwid)
.then(function (controllerRes) {
@ -87,4 +92,4 @@ router.delete("/:nwid", auth.isAuthorized, async function (req, res) {
});
});
module.exports = router;
export default router;