import { Router } from "express"; import { noteInputSchema } from "@notes/shared"; import { getOrCreateTopicPage } from "../nextcloud/topicCache.js"; import { appendEntry } from "../nextcloud/webdavClient.js"; import { formatEntry } from "../nextcloud/noteFormatter.js"; export const notesRoutes = Router(); notesRoutes.post("/", async (req, res) => { const parsed = noteInputSchema.safeParse(req.body); if (!parsed.success) { res.status(400).json({ error: "Invalid note payload", details: parsed.error.flatten() }); return; } const { topic, page, text } = parsed.data; try { const targetPage = await getOrCreateTopicPage(topic); const entry = formatEntry(text, page); await appendEntry(targetPage, entry); res.status(201).json({ ok: true }); } catch (err) { console.error("Failed to save note", err); res.status(502).json({ error: "Failed to save note to Nextcloud" }); } });