kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { eq } from "drizzle-orm";
2import { HTTPException } from "hono/http-exception";
3import db from "../../database";
4import { timeEntryTable } from "../../database/schema";
5
6type UpdateTimeEntryParams = {
7 timeEntryId: string;
8 startTime: Date;
9 endTime?: Date;
10 description?: string;
11};
12
13async function updateTimeEntry(params: UpdateTimeEntryParams) {
14 const { timeEntryId, startTime, endTime, description } = params;
15
16 const [existingTimeEntry] = await db
17 .select()
18 .from(timeEntryTable)
19 .where(eq(timeEntryTable.id, timeEntryId));
20
21 if (!existingTimeEntry) {
22 throw new HTTPException(404, {
23 message: "Time entry not found",
24 });
25 }
26
27 // Calculate duration if both startTime and endTime are provided
28 let duration: number | null = null;
29 if (endTime) {
30 duration = Math.floor((endTime.getTime() - startTime.getTime()) / 1000); // duration in seconds
31 }
32
33 const [updatedTimeEntry] = await db
34 .update(timeEntryTable)
35 .set({
36 startTime,
37 endTime: endTime || null,
38 duration,
39 ...(description !== undefined && { description }),
40 })
41 .where(eq(timeEntryTable.id, timeEntryId))
42 .returning();
43
44 return updatedTimeEntry;
45}
46
47export default updateTimeEntry;