The availability tests are failing due to inconsistent date handling between the database (DATE type) and the application code (Date objects with time components). When availability rows are created with one timestamp and later queried with another, they're not found, causing the tests to fail.
- Database stores dates as
DATEtype (date-only) - Application code creates Date objects with varying time components
- SQL joins and comparisons fail when one side has a time component and the other doesn't
- The
formatDateForDb()function extracts only the date part, but Date objects still retain time components
In app/lib/repositories/utils.ts, add a normalizeToDateOnly() function:
export function normalizeToDateOnly(date: Date): Date {
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()))
}-
In
createAvailability()andupsertAvailability():- Normalize the date before calling
formatDateForDb()
- Normalize the date before calling
-
In
getAvailabilitiesForStaffAndWeek():- No changes needed (already filters by staff_id and week_schedule_id only)
-
In
getStaffAvailabilityForShift():- Update the SQL join to use date-only comparison:
a.date::date = sh.date::date
- Update the SQL join to use date-only comparison:
-
In
getAvailabilityForToken():- Normalize dates when comparing availability entries with grid dates
-
In
updateAvailabilityForToken():- Normalize the date before calling
upsertAvailability() - Use the same normalization logic as in the repository
- Normalize the date before calling
-
In
createTestWeekSchedule():- Normalize the week_start_date to avoid time components
-
In
createTestAvailability():- Normalize the availability date
- In test files that compare dates:
- Ensure consistent date-only comparison using
.toISOString().split('T')[0]
- Ensure consistent date-only comparison using
- Add the date normalization helper
- Update the availability repository
- Update the availability helpers
- Update test data creation
- Update test expectations if needed
- Run tests to verify fixes
After these changes:
- All availability dates will be stored and queried consistently as date-only values
- The 7×4 grid will correctly show updated availability states
- The
getStaffAvailabilityForShift()query will correctly find matching availability rows - All failing tests should pass