2020-04-10 00:43:15 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
|
|
|
|
import Constraints from "../validation/constraints";
|
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import * as MonitoringService from "../services/monitoringService";
|
|
|
|
import * as NodeService from "../services/nodeService";
|
|
|
|
import {normalizeMac, normalizeString} from "../utils/strings";
|
|
|
|
import {forConstraint, forConstraints} from "../validation/validator";
|
|
|
|
import * as Resources from "../utils/resources";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {handleJSONWithData} from "../utils/resources";
|
2020-04-10 00:43:15 +02:00
|
|
|
import {Request, Response} from "express";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {
|
|
|
|
CreateOrUpdateNode,
|
|
|
|
DomainSpecificNodeResponse,
|
|
|
|
isNodeSortField,
|
|
|
|
isToken, JSONObject,
|
|
|
|
MAC,
|
|
|
|
NodeResponse,
|
|
|
|
NodeStateData,
|
|
|
|
NodeTokenResponse,
|
|
|
|
StoredNode,
|
|
|
|
toDomainSpecificNodeResponse,
|
|
|
|
Token,
|
|
|
|
toNodeResponse,
|
|
|
|
toNodeTokenResponse
|
|
|
|
} from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const nodeFields = ['hostname', 'key', 'email', 'nickname', 'mac', 'coords', 'monitoring'];
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
function getNormalizedNodeData(reqData: any): CreateOrUpdateNode {
|
|
|
|
const node: { [key: string]: any } = {};
|
2020-04-10 00:43:15 +02:00
|
|
|
_.each(nodeFields, function (field) {
|
|
|
|
let value = normalizeString(reqData[field]);
|
|
|
|
if (field === 'mac') {
|
|
|
|
value = normalizeMac(value);
|
|
|
|
}
|
|
|
|
node[field] = value;
|
|
|
|
});
|
2022-07-21 18:39:33 +02:00
|
|
|
return node as CreateOrUpdateNode;
|
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)) {
|
|
|
|
throw {data: 'Missing token.', type: ErrorTypes.badRequest};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
const token = normalizeString(data.token);
|
|
|
|
if (!isValidToken(token)) {
|
2022-07-21 18:39:33 +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-07-21 18:39:33 +02:00
|
|
|
export const create = handleJSONWithData<NodeTokenResponse>(async data => {
|
|
|
|
const baseNode = getNormalizedNodeData(data);
|
|
|
|
if (!isValidNode(baseNode)) {
|
|
|
|
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-07-21 18:39:33 +02:00
|
|
|
export const update = handleJSONWithData<NodeTokenResponse>(async data => {
|
|
|
|
const validatedToken: Token = getValidatedToken(data);
|
|
|
|
const baseNode = getNormalizedNodeData(data);
|
|
|
|
if (!isValidNode(baseNode)) {
|
|
|
|
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-07-21 18:39:33 +02:00
|
|
|
export const remove = handleJSONWithData<void>(async data => {
|
|
|
|
const validatedToken = getValidatedToken(data);
|
|
|
|
await NodeService.deleteNode(validatedToken);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export const get = handleJSONWithData<NodeResponse>(async data => {
|
|
|
|
const validatedToken: Token = getValidatedToken(data);
|
|
|
|
const node = await NodeService.getNodeDataByToken(validatedToken);
|
|
|
|
return toNodeResponse(node);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
async function doGetAll(req: Request): Promise<{ total: number; pageNodes: any }> {
|
|
|
|
const restParams = await Resources.getValidRestParams('list', 'node', req);
|
|
|
|
|
|
|
|
const nodes = await NodeService.getAllNodes();
|
|
|
|
|
|
|
|
const realNodes = _.filter(nodes, node =>
|
|
|
|
// We ignore nodes without tokens as those are only manually added ones like gateways.
|
|
|
|
!!node.token
|
|
|
|
);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const macs: MAC[] = _.map(realNodes, (node: StoredNode): MAC => node.mac);
|
2020-04-10 00:43:15 +02:00
|
|
|
const nodeStateByMac = await MonitoringService.getByMacs(macs);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const domainSpecificNodes: DomainSpecificNodeResponse[] = _.map(realNodes, (node: StoredNode): DomainSpecificNodeResponse => {
|
|
|
|
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
|
|
|
[
|
|
|
|
'hostname',
|
|
|
|
'nickname',
|
|
|
|
'email',
|
|
|
|
'token',
|
|
|
|
'mac',
|
|
|
|
'site',
|
|
|
|
'domain',
|
|
|
|
'key',
|
|
|
|
'onlineState'
|
|
|
|
],
|
|
|
|
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);
|
|
|
|
|
|
|
|
return {total, pageNodes};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAll(req: Request, res: Response): void {
|
|
|
|
doGetAll(req)
|
2022-07-21 18:39:33 +02:00
|
|
|
.then((result: { total: number, pageNodes: any[] }) => {
|
2020-04-10 00:43:15 +02:00
|
|
|
res.set('X-Total-Count', result.total.toString(10));
|
|
|
|
return Resources.success(res, result.pageNodes);
|
|
|
|
})
|
|
|
|
.catch((err: any) => Resources.error(res, err));
|
|
|
|
}
|