// server/routes/postInstall.ts import { Hono } from "hono"; import { PROFILES, previewProfile, validateProfileValues } from "../services/postInstall.js"; import { createActionRequest } from "../services/actionRequests.js"; import { runAction } from "../services/execute.js"; export const postInstallRoutes = new Hono(); // Catalogue des profils (manifestes, sans secret). postInstallRoutes.get("/profiles", (c) => c.json(Object.values(PROFILES))); // Preview du script rendu (secrets masqués) + validation des champs. postInstallRoutes.post("/machines/:id/profiles/:profileId/preview", async (c) => { const profileId = c.req.param("profileId"); const manifest = PROFILES[profileId]; if (!manifest) return c.json({ error: "Profil inconnu" }, 404); const { values } = (await c.req.json().catch(() => ({}))) as { values?: Record }; const validation = validateProfileValues(manifest, values ?? {}); return c.json({ script: previewProfile(profileId, values ?? {}), validation, requiresConfirmation: manifest.requiresConfirmation }); }); // Exécute un profil : confirmation explicite (action_request) si requise, sinon direct. postInstallRoutes.post("/machines/:id/profiles/:profileId/run", async (c) => { const machineId = c.req.param("id"); const profileId = c.req.param("profileId"); const manifest = PROFILES[profileId]; if (!manifest) return c.json({ error: "Profil inconnu" }, 404); const { values } = (await c.req.json().catch(() => ({}))) as { values?: Record }; const validation = validateProfileValues(manifest, values ?? {}); if (!validation.ok) return c.json({ error: "Champs invalides", validation }, 400); if (manifest.requiresConfirmation) { const reqRow = createActionRequest({ machineId, action: "post_install", summary: `Profil ${manifest.label}`, payload: { profileId, values: values ?? {} }, }); return c.json({ actionRequest: reqRow, requiresConfirmation: true }, 202); } runAction(machineId, "post_install", { profileId, values: values ?? {} }).catch((err) => console.error("[post_install]", (err as Error).message), ); return c.json({ ok: true, action: "post_install", profileId }, 202); });