oneuptime/Ingestor/API/Register.ts
Simon Larsen 56d3e7e1f2
chore: Update probeKey type to string in CustomProbeDocumentation and ProbeView components
This commit updates the probeKey prop type from ObjectID to string in the CustomProbeDocumentation and ProbeView components. The probeKey is used to identify the probe and was previously defined as ObjectID, but it should be a string. This change ensures that the probeKey is correctly passed and used throughout the components, improving the accuracy and reliability of the application.
2024-07-01 20:00:16 +01:00

98 lines
2.7 KiB
TypeScript

import OneUptimeDate from "Common/Types/Date";
import BadDataException from "Common/Types/Exception/BadDataException";
import { JSONObject } from "Common/Types/JSON";
import ClusterKeyAuthorization from "CommonServer/Middleware/ClusterKeyAuthorization";
import ProbeService from "CommonServer/Services/ProbeService";
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
NextFunction,
} from "CommonServer/Utils/Express";
import Response from "CommonServer/Utils/Response";
import Probe, { ProbeConnectionStatus } from "Model/Models/Probe";
const router: ExpressRouter = Express.getRouter();
// Register Global Probe. Custom Probe can be registered via dashboard.
router.post(
"/register",
ClusterKeyAuthorization.isAuthorizedServiceMiddleware,
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction,
): Promise<void> => {
try {
const data: JSONObject = req.body;
if (!data["probeKey"]) {
return Response.sendErrorResponse(
req,
res,
new BadDataException("ProbeId or ProbeKey is missing"),
);
}
const probeKey: string = data["probeKey"] as string;
const probe: Probe | null = await ProbeService.findOneBy({
query: {
key: probeKey,
isGlobalProbe: true,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
if (probe) {
await ProbeService.updateOneById({
id: probe.id!,
data: {
name: data["probeName"] as string,
description: data["probeDescription"] as string,
lastAlive: OneUptimeDate.getCurrentDate(),
connectionStatus: ProbeConnectionStatus.Connected,
},
props: {
isRoot: true,
},
});
return Response.sendJsonObjectResponse(req, res, {
_id: probe._id?.toString(),
message: "Probe already registered",
});
}
let newProbe: Probe = new Probe();
newProbe.isGlobalProbe = true;
newProbe.key = probeKey;
newProbe.name = data["probeName"] as string;
newProbe.description = data["probeDescription"] as string;
newProbe.lastAlive = OneUptimeDate.getCurrentDate();
newProbe.connectionStatus = ProbeConnectionStatus.Connected;
newProbe.shouldAutoEnableProbeOnNewMonitors = true;
newProbe = await ProbeService.create({
data: newProbe,
props: {
isRoot: true,
},
});
return Response.sendJsonObjectResponse(req, res, {
_id: newProbe._id?.toString(),
message: "Probe registered successfully",
});
} catch (err) {
return next(err);
}
},
);
export default router;