generated from Dicken/dickendock
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import type { FastifyInstance } from "fastify";
|
||
import * as deviceRepo from "../db/repositories/devices.js";
|
||
import * as logRepo from "../db/repositories/logs.js";
|
||
|
||
/**
|
||
* Löscht ALLE Geräte und (per FK-Cascade) alle zugehörigen Dienste.
|
||
* Kategorien und Logs bleiben unangetastet.
|
||
*
|
||
* Erfordert { confirm: true } im Body – zusätzlich zur Bestätigung im
|
||
* Frontend eine zweite Sicherung gegen versehentliche Aufrufe (z. B. per
|
||
* Skript oder Browser-Erweiterung).
|
||
*/
|
||
export async function resetRoutes(app: FastifyInstance): Promise<void> {
|
||
app.post("/api/reset", async (request, reply) => {
|
||
const body = request.body as { confirm?: boolean } | undefined;
|
||
|
||
if (body?.confirm !== true) {
|
||
return reply.code(400).send({
|
||
error: "Bestätigung erforderlich. Bitte { \"confirm\": true } im Body senden.",
|
||
});
|
||
}
|
||
|
||
const deviceCount = deviceRepo.listDevices().length;
|
||
const deletedCount = deviceRepo.deleteAllDevices();
|
||
|
||
logRepo.logScan({
|
||
type: "device",
|
||
level: "info",
|
||
message: `Reset: ${deletedCount} Gerät(e) und alle zugehörigen Dienste gelöscht`,
|
||
});
|
||
|
||
return { deletedDevices: deletedCount, previousDeviceCount: deviceCount };
|
||
});
|
||
}
|