import { Hono } from 'hono'; import { HTTPException } from 'hono/http-exception'; import type { Db } from '../db/index.ts'; import { queues } from '../db/schema.ts'; import { asc, eq } from 'drizzle-orm'; import { CreateQueueSchema } from '../models/queue.ts'; export function createQueuesRouter(db: Db): Hono { const router = new Hono(); router.get('/', async (c) => { const result = await db.query.queues.findMany({ orderBy: asc(queues.name), }); return c.json(result); }); router.post('/', async (c) => { const body = await c.req.json(); const parsed = CreateQueueSchema.parse(body); const [queue] = await db.insert(queues).values({ name: parsed.name, description: parsed.description ?? null, lifecycle_id: parsed.lifecycle_id ?? null, team_id: parsed.team_id ?? null, }).returning(); if (!queue) { throw new HTTPException(500, { message: 'Failed to create queue' }); } return c.json(queue, 201); }); router.patch('/:id', async (c) => { const id = c.req.param('id'); const body = await c.req.json(); const existing = await db.query.queues.findFirst({ where: eq(queues.id, id), }); if (!existing) { throw new HTTPException(404, { message: 'Queue not found' }); } const updateData: Partial = {}; if (body.name !== undefined) updateData.name = String(body.name); if (body.description !== undefined) updateData.description = body.description ? String(body.description) : null; if (body.lifecycle_id !== undefined) updateData.lifecycle_id = body.lifecycle_id || null; if (body.team_id !== undefined) updateData.team_id = body.team_id || null; const [updated] = await db.update(queues) .set(updateData) .where(eq(queues.id, id)) .returning(); return c.json(updated); }); return router; }