kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { and, eq } from "drizzle-orm";
2import { HTTPException } from "hono/http-exception";
3import db from "../../database";
4import { columnTable, taskTable, userTable } from "../../database/schema";
5import { publishEvent } from "../../events";
6import getNextTaskNumber from "./get-next-task-number";
7
8async function createTask({
9 projectId,
10 userId,
11 title,
12 status,
13 dueDate,
14 description,
15 priority,
16}: {
17 projectId: string;
18 userId?: string;
19 title: string;
20 status: string;
21 dueDate?: Date;
22 description?: string;
23 priority?: string;
24}) {
25 const [assignee] = await db
26 .select({ name: userTable.name })
27 .from(userTable)
28 .where(eq(userTable.id, userId ?? ""));
29
30 const nextTaskNumber = await getNextTaskNumber(projectId);
31
32 const column = await db.query.columnTable.findFirst({
33 where: and(
34 eq(columnTable.projectId, projectId),
35 eq(columnTable.slug, status || "to-do"),
36 ),
37 });
38
39 const [createdTask] = await db
40 .insert(taskTable)
41 .values({
42 projectId,
43 userId: userId || null,
44 title: title || "",
45 status: status || "",
46 columnId: column?.id ?? null,
47 dueDate: dueDate || null,
48 description: description || "",
49 priority: priority || "",
50 number: nextTaskNumber + 1,
51 })
52 .returning();
53
54 if (!createdTask) {
55 throw new HTTPException(500, {
56 message: "Failed to create task",
57 });
58 }
59
60 await publishEvent("task.created", {
61 ...createdTask,
62 taskId: createdTask.id,
63 userId: createdTask.userId ?? "",
64 type: "task",
65 content: "created the task",
66 });
67
68 return {
69 ...createdTask,
70 assigneeName: assignee?.name,
71 };
72}
73
74export default createTask;