import { eq, sql } from "drizzle-orm"; import { HTTPException } from "hono/http-exception"; import db from "../../database"; import { columnTable } from "../../database/schema"; function toSlug(name: string): string { return name .toLowerCase() .trim() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, ""); } async function createColumn({ projectId, name, icon, color, isFinal, }: { projectId: string; name: string; icon?: string; color?: string; isFinal?: boolean; }) { const slug = toSlug(name); const existing = await db .select({ id: columnTable.id }) .from(columnTable) .where( sql`${columnTable.projectId} = ${projectId} AND ${columnTable.slug} = ${slug}`, ); if (existing.length > 0) { throw new HTTPException(409, { message: `Column with slug "${slug}" already exists in this project`, }); } const [maxPos] = await db .select({ maxPosition: sql`COALESCE(MAX(${columnTable.position}), -1)`, }) .from(columnTable) .where(eq(columnTable.projectId, projectId)); const position = (maxPos?.maxPosition ?? -1) + 1; const [created] = await db .insert(columnTable) .values({ projectId, name, slug, position, icon: icon || null, color: color || null, isFinal: isFinal ?? false, }) .returning(); if (!created) { throw new HTTPException(500, { message: "Failed to create column" }); } return created; } export default createColumn;