Fix: Session numbering after deletion (Issue #413)#416
Fix: Session numbering after deletion (Issue #413)#416
Conversation
- Replace hardcoded sessionNumber=1 with getNextSessionNumber() in deleteSession() - Add findAllByBookIdOrdered() repository method for chronological sorting - Implement getSessionsWithDisplayNumbers() service method - Calculate displayNumber based on array index from chronologically ordered sessions - Update API endpoint to include displayNumber in responses - Update UI components (ReadingHistoryTab, modals) to display calculated numbers - Add comprehensive integration tests for session deletion and renumbering Fixes #413 - session numbers now display correctly after deletion Display numbers are sequential (1st read, 2nd read, 3rd read) without gaps Database sessionNumber preserved for backward compatibility
Filter sessions before calculating displayNumber to match the Reading History UI filter (!isActive || status=read || status=dnf). This ensures: - Active to-read/reading sessions don't get displayNumbers - Archived sessions (any status) get sequential displayNumbers - Completed read/dnf sessions get displayNumbers (even if active) - No gaps in numbering sequence Adds comprehensive integration test covering filter edge cases. Relates to #413
The 'should handle threshold change mid-day' test was failing between 8pm-midnight EST because it created progress using UTC dates while updateStreaks() queries using America/New_York timezone dates. When UTC and local dates differ (e.g., 11:50pm EST = 3:50am UTC next day), the test would create progress for April 14 (UTC) but updateStreaks would query for April 13 (EST), find no progress, and return early. Fix: Use getTodayInUserTimezone() helper to create progress dates in the same timezone that updateStreaks() uses (America/New_York). Relates to #413 (needed for PR merge)
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## develop #416 +/- ##
===========================================
+ Coverage 78.67% 78.85% +0.18%
===========================================
Files 167 168 +1
Lines 7559 7620 +61
Branches 1850 1861 +11
===========================================
+ Hits 5947 6009 +62
Misses 1127 1127
+ Partials 485 484 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 2 files with indirect coverage changes 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes incorrect “Read #” display after deleting sessions by introducing a computed displayNumber derived from chronologically ordered sessions while keeping the persisted sessionNumber for compatibility.
Changes:
- Update
deleteSession()to compute the nextsessionNumberviagetNextSessionNumber()rather than hardcoding1. - Add chronological session ordering + compute/return
displayNumberfrom/api/books/[id]/sessions. - Update UI components/modals to render
displayNumber(fallback tosessionNumber) and add integration tests for Issue #413.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
lib/services/session.service.ts |
Adds getSessionsWithDisplayNumbers() and updates deleteSession() to use computed next session number. |
lib/repositories/session.repository.ts |
Adds findAllByBookIdOrdered() to order sessions chronologically for display numbering. |
app/api/books/[id]/sessions/route.ts |
Computes displayNumber and includes it in the sessions API response. |
components/CurrentlyReading/ReadingHistoryTab.tsx |
Displays displayNumber and passes it into modals/labels. |
components/Modals/SessionProgressModal.tsx |
Uses displayNumber (fallback to sessionNumber) in subtitle. |
components/Modals/SessionEditModal.tsx |
Uses displayNumber (fallback to sessionNumber) in title. |
components/Modals/DeleteSessionModal.tsx |
Uses displayNumber (fallback to sessionNumber) in confirmation header. |
__tests__/integration/services/streaks-coverage.test.ts |
Adjusts date handling for a streak test; adds a timezone helper. |
__tests__/integration/issues/issue-413-session-number-after-deletion.test.ts |
Adds integration tests covering Issue #413 scenarios. |
- Fix CRITICAL COALESCE type mismatch in session ordering (TEXT vs INTEGER) - Remove code duplication in API route, use sessionService layer - Add ordering assertions to test null startedDate scenarios - Add regression test for getNextSessionNumber with existing sessions - Remove unused imports from streaks test - Preserve chronological ordering with createdAt fallback All 4019 tests passing. Co-authored-by: GitHub Copilot <copilot@github.com>
|
@copilot please review this PR |
Here's a review of this PR: ✅ Overall Assessment: Ready to mergeThe PR correctly fixes the session numbering regression (#413) and all previous reviewer feedback has been addressed. CI is green (4016 passing tests). What's implementedRoot cause fix ( Display number computation ( Chronological ordering ( Frontend ( Previous reviewer feedback — all addressed ✅
Minor observations (non-blocking)
No security or logic issues found. 🚀 |
…tton After successfully deleting a read session, the DeleteSessionModal's submitting state remained true when the modal closed. This caused the 'Delete Session' button to be stuck showing 'Deleting...' and be disabled when the modal was reopened for a subsequent deletion. Added useEffect hook that resets submitting state to false whenever the modal closes (isOpen changes to false). This ensures clean state on each modal open and handles all close scenarios (success, cancel, X button). - Import useEffect from React - Add cleanup effect that watches isOpen prop - Reset submitting to false when modal closes - All 4019 tests pass
Summary
Fixes #413 - Session numbers now display correctly after deleting sessions.
Problem: When a session was deleted and a new one was created, the session number would increment incorrectly (e.g., showing "Read # 2" when it should be "Read # 1").
Root Cause: The
deleteSession()method hardcodedsessionNumber: 1when creating a new "to-read" session after deletion, which conflicted with thegetNextSessionNumber()logic that increments based on the highest existing session number.Solution: Implemented array index-based display numbers that are calculated from chronologically ordered sessions.
Changes
Backend
deleteSession()to usegetNextSessionNumber()instead of hardcoded1findAllByBookIdOrdered()method that sorts sessions byCOALESCE(startedDate, createdAt)for chronological orderinggetSessionsWithDisplayNumbers()method that calculatesdisplayNumberbased on array position (index + 1)/api/books/[id]/sessionsendpoint to includedisplayNumberin responsesFrontend
ReadingHistoryTab.tsxto displaydisplayNumber(with fallback tosessionNumber)displayNumber:SessionEditModal.tsxDeleteSessionModal.tsxSessionProgressModal.tsxTesting
__tests__/integration/issues/issue-413-session-number-after-deletion.test.tsBehavior
Before:
After:
getNextSessionNumber())Key Features:
sessionNumberpreserved for backward compatibilityTesting
Files Changed
lib/services/session.service.ts- Quick fix + display number service methodlib/repositories/session.repository.ts- Chronological ordering methodapp/api/books/[id]/sessions/route.ts- Add displayNumber to API responsecomponents/CurrentlyReading/ReadingHistoryTab.tsx- Display calculated numberscomponents/Modals/*.tsx- Update modals to use displayNumber__tests__/integration/issues/issue-413-session-number-after-deletion.test.ts- Integration testsManual Testing Needed