⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix: preserve time entry end time (#1554)
Emre K.5e0213009a6a
diff --git a/apps/api/src/time-entry/controllers/update-time-entry.ts b/apps/api/src/time-entry/controllers/update-time-entry.tsindex dc824d8d6..24dfcaee8 100644--- a/apps/api/src/time-entry/controllers/update-time-entry.ts+++ b/apps/api/src/time-entry/controllers/update-time-entry.ts@@ -24,17 +24,21 @@ async function updateTimeEntry(params: UpdateTimeEntryParams) {});}- // Calculate duration if both startTime and endTime are provided+ const effectiveEndTime = endTime ?? existingTimeEntry.endTime;++ // Calculate duration if the entry has an endTime after this update.let duration: number | null = null;- if (endTime) {- duration = Math.floor((endTime.getTime() - startTime.getTime()) / 1000); // duration in seconds+ if (effectiveEndTime) {+ duration = Math.floor(+ (effectiveEndTime.getTime() - startTime.getTime()) / 1000,+ ); // duration in seconds}const [updatedTimeEntry] = await db.update(timeEntryTable).set({startTime,- endTime: endTime || null,+ endTime: effectiveEndTime,duration,...(description !== undefined && { description }),})diff --git a/tests/api/time-entry/update-time-entry.test.ts b/tests/api/time-entry/update-time-entry.test.tsnew file mode 100644index 000000000..091d39c57--- /dev/null+++ b/tests/api/time-entry/update-time-entry.test.ts@@ -0,0 +1,73 @@+import { beforeEach, describe, expect, it, type Mock, vi } from "vitest";++const mockSelect = vi.fn();+const mockUpdate = vi.fn();++vi.mock("../../../apps/api/src/database", () => ({+ default: {+ select: (...args: unknown[]) => mockSelect(...args),+ update: (...args: unknown[]) => mockUpdate(...args),+ },+}));++import updateTimeEntry from "../../../apps/api/src/time-entry/controllers/update-time-entry";++function makeSelectMock(rows: unknown[]) {+ const chain: Record<string, Mock> = {+ from: vi.fn(() => chain),+ where: vi.fn(() => Promise.resolve(rows)),+ };+ return chain;+}++function makeUpdateMock(updatedRow: unknown) {+ const returning = vi.fn(() => Promise.resolve([updatedRow]));+ const where = vi.fn(() => ({ returning }));+ const set = vi.fn(() => ({ where }));+ return { set, where, returning };+}++describe("updateTimeEntry", () => {+ beforeEach(() => {+ vi.resetAllMocks();+ });++ it("preserves a stored endTime and duration when endTime is omitted", async () => {+ const storedStartTime = new Date("2026-08-10T10:00:00.000Z");+ const storedEndTime = new Date("2026-08-10T11:00:00.000Z");+ const nextStartTime = new Date("2026-08-10T10:15:00.000Z");+ const updatedRow = {+ id: "time-entry-1",+ startTime: nextStartTime,+ endTime: storedEndTime,+ duration: 2700,+ description: "renamed",+ };+ const updateChain = makeUpdateMock(updatedRow);++ mockSelect.mockReturnValue(+ makeSelectMock([+ {+ id: "time-entry-1",+ startTime: storedStartTime,+ endTime: storedEndTime,+ duration: 3600,+ },+ ]),+ );+ mockUpdate.mockReturnValue(updateChain);++ await updateTimeEntry({+ timeEntryId: "time-entry-1",+ startTime: nextStartTime,+ description: "renamed",+ });++ expect(updateChain.set).toHaveBeenCalledWith({+ startTime: nextStartTime,+ endTime: storedEndTime,+ duration: 2700,+ description: "renamed",+ });+ });+});