diff --git a/CONVENTIONS.md b/CONVENTIONS.md index 3f7777dbf..326c229ee 100644 --- a/CONVENTIONS.md +++ b/CONVENTIONS.md @@ -2552,6 +2552,13 @@ every session. CLAUDE.md keeps a one-line index of these topics. `DirectPlayable` in SQL) + `/admin/books/facets` (each dimension counted without its own filter, the unfiltered yes/no ones in one pass); `POST /admin/books/bulk` (one edit over <= 1000 books, all or nothing); + `POST /admin/books/merge` (`{field, sources, target, library_id?}`) selects exact whole credits + or main series spellings and edits all matching books in one writer transaction (at most + `catalog.MaxMergeBooks`, else 400 `too_large`; an unknown `library_id` 404, a podcast one 409). Its + `books` reply identifies each changed path and its prior override value and source; Undo posts it + back with `field` and `target` to `POST /admin/books/merge/undo` (`catalog.UndoMerge`, one + transaction): a book still carrying `target` as its override goes back, one gone (`not_found`) or + edited since (`changed`) is left and listed in `skipped`, beside `restored`; `GET /admin/authors|narrators` (each person a credit names, `catalog.creditNames` over `names.Split`, so a co-credit counts for both; `merge_suggestions` stay over WHOLE credits, from `personKey`, keyed by `match.Fold`, which keeps every script's @@ -3194,8 +3201,8 @@ every session. CLAUDE.md keeps a one-line index of these topics. its library spells its main series another way, same `series_key`; listed per series by `GET /admin/issues/series-spellings[?library_id=&ignored=true]` -> `{groups: [{library_id, suggested, spellings: [{name, books}], books}]}`, from the same predicate, `suggested` being `seriesName`; - open = a book not ignored, ignored view = a book ignored; the console's fix is the author merge's bulk - `series` edit), `split_discs` (one book split across disc folders, listed by + open = a book not ignored, ignored view = a book ignored; the console merges the listed spellings with + `POST /admin/books/merge`), `split_discs` (one book split across disc folders, listed by its lowest disc number, with ties ordered by path. The ignore belongs to `books.split_parent`. That folder must have no override; the console's fix sets `book` on it, which joins them; its discs never also group as duplicates). `split_parent` is worked out during discovery (`library.markSplitDiscs`: a folder book whose folder is a disc diff --git a/admin-ui/src/api/client.ts b/admin-ui/src/api/client.ts index 681a8581e..061c78ebe 100644 --- a/admin-ui/src/api/client.ts +++ b/admin-ui/src/api/client.ts @@ -68,6 +68,9 @@ import type { SessionPage, NarratorsResponse, PeopleResponse, + MergedBook, + MergeField, + MergeUndone, PersonField, SeriesCount, AdminLibrary, @@ -837,6 +840,16 @@ export const api = { /** One field edit over many books, all or nothing (at most BULK_LIMIT). */ bulkEdit: (books: BookRef[], edit: Omit) => request<{ updated: number }>('POST', '/admin/books/bulk', { books, ...edit }), + mergeSpellings: (field: MergeField, sources: string[], target: string, libraryId?: number) => + request<{ books: MergedBook[] }>('POST', '/admin/books/merge', { + field, + sources, + target, + library_id: libraryId, + }), + /** Puts a merge's books back (its reply sent back) in one transaction. */ + undoMerge: (field: MergeField, target: string, books: MergedBook[]) => + request('POST', '/admin/books/merge/undo', { field, target, books }), /** GET /admin/authors or /admin/narrators, as one shape. */ people: async (field: PersonField, libraryId?: number): Promise => { const query = bookQuery({ library_id: libraryId }); diff --git a/admin-ui/src/api/types.ts b/admin-ui/src/api/types.ts index ada2a2cee..53b68841a 100644 --- a/admin-ui/src/api/types.ts +++ b/admin-ui/src/api/types.ts @@ -1835,6 +1835,22 @@ export interface NarratorsResponse { /** The two people aggregates (each person a credit names is one; see PersonCount). */ export type PersonField = 'author' | 'narrator'; +export type MergeField = PersonField | 'series'; + +/** One changed book from POST /admin/books/merge. A null previous value means + * Undo should remove the field override and follow the book's metadata again. */ +export interface MergedBook extends BookRef { + previous: string | null; + source?: 'edited' | 'community'; +} + +/** POST /admin/books/merge/undo: how many books went back, and the books left as + * they are (gone from the index, or their field edited since the merge). */ +export interface MergeUndone { + restored: number; + skipped: (BookRef & { reason: 'not_found' | 'changed' })[]; +} + /** Either people aggregate as the console reads it (api.people; not a wire shape). */ export interface PeopleResponse { people: PersonCount[]; diff --git a/admin-ui/src/features/health/health.test.tsx b/admin-ui/src/features/health/health.test.tsx index e681110c0..b0c8d216c 100644 --- a/admin-ui/src/features/health/health.test.tsx +++ b/admin-ui/src/features/health/health.test.tsx @@ -1420,7 +1420,14 @@ describe('library health', () => { 'GET /admin/books': (req) => ({ body: { books: req.query.get('series') ? tagged : [] }, }), - 'POST /admin/books/bulk': { body: { updated: 0 } }, + 'POST /admin/books/merge': { + body: { + books: [ + { library_id: 1, path: 'M/1', previous: null }, + { library_id: 1, path: 'M/3', previous: null }, + ], + }, + }, 'POST /admin/issues/ignore': { status: 204 }, }), ); @@ -1437,17 +1444,14 @@ describe('library health', () => { const keep = screen.getByRole('combobox', { name: 'Spelling to keep' }); await user.selectOptions(keep, 'He Who Fights With Monsters'); expect(screen.getByText(/2 books are tagged with the other spelling/)).toBeInTheDocument(); - await user.selectOptions(keep, 'He Who Fights with Monsters'); await user.click(screen.getByRole('button', { name: 'Use this spelling' })); await waitFor(() => - expect(calls.find((c) => c.path === '/admin/books/bulk')?.body).toEqual({ - books: [ - { library_id: 1, path: 'M/2' }, - { library_id: 1, path: 'M/3' }, - { library_id: 1, path: 'M/4' }, - ], - set: { series: 'He Who Fights with Monsters' }, + expect(calls.find((c) => c.path === '/admin/books/merge')?.body).toEqual({ + library_id: 1, + field: 'series', + sources: ['He Who Fights with Monsters', 'He Who Fights WIth Monsters'], + target: 'He Who Fights With Monsters', }), ); diff --git a/admin-ui/src/features/health/series-spellings.tsx b/admin-ui/src/features/health/series-spellings.tsx index 203531956..a8e6b6f88 100644 --- a/admin-ui/src/features/health/series-spellings.tsx +++ b/admin-ui/src/features/health/series-spellings.tsx @@ -1,14 +1,14 @@ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Eye, EyeOff, Loader2, Merge } from 'lucide-react'; +import { api } from '@/api/client'; import { useSeriesSpellings } from '@/api/hooks'; import type { AdminBook, SeriesSpellingGroup } from '@/api/types'; import { Notice } from '@/components/notice'; import { QueryError } from '@/components/query-error'; import { Button } from '@/components/ui/button'; import { NativeSelect } from '@/components/ui/native-select'; -import { otherSpellings } from '@/features/library/people/people-model'; -import { booksWhere, useMerge } from '@/features/library/people/use-merge'; +import { useMerge } from '@/features/library/people/use-merge'; import { toastError } from '@/lib/errors'; import { counted, formatList } from '@/lib/format'; import { seriesKey } from '@/lib/series-key'; @@ -18,7 +18,7 @@ import { useIssueActions } from './use-issue-actions'; /** * Health > Series spelled several ways: one notice per series a library's books * spell more than one way, offering to set one spelling on every book of it (the - * Authors page's merge: a bulk edit, files untouched), the Series page's name + * Authors page's merge: one server transaction, files untouched), the Series page's name * first. Ignore stops a series being suggested; with `ignored`, those series, each * with Show again. */ @@ -58,12 +58,21 @@ export function SeriesSpellings({ ignored }: { ignored: boolean }) { * spelling (its main series; the series filter also finds the books listing it * among their other series, which a rename doesn't touch). */ -function seriesBooks(g: SeriesSpellingGroup): Promise { +async function seriesBooks(g: SeriesSpellingGroup): Promise { const key = seriesKey(g.suggested); - return booksWhere( - { series: g.suggested, library_id: g.library_id }, - (b) => b.series !== '' && seriesKey(b.series) === key, - ); + const books: AdminBook[] = []; + let cursor: string | undefined; + do { + const page = await api.adminBooks({ + series: g.suggested, + library_id: g.library_id, + limit: 200, + cursor, + }); + books.push(...page.books.filter((b) => b.series !== '' && seriesKey(b.series) === key)); + cursor = page.next_cursor; + } while (cursor); + return books; } function SpellingNotice({ group: g, ignored }: { group: SeriesSpellingGroup; ignored: boolean }) { @@ -75,7 +84,7 @@ function SpellingNotice({ group: g, ignored }: { group: SeriesSpellingGroup; ign const [ignoring, setIgnoring] = useState(false); const names = g.spellings.map((s) => s.name); const chosen = names.includes(keep) ? keep : g.suggested; - const others = otherSpellings({ names, suggested: chosen, books: g.books, other_books: 0 }); + const others = names.filter((name) => name !== chosen); const rewrites = g.books - (g.spellings.find((s) => s.name === chosen)?.books ?? 0); const busy = merging !== null || ignoring; @@ -126,17 +135,7 @@ function SpellingNotice({ group: g, ignored }: { group: SeriesSpellingGroup; ign ))} - diff --git a/admin-ui/src/features/library/people/merge-suggestions.tsx b/admin-ui/src/features/library/people/merge-suggestions.tsx index 49c2d8446..7f0b76c46 100644 --- a/admin-ui/src/features/library/people/merge-suggestions.tsx +++ b/admin-ui/src/features/library/people/merge-suggestions.tsx @@ -45,7 +45,11 @@ export function MergeSuggestions({ suggested: s.suggested, })} actions={ -