2022-08-04 15:31:01 +02:00
|
|
|
import Constraints from "../shared/validation/constraints";
|
2020-04-10 00:43:15 +02:00
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import * as MonitoringService from "../services/monitoringService";
|
|
|
|
import * as NodeService from "../services/nodeService";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { normalizeMac, normalizeString } from "../shared/utils/strings";
|
|
|
|
import { forConstraint, forConstraints } from "../shared/validation/validator";
|
2020-04-10 00:43:15 +02:00
|
|
|
import * as Resources from "../utils/resources";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { handleJSONWithData } from "../utils/resources";
|
|
|
|
import { Request, Response } from "express";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {
|
|
|
|
CreateOrUpdateNode,
|
|
|
|
DomainSpecificNodeResponse,
|
2022-07-28 13:56:47 +02:00
|
|
|
isCreateOrUpdateNode,
|
2022-07-21 18:39:33 +02:00
|
|
|
isNodeSortField,
|
2022-07-28 13:56:47 +02:00
|
|
|
isString,
|
2022-07-28 13:16:13 +02:00
|
|
|
isToken,
|
2022-07-28 13:56:47 +02:00
|
|
|
isUndefined,
|
2022-07-28 13:16:13 +02:00
|
|
|
JSONObject,
|
2022-07-28 13:56:47 +02:00
|
|
|
JSONValue,
|
2022-07-21 18:39:33 +02:00
|
|
|
MAC,
|
|
|
|
NodeResponse,
|
|
|
|
NodeStateData,
|
|
|
|
NodeTokenResponse,
|
|
|
|
toDomainSpecificNodeResponse,
|
|
|
|
Token,
|
|
|
|
toNodeResponse,
|
2022-08-23 20:08:53 +02:00
|
|
|
toNodeTokenResponse,
|
2022-07-21 18:39:33 +02:00
|
|
|
} from "../types";
|
2022-09-06 19:09:25 +02:00
|
|
|
import { filterUndefinedFromJSON } from "../shared/utils/json";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const nodeFields = [
|
|
|
|
"hostname",
|
|
|
|
"key",
|
|
|
|
"email",
|
|
|
|
"nickname",
|
|
|
|
"mac",
|
|
|
|
"coords",
|
|
|
|
"monitoring",
|
|
|
|
];
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-28 13:56:47 +02:00
|
|
|
function getNormalizedNodeData(reqData: JSONObject): CreateOrUpdateNode {
|
2022-08-23 20:08:53 +02:00
|
|
|
const node: { [key: string]: unknown } = {};
|
2022-07-28 13:16:13 +02:00
|
|
|
for (const field of nodeFields) {
|
2022-07-28 13:56:47 +02:00
|
|
|
let value: JSONValue | undefined = reqData[field];
|
|
|
|
if (isString(value)) {
|
|
|
|
value = normalizeString(value);
|
2022-08-23 20:08:53 +02:00
|
|
|
if (field === "mac") {
|
2022-07-28 13:56:47 +02:00
|
|
|
value = normalizeMac(value as MAC);
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-07-28 13:56:47 +02:00
|
|
|
|
|
|
|
if (!isUndefined(value)) {
|
|
|
|
node[field] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCreateOrUpdateNode(node)) {
|
|
|
|
return node;
|
2022-07-28 13:16:13 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Invalid node data.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const isValidNode = forConstraints(Constraints.node, false);
|
|
|
|
const isValidToken = forConstraint(Constraints.token, false);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function getValidatedToken(data: JSONObject): Token {
|
|
|
|
if (!isToken(data.token)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Missing token.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
const token = normalizeString(data.token);
|
|
|
|
if (!isValidToken(token)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Invalid token.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
return token as Token;
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const create = handleJSONWithData<NodeTokenResponse>(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
const baseNode = getNormalizedNodeData(data);
|
|
|
|
if (!isValidNode(baseNode)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Invalid node data.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = await NodeService.createNode(baseNode);
|
|
|
|
return toNodeTokenResponse(node);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const update = handleJSONWithData<NodeTokenResponse>(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
const validatedToken: Token = getValidatedToken(data);
|
|
|
|
const baseNode = getNormalizedNodeData(data);
|
|
|
|
if (!isValidNode(baseNode)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Invalid node data.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = await NodeService.updateNode(validatedToken, baseNode);
|
|
|
|
return toNodeTokenResponse(node);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const remove = handleJSONWithData<void>(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
const validatedToken = getValidatedToken(data);
|
|
|
|
await NodeService.deleteNode(validatedToken);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const get = handleJSONWithData<NodeResponse>(async (data) => {
|
2022-07-21 18:39:33 +02:00
|
|
|
const validatedToken: Token = getValidatedToken(data);
|
|
|
|
const node = await NodeService.getNodeDataByToken(validatedToken);
|
|
|
|
return toNodeResponse(node);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function doGetAll(
|
|
|
|
req: Request
|
2022-08-23 21:38:37 +02:00
|
|
|
): Promise<{ total: number; pageNodes: DomainSpecificNodeResponse[] }> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const restParams = await Resources.getValidRestParams("list", "node", req);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const nodes = await NodeService.getAllNodes();
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const realNodes = nodes.filter(
|
|
|
|
(node) =>
|
|
|
|
// We ignore nodes without tokens as those are only manually added ones like gateways.
|
|
|
|
!!node.token // FIXME: As node.token may not be undefined or null here, handle this when loading!
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const macs: MAC[] = realNodes.map((node) => node.mac);
|
2020-04-10 00:43:15 +02:00
|
|
|
const nodeStateByMac = await MonitoringService.getByMacs(macs);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const domainSpecificNodes: DomainSpecificNodeResponse[] = realNodes.map(
|
|
|
|
(node) => {
|
|
|
|
const nodeState: NodeStateData = nodeStateByMac[node.mac] || {};
|
|
|
|
return toDomainSpecificNodeResponse(node, nodeState);
|
|
|
|
}
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const filteredNodes = Resources.filter<DomainSpecificNodeResponse>(
|
|
|
|
domainSpecificNodes,
|
2020-04-10 00:43:15 +02:00
|
|
|
[
|
2022-08-23 20:08:53 +02:00
|
|
|
"hostname",
|
|
|
|
"nickname",
|
|
|
|
"email",
|
|
|
|
"token",
|
|
|
|
"mac",
|
|
|
|
"site",
|
|
|
|
"domain",
|
|
|
|
"key",
|
|
|
|
"onlineState",
|
2020-04-10 00:43:15 +02:00
|
|
|
],
|
|
|
|
restParams
|
|
|
|
);
|
|
|
|
|
|
|
|
const total = filteredNodes.length;
|
|
|
|
|
|
|
|
const sortedNodes = Resources.sort(
|
|
|
|
filteredNodes,
|
2022-06-23 14:26:15 +02:00
|
|
|
isNodeSortField,
|
2020-04-10 00:43:15 +02:00
|
|
|
restParams
|
|
|
|
);
|
|
|
|
const pageNodes = Resources.getPageEntities(sortedNodes, restParams);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
return { total, pageNodes };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getAll(req: Request, res: Response): void {
|
|
|
|
doGetAll(req)
|
2022-08-23 21:38:37 +02:00
|
|
|
.then(
|
|
|
|
(result: {
|
|
|
|
total: number;
|
|
|
|
pageNodes: DomainSpecificNodeResponse[];
|
|
|
|
}) => {
|
|
|
|
res.set("X-Total-Count", result.total.toString(10));
|
|
|
|
return Resources.success(
|
|
|
|
res,
|
|
|
|
result.pageNodes.map(filterUndefinedFromJSON)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.catch((err) => Resources.error(res, err));
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|