From 1dbf214199c0b41bc6ef5d5e2a355fc462917d0f Mon Sep 17 00:00:00 2001 From: CounterClops <19741838+CounterClops@users.noreply.github.com> Date: Tue, 25 Nov 2025 01:26:21 +1100 Subject: [PATCH] fix: kobo sync only update finished date when one doesn't already exist (#1614) * fix: only update finished date on kobo sync when an existing date doesn't exist * chore: add unit tests to confirm this is the case --- .../service/kobo/KoboReadingStateService.java | 4 +- .../service/KoboReadingStateServiceTest.java | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/kobo/KoboReadingStateService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/kobo/KoboReadingStateService.java index 43cc43187..246fb9ec3 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/kobo/KoboReadingStateService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/kobo/KoboReadingStateService.java @@ -185,7 +185,9 @@ public class KoboReadingStateService { if (progressPercent >= finishedThreshold) { userProgress.setReadStatus(ReadStatus.READ); - userProgress.setDateFinished(Instant.now()); + if (userProgress.getDateFinished() == null) { + userProgress.setDateFinished(Instant.now()); + } } else if (progressPercent >= readingThreshold) { userProgress.setReadStatus(ReadStatus.READING); } else { diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/KoboReadingStateServiceTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/KoboReadingStateServiceTest.java index 206efdab0..b75892bc2 100644 --- a/booklore-api/src/test/java/com/adityachandel/booklore/service/KoboReadingStateServiceTest.java +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/KoboReadingStateServiceTest.java @@ -216,6 +216,50 @@ class KoboReadingStateServiceTest { assertNotNull(savedProgress.getDateFinished()); } + @Test + @DisplayName("Should not overwrite existing finished date when syncing completed book") + void testSyncKoboProgressToUserBookProgress_PreserveExistingFinishedDate() { + String entitlementId = "100"; + testSettings.setProgressMarkAsFinishedThreshold(99f); + + Instant originalFinishedDate = Instant.parse("2025-01-15T10:30:00Z"); + UserBookProgressEntity existingProgress = new UserBookProgressEntity(); + existingProgress.setUser(testUserEntity); + existingProgress.setBook(testBook); + existingProgress.setKoboProgressPercent(99.5f); + existingProgress.setReadStatus(ReadStatus.READ); + existingProgress.setDateFinished(originalFinishedDate); + + KoboReadingState.CurrentBookmark bookmark = KoboReadingState.CurrentBookmark.builder() + .progressPercent(100) + .build(); + + KoboReadingState readingState = KoboReadingState.builder() + .entitlementId(entitlementId) + .currentBookmark(bookmark) + .build(); + + KoboReadingStateEntity entity = new KoboReadingStateEntity(); + when(mapper.toEntity(any())).thenReturn(entity); + when(mapper.toDto(any(KoboReadingStateEntity.class))).thenReturn(readingState); + when(repository.findByEntitlementId(entitlementId)).thenReturn(Optional.empty()); + when(repository.save(any())).thenReturn(entity); + when(bookRepository.findById(100L)).thenReturn(Optional.of(testBook)); + when(userRepository.findById(1L)).thenReturn(Optional.of(testUserEntity)); + when(progressRepository.findByUserIdAndBookId(1L, 100L)).thenReturn(Optional.of(existingProgress)); + + ArgumentCaptor progressCaptor = ArgumentCaptor.forClass(UserBookProgressEntity.class); + when(progressRepository.save(progressCaptor.capture())).thenReturn(existingProgress); + + service.saveReadingState(List.of(readingState)); + + UserBookProgressEntity savedProgress = progressCaptor.getValue(); + assertEquals(100.0f, savedProgress.getKoboProgressPercent()); + assertEquals(ReadStatus.READ, savedProgress.getReadStatus()); + assertEquals(originalFinishedDate, savedProgress.getDateFinished(), + "Existing finished date should not be overwritten during sync"); + } + @Test @DisplayName("Should mark book as READING when progress exceeds reading threshold") void testSyncKoboProgressToUserBookProgress_MarkAsReading() {