fix: correct exit time handling in calculateDay function and update tests

This commit is contained in:
2025-10-29 19:36:15 +01:00
parent 54de913c16
commit e3656203ed
2 changed files with 11 additions and 4 deletions

View File

@@ -96,7 +96,13 @@ function isValidWorkDay(day) {
} }
function calculateWorkedTime(entryTimes, exitTimes) { 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 return 0
} }
@@ -144,7 +150,7 @@ function floatHoursToHHMM(decimalHours) {
function calculateDay(day) { function calculateDay(day) {
// Create a mutable copy of exit times to potentially add the current time. // 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 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) { 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 mm = String(now.getMinutes()).padStart(2, '0')
const ss = String(now.getSeconds()).padStart(2, '0') const ss = String(now.getSeconds()).padStart(2, '0')
// Add the current time as the "exit" for the last entry. // 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) { 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. // 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. // It should probably only be subtracted once per day, not per interval.
// Let's subtract it from the total. // Let's subtract it from the total.
let totalHours = calculateWorkedTime(day.entryTime, exitTimes) let totalHours = calculateWorkedTime(day.entryTime, day.exitTime)
if (totalHours > 0) { if (totalHours > 0) {
totalHours -= 0.25 totalHours -= 0.25
} }

View File

@@ -58,6 +58,7 @@ describe('calculateDay', () => {
expect(day.workedHours).toBe(2.75) expect(day.workedHours).toBe(2.75)
expect(day.overtime).toBe(2.75 - 8) // -5.25 expect(day.overtime).toBe(2.75 - 8) // -5.25
expect(totalHours).toBe(2.75) expect(totalHours).toBe(2.75)
expect(day.exitTime[0]).toBe('12:00:00')
vi.useRealTimers() vi.useRealTimers()
}) })