fix: Refactor HardcoverSyncService to improve edition and page count (#2389)
* fix: Refactor HardcoverSyncService to improve edition and page count fetching logic. Always fetch edition and page info from Hardcover when a stored book ID is available. Update variable names for clarity and ensure fallback mechanisms are in place for missing data. * fix: Update key in HardcoverSyncServiceTest to reflect correct edition ID naming convention * feat: Enhance reading progress tracking in HardcoverSyncService by adding isFinished parameter to upsert methods. This allows for recording completion dates when a book is finished. --------- Co-authored-by: akiraslingshot <akiraslingshot@gmail.com>
This commit is contained in:
co-authored by
akiraslingshot
parent
7c3c0bba01
commit
67c12223ea
+45
-36
@@ -95,22 +95,19 @@ public class HardcoverSyncService {
|
|||||||
// Find the book on Hardcover - use stored ID if available
|
// Find the book on Hardcover - use stored ID if available
|
||||||
HardcoverBookInfo hardcoverBook;
|
HardcoverBookInfo hardcoverBook;
|
||||||
if (metadata.getHardcoverBookId() != null) {
|
if (metadata.getHardcoverBookId() != null) {
|
||||||
// Use the stored numeric book ID directly
|
// Use the stored numeric book ID and fetch edition/page info from Hardcover
|
||||||
hardcoverBook = new HardcoverBookInfo();
|
hardcoverBook = new HardcoverBookInfo();
|
||||||
hardcoverBook.bookId = metadata.getHardcoverBookId();
|
hardcoverBook.bookId = metadata.getHardcoverBookId();
|
||||||
hardcoverBook.pages = metadata.getPageCount();
|
|
||||||
log.debug("Using stored Hardcover book ID: {}", hardcoverBook.bookId);
|
log.debug("Using stored Hardcover book ID: {}", hardcoverBook.bookId);
|
||||||
|
|
||||||
if (hardcoverBook.bookId != null && (hardcoverBook.pages == null || hardcoverBook.pages <= 0 || hardcoverBook.editionId == null)) {
|
// Always fetch the default edition and page count from Hardcover
|
||||||
HardcoverBookInfo fetched = findHardcoverBookById(hardcoverBook.bookId);
|
HardcoverBookInfo fetched = findHardcoverBookById(hardcoverBook.bookId);
|
||||||
if (fetched != null) {
|
if (fetched != null) {
|
||||||
if (hardcoverBook.editionId == null) {
|
hardcoverBook.editionId = fetched.editionId;
|
||||||
hardcoverBook.editionId = fetched.editionId;
|
hardcoverBook.pages = fetched.pages;
|
||||||
}
|
log.debug("Fetched from Hardcover: editionId={}, pages={}", hardcoverBook.editionId, hardcoverBook.pages);
|
||||||
if ((hardcoverBook.pages == null || hardcoverBook.pages <= 0) && fetched.pages != null) {
|
} else {
|
||||||
hardcoverBook.pages = fetched.pages;
|
log.warn("Could not fetch edition info from Hardcover for book ID: {}", hardcoverBook.bookId);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Search by ISBN
|
// Search by ISBN
|
||||||
@@ -141,7 +138,8 @@ public class HardcoverSyncService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Create or update the reading progress
|
// Step 2: Create or update the reading progress
|
||||||
boolean success = upsertReadingProgress(userBookId, hardcoverBook.editionId, progressPages);
|
boolean isFinished = progressPercent >= 99.0f;
|
||||||
|
boolean success = upsertReadingProgress(userBookId, hardcoverBook.editionId, progressPages, isFinished);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
log.info("Synced progress to Hardcover: userId={}, book={}, hardcoverBookId={}, progress={}% ({}pages)",
|
log.info("Synced progress to Hardcover: userId={}, book={}, hardcoverBookId={}, progress={}% ({}pages)",
|
||||||
@@ -244,19 +242,19 @@ public class HardcoverSyncService {
|
|||||||
info.pages = ((Number) pagesObj).intValue();
|
info.pages = ((Number) pagesObj).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to get default_edition_id from the search results
|
// Try to get default_physical_edition_id from the search results
|
||||||
Object defaultEditionObj = document.get("default_edition_id");
|
Object defaultPhysicalEditionObj = document.get("default_physical_edition_id");
|
||||||
if (defaultEditionObj instanceof Number) {
|
if (defaultPhysicalEditionObj instanceof Number) {
|
||||||
info.editionId = ((Number) defaultEditionObj).intValue();
|
info.editionId = ((Number) defaultPhysicalEditionObj).intValue();
|
||||||
} else if (defaultEditionObj instanceof String) {
|
} else if (defaultPhysicalEditionObj instanceof String) {
|
||||||
try {
|
try {
|
||||||
info.editionId = Integer.parseInt((String) defaultEditionObj);
|
info.editionId = Integer.parseInt((String) defaultPhysicalEditionObj);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
// Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no default edition, try to look up edition by ISBN
|
// If no default physical edition found, try to look up edition by ISBN as fallback
|
||||||
if (info.bookId != null && info.editionId == null) {
|
if (info.bookId != null && info.editionId == null) {
|
||||||
EditionInfo edition = findEditionByIsbn(info.bookId, isbn);
|
EditionInfo edition = findEditionByIsbn(info.bookId, isbn);
|
||||||
if (edition != null) {
|
if (edition != null) {
|
||||||
@@ -264,10 +262,12 @@ public class HardcoverSyncService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.editionId != null && (info.pages == null || info.pages <= 0)) {
|
// Fetch page count from the edition (prioritizing edition page count over book-level page count)
|
||||||
|
if (info.editionId != null) {
|
||||||
EditionInfo edition = findEditionById(info.editionId);
|
EditionInfo edition = findEditionById(info.editionId);
|
||||||
if (edition != null && edition.pages != null && edition.pages > 0) {
|
if (edition != null && edition.pages != null && edition.pages > 0) {
|
||||||
info.pages = edition.pages;
|
info.pages = edition.pages;
|
||||||
|
log.debug("Using page count from edition {}: {} pages", info.editionId, info.pages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,7 +388,7 @@ public class HardcoverSyncService {
|
|||||||
query FindBookById($bookId: Int!) {
|
query FindBookById($bookId: Int!) {
|
||||||
books(where: {id: {_eq: $bookId}}, limit: 1) {
|
books(where: {id: {_eq: $bookId}}, limit: 1) {
|
||||||
id
|
id
|
||||||
default_edition_id
|
default_physical_edition_id
|
||||||
pages
|
pages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -412,26 +412,29 @@ public class HardcoverSyncService {
|
|||||||
HardcoverBookInfo info = new HardcoverBookInfo();
|
HardcoverBookInfo info = new HardcoverBookInfo();
|
||||||
info.bookId = bookId;
|
info.bookId = bookId;
|
||||||
|
|
||||||
Object defaultEditionObj = book.get("default_edition_id");
|
Object defaultPhysicalEditionObj = book.get("default_physical_edition_id");
|
||||||
if (defaultEditionObj instanceof Number) {
|
if (defaultPhysicalEditionObj instanceof Number) {
|
||||||
info.editionId = ((Number) defaultEditionObj).intValue();
|
info.editionId = ((Number) defaultPhysicalEditionObj).intValue();
|
||||||
} else if (defaultEditionObj instanceof String) {
|
} else if (defaultPhysicalEditionObj instanceof String) {
|
||||||
try {
|
try {
|
||||||
info.editionId = Integer.parseInt((String) defaultEditionObj);
|
info.editionId = Integer.parseInt((String) defaultPhysicalEditionObj);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
// Ignore
|
// Ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get pages from the book level first
|
||||||
Object pagesObj = book.get("pages");
|
Object pagesObj = book.get("pages");
|
||||||
if (pagesObj instanceof Number) {
|
if (pagesObj instanceof Number) {
|
||||||
info.pages = ((Number) pagesObj).intValue();
|
info.pages = ((Number) pagesObj).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.editionId != null && (info.pages == null || info.pages <= 0)) {
|
// If we have an edition ID, fetch the page count from that edition
|
||||||
|
if (info.editionId != null) {
|
||||||
EditionInfo edition = findEditionById(info.editionId);
|
EditionInfo edition = findEditionById(info.editionId);
|
||||||
if (edition != null && edition.pages != null && edition.pages > 0) {
|
if (edition != null && edition.pages != null && edition.pages > 0) {
|
||||||
info.pages = edition.pages;
|
info.pages = edition.pages;
|
||||||
|
log.debug("Using page count from default physical edition {}: {} pages", info.editionId, info.pages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -552,21 +555,21 @@ public class HardcoverSyncService {
|
|||||||
/**
|
/**
|
||||||
* Create or update reading progress for a user_book.
|
* Create or update reading progress for a user_book.
|
||||||
*/
|
*/
|
||||||
private boolean upsertReadingProgress(Integer userBookId, Integer editionId, int progressPages) {
|
private boolean upsertReadingProgress(Integer userBookId, Integer editionId, int progressPages, boolean isFinished) {
|
||||||
log.info("upsertReadingProgress: userBookId={}, editionId={}, progressPages={}",
|
log.info("upsertReadingProgress: userBookId={}, editionId={}, progressPages={}, isFinished={}",
|
||||||
userBookId, editionId, progressPages);
|
userBookId, editionId, progressPages, isFinished);
|
||||||
|
|
||||||
// First, try to find existing user_book_read
|
// First, try to find existing user_book_read
|
||||||
Integer existingReadId = findExistingUserBookRead(userBookId);
|
Integer existingReadId = findExistingUserBookRead(userBookId);
|
||||||
|
|
||||||
if (existingReadId != null) {
|
if (existingReadId != null) {
|
||||||
// Update existing
|
// Update existing
|
||||||
log.info("Updating existing user_book_read: id={}", existingReadId);
|
log.info("Updating existing user_book_read: id={}", existingReadId);
|
||||||
return updateUserBookRead(existingReadId, editionId, progressPages);
|
return updateUserBookRead(existingReadId, editionId, progressPages, isFinished);
|
||||||
} else {
|
} else {
|
||||||
// Create new
|
// Create new
|
||||||
log.info("Creating new user_book_read for userBookId={}", userBookId);
|
log.info("Creating new user_book_read for userBookId={}", userBookId);
|
||||||
return insertUserBookRead(userBookId, editionId, progressPages);
|
return insertUserBookRead(userBookId, editionId, progressPages, isFinished);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -606,7 +609,7 @@ public class HardcoverSyncService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean insertUserBookRead(Integer userBookId, Integer editionId, int progressPages) {
|
private boolean insertUserBookRead(Integer userBookId, Integer editionId, int progressPages, boolean isFinished) {
|
||||||
String mutation = """
|
String mutation = """
|
||||||
mutation InsertUserBookRead($userBookId: Int!, $object: DatesReadInput!) {
|
mutation InsertUserBookRead($userBookId: Int!, $object: DatesReadInput!) {
|
||||||
insert_user_book_read(user_book_id: $userBookId, user_book_read: $object) {
|
insert_user_book_read(user_book_id: $userBookId, user_book_read: $object) {
|
||||||
@@ -621,6 +624,9 @@ public class HardcoverSyncService {
|
|||||||
Map<String, Object> readInput = new java.util.HashMap<>();
|
Map<String, Object> readInput = new java.util.HashMap<>();
|
||||||
readInput.put("started_at", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
|
readInput.put("started_at", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
|
||||||
readInput.put("progress_pages", progressPages);
|
readInput.put("progress_pages", progressPages);
|
||||||
|
if (isFinished) {
|
||||||
|
readInput.put("finished_at", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
|
||||||
|
}
|
||||||
if (editionId != null) {
|
if (editionId != null) {
|
||||||
readInput.put("edition_id", editionId);
|
readInput.put("edition_id", editionId);
|
||||||
}
|
}
|
||||||
@@ -650,7 +656,7 @@ public class HardcoverSyncService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean updateUserBookRead(Integer readId, Integer editionId, int progressPages) {
|
private boolean updateUserBookRead(Integer readId, Integer editionId, int progressPages, boolean isFinished) {
|
||||||
String mutation = """
|
String mutation = """
|
||||||
mutation UpdateUserBookRead($id: Int!, $object: DatesReadInput!) {
|
mutation UpdateUserBookRead($id: Int!, $object: DatesReadInput!) {
|
||||||
update_user_book_read(id: $id, object: $object) {
|
update_user_book_read(id: $id, object: $object) {
|
||||||
@@ -665,6 +671,9 @@ public class HardcoverSyncService {
|
|||||||
|
|
||||||
Map<String, Object> readInput = new java.util.HashMap<>();
|
Map<String, Object> readInput = new java.util.HashMap<>();
|
||||||
readInput.put("progress_pages", progressPages);
|
readInput.put("progress_pages", progressPages);
|
||||||
|
if (isFinished) {
|
||||||
|
readInput.put("finished_at", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
|
||||||
|
}
|
||||||
if (editionId != null) {
|
if (editionId != null) {
|
||||||
readInput.put("edition_id", editionId);
|
readInput.put("edition_id", editionId);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -500,7 +500,7 @@ class HardcoverSyncServiceTest {
|
|||||||
Map<String, Object> bookResponse = new HashMap<>();
|
Map<String, Object> bookResponse = new HashMap<>();
|
||||||
Map<String, Object> bookData = new HashMap<>();
|
Map<String, Object> bookData = new HashMap<>();
|
||||||
Map<String, Object> book = new HashMap<>();
|
Map<String, Object> book = new HashMap<>();
|
||||||
book.put("default_edition_id", "88");
|
book.put("default_physical_edition_id", "88");
|
||||||
bookData.put("books", List.of(book));
|
bookData.put("books", List.of(book));
|
||||||
bookResponse.put("data", bookData);
|
bookResponse.put("data", bookData);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user