Merge branch 'main' into new-admin

This commit is contained in:
baldo 2022-07-21 18:54:26 +02:00
commit e6b50a0291
2 changed files with 12 additions and 8 deletions

View file

@ -8,7 +8,7 @@ import {handleJSONWithData} from "../utils/resources";
import {normalizeString} from "../utils/strings"; import {normalizeString} from "../utils/strings";
import {forConstraint} from "../validation/validator"; import {forConstraint} from "../validation/validator";
import {Request, Response} from "express"; import {Request, Response} from "express";
import {MonitoringResponse, MonitoringToken, toMonitoringResponse} from "../types"; import {isMonitoringToken, JSONObject, MonitoringResponse, MonitoringToken, toMonitoringResponse} from "../types";
const isValidToken = forConstraint(CONSTRAINTS.token, false); const isValidToken = forConstraint(CONSTRAINTS.token, false);
@ -33,23 +33,26 @@ export function getAll(req: Request, res: Response): void {
.catch(err => Resources.error(res, err)); .catch(err => Resources.error(res, err));
} }
export const confirm = handleJSONWithData<MonitoringResponse>(async data => { function getValidatedToken(data: JSONObject): MonitoringToken {
if (!isMonitoringToken(data.token)) {
throw {data: 'Missing token.', type: ErrorTypes.badRequest};
}
const token = normalizeString(data.token); const token = normalizeString(data.token);
if (!isValidToken(token)) { if (!isValidToken(token)) {
throw {data: 'Invalid token.', type: ErrorTypes.badRequest}; throw {data: 'Invalid token.', type: ErrorTypes.badRequest};
} }
const validatedToken: MonitoringToken = token as MonitoringToken; return token as MonitoringToken;
}
export const confirm = handleJSONWithData<MonitoringResponse>(async data => {
const validatedToken = getValidatedToken(data);
const node = await MonitoringService.confirm(validatedToken); const node = await MonitoringService.confirm(validatedToken);
return toMonitoringResponse(node); return toMonitoringResponse(node);
}); });
export const disable = handleJSONWithData<MonitoringResponse>(async data => { export const disable = handleJSONWithData<MonitoringResponse>(async data => {
const token = normalizeString(data.token); const validatedToken: MonitoringToken = getValidatedToken(data);
if (!isValidToken(token)) {
throw {data: 'Invalid token.', type: ErrorTypes.badRequest};
}
const validatedToken: MonitoringToken = token as MonitoringToken;
const node = await MonitoringService.disable(validatedToken); const node = await MonitoringService.disable(validatedToken);
return toMonitoringResponse(node); return toMonitoringResponse(node);

View file

@ -337,6 +337,7 @@ export function toUnixTimestampSeconds(ms: UnixTimestampMilliseconds): UnixTimes
} }
export type MonitoringToken = string & { readonly __tag: unique symbol }; export type MonitoringToken = string & { readonly __tag: unique symbol };
export const isMonitoringToken = isString;
export enum MonitoringState { export enum MonitoringState {
ACTIVE = "active", ACTIVE = "active",