From e3656203ed6db6b795e89852d24dfefd901c4ca9 Mon Sep 17 00:00:00 2001 From: bartool Date: Wed, 29 Oct 2025 19:36:15 +0100 Subject: [PATCH] fix: correct exit time handling in calculateDay function and update tests --- frontend/src/utils/utils.js | 14 ++++++++++---- frontend/src/utils/utils.spec.js | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index ea547c4..4a3919d 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -96,7 +96,13 @@ function isValidWorkDay(day) { } function calculateWorkedTime(entryTimes, exitTimes) { - if (!entryTimes || !exitTimes || !Array.isArray(entryTimes) || !Array.isArray(exitTimes) || entryTimes.length === 0) { + if ( + !entryTimes || + !exitTimes || + !Array.isArray(entryTimes) || + !Array.isArray(exitTimes) || + entryTimes.length === 0 + ) { return 0 } @@ -144,7 +150,7 @@ function floatHoursToHHMM(decimalHours) { function calculateDay(day) { // Create a mutable copy of exit times to potentially add the current time. - const exitTimes = [...day.exitTime] + // const exitTimes = [...day.exitTime] // If there's one more entry than exits, it means the user is currently clocked in. if (day.entryTime.length > 0 && day.entryTime.length === day.exitTime.length + 1) { @@ -153,7 +159,7 @@ function calculateDay(day) { const mm = String(now.getMinutes()).padStart(2, '0') const ss = String(now.getSeconds()).padStart(2, '0') // Add the current time as the "exit" for the last entry. - exitTimes.push(`${hh}:${mm}:${ss}`) + day.exitTime.push(`${hh}:${mm}:${ss}`) } if (day.entryTime.length === 0) { @@ -162,7 +168,7 @@ function calculateDay(day) { // The -0.25 for a 15-minute break seems to be a business rule. // It should probably only be subtracted once per day, not per interval. // Let's subtract it from the total. - let totalHours = calculateWorkedTime(day.entryTime, exitTimes) + let totalHours = calculateWorkedTime(day.entryTime, day.exitTime) if (totalHours > 0) { totalHours -= 0.25 } diff --git a/frontend/src/utils/utils.spec.js b/frontend/src/utils/utils.spec.js index 61d3a81..0dadc69 100644 --- a/frontend/src/utils/utils.spec.js +++ b/frontend/src/utils/utils.spec.js @@ -58,6 +58,7 @@ describe('calculateDay', () => { expect(day.workedHours).toBe(2.75) expect(day.overtime).toBe(2.75 - 8) // -5.25 expect(totalHours).toBe(2.75) + expect(day.exitTime[0]).toBe('12:00:00') vi.useRealTimers() })