kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { createId } from "@paralleldrive/cuid2";
2import { eq } from "drizzle-orm";
3import { HTTPException } from "hono/http-exception";
4import db from "../../database";
5import { taskTable, timeEntryTable } from "../../database/schema";
6import { publishEvent } from "../../events";
7
8async function createTimeEntry({
9 taskId,
10 userId,
11 description,
12 startTime,
13 endTime,
14 duration,
15}: {
16 taskId: string;
17 userId: string;
18 description?: string;
19 startTime: Date;
20 endTime?: Date;
21 duration?: number;
22}) {
23 const [createdTimeEntry] = await db
24 .insert(timeEntryTable)
25 .values({
26 id: createId(),
27 taskId,
28 userId,
29 description: description || "",
30 startTime,
31 endTime: endTime || null,
32 duration: duration || 0,
33 })
34 .returning();
35
36 if (!createdTimeEntry) {
37 throw new HTTPException(500, {
38 message: "Failed to create time entry",
39 });
40 }
41
42 const [task] = await db
43 .select({ userId: taskTable.userId, title: taskTable.title })
44 .from(taskTable)
45 .where(eq(taskTable.id, taskId));
46
47 await publishEvent("time-entry.created", {
48 timeEntryId: createdTimeEntry.id,
49 taskId: createdTimeEntry.taskId,
50 userId,
51 type: "create",
52 content: "started time tracking",
53 taskOwnerId: task?.userId,
54 taskTitle: task?.title,
55 });
56
57 return createdTimeEntry;
58}
59
60export default createTimeEntry;