New routes: - GET /users — list all users - GET/POST /templates — list and create templates - PATCH /templates/:id — update template - POST /templates/preview — render template with ticket/demo context Enhanced routes: - tickets: custom field support on create, status classification helper - custom-fields: PATCH endpoint, auto-generate short key from name - lifecycles: PATCH endpoint - queues: PATCH endpoint Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import { Hono } from 'hono';
|
|
import { HTTPException } from 'hono/http-exception';
|
|
import type { Db } from '../db/index.ts';
|
|
import { lifecycles } from '../db/schema.ts';
|
|
import { asc, eq } from 'drizzle-orm';
|
|
|
|
export function createLifecyclesRouter(db: Db): Hono {
|
|
const router = new Hono();
|
|
|
|
router.get('/', async (c) => {
|
|
const result = await db.query.lifecycles.findMany({
|
|
orderBy: asc(lifecycles.name),
|
|
});
|
|
return c.json(result);
|
|
});
|
|
|
|
router.post('/', async (c) => {
|
|
const body = await c.req.json();
|
|
const { name, definition } = body;
|
|
|
|
if (!name || !definition) {
|
|
throw new HTTPException(400, { message: 'name and definition are required' });
|
|
}
|
|
|
|
const [lifecycle] = await db.insert(lifecycles).values({
|
|
name,
|
|
definition,
|
|
}).returning();
|
|
|
|
if (!lifecycle) {
|
|
throw new HTTPException(500, { message: 'Failed to create lifecycle' });
|
|
}
|
|
|
|
return c.json(lifecycle, 201);
|
|
});
|
|
|
|
router.patch('/:id', async (c) => {
|
|
const id = c.req.param('id');
|
|
const body = await c.req.json();
|
|
|
|
const existing = await db.query.lifecycles.findFirst({
|
|
where: eq(lifecycles.id, id),
|
|
});
|
|
|
|
if (!existing) {
|
|
throw new HTTPException(404, { message: 'Lifecycle not found' });
|
|
}
|
|
|
|
const updateData: Partial<typeof lifecycles.$inferInsert> = {};
|
|
if (body.name !== undefined) updateData.name = String(body.name);
|
|
if (body.definition !== undefined) updateData.definition = body.definition;
|
|
|
|
const [updated] = await db.update(lifecycles)
|
|
.set(updateData)
|
|
.where(eq(lifecycles.id, id))
|
|
.returning();
|
|
|
|
return c.json(updated);
|
|
});
|
|
|
|
return router;
|
|
}
|