Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(api): implement updateTimeEntry functionality in time-entry API

Andrej Acevski4b92c46e399f
diff --git a/apps/api/src/time-entry/controllers/update-time-entry.ts b/apps/api/src/time-entry/controllers/update-time-entry.ts
index 932f69d97..96786d538 100644
--- a/apps/api/src/time-entry/controllers/update-time-entry.ts
+++ b/apps/api/src/time-entry/controllers/update-time-entry.ts
@@ -3,11 +3,16 @@ import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { timeEntryTable } from "../../database/schema";
-async function updateTimeEntry(
- timeEntryId: string,
- endTime: Date,
- duration: number,
-) {
+interface UpdateTimeEntryParams {
+ timeEntryId: string;
+ startTime: Date;
+ endTime?: Date;
+ description?: string;
+}
+
+async function updateTimeEntry(params: UpdateTimeEntryParams) {
+ const { timeEntryId, startTime, endTime, description } = params;
+
const [existingTimeEntry] = await db
.select()
.from(timeEntryTable)
@@ -19,11 +24,19 @@ async function updateTimeEntry(
});
}
+ // Calculate duration if both startTime and endTime are provided
+ let duration: number | null = null;
+ if (endTime) {
+ duration = Math.floor((endTime.getTime() - startTime.getTime()) / 1000); // duration in seconds
+ }
+
const [updatedTimeEntry] = await db
.update(timeEntryTable)
.set({
- endTime,
+ startTime,
+ endTime: endTime || null,
duration,
+ ...(description !== undefined && { description }),
})
.where(eq(timeEntryTable.id, timeEntryId))
.returning();
diff --git a/apps/api/src/time-entry/index.ts b/apps/api/src/time-entry/index.ts
index b84510a13..5f9fef549 100644
--- a/apps/api/src/time-entry/index.ts
+++ b/apps/api/src/time-entry/index.ts
@@ -5,6 +5,7 @@ import { timeEntrySchema } from "../schemas";
import createTimeEntry from "./controllers/create-time-entry";
import getTimeEntriesByTaskId from "./controllers/get-time-entries";
import getTimeEntry from "./controllers/get-time-entry";
+import updateTimeEntry from "./controllers/update-time-entry";
const timeEntry = new Hono<{
Variables: {
@@ -113,19 +114,19 @@ const timeEntry = new Hono<{
v.object({
startTime: v.string(),
endTime: v.optional(v.string()),
+ description: v.optional(v.string()),
}),
),
async (c) => {
- // const { id } = c.req.valid("param");
- // const { startTime, endTime } = c.req.valid("json");
- // TODO: Fix this
- // const timeEntry = await updateTimeEntry(
- // id,
- // new Date(startTime),
- // endTime ? new Date(endTime) : new Date(),
- // );
- // return c.json(timeEntry);
- return c.json({ message: "Not implemented" });
+ const { id } = c.req.valid("param");
+ const { startTime, endTime, description } = c.req.valid("json");
+ const timeEntry = await updateTimeEntry({
+ timeEntryId: id,
+ startTime: new Date(startTime),
+ endTime: endTime ? new Date(endTime) : undefined,
+ description,
+ });
+ return c.json(timeEntry);
},
);