import { and, eq } from "drizzle-orm"; import { HTTPException } from "hono/http-exception"; import db from "../../database"; import { columnTable, taskTable } from "../../database/schema"; async function updateTask( id: string, title: string, status: string, dueDate: Date | undefined, projectId: string, description: string, priority: string, position: number, userId?: string, ) { const existingTask = await db.query.taskTable.findFirst({ where: eq(taskTable.id, id), }); if (!existingTask) { throw new HTTPException(404, { message: "Task not found", }); } const column = await db.query.columnTable.findFirst({ where: and( eq(columnTable.projectId, projectId), eq(columnTable.slug, status), ), }); const [updatedTask] = await db .update(taskTable) .set({ title, status, columnId: column?.id ?? null, dueDate: dueDate || null, projectId, description, priority, position, userId: userId || null, }) .where(eq(taskTable.id, id)) .returning(); if (!updatedTask) { throw new HTTPException(500, { message: "Failed to update task", }); } return updatedTask; } export default updateTask;