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 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 {normalizeString} from "../utils/strings";
|
|
|
|
import {forConstraint} from "../validation/validator";
|
|
|
|
import {Request, Response} from "express";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {MonitoringResponse, MonitoringToken, toMonitoringResponse} from "../types";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const isValidToken = forConstraint(CONSTRAINTS.token, false);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function doGetAll(req: Request): Promise<{ total: number, result: any }> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const restParams = await Resources.getValidRestParams('list', null, req);
|
|
|
|
const {monitoringStates, total} = await MonitoringService.getAll(restParams);
|
|
|
|
return {
|
|
|
|
total,
|
|
|
|
result: _.map(monitoringStates, function (state) {
|
|
|
|
state.mapId = _.toLower(state.mac).replace(/:/g, '');
|
|
|
|
return state;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAll(req: Request, res: Response): void {
|
|
|
|
doGetAll(req)
|
|
|
|
.then(({total, result}) => {
|
|
|
|
res.set('X-Total-Count', total.toString(10));
|
|
|
|
Resources.success(res, result)
|
|
|
|
})
|
|
|
|
.catch(err => Resources.error(res, err));
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export const confirm = handleJSONWithData<MonitoringResponse>(async data => {
|
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-18 17:49:42 +02:00
|
|
|
const validatedToken: MonitoringToken = token as MonitoringToken;
|
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-07-21 18:39:33 +02:00
|
|
|
export const disable = handleJSONWithData<MonitoringResponse>(async data => {
|
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-18 17:49:42 +02:00
|
|
|
const validatedToken: MonitoringToken = token as MonitoringToken;
|
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);
|
|
|
|
});
|