export type ReadStatus = "unread" | "want_to_read" | "reading" | "rereading" | "on_hold" | "read" | "skimmed" | "abandoned"; /** What the sync knows about one BookOrbit book, apart from progress. Dates are `YYYY-MM-DD`. */ export interface BookSnapshot { bookId: number; title: string; authors: string[]; isbn13: string | null; /** The Goodreads edition that the user paired with this book in BookOrbit. */ goodreadsId: string | null; status: ReadStatus; startedOn: string | null; finishedOn: string | null; /** Whole stars from 1 to 5. */ rating: number | null; review: string | null; } export type GoodreadsShelf = "to-read" | "currently-reading" | "read" | "paused" | "did-not-finish"; export interface GoodreadsBookFields { shelf: GoodreadsShelf; rating: number | null; /** `YYYY-MM-DD`. Goodreads has no start date, so none is sent. */ readAt: string | null; review: string | null; } export type BookHiveStatus = | "buzz.bookhive.defs#wantToRead" | "buzz.bookhive.defs#reading" | "buzz.bookhive.defs#finished" | "buzz.bookhive.defs#abandoned"; export interface BookHiveBookFields { status: BookHiveStatus; /** Half stars from 1 to 10. */ stars: number | null; /** ISO datetimes. */ startedAt: string | null; finishedAt: string | null; review: string | null; } const GOODREADS_SHELF_BY_STATUS: Record = { unread: null, want_to_read: "to-read", reading: "currently-reading", rereading: "currently-reading", on_hold: "paused", read: "read", skimmed: "read", abandoned: "did-not-finish", }; // BookHive has no paused status, so a book on hold stays a book in progress. const BOOKHIVE_STATUS_BY_STATUS: Record = { unread: null, want_to_read: "buzz.bookhive.defs#wantToRead", reading: "buzz.bookhive.defs#reading", rereading: "buzz.bookhive.defs#reading", on_hold: "buzz.bookhive.defs#reading", read: "buzz.bookhive.defs#finished", skimmed: "buzz.bookhive.defs#finished", abandoned: "buzz.bookhive.defs#abandoned", }; const BOOKHIVE_STARS_PER_BOOKORBIT_STAR = 2; const MIN_PROGRESS_PERCENT = 0; const MAX_PROGRESS_PERCENT = 100; /** Null for an unread book: setting a book back to unread changes nothing on the targets. */ export function toGoodreadsFields(book: BookSnapshot): GoodreadsBookFields | null { const shelf = GOODREADS_SHELF_BY_STATUS[book.status]; if (!shelf) return null; return { shelf, rating: book.rating, readAt: book.finishedOn, review: book.review }; } /** Null for an unread book: setting a book back to unread changes nothing on the targets. */ export function toBookHiveFields(book: BookSnapshot): BookHiveBookFields | null { const status = BOOKHIVE_STATUS_BY_STATUS[book.status]; if (!status) return null; return { status, stars: book.rating === null ? null : book.rating * BOOKHIVE_STARS_PER_BOOKORBIT_STAR, startedAt: toStartOfDayIso(book.startedOn), finishedAt: toStartOfDayIso(book.finishedOn), review: book.review, }; } /** Both targets take a whole percentage. */ export function toProgressPercent(endProgress: number): number { return Math.min(MAX_PROGRESS_PERCENT, Math.max(MIN_PROGRESS_PERCENT, Math.round(endProgress))); } function toStartOfDayIso(date: string | null): string | null { return date === null ? null : `${date}T00:00:00.000Z`; }