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 Resources from "../utils/resources";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { handleJSONWithData } from "../utils/resources";
|
|
|
|
import { normalizeString } from "../shared/utils/strings";
|
|
|
|
import { forConstraint } from "../shared/validation/validator";
|
2022-09-14 16:33:43 +02:00
|
|
|
import type { Request, Response } from "express";
|
2022-08-23 20:08:53 +02:00
|
|
|
import {
|
|
|
|
isMonitoringToken,
|
|
|
|
JSONObject,
|
|
|
|
MonitoringResponse,
|
|
|
|
MonitoringToken,
|
2022-08-24 18:20:49 +02:00
|
|
|
NodeMonitoringStateResponse,
|
2022-08-23 20:08:53 +02:00
|
|
|
toMonitoringResponse,
|
|
|
|
} from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const isValidToken = forConstraint(CONSTRAINTS.token, false);
|
|
|
|
|
2022-08-24 18:20:49 +02:00
|
|
|
async function doGetAll(
|
|
|
|
req: Request
|
|
|
|
): Promise<{ total: number; result: NodeMonitoringStateResponse[] }> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const restParams = await Resources.getValidRestParams("list", null, req);
|
|
|
|
const { monitoringStates, total } = await MonitoringService.getAll(
|
|
|
|
restParams
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
return {
|
|
|
|
total,
|
2022-08-24 18:20:49 +02:00
|
|
|
result: monitoringStates,
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAll(req: Request, res: Response): void {
|
|
|
|
doGetAll(req)
|
2022-08-23 20:08:53 +02:00
|
|
|
.then(({ total, result }) => {
|
|
|
|
res.set("X-Total-Count", total.toString(10));
|
|
|
|
Resources.success(res, result);
|
2020-04-10 00:43:15 +02:00
|
|
|
})
|
2022-08-23 20:08:53 +02:00
|
|
|
.catch((err) => Resources.error(res, err));
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:49:49 +02:00
|
|
|
function getValidatedToken(data: JSONObject): MonitoringToken {
|
|
|
|
if (!isMonitoringToken(data.token)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw { data: "Missing token.", type: ErrorTypes.badRequest };
|
2022-07-21 18:49:49 +02:00
|
|
|
}
|
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:49:49 +02:00
|
|
|
return token as MonitoringToken;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const confirm = handleJSONWithData<MonitoringResponse>(async (data) => {
|
2022-07-21 18:49:49 +02:00
|
|
|
const validatedToken = getValidatedToken(data);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = await MonitoringService.confirm(validatedToken);
|
|
|
|
return toMonitoringResponse(node);
|
|
|
|
});
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export const disable = handleJSONWithData<MonitoringResponse>(async (data) => {
|
2022-07-21 18:49:49 +02:00
|
|
|
const validatedToken: MonitoringToken = getValidatedToken(data);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = await MonitoringService.disable(validatedToken);
|
|
|
|
return toMonitoringResponse(node);
|
|
|
|
});
|