Skip to content

Fix clock-out and the midnight auto clock-out - #34

Merged
HeftyB merged 3 commits into
mainfrom
fix/timeclock-clockout
Sep 19, 2026
Merged

HeftyB merged 3 commits into
mainfrom
fix/timeclock-clockout

Conversation

@HeftyB

@HeftyB HeftyB commented Sep 19, 2026

Copy link
Copy Markdown
Owner

Closes #15

What

Fixes clock-out, and fixes the midnight auto-clock-out job, which had a second bug hidden behind the first.

1. Clock-out always threw. findCurrentTimeClockPunchSetByUser passed each shift's LocalDate through Instant.from(). That conversion is impossible, because a LocalDate has no time or zone, so it threw DateTimeException for any employee with a shift on record. This was left over from the Date → LocalDate migration in October 2024. Today's shift is now found with the repository's existing findByEmployeeAndDate, a database query instead of loading every shift for the employee and filtering in Java. The Calendar-based isToday helper is gone.

2. The midnight job could never have worked. It clocked each open shift out through clockOut(username), which looks for today's open shift. At 00:00, "today" is already the next day and the shift being closed started the day before, so fixing the crash alone would have turned it into a TimeClockException for every employee. The first exception also ended the loop for everyone.

  • New closeTimeClockPunchSet(shift, code) closes a specific shift. clockOut and the job both use it.
  • The job loads only open shifts (findByOutIsNull) instead of every shift ever recorded.
  • Each shift closes in its own transaction. A failure is logged with the shift id and the job moves on.

Test profile change

Writing a test with two open shifts turned up a unique-index violation on time_clock_punch_sets.out_id. That is an artifact of the test setup, not a production bug:

  • On SQL Server, Hibernate creates filtered unique indexes for nullable @OneToOne columns (WHERE out_id IS NOT NULL, visible in database_init.sql), so any number of open shifts is fine.
  • H2's MODE=MSSQLServer applies SQL Server's one-NULL rule, but the H2 dialect creates plain unique indexes, so the combination rejects data SQL Server accepts.

The mode is removed from the test profile. It bought nothing else: the H2 dialect generates H2 SQL, and the project's @Query methods are JPQL. All other suites pass unchanged, with 0 DDL errors.

Tests

TimeClockServiceImpTest, Spring context + H2, committed failing first:

Test Before this PR
Clock in, then clock out, closes the shift and clears clockedIn DateTimeException
Clock-out with no open shift is rejected with TimeClockException passed (control)
Midnight job closes a shift that started the previous evening, with code SYSTEM DateTimeException
Midnight job still closes the others when one shift fails DateTimeException

For the last test I confirmed from the log that the broken shift really does fail (Could not auto clock out time clock punch set 1, caused by its missing pay period). The test isn't passing vacuously.

38 tests passing overall.

🤖 Generated with Claude Code

HeftyB and others added 3 commits September 18, 2026 20:29
MODE=MSSQLServer makes H2 allow only one NULL in a unique index, as
SQL Server does. On SQL Server that is harmless here: Hibernate's
SQL Server dialect creates filtered unique indexes (WHERE col IS NOT
NULL) for nullable @OnetoOne columns. Hibernate's H2 dialect creates
plain unique indexes, so under this mode a second open time clock
shift (out_id NULL) was rejected in tests although SQL Server
accepts it.

The mode bought nothing else: the H2 dialect generates H2 SQL, and
the project's @query methods are JPQL, not native SQL.

[Ticket: #15]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
findCurrentTimeClockPunchSetByUser calls Instant.from() on the
punch set's LocalDate, left over from the Date to LocalDate
migration. It always throws, so any employee with a punch set
cannot clock out, and the midnight auto-clock-out job fails too.

The midnight tests also cover a second problem behind the first.
The job clocks each user out through clockOut(), which looks for
today's open shift. At 00:00 the shift being closed started the day
before, so it would never be found. The job must close the specific
shift it selected, and one bad shift must not stop the rest.

[Ticket: #15]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
- Find today's shift with findByEmployeeAndDate instead of loading
  every shift for the employee and converting each LocalDate through
  Instant, which always threw. Removes the Calendar-based isToday
  helper left over from the Date to LocalDate migration.
- Add closeTimeClockPunchSet to close a specific shift; clockOut
  uses it after finding today's shift.
- The midnight job now loads only open shifts (findByOutIsNull, not
  every shift ever recorded) and closes each one directly, since a
  shift open at 00:00 started the previous day. Each shift closes in
  its own transaction, and a failure is logged instead of aborting
  the rest of the run.

[Ticket: #15]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@HeftyB HeftyB added this to the Portfolio readiness milestone Sep 19, 2026
@HeftyB HeftyB added bug Something isn't working testing Tests and test infrastructure labels Sep 19, 2026
@HeftyB HeftyB self-assigned this Sep 19, 2026
@HeftyB
HeftyB merged commit 13ca652 into main Sep 19, 2026
1 check passed
@HeftyB
HeftyB deleted the fix/timeclock-clockout branch September 19, 2026 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working testing Tests and test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clock-out throws DateTimeException for employees with an existing punch set

1 participant