Add QR codes to cards, stop inventing call number parts

Cards now carry a QR code holding the book's own record (call number,
title, authors, ISBN, year), and the scanner reads it back to find or
add that book.

Call number selection no longer pads catalogue numbers with generated
cutters. A number is "exact" only when it comes verbatim from a record
for this edition; a bare class is a hint that steers the other-edition
and similar-books lookups; a record fetched by LCCN must list the
book's ISBN to count as this edition. "Look up again" bypasses every
cached result, and shelf order breaks ties by author then title.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-19 12:53:12 -07:00
1 parent 6cec404e21
commit ad2956e868
56 files changed
+4020 -156

No files matched your search

+28 -3
View File
@@ -1,9 +1,11 @@
import { mkdirSync, readdirSync, unlinkSync, writeFileSync } from 'node:fs';
import { compareShelf } from '../shared/shelf.ts';
import { join } from 'node:path';
import { Hono } from 'hono';
import type { Context } from 'hono';
import type { Book, CardData, SearchResult, Settings } from '../shared/types.ts';
import { normalizeIsbn } from '../shared/isbn.ts';
import { parseCardCode } from '../shared/cardcode.ts';
import { formatCallNumber, parseCallNumber, sortKeyOfRaw } from '../shared/callnumber.ts';
import type { Db, Filter, NewBook } from './db.ts';
import type { Jobs, ResolverLike } from './jobs.ts';
@@ -153,6 +155,26 @@ export function createApp(deps: AppDeps): Hono {
return c.json({ book: addPending({ lccn, title: lccn }).book });
});
// A card's QR code carries the whole record, so the book is made straight from it, with no lookup. It is not
// queued for printing: it already has its card.
app.post('/api/books/from-card', async (c) => {
const text = (await body(c)).text;
const card = typeof text === 'string' ? parseCardCode(text) : null;
if (!card) throw new HttpError(400, 'Not a Booksorter card');
const sortKey = sortKeyOfRaw(card.callNumber);
const title = card.title.trim();
const same = (b: Book) => b.title.trim().toLowerCase() === title.toLowerCase() && b.sortKey === sortKey;
const existing = card.isbn13 ? db.findByIsbn(card.isbn13) : db.listBooks().find(same) ?? null;
if (existing) return c.json({ book: existing, existed: true });
const { book, created } = db.insertPending({
isbn13: card.isbn13, title: title || card.callNumber, authors: card.authors, year: card.year,
callNumber: card.callNumber, sortKey, confidence: card.approximate ? 'approximate' : 'exact', source: 'card',
status: 'resolved', manualOverride: false, inQueue: false,
});
if (created) events.emitBook(book);
return c.json({ book, existed: !created });
});
app.get('/api/search', async (c) => {
const title = c.req.query('title')?.trim() ?? '';
const author = c.req.query('author')?.trim() ?? '';
@@ -166,7 +188,8 @@ export function createApp(deps: AppDeps): Hono {
const title = r && str(r.title);
if (!r || !title) throw new HttpError(400, 'A search result is required');
const { book } = addPending({
title, authors: strList(r.authors) ?? [], year: yearOf(r.year), publisher: str(r.publisher), lccn: str(r.lccn),
title, authors: strList(r.authors) ?? [], year: yearOf(r.year), publisher: str(r.publisher),
// (The result's LCCN is a work-level one: it may belong to another edition, so it is not saved.)
});
return c.json({ book });
});
@@ -238,9 +261,11 @@ export function createApp(deps: AppDeps): Hono {
// "Look up again" must really ask again, so forget what the sources told us for this book.
for (const key of cacheKeys(book)) db.cache.delete(key);
if (book.title) db.cache.deletePrefix(`loc-title:${book.title}|`);
// The title search and the similar-books searches are keyed by words as the lookup saw them (not necessarily
// as stored now), so the lookup itself is told to ignore its cache and write fresh answers.
const updated = db.updateBook(book.id, { status: 'pending', attentionReason: null })!;
events.emitBook(updated);
jobs.enqueue(book.id);
jobs.enqueue(book.id, { fresh: true });
return c.json({ book: updated });
});
@@ -249,7 +274,7 @@ export function createApp(deps: AppDeps): Hono {
const startPosition = (s === 2 || s === 3 ? s : 1) as 1 | 2 | 3;
const books = db.listBooks({ filter: 'queue' })
.filter((b) => b.status === 'resolved' && b.callNumber)
.sort((a, b) => ((a.sortKey ?? '~') < (b.sortKey ?? '~') ? -1 : (a.sortKey ?? '~') > (b.sortKey ?? '~') ? 1 : 0));
.sort(compareShelf);
if (!books.length) throw new HttpError(400, 'Nothing to print');
const cards: CardData[] = books.map((b) => {
const cn = parseCallNumber(b.callNumber!);