import { eq } from "drizzle-orm"; import { Hono } from "hono"; import { db } from "../db/index.js"; import { comments, ISSUE_STATUSES, issues, type IssueStatus } from "../db/schema.js"; import { requireAuth, type AuthEnv } from "../auth/session.js"; // Mutations addressed by global issue id (carried in the detail payload). // Reading lives under /api/spools/:slug/issues — see routes/spools.ts. const router = new Hono(); // PATCH /api/issues/:id — update status and/or title (auth required). // Title changes flow into the search index via the issues_search_update trigger. router.patch("/:id", requireAuth, async (c) => { const id = Number(c.req.param("id")); const data = await c.req.json<{ status?: string; title?: string }>(); const patch: { status?: IssueStatus; title?: string } = {}; if (data.status !== undefined) { if (!ISSUE_STATUSES.includes(data.status as IssueStatus)) { return c.json({ error: `Status must be one of: ${ISSUE_STATUSES.join(", ")}` }, 400); } patch.status = data.status as IssueStatus; } if (data.title !== undefined) { const title = data.title.trim(); if (!title) return c.json({ error: "Title cannot be empty" }, 400); patch.title = title; } if (Object.keys(patch).length === 0) { return c.json({ error: "Nothing to update" }, 400); } const [updated] = await db .update(issues) .set({ ...patch, updatedAt: new Date() }) .where(eq(issues.id, id)) .returning(); if (!updated) return c.json({ error: "Issue not found" }, 404); return c.json(updated); }); // POST /api/issues/:id/comments — add a comment (auth required) router.post("/:id/comments", requireAuth, async (c) => { const id = Number(c.req.param("id")); const [issue] = await db.select({ id: issues.id }).from(issues).where(eq(issues.id, id)); if (!issue) return c.json({ error: "Issue not found" }, 404); const data = await c.req.json<{ body?: string }>(); const text = data.body?.trim(); if (!text) return c.json({ error: "Comment body is required" }, 400); const [comment] = await db .insert(comments) .values({ issueId: id, authorId: c.get("user")!.id, body: text }) .returning(); return c.json(comment, 201); }); export default router;