From e03469cf774c93b37372467b7aba051458d73a70 Mon Sep 17 00:00:00 2001 From: KodeStar Date: Sun, 22 Nov 2026 17:07:09 +0000 Subject: [PATCH 1/8] Make admin author, narrator and series spelling merges atomic Merging spellings of a person or a series is now one admin request that rewrites every affected book in one transaction, so a failure or a closed tab no longer leaves some books merged and others not. Undo restores the prior values and their sources through the existing bulk-edit path. Fixes part of #224 (People merge isn't atomic). --- CONVENTIONS.md | 7 +- admin-ui/src/api/client.ts | 9 ++ admin-ui/src/api/types.ts | 9 ++ admin-ui/src/features/health/health.test.tsx | 22 ++-- .../src/features/health/series-spellings.tsx | 37 +++--- .../library/people/merge-suggestions.tsx | 6 +- .../library/people/people-catalog.test.tsx | 103 ++++++++-------- .../library/people/people-model.test.ts | 76 ++++-------- .../features/library/people/people-model.ts | 65 +++------- .../src/features/library/people/use-merge.ts | 74 ++---------- internal/api/api.go | 1 + internal/api/handlers_catalog.go | 29 +++++ internal/api/handlers_catalog_test.go | 80 ++++++++++++ internal/catalog/merge.go | 92 ++++++++++++++ internal/catalog/merge_test.go | 114 ++++++++++++++++++ internal/catalog/seriesspellings_test.go | 11 +- 16 files changed, 490 insertions(+), 245 deletions(-) create mode 100644 internal/catalog/merge.go create mode 100644 internal/catalog/merge_test.go diff --git a/CONVENTIONS.md b/CONVENTIONS.md index 3f7777dbf..670110b59 100644 --- a/CONVENTIONS.md +++ b/CONVENTIONS.md @@ -2552,6 +2552,9 @@ 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. Its `books` + reply identifies each changed path and its prior override value and source for bulk Undo. `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 +3197,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..4d8763d9a 100644 --- a/admin-ui/src/api/client.ts +++ b/admin-ui/src/api/client.ts @@ -68,6 +68,8 @@ import type { SessionPage, NarratorsResponse, PeopleResponse, + MergedBook, + MergeField, PersonField, SeriesCount, AdminLibrary, @@ -837,6 +839,13 @@ 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, + }), /** 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..e151cb852 100644 --- a/admin-ui/src/api/types.ts +++ b/admin-ui/src/api/types.ts @@ -1835,6 +1835,15 @@ 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'; +} + /** 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..4aaab46ad 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'; @@ -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={ -