diff --git a/TODO.md b/TODO.md index bd33c28cf4..490e5d35a8 100644 --- a/TODO.md +++ b/TODO.md @@ -257,10 +257,14 @@ re-rendering. Migration is deliberately gradual and hardest-first. - **Slice 4 — the long tail.** Questions and `history/:hmac` remain. Groups, holidays, users, built sites, and attendee statuses are complete. -- **Slice 5 — generalize `system_notes`** from attendee-only to - `(entity_type, entity_id)` so any entity page can carry a notes section. The - notes DB module currently has no `entity_type` column; this is a small - migration + query change that unblocks notes tabs on the other entities. +- **Slice 5 — generalize `system_notes`: done.** A note now names the kind of + record it is about and which one (`entity_type`, `entity_id`), and the notes + module works in those terms (`src/shared/db/notes/`). What remains is per-page + work: add `"listing"` (or whichever record is next) to `NOTE_ENTITIES` in + `notes/target.ts`, give that record's delete path the notes delete statement, + and add the notes section to its entity page. The list drives the database + `CHECK`, so adding a kind ships with a small rebuild migration — as + `2026-06-20_free_text_questions` did for `questions.display_type`. --- @@ -1846,3 +1850,31 @@ as the supervisor is up, closing the gap. That means changing what and thinking again about the startup grace, which exists because a process id can be given to somebody else after the original has gone. Start at `RUN_STARTUP_GRACE_MS` in `isolation-state.ts` and the comment above it. + +## Four feature modules have no test at their mirrored path + +*Origin: `deno task precommit:mutation` on the notes-migration branch, which +could not start.* + +The mutation gate refuses to run a source that has mutants but no test at its +mirrored path under `test/`. Four modules are in that state: + +- `src/features/admin/attendee-page.ts` (55 mutants) +- `src/features/admin/attendees-list.ts` (37) +- `src/features/admin/listing-page-data.ts` (45) +- `src/features/api/payment-processing/store-refund.ts` (29) + +Nothing needs moving: no test imports any of them. They are reached only +through the app, by integration and Cucumber journeys, so each needs a direct +test written at `test/features/…` to match its path. + +This blocks the gate for *any* branch that touches one of them, however small +the change — the notes migration only swapped an import in each. Until they +have direct tests, a branch touching them can prove its own work with a +targeted `deno task mutation ` run instead. + +`src/features/admin/attendee-notes.ts` was in the same state and is now fixed: +its route suite drives real pages through the session helpers, so it moved from +`test/integration/admin/` to `test/features/admin/`, which is where that kind +of suite belongs (see "Let the misplaced-test list see past request helpers" +above). The other four have no such suite to move. diff --git a/docs/importer-plan.md b/docs/importer-plan.md index 44b08b2382..99dd758ac5 100644 --- a/docs/importer-plan.md +++ b/docs/importer-plan.md @@ -313,8 +313,9 @@ Attendee notes / legacy metadata storage: - The original plan added a per-attendee encrypted `notes` column as the single home for `Customer Notes`, `Operator Notes`, payment metadata, etc. The notes - rework has since **landed**: main added a per-attendee **`system_notes`** table - (`src/shared/db/system-notes.ts`) — owner-public-key-encrypted `owner` notes + rework has since **landed**: main added a **`system_notes`** table, whose rows + name the kind of record they are about and which one + (`src/shared/db/notes/queries.ts`) — owner-public-key-encrypted `owner` notes (`createOwnerNote`) plus DB-key `system` notes — alongside per-contact `/admin/history/:hmac` records. **Do not** add a competing per-attendee `notes` column; `system_notes` is the home. @@ -1429,10 +1430,12 @@ Implementation notes: raw-deletes the source attendee and never touches `system_notes`, so the imported audit trail (stored as a `system_notes` owner note) would be left orphaned on the deleted source id. The merge must **repoint the source's - `system_notes` rows to the target** (`UPDATE system_notes SET attendee_id = - targetId WHERE attendee_id = sourceId`, in the same transaction — the - `booking_imports` remap's sibling), with a test that a merged imported source's - audit note survives on the target. + `system_notes` rows to the target** (`UPDATE system_notes SET entity_id = + targetId WHERE entity_type = 'attendee' AND entity_id = sourceId`, in the same + transaction — the `booking_imports` remap's sibling), with a test that a merged + imported source's audit note survives on the target. A note names the kind of + record it is about and which one, so both parts are matched; see + `src/shared/db/notes/target.ts`. - The `attendee_answers` XOR/validation triggers will `ABORT` a malformed answer row (e.g. both `answer_id` and `string_id` set). The importer only ever writes the text-answer shape (`answer_id` NULL, `question_id` + `string_id` set), so a diff --git a/scripts/mutation.ts b/scripts/mutation.ts index 4520abf6ca..9845a6d45a 100644 --- a/scripts/mutation.ts +++ b/scripts/mutation.ts @@ -17,8 +17,8 @@ * Usage: deno task mutation [options] */ -import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; import { DEFAULT_TIMEOUT, parseArgs } from "./mutation/args.ts"; +import { expand } from "./mutation/expand.ts"; import { runIsolatedMutationCommand } from "./mutation/isolation.ts"; import { isSnapshotChild, @@ -45,63 +45,6 @@ Examples: deno task mutation src/shared/dates.ts test/shared/dates.test.ts deno task mutation 'src/shared/forms/definition.ts' 'test/shared/forms/definition/*.test.ts' --exhaustive`; -/** Glob metacharacters; a path segment with none is a fixed directory name. */ -const GLOB_CHARS = /[*?{}[\]]/; - -/** Every file under `dir`, recursively; a missing directory yields nothing. */ -async function* walkFiles(dir: string): AsyncGenerator { - let entries: AsyncIterable; - try { - entries = Deno.readDir(dir); - } catch { - return; - } - for await (const entry of entries) { - const path = join(dir, entry.name); - if (entry.isDirectory) yield* walkFiles(path); - else if (entry.isFile) yield path; - } -} - -/** The leading, glob-free directory of `glob` — where a walk can start without - * scanning the whole tree. An exact path (no metacharacters) returns itself. */ -const staticBase = (glob: string): string => { - const fixed: string[] = []; - for (const segment of normalize(glob).split(SEPARATOR)) { - if (GLOB_CHARS.test(segment)) break; - fixed.push(segment); - } - return fixed.length > 0 ? fixed.join(SEPARATOR) : "."; -}; - -/** - * Expand source/test globs to absolute file paths. Replaces `@std/fs`'s - * `expandGlob` (not in this project's lock, so unfetchable in a sandboxed run) - * with `@std/path`'s `globToRegExp` over a `Deno.readDir` walk — same contract: - * absolute paths to existing files, sorted and de-duplicated. - */ -const expand = async (globs: string[]): Promise => { - const cwd = Deno.cwd(); - const paths = new Set(); - for (const glob of globs) { - const absGlob = join(cwd, glob); - const pattern = globToRegExp(absGlob, { extended: true, globstar: true }); - const base = staticBase(absGlob); - try { - if ((await Deno.stat(base)).isFile) { - if (pattern.test(base)) paths.add(base); - continue; - } - } catch { - // base doesn't exist; the walk below simply yields nothing - } - for await (const path of walkFiles(base)) { - if (pattern.test(path)) paths.add(path); - } - } - return [...paths].sort(); -}; - const main = async (): Promise => { const args = parseArgs(Deno.args); if (args.error !== null) { diff --git a/scripts/mutation/equivalent-mutants.txt b/scripts/mutation/equivalent-mutants.txt index a5575633f6..a811a2266a 100644 --- a/scripts/mutation/equivalent-mutants.txt +++ b/scripts/mutation/equivalent-mutants.txt @@ -247,9 +247,9 @@ test/test-utils/csrf.ts:236:20 ?? → || # quantity id capture: string|undefin test/test-utils/test-browser.ts:80:73 ?? → || # find(): LinkMatch|undefined; a found object is truthy test/test-utils/test-browser.ts:100:26 ?? → || # missing input type defaults to text; empty/invalid type follows the same successful-input path as text test/test-utils/test-browser.ts:150:19 ?? → || # submitted option tags are non-empty strings when present, otherwise undefined -test/test-utils/test-browser.ts:256:49 ?? → || # button value fallback is "", so empty value and absent value agree -test/test-utils/test-browser.ts:357:58 ?? → || # RequestInit.method is omitted or a non-empty method string in every caller -test/test-utils/test-browser.ts:422:28 ?? → || # forms[0] is a FormInfo object when present, otherwise undefined, so both operators reach throwNoForm() on the same input +test/test-utils/test-browser.ts:249:45 ?? → || # button value fallback is "", so empty value and absent value agree +test/test-utils/test-browser.ts:382:58 ?? → || # RequestInit.method is omitted or a non-empty method string in every caller +test/test-utils/test-browser.ts:460:28 ?? → || # forms[0] is a FormInfo object when present, otherwise undefined, so both operators reach throwNoForm() on the same input src/ui/templates/admin/listing-defaults.tsx:105:17 ?? → || # UrlControl value: string|undefined; the only falsy-non-null string is "" which equals the "" fallback, so ?? and || coincide src/ui/templates/admin/listing-defaults.tsx:128:38 ?? → || # value?.includes(day): boolean|undefined; for boolean b, b ?? false === b || false, and undefined ?? false === undefined || false src/shared/booking/build-tree.ts:130:48 ?? → || # childrenByParentId?.get(): readonly TicketListing[]|undefined — an array is always truthy, so ?? and || coincide @@ -354,7 +354,7 @@ src/shared/db/listing-prices.ts:201:45 ?? → || # foldGroupDayRows result.g # attendee entity page: `?? → ||` where the operand's only falsy-non-null # value equals the "" fallback. -src/features/admin/attendee-page.ts:265:60 ?? → || # ctx.query.get("token"): string|null; the only falsy-non-null value "" equals the "" fallback +src/features/admin/attendee-page.ts:266:60 ?? → || # ctx.query.get("token"): string|null; the only falsy-non-null value "" equals the "" fallback src/shared/db/name-registry.ts:89:51 ?? → || # matchName index.get(): number[]|undefined — an array is always truthy src/shared/db/name-registry.ts:109:20 ?? → || # isNameTakenAnywhere index.get(): number[]|undefined — an array is always truthy src/features/admin/catalog-transfer/import.ts:182:33 ?? → || # membershipSpec quantity comes from the PositiveInt schema (at least 1) or is undefined, so it is never 0 diff --git a/scripts/mutation/expand.ts b/scripts/mutation/expand.ts new file mode 100644 index 0000000000..d1683a9010 --- /dev/null +++ b/scripts/mutation/expand.ts @@ -0,0 +1,67 @@ +/** + * Turning source/test globs into the files they name. + * + * Replaces `@std/fs`'s `expandGlob` (not in this project's lock, so unfetchable + * in a sandboxed run) with `@std/path`'s `globToRegExp` over a `Deno.readDir` + * walk — same contract: absolute paths to existing files, sorted and without + * repeats. + */ + +import { globToRegExp, join, normalize, SEPARATOR } from "@std/path"; +import { statOrNull } from "#scripts/not-found.ts"; +import { collectFiles } from "#scripts/walk-files.ts"; + +/** Glob metacharacters; a path segment with none is a fixed directory name. */ +const GLOB_CHARS = /[*?{}[\]]/; + +/** + * The leading, glob-free directory of `glob` — where a walk can start without + * scanning the whole tree. An exact path (no metacharacters) returns itself. + * Every glob reaching here is an absolute path, so there is always at least the + * leading separator to start from. + */ +const staticBase = (glob: string): string => { + const fixed: string[] = []; + for (const segment of normalize(glob).split(SEPARATOR)) { + if (GLOB_CHARS.test(segment)) break; + fixed.push(segment); + } + return fixed.join(SEPARATOR); +}; + +/** Every file a glob names below `basePath` — or the path itself, when the + * glob named an exact file rather than somewhere to look. */ +const matchesUnder = ( + base: Deno.FileInfo, + basePath: string, + matches: (path: string) => boolean, +): Promise => + base.isDirectory + ? collectFiles(basePath, matches) + : Promise.resolve(matches(basePath) ? [basePath] : []); + +/** + * The files these globs name, sorted and without repeats. A glob whose + * directory is not there names no files rather than failing: the changed set a + * run is given is a branch's committed diff, which still names test files that + * have since moved. + */ +export const expand = async ( + globs: string[], + from: string = Deno.cwd(), +): Promise => { + const paths = new Set(); + for (const glob of globs) { + const absGlob = join(from, glob); + const pattern = globToRegExp(absGlob, { extended: true, globstar: true }); + const basePath = staticBase(absGlob); + const base = await statOrNull(basePath); + if (base === null) continue; + for (const path of await matchesUnder(base, basePath, (candidate) => + pattern.test(candidate), + )) { + paths.add(path); + } + } + return [...paths].sort(); +}; diff --git a/scripts/mutation/isolation-state.ts b/scripts/mutation/isolation-state.ts index c28fc897d6..405e4cd613 100644 --- a/scripts/mutation/isolation-state.ts +++ b/scripts/mutation/isolation-state.ts @@ -8,6 +8,7 @@ import { isAbsolute, join, relative, resolve, SEPARATOR } from "@std/path"; import * as v from "valibot"; import { projectRoot } from "#scripts/project-root.ts"; +import { directoryEntries } from "#scripts/walk-files.ts"; export const MUTATION_RUNS_DIR = ".mutation-runs"; export const MUTATION_WORK_DIR = "work"; @@ -139,8 +140,7 @@ const copyDirectory = async ( const toDir = join(toRoot, relativePath); await Deno.mkdir(toDir, { recursive: true }); - const entries: Deno.DirEntry[] = []; - for await (const entry of Deno.readDir(fromDir)) entries.push(entry); + const entries = await directoryEntries(fromDir); for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name), diff --git a/scripts/walk-files.ts b/scripts/walk-files.ts index e8af935e03..717b5d1f41 100644 --- a/scripts/walk-files.ts +++ b/scripts/walk-files.ts @@ -1,7 +1,22 @@ +import { join } from "@std/path"; + +/** + * What `directory` holds, as a list. A directory that is not there fails here, + * loudly: a caller asking to walk somewhere that has gone is asking about a + * root it believes in, and answering "nothing" would read as "nothing to do". + */ +export const directoryEntries = async ( + directory: string, +): Promise => { + const entries: Deno.DirEntry[] = []; + for await (const entry of Deno.readDir(directory)) entries.push(entry); + return entries; +}; + /** Recursively yield every file path beneath `directory` (depth-first). */ export async function* walkFiles(directory: string): AsyncGenerator { - for await (const entry of Deno.readDir(directory)) { - const path = `${directory}/${entry.name}`; + for (const entry of await directoryEntries(directory)) { + const path = join(directory, entry.name); if (entry.isDirectory) { yield* walkFiles(path); continue; diff --git a/src/features/admin/attendee-notes.ts b/src/features/admin/attendee-notes.ts index 3e9b1fbcde..a0015c41c8 100644 --- a/src/features/admin/attendee-notes.ts +++ b/src/features/admin/attendee-notes.ts @@ -26,10 +26,11 @@ import { getSearchParam } from "#routes/url.ts"; import { getAttendeeOrNull } from "#shared/db/attendees/queries.ts"; import { createOwnerNote, - deleteAttendeeNote, - getAttendeeNote, - type SystemNote, -} from "#shared/db/system-notes.ts"; + deleteNotes, + getNote, +} from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import type { ResponseHandler } from "#shared/response-steps.ts"; import { requireRequestPrivateKey } from "#shared/session-private-key.ts"; import type { AdminSession, Attendee } from "#shared/types.ts"; @@ -73,7 +74,7 @@ const loadNote = async ({ attendeeId, noteId, }: NoteRouteParams): Promise => - getAttendeeNote(attendeeId, noteId, await requireRequestPrivateKey()); + getNote(attendeeNotes(attendeeId), noteId, await requireRequestPrivateKey()); const noteEntityHandler = createEntityHandler( loadNote, ); @@ -134,7 +135,7 @@ const handleAddNotePost: TypedRouteHandler<"POST /admin/attendee/:attendeeId/not 400, ); } - await createOwnerNote(attendeeId, note); + await createOwnerNote(attendeeNotes(attendeeId), note); return redirect( returnTarget(attendeeId, returnUrl), t("notes.added"), @@ -162,7 +163,7 @@ const handleDeleteNoteGet: TypedRouteHandler<"GET /admin/attendee/:attendeeId/no /** POST /admin/attendee/:attendeeId/note/:noteId/delete — delete the note. */ const handleDeleteNotePost: TypedRouteHandler<"POST /admin/attendee/:attendeeId/note/:noteId/delete"> = noteHandlers.post(async (note, _session, form, _request, { attendeeId }) => { - await deleteAttendeeNote(attendeeId, note.id); + await deleteNotes(attendeeNotes(attendeeId), [note.id]); return redirect( returnTarget(attendeeId, form.getString("return_url")), t("notes.deleted"), diff --git a/src/features/admin/attendee-page.ts b/src/features/admin/attendee-page.ts index 13c5b4cfd5..385fbb6fc0 100644 --- a/src/features/admin/attendee-page.ts +++ b/src/features/admin/attendee-page.ts @@ -45,8 +45,9 @@ import { loadPreviousBookings } from "#routes/admin/previous-bookings.ts"; import { requireSessionOr } from "#routes/auth.ts"; import { getEffectiveDomain } from "#shared/config.ts"; import { attendeeStatuses } from "#shared/db/attendee-statuses.ts"; +import { getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { settings } from "#shared/db/settings.ts"; -import { getNotesForAttendee } from "#shared/db/system-notes.ts"; import { isReadOnly } from "#shared/env.ts"; import { requireRequestPrivateKey } from "#shared/session-private-key.ts"; import { isOwnerRole } from "#shared/types.ts"; @@ -201,8 +202,8 @@ export const attendeePage: EntityPage = defineEntityPage({ attendeeBanner({ attendee, isOwner: ctx.session.adminLevel === "owner", - notes: await getNotesForAttendee( - attendee.id, + notes: await getNotesFor( + attendeeNotes(attendee.id), await requireRequestPrivateKey(), ), statuses: await attendeeStatuses.getAll(), diff --git a/src/features/admin/attendees-edit.ts b/src/features/admin/attendees-edit.ts index 6f36eb8936..d5b06a948e 100644 --- a/src/features/admin/attendees-edit.ts +++ b/src/features/admin/attendees-edit.ts @@ -22,16 +22,17 @@ import { logActivity } from "#shared/db/activityLog.ts"; import { decryptAttendeeOrNull } from "#shared/db/attendees/pii.ts"; import { getAttendeeRaw } from "#shared/db/attendees/queries.ts"; import { queryOne } from "#shared/db/client.ts"; +import { + createSystemNote, + deleteNotes, + getNotesFor, +} from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { getRefundPaymentReferencesForAttendee, markPaymentReferencesProviderRefunded, type RefundPaymentReference, } from "#shared/db/payment-references.ts"; -import { - createSystemNote, - deleteAttendeeNote, - getNotesForAttendee, -} from "#shared/db/system-notes.ts"; import type { FormParams } from "#shared/form-data.ts"; import { legMatches } from "#shared/ledger/legs.ts"; import type { PaymentProvider } from "#shared/payments.ts"; @@ -151,7 +152,10 @@ const recordConfirmedRefund = async ( // stale note could still be there if the previous cleanup failed). await cleanupStaleManualRefundNote(attendeeId, privateKey); if (isPlaceholder) { - await createSystemNote(attendeeId, t("note.placeholder_refund_confirmed")); + await createSystemNote( + attendeeNotes(attendeeId), + t("note.placeholder_refund_confirmed"), + ); } return null; }; @@ -164,12 +168,15 @@ const cleanupStaleManualRefundNote = async ( attendeeId: number, privateKey: CryptoKey, ): Promise => { - const notes = await getNotesForAttendee(attendeeId, privateKey); - for (const note of notes) { - if (note.type === "system" && note.note.includes("could NOT be refunded")) { - await deleteAttendeeNote(attendeeId, note.id); - } - } + const notes = await getNotesFor(attendeeNotes(attendeeId), privateKey); + const stale = notes.filter( + (note) => + note.type === "system" && note.note.includes("could NOT be refunded"), + ); + await deleteNotes( + attendeeNotes(attendeeId), + stale.map((note) => note.id), + ); }; /** Load the attendee, listing, and payment references for a refresh. Returns diff --git a/src/features/admin/attendees-list.ts b/src/features/admin/attendees-list.ts index 4ee01433eb..b76b1ca782 100644 --- a/src/features/admin/attendees-list.ts +++ b/src/features/admin/attendees-list.ts @@ -27,8 +27,8 @@ import { } from "#shared/db/attendees/queries.ts"; import { getActiveHolidays } from "#shared/db/holidays.ts"; import { getAllListings } from "#shared/db/listings/records.ts"; +import { loadNotesForAttendees } from "#shared/db/notes/queries.ts"; import { settings } from "#shared/db/settings.ts"; -import { loadNotesForAttendees } from "#shared/db/system-notes.ts"; import { type ListingFilter, listingCategory, diff --git a/src/features/admin/listing-page-data.ts b/src/features/admin/listing-page-data.ts index aa6aec345d..a8717d3e50 100644 --- a/src/features/admin/listing-page-data.ts +++ b/src/features/admin/listing-page-data.ts @@ -33,16 +33,16 @@ import { } from "#shared/db/listing-parents.ts"; import { getListingAggregateRecalculation } from "#shared/db/listings/aggregates.ts"; import { getAttendeesByListingIds } from "#shared/db/listings/attendees.ts"; +import { + loadNotesForAttendees, + loadNotesForListing, +} from "#shared/db/notes/queries.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { getAttendeeIdsWithPaymentReference } from "#shared/db/payment-references.ts"; import { deleteAllStaleReservations } from "#shared/db/processed-payments.ts"; import { getListingChoiceAnswerMap } from "#shared/db/questions/attendee-answers/reads.ts"; import { getQuestionsForListing } from "#shared/db/questions/queries.ts"; import { settings } from "#shared/db/settings.ts"; -import { - loadNotesForAttendees, - loadNotesForListing, - type SystemNote, -} from "#shared/db/system-notes.ts"; import { listingLedgerHref } from "#shared/ledger-links.ts"; import { requireRequestPrivateKey } from "#shared/session-private-key.ts"; import { @@ -189,7 +189,7 @@ const noteAuthorNames = async ( notes.length === 0 ? new Map() : getAttendeeNamesByIds( - unique(notes.map((note) => note.attendee_id)), + unique(notes.map((note) => note.entity_id)), await requireRequestPrivateKey(), ); diff --git a/src/features/api/payment-processing/store-refund.ts b/src/features/api/payment-processing/store-refund.ts index 7667a69f01..02fcdc95ca 100644 --- a/src/features/api/payment-processing/store-refund.ts +++ b/src/features/api/payment-processing/store-refund.ts @@ -32,8 +32,9 @@ import { bookingDateFields } from "#shared/booking-date-fields.ts"; import { logActivity } from "#shared/db/activityLog.ts"; import { attendeesApi } from "#shared/db/attendees/api.ts"; import { settleAttendeeBalance } from "#shared/db/attendees/balance.ts"; +import { createSystemNote } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { balanceFinalizeStatements } from "#shared/db/payment-finalize.ts"; -import { createSystemNote } from "#shared/db/system-notes.ts"; import { ErrorCode, logError } from "#shared/logger.ts"; import { sendNtfyError } from "#shared/ntfy.ts"; import type { BookingItem, ValidatedPaymentSession } from "#shared/payments.ts"; @@ -195,7 +196,7 @@ export const storeRefundedBooking = async ( }); } await createSystemNote( - attendeeId, + attendeeNotes(attendeeId), refundedNoteText(attendeeId, spec, refunded, session.paymentReference), ); // Status 200: a fully-handled terminal outcome (booking kept, money returned or diff --git a/src/shared/db/attendees/delete.ts b/src/shared/db/attendees/delete.ts index 125b8151e4..a7658e44e3 100644 --- a/src/shared/db/attendees/delete.ts +++ b/src/shared/db/attendees/delete.ts @@ -9,6 +9,7 @@ import { type SqlStatement, } from "#shared/db/client.ts"; import { ticketCountSumExpr } from "#shared/db/migrations/schema/listing-aggregates.ts"; +import { noteDeleteStatement } from "#shared/db/notes/queries.ts"; type DeleteAttendeeOptions = { releaseBookings?: boolean }; type ListingContribution = { @@ -63,7 +64,6 @@ const DEPENDENT_ROW_TARGETS = [ { field: "attendee_id", table: "processed_payments" }, { field: "attendee_id", table: "attendee_answers" }, { field: "attendee_id", table: "listing_attendees" }, - { field: "attendee_id", table: "system_notes" }, { field: "servicing_attendee_id", table: "service_costs" }, ] as const; @@ -83,10 +83,14 @@ export const checkoutStageDeleteStatement = ( /** Build the common dependent-row deletes for one or many attendee ids. */ export const attendeeDependentDeleteStatements = ( attendeeIds: SqlStatement, -): SqlStatement[] => - DEPENDENT_ROW_TARGETS.map((target) => +): SqlStatement[] => [ + ...DEPENDENT_ROW_TARGETS.map((target) => dependentDeleteStatement(target, attendeeIds), - ); + ), + // Notes are named by the kind of record they are about, so the notes module + // builds this one rather than the plain "column = id" shape above. + noteDeleteStatement("attendee", attendeeIds), +]; /** Delete an attendee and all dependent data tied to the attendee record. */ const purgeAttendee = ( diff --git a/src/shared/db/migrations/2026-06-23_system_notes.ts b/src/shared/db/migrations/2026-06-23_system_notes.ts index de51785ac5..e768adda94 100644 --- a/src/shared/db/migrations/2026-06-23_system_notes.ts +++ b/src/shared/db/migrations/2026-06-23_system_notes.ts @@ -2,9 +2,8 @@ import { schemaMigration } from "./define.ts"; export default schemaMigration( "2026-06-23_system_notes", - "Add system_notes table of per-attendee operator-visible notes (encrypted: system notes with DB_ENCRYPTION_KEY, owner notes with the owner public key)", + "Add system_notes table of per-attendee operator-visible notes (encrypted: system notes with DB_ENCRYPTION_KEY, owner notes with the owner public key). (This historically also owned an attendee_id column and its idx_system_notes_attendee_id index, since replaced — a note now names the kind of record it is about and which one; see 2026-07-28_note_entities.)", { - indexes: ["idx_system_notes_attendee_id"], newTables: ["system_notes"], }, ); diff --git a/src/shared/db/migrations/2026-07-28_note_entities.ts b/src/shared/db/migrations/2026-07-28_note_entities.ts new file mode 100644 index 0000000000..1c44644914 --- /dev/null +++ b/src/shared/db/migrations/2026-07-28_note_entities.ts @@ -0,0 +1,85 @@ +import { queryBatchPrimary } from "#shared/db/client.ts"; +import { errorMessage } from "#shared/error-message.ts"; +import { bareSchemaMigration } from "./define.ts"; + +/** What the database says when a column is already there. */ +const ALREADY_ADDED = "duplicate column name"; + +/** What it says when `attendee_id` has already gone, so its notes are moved. */ +const ALREADY_MOVED = "no such column: attendee_id"; + +type RunSql = (sql: string) => Promise; + +/** + * Do a step, treating the database's own "that is already so" as done. + * + * A run that got partway and died leaves the database ahead of what a read + * reports — even a read from the primary, which can briefly answer with the + * schema as it was (see the backoff note in ./runner.ts). So each step asks the + * database to do the thing and believes its answer, rather than deciding from a + * picture of the schema that may be out of date. + */ +const stepDone = async ( + run: () => Promise, + alreadySo: string, +): Promise => { + try { + await run(); + } catch (error) { + if (!errorMessage(error).includes(alreadySo)) throw error; + } +}; + +const addColumn = (execute: RunSql, column: string): Promise => + stepDone( + () => execute(`ALTER TABLE system_notes ADD COLUMN ${column}`), + ALREADY_ADDED, + ); + +const moveNotesToTargets = (execute: RunSql): Promise => + stepDone( + () => + execute( + "UPDATE system_notes SET entity_type = 'attendee', entity_id = attendee_id", + ), + ALREADY_MOVED, + ); + +/** + * Notes used to belong to an attendee and nothing else. They now name the kind + * of record they are about and which one, so any record can carry notes. + * + * The two new columns are added with a default so the database accepts them on + * a table that already has rows; the rebuild at the end drops `attendee_id` and + * takes the columns' final form from SCHEMA, which has no default. + * + * Every step can be done again, and each one takes the database's word for + * whether it is already so — a column that is there, an `attendee_id` that has + * gone. The rebuild and the index sync describe an end state rather than a + * change. So a run that stopped anywhere is picked up by the next one, even + * when what it reads about the schema is a moment behind what is true. + */ +export default bareSchemaMigration( + "2026-07-28_note_entities", + "Replace system_notes.attendee_id with entity_type + entity_id, so notes can be about any record and not only an attendee. Existing notes become notes about an attendee", + async ({ getDb, recreateTable, syncIndexes }) => { + const [info] = await queryBatchPrimary([ + { args: [], sql: "SELECT name FROM pragma_table_info('system_notes')" }, + ]); + const columns = new Set(info!.rows.map((row) => String(row.name))); + if (!columns.has("attendee_id")) { + // The columns are already moved, but a run that died right after the + // rebuild never reached the index. Asking for it again is cheap. + await syncIndexes(); + return; + } + + const execute: RunSql = (sql) => getDb().execute(sql); + await addColumn(execute, "entity_type TEXT NOT NULL DEFAULT 'attendee'"); + await addColumn(execute, "entity_id INTEGER NOT NULL DEFAULT 0"); + await moveNotesToTargets(execute); + // The rebuild drops attendee_id, and takes the table's indexes from SCHEMA + // as it goes, so idx_system_notes_entity arrives with it. + await recreateTable("system_notes"); + }, +); diff --git a/src/shared/db/migrations/registry.ts b/src/shared/db/migrations/registry.ts index 559e1b4b88..70a78de9af 100644 --- a/src/shared/db/migrations/registry.ts +++ b/src/shared/db/migrations/registry.ts @@ -382,6 +382,10 @@ export const MIGRATION_REGISTRY: MigrationRegistryEntry[] = [ "2026-07-22_maintenance_completion", () => import("./2026-07-22_maintenance_completion.ts"), ), + entry( + "2026-07-28_note_entities", + () => import("./2026-07-28_note_entities.ts"), + ), ]; /* jscpd:ignore-end */ diff --git a/src/shared/db/migrations/schema/tables-questions.ts b/src/shared/db/migrations/schema/tables-questions.ts index 92929bdb3c..b5a8e91c1b 100644 --- a/src/shared/db/migrations/schema/tables-questions.ts +++ b/src/shared/db/migrations/schema/tables-questions.ts @@ -1,8 +1,14 @@ /** Form tables: questions, answers, attributes, built sites, notes. */ +import { NOTE_ENTITIES } from "#shared/db/notes/target.ts"; import { createdColumn, sortOrderColumn } from "./scalar-columns.ts"; import type { Table } from "./types.ts"; +/** The record kinds a note may be about, as a CHECK the database enforces. */ +const noteEntityCheck = `entity_type IN (${NOTE_ENTITIES.map( + (entity) => `'${entity}'`, +).join(", ")})`; + export const questionTables: [name: string, table: Table][] = [ [ "questions", @@ -211,18 +217,22 @@ export const questionTables: [name: string, table: Table][] = [ ], [ - // Per-attendee notes the operator sees on the attendee record. `note` is - // always stored encrypted — a `system` note (auto-generated, e.g. the + // Notes the operator sees on a record, named by which kind of record they + // are about (`entity_type`) and which one (`entity_id`). `note` is always + // stored encrypted — a `system` note (auto-generated, e.g. the // refunded-but-stored booking warning) with the symmetric DB_ENCRYPTION_KEY // so a key-less system path can both write and read it back, an `owner` note // (operator-authored) with the owner public key so only the owner can read - // it. System notes are kept PII-free by convention. No FKs — the attendee + // it. System notes are kept PII-free by convention. No FKs — each record's // delete path prunes these rows explicitly. "system_notes", { columns: [ ["id", "INTEGER PRIMARY KEY AUTOINCREMENT"], - ["attendee_id", "INTEGER NOT NULL"], + // The kinds come from NOTE_ENTITIES, so the stored values and the ones + // the code accepts cannot drift apart. + ["entity_type", `TEXT NOT NULL CHECK (${noteEntityCheck})`], + ["entity_id", "INTEGER NOT NULL"], [ "type", "TEXT NOT NULL DEFAULT 'system' CHECK (type IN ('system', 'owner'))", @@ -231,7 +241,10 @@ export const questionTables: [name: string, table: Table][] = [ createdColumn, ], indexes: [ - { columns: ["attendee_id"], name: "idx_system_notes_attendee_id" }, + { + columns: ["entity_type", "entity_id"], + name: "idx_system_notes_entity", + }, ], }, ], diff --git a/src/shared/db/migrations/schema/version.ts b/src/shared/db/migrations/schema/version.ts index 59627ffeea..0dbf8a5ead 100644 --- a/src/shared/db/migrations/schema/version.ts +++ b/src/shared/db/migrations/schema/version.ts @@ -1,6 +1,7 @@ /** Schema version label and the migrations bookkeeping table name. */ -export const LATEST_UPDATE = "Keep completed maintenance work dormant."; +export const LATEST_UPDATE = + "Let a note be about any record, not only an attendee."; export const SCHEMA_MIGRATIONS_TABLE = "schema_migrations"; export const LATEST_DB_UPDATE_KEY = "latest_db_update"; diff --git a/src/shared/db/notes/queries.ts b/src/shared/db/notes/queries.ts new file mode 100644 index 0000000000..5c99a13a0a --- /dev/null +++ b/src/shared/db/notes/queries.ts @@ -0,0 +1,181 @@ +/** + * Reading and writing notes (the `system_notes` table). + * + * Every read and write names the record the notes are about, so one shape + * serves every kind of record: see ./target.ts. + */ + +import { ATTENDEE_KIND } from "#shared/db/attendees/kind.ts"; +import { + execute, + executeBatch, + insert, + queryAll, + type SqlStatement, +} from "#shared/db/client.ts"; +import { nowIso } from "#shared/now.ts"; +import { openNote, openNotes, sealNote } from "./sealing.ts"; +import { + type NoteEntity, + type NoteTarget, + targetsSelectedBy, + targetsWhere, + targetWhere, +} from "./target.ts"; +import type { SystemNote, SystemNoteRow, SystemNoteType } from "./types.ts"; + +const NOTE_COLUMNS = "id, entity_type, entity_id, type, note, created"; + +/** Build a "record a note" function for one kind of note: it seals the text the + * way that kind is sealed, then stores it against the record it is about. */ +const noteWriterOf = + (type: SystemNoteType) => + async (target: NoteTarget, note: string): Promise => { + const { sql, args } = insert("system_notes", { + created: nowIso(), + entity_id: target.id, + entity_type: target.entity, + note: await sealNote(type, note), + type, + }); + await execute(sql, args); + }; + +/** + * Record a note the app wrote itself. The text MUST be free of personal + * details: it is sealed with the symmetric DB key (readable from a database + * dump plus that key), and exists so a path with no owner session can write it. + */ +export const createSystemNote = noteWriterOf("system"); + +/** Record an operator's note, sealed with the owner public key. */ +export const createOwnerNote = noteWriterOf("owner"); + +/** The still-sealed rows matching a WHERE body, oldest first per record. Shared + * by every read so the column list and the ordering live in one place. */ +const noteRowsWhere = ({ args, sql }: SqlStatement): Promise => + queryAll( + `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE ${sql} ORDER BY entity_id, id`, + args, + ); + +/** + * The still-sealed rows for several records of one kind, oldest first. Cheap + * (no key material): a caller can use it to decide whether any notes exist at + * all before working out the owner private key to open them. + */ +export const getNoteRows = ( + entity: NoteEntity, + ids: number[], +): Promise => + // Nothing to ask about is answered here rather than by the database: an empty + // list would otherwise cost a round trip to be told what we already know. + ids.length === 0 + ? Promise.resolve([]) + : noteRowsWhere(targetsWhere(entity, ids)); + +/** + * The still-sealed rows for every real (`kind = 'attendee'`) attendee booked + * onto one listing, oldest first — chosen in SQL rather than by passing a list + * of attendee ids, so the Overview never loads (nor binds) a row per attendee + * just to find the handful with notes. + */ +export const getNoteRowsForListing = ( + listingId: number, +): Promise => + noteRowsWhere( + targetsSelectedBy("attendee", { + args: [listingId], + sql: `SELECT listingAttendee.attendee_id + FROM listing_attendees AS listingAttendee + JOIN attendees AS attendee ON attendee.id = listingAttendee.attendee_id + WHERE listingAttendee.listing_id = ? AND attendee.kind = '${ATTENDEE_KIND}'`, + }), + ); + +/** All of one record's notes, opened up, oldest first. One record is a list of + * one, so it is asked for the same way several are. */ +export const getNotesFor = async ( + target: NoteTarget, + privateKey: CryptoKey, +): Promise => + openNotes(await getNoteRows(target.entity, [target.id]), privateKey); + +/** A "load the notes for " reader: takes the selector value and a way + * to get the owner private key, and answers with opened-up notes. */ +type NotesLoader = ( + selector: A, + getPrivateKey: () => Promise, +) => Promise; + +/** + * Build a loader from a row-fetcher: fetch the still-sealed rows, then open + * them, working out the owner private key only when at least one note exists — + * so a note-free view never pays for a key unwrap. + */ +const notesLoaderVia = + (fetchRows: (selector: A) => Promise): NotesLoader => + async (selector, getPrivateKey) => { + const rows = await fetchRows(selector); + return rows.length === 0 ? [] : openNotes(rows, await getPrivateKey()); + }; + +/** + * The notes of several attendees, oldest first, for an attendee-list view. + * Answers with [] when none of them have notes. + */ +export const loadNotesForAttendees: NotesLoader = notesLoaderVia( + (attendeeIds) => getNoteRows("attendee", attendeeIds), +); + +/** + * The notes of every real attendee on one listing, oldest first — chosen in SQL + * (see {@link getNoteRowsForListing}). Answers with [] when none have notes, so + * a note-free listing's Overview skips the key unwrap. + */ +export const loadNotesForListing: NotesLoader = notesLoaderVia( + getNoteRowsForListing, +); + +/** One record's note, opened up, or null when it isn't that record's. */ +export const getNote = async ( + target: NoteTarget, + noteId: number, + privateKey: CryptoKey, +): Promise => { + const { args, sql } = targetWhere(target); + const rows = await queryAll( + `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE id = ? AND ${sql}`, + [noteId, ...args], + ); + const row = rows[0]; + return row ? openNote(row, privateKey) : null; +}; + +/** + * Delete notes, tied to their record so a stray id can't reach another's. They + * go in one batch: several stale notes cost one round trip, not one each. + */ +export const deleteNotes = ( + target: NoteTarget, + noteIds: number[], +): Promise => { + if (noteIds.length === 0) return Promise.resolve(); + const { args, sql } = targetWhere(target); + return executeBatch( + noteIds.map((noteId) => ({ + args: [noteId, ...args], + sql: `DELETE FROM system_notes WHERE id = ? AND ${sql}`, + })), + ); +}; + +/** Delete the notes of records chosen by a subquery — for a delete path that + * clears a record's dependent rows in one batch. */ +export const noteDeleteStatement = ( + entity: NoteEntity, + idsQuery: SqlStatement, +): SqlStatement => { + const { args, sql } = targetsSelectedBy(entity, idsQuery); + return { args, sql: `DELETE FROM system_notes WHERE ${sql}` }; +}; diff --git a/src/shared/db/notes/sealing.ts b/src/shared/db/notes/sealing.ts new file mode 100644 index 0000000000..568a0de577 --- /dev/null +++ b/src/shared/db/notes/sealing.ts @@ -0,0 +1,61 @@ +/** + * Sealing a note's text, and opening it back up. + * + * The two note kinds seal differently (see ./types.ts), so each way of sealing + * is named once here and everything else works in terms of a sealed note. + */ + +import { decrypt, encrypt } from "#shared/crypto/encryption.ts"; +import { + decryptWithOwnerKey, + encryptWithOwnerKey, +} from "#shared/crypto/keys.ts"; +import type { + EnvKeyEncrypted, + OwnerKeyEncrypted, +} from "#shared/crypto/sealed.ts"; +import { settings } from "#shared/db/settings.ts"; +import type { SystemNote, SystemNoteRow, SystemNoteType } from "./types.ts"; + +type SealedNote = OwnerKeyEncrypted | EnvKeyEncrypted; + +/** How each kind of note is sealed. A new kind is a new entry here, and the + * compiler asks for it everywhere the kinds are handled. */ +const SEAL_AS: Record Promise> = { + owner: (note) => encryptWithOwnerKey(note, settings.publicKey), + system: (note) => encrypt(note), +}; + +export const sealNote = ( + type: SystemNoteType, + note: string, +): Promise => SEAL_AS[type](note); + +/** + * Open one note's text. We do not guard this: a failure here can only mean the + * data key itself is broken — in which case the record's own personal details + * would not open either and the page never renders — so a placeholder would + * hide a whole-system failure behind a branch nothing can reach. Let it throw. + */ +export const openNote = async ( + row: SystemNoteRow, + privateKey: CryptoKey, +): Promise => ({ + ...row, + note: await openText(row, privateKey), +}); + +const openText = ( + row: SystemNoteRow, + privateKey: CryptoKey, +): Promise => + row.type === "owner" + ? decryptWithOwnerKey(row.note, privateKey) + : decrypt(row.note); + +/** Open a batch of note rows, keeping their order. */ +export const openNotes = ( + rows: SystemNoteRow[], + privateKey: CryptoKey, +): Promise => + Promise.all(rows.map((row) => openNote(row, privateKey))); diff --git a/src/shared/db/notes/target.ts b/src/shared/db/notes/target.ts new file mode 100644 index 0000000000..952a59a545 --- /dev/null +++ b/src/shared/db/notes/target.ts @@ -0,0 +1,73 @@ +/** + * What a note is about. + * + * A note names the kind of record it is about and which one of that kind, so + * any record the operator can open can carry notes. This module is pure: it + * says what a target is and how to ask for one in SQL, and never touches the + * database itself. + */ + +import * as v from "valibot"; +import type { SqlStatement } from "#shared/db/client.ts"; +import { inPlaceholders } from "#shared/db/client.ts"; + +/** The kinds of record a note can be about. Adding one here is what lets a + * page carry notes; the stored values are checked against this list. */ +export const NoteEntitySchema = v.picklist(["attendee"]); + +export type NoteEntity = v.InferOutput; + +export const NOTE_ENTITIES = NoteEntitySchema.options; + +/** One record a note is about: its kind, and which one of that kind. */ +export interface NoteTarget { + entity: NoteEntity; + id: number; +} + +/** Build "a note about this kind of thing" once, then name records with it. */ +const notesAbout = + (entity: NoteEntity) => + (id: number): NoteTarget => ({ entity, id }); + +/** Notes about one attendee. */ +export const attendeeNotes = notesAbout("attendee"); + +/** Group notes by which record they are about — for a batch about one kind of + * record, where the id alone tells them apart. Input order is kept per group. */ +export const groupNotesByTargetId = ( + notes: Note[], +): Map => Map.groupBy(notes, (note) => note.entity_id); + +/** Ask for one record's notes. */ +export const targetWhere = ({ entity, id }: NoteTarget): SqlStatement => ({ + args: [entity, id], + sql: "entity_type = ? AND entity_id = ?", +}); + +/** + * Ask for several records of one kind at once. There must be at least one: a + * caller with nothing to ask about has no question for the database, and asking + * anyway would build SQL no database accepts. + */ +export const targetsWhere = ( + entity: NoteEntity, + ids: number[], +): SqlStatement => { + if (ids.length === 0) { + throw new Error(`Asked for the notes of no ${entity} records`); + } + return { + args: [entity, ...ids], + sql: `entity_type = ? AND entity_id IN (${inPlaceholders(ids)})`, + }; +}; + +/** Ask for the notes of one kind of record, chosen by a subquery of ids. */ +export const targetsSelectedBy = ( + entity: NoteEntity, + idsQuery: SqlStatement, +): SqlStatement => ({ + args: [entity, ...idsQuery.args], + sql: `entity_type = ? AND entity_id IN (${idsQuery.sql})`, +}); diff --git a/src/shared/db/notes/types.ts b/src/shared/db/notes/types.ts new file mode 100644 index 0000000000..c53ceb4cb0 --- /dev/null +++ b/src/shared/db/notes/types.ts @@ -0,0 +1,38 @@ +/** + * What a note is, stored and unstored. + * + * Two note kinds share one table and differ only in how the text is sealed: + * - `system` — written by the app itself (e.g. the refunded-but-stored booking + * warning). Sealed with the symmetric DB_ENCRYPTION_KEY so a path with no + * logged-in owner can both write it and read it back. Kept free of personal + * details by convention. + * - `owner` — written by an operator. Sealed with the owner public key, so it + * can be written without a session but read only with the owner private key. + */ + +import type { + EnvKeyEncrypted, + OwnerKeyEncrypted, +} from "#shared/crypto/sealed.ts"; +import type { NoteEntity } from "./target.ts"; + +export type SystemNoteType = "system" | "owner"; + +/** A stored note with its `note` field opened up to plain text. */ +export interface SystemNote { + created: string; + entity_id: number; + entity_type: NoteEntity; + id: number; + note: string; + type: SystemNoteType; +} + +/** The row as stored, its `note` still sealed. Which sealing was used follows + * the note's kind, so a row is a choice between the two rather than a shape + * where either ciphertext might turn up under either kind. */ +export type SystemNoteRow = Omit & + ( + | { type: "owner"; note: OwnerKeyEncrypted } + | { type: "system"; note: EnvKeyEncrypted } + ); diff --git a/src/shared/db/system-notes.ts b/src/shared/db/system-notes.ts deleted file mode 100644 index 459a0705f2..0000000000 --- a/src/shared/db/system-notes.ts +++ /dev/null @@ -1,235 +0,0 @@ -/** - * Per-attendee operator-visible notes (the `system_notes` table). - * - * Two note types share one table: - * - `system` — auto-generated by the app (e.g. the refunded-but-stored booking - * warning). Encrypted with the symmetric DB_ENCRYPTION_KEY so a key-less - * system path (a webhook with no logged-in owner) can both write and read it - * back. Kept PII-free by convention. - * - `owner` — operator-authored. Encrypted with the owner public key, so it can - * be written without a session but read only with the owner private key. - */ - -import type { InValue } from "@libsql/client"; -/* jscpd:ignore-start */ -import { decrypt, encrypt } from "#shared/crypto/encryption.ts"; -import { - decryptWithOwnerKey, - encryptWithOwnerKey, -} from "#shared/crypto/keys.ts"; -import type { - EnvKeyEncrypted, - OwnerKeyEncrypted, -} from "#shared/crypto/sealed.ts"; -import { ATTENDEE_KIND } from "#shared/db/attendees/kind.ts"; -import { - execute, - inPlaceholders, - insert, - queryAll, -} from "#shared/db/client.ts"; -import { settings } from "#shared/db/settings.ts"; -import { nowIso } from "#shared/now.ts"; -/* jscpd:ignore-end */ - -export type SystemNoteType = "system" | "owner"; - -/** A stored note with its `note` field decrypted to plaintext. */ -export type SystemNote = { - id: number; - attendee_id: number; - type: SystemNoteType; - note: string; - created: string; -}; - -/** The raw row as stored — `note` still encrypted. The note's sealing scheme - * follows its type: owner notes are owner-key ciphertext, system notes env-key - * ciphertext, so the row is a discriminated union on `type`. */ -type SystemNoteRow = Omit & - ( - | { type: "owner"; note: OwnerKeyEncrypted } - | { type: "system"; note: EnvKeyEncrypted } - ); - -const NOTE_COLUMNS = "id, attendee_id, type, note, created"; - -/** Insert one already-encrypted note of the given type. */ -const insertNote = async ( - attendeeId: number, - type: SystemNoteType, - encryptedNote: OwnerKeyEncrypted | EnvKeyEncrypted, -): Promise => { - const { sql, args } = insert("system_notes", { - attendee_id: attendeeId, - created: nowIso(), - note: encryptedNote, - type, - }); - await execute(sql, args); -}; - -/** Build a "record a note" function: it seals the text its own way, then stores - * the result under the given note type. The two note kinds differ only in how - * they seal, so they share this one body. */ -const noteCreatorVia = - ( - type: SystemNoteType, - seal: (note: string) => Promise, - ) => - async (attendeeId: number, note: string): Promise => - insertNote(attendeeId, type, await seal(note)); - -/** - * Record an auto-generated system note. The text MUST be PII-free: it is - * encrypted with the symmetric DB key (recoverable from a DB dump + that key), - * and exists so a system path with no owner session can write it. - */ -export const createSystemNote = noteCreatorVia("system", (note) => - encrypt(note), -); - -/** Record an operator-authored note, encrypted with the owner public key. */ -export const createOwnerNote = noteCreatorVia("owner", (note) => - encryptWithOwnerKey(note, settings.publicKey), -); - -/** Decrypt one note by its type. We do not guard this: a failure here can only - * mean the data key itself is broken — in which case the attendee's own PII - * wouldn't decrypt and the page never renders at all — so masking it with a - * placeholder would only hide a system-wide failure behind an untestable - * branch. Let it throw. */ -const decryptNote = ( - row: SystemNoteRow, - privateKey: CryptoKey, -): Promise => - row.type === "owner" - ? decryptWithOwnerKey(row.note, privateKey) - : decrypt(row.note); - -/** Decrypt a batch of note rows, preserving order. */ -export const decryptNotes = ( - rows: SystemNoteRow[], - privateKey: CryptoKey, -): Promise => - Promise.all( - rows.map(async (row) => ({ - ...row, - note: await decryptNote(row, privateKey), - })), - ); - -/** Fetch the still-encrypted note rows matching a WHERE body (no leading - * `WHERE`), oldest first per attendee. Shared by the by-ids and by-listing - * reads so the column list and ordering live in one place. */ -const noteRowsWhere = ( - where: string, - args: InValue[], -): Promise => - queryAll( - `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE ${where} ORDER BY attendee_id, id`, - args, - ); - -/** - * The still-encrypted note rows for a set of attendees, oldest first. Cheap (no - * key material): a caller can use it to decide whether any notes exist before - * deriving the owner private key to decrypt them. - */ -export const getNoteRows = (attendeeIds: number[]): Promise => - attendeeIds.length === 0 - ? Promise.resolve([]) - : noteRowsWhere( - `attendee_id IN (${inPlaceholders(attendeeIds)})`, - attendeeIds, - ); - -/** - * The still-encrypted note rows for every real (`kind = 'attendee'`) attendee - * booked onto one listing, oldest first — scoped in SQL rather than by passing - * a per-attendee id list, so the Overview never materialises (nor binds) a row - * per attendee just to find the handful with notes. - */ -export const getNoteRowsForListing = ( - listingId: number, -): Promise => - noteRowsWhere( - `attendee_id IN (SELECT listingAttendee.attendee_id - FROM listing_attendees AS listingAttendee - JOIN attendees AS attendee ON attendee.id = listingAttendee.attendee_id - WHERE listingAttendee.listing_id = ? AND attendee.kind = '${ATTENDEE_KIND}')`, - [listingId], - ); - -/** All decrypted notes for one attendee, oldest first. */ -export const getNotesForAttendee = async ( - attendeeId: number, - privateKey: CryptoKey, -): Promise => - decryptNotes(await getNoteRows([attendeeId]), privateKey); - -/** - * A "load decrypted notes for " reader: fetch the still-encrypted rows - * for a selector value, then decrypt them, deriving the owner private key lazily - * — only when at least one note exists — so a note-free view never forces a key - * unwrap. Curried over the row-fetcher so the attendee-list and listing-scoped - * loaders share one body. - */ -/** A "load decrypted notes for " reader: takes the selector value and - * a lazy owner-private-key getter, and returns the decrypted notes. */ -type NotesLoader = ( - selector: A, - getPrivateKey: () => Promise, -) => Promise; - -const notesLoaderVia = - (fetchRows: (selector: A) => Promise): NotesLoader => - async (selector, getPrivateKey) => { - const rows = await fetchRows(selector); - return rows.length === 0 ? [] : decryptNotes(rows, await getPrivateKey()); - }; - -/** - * Decrypted notes for a set of attendees, oldest first, for an attendee-list - * view. Returns [] when none have notes. - */ -export const loadNotesForAttendees: NotesLoader = - notesLoaderVia(getNoteRows); - -/** - * Decrypted notes for every real attendee on one listing, oldest first — - * scoped in SQL (see {@link getNoteRowsForListing}). Returns [] when none have - * notes, so the Overview's key unwrap is skipped for a note-free listing. - */ -export const loadNotesForListing: NotesLoader = notesLoaderVia( - getNoteRowsForListing, -); - -/** Group decrypted notes by attendee id (input order preserved within a group). */ -export const groupNotesByAttendee = ( - notes: SystemNote[], -): Map => Map.groupBy(notes, (note) => note.attendee_id); - -/** Load one decrypted note scoped to its attendee, or null when it doesn't exist. */ -export const getAttendeeNote = async ( - attendeeId: number, - noteId: number, - privateKey: CryptoKey, -): Promise => { - const rows = await queryAll( - `SELECT ${NOTE_COLUMNS} FROM system_notes WHERE id = ? AND attendee_id = ?`, - [noteId, attendeeId], - ); - if (rows.length === 0) return null; - return { ...rows[0]!, note: await decryptNote(rows[0]!, privateKey) }; -}; - -/** Delete one note, scoped to its attendee so a stray id can't reach another's. */ -export const deleteAttendeeNote = ( - attendeeId: number, - noteId: number, -): Promise => - execute("DELETE FROM system_notes WHERE id = ? AND attendee_id = ?", [ - noteId, - attendeeId, - ]); diff --git a/src/ui/templates/admin/attendee-notes.tsx b/src/ui/templates/admin/attendee-notes.tsx index 22e965b60f..7688b811c9 100644 --- a/src/ui/templates/admin/attendee-notes.tsx +++ b/src/ui/templates/admin/attendee-notes.tsx @@ -13,10 +13,8 @@ /* jscpd:ignore-start */ import { t } from "#i18n"; import { formatDatetimeShort } from "#shared/dates.ts"; -import { - groupNotesByAttendee, - type SystemNote, -} from "#shared/db/system-notes.ts"; +import { groupNotesByTargetId } from "#shared/db/notes/target.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { CsrfForm } from "#shared/forms/csrf-form.tsx"; import { Flash } from "#shared/forms/flash.tsx"; import { hiddenInputs } from "#shared/forms/hidden-inputs.tsx"; @@ -47,7 +45,7 @@ const attendeeUrl = (attendeeId: number): string => /** The are-you-sure delete page for one note, returning to `returnUrl`. */ const deleteNoteUrl = (note: SystemNote, returnUrl: string): string => - `/admin/attendee/${note.attendee_id}/note/${note.id}/delete?return_url=${encodeURIComponent( + `/admin/attendee/${note.entity_id}/note/${note.id}/delete?return_url=${encodeURIComponent( returnUrl, )}`; @@ -116,7 +114,7 @@ const NoteBox = ({ note, isOwner }: NoteViewProps): JSX.Element => { × @@ -180,7 +178,7 @@ export const AttendeeNotesSummary = ({ isOwner, }: NotesViewProps & { names: Map }): JSX.Element | null => { if (notes.length === 0) return null; - const grouped = groupNotesByAttendee(notes); + const grouped = groupNotesByTargetId(notes); return (
{t("notes.summary", { count: grouped.size })} @@ -254,7 +252,7 @@ export const adminDeleteNotePage = ({ error, }: { note: SystemNote } & NotePageProps): string => ConfirmPage({ - action: `/admin/attendee/${note.attendee_id}/note/${note.id}/delete`, + action: `/admin/attendee/${note.entity_id}/note/${note.id}/delete`, active: { section: "/admin/attendees" }, buttonText: t("notes.delete_submit"), children: ( diff --git a/src/ui/templates/admin/attendees-list.tsx b/src/ui/templates/admin/attendees-list.tsx index 7033a1819d..6a384b2c6d 100644 --- a/src/ui/templates/admin/attendees-list.tsx +++ b/src/ui/templates/admin/attendees-list.tsx @@ -8,7 +8,7 @@ import { sort } from "#fp"; import { t } from "#i18n"; import type { AttendeeSort } from "#shared/db/attendees/queries.ts"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { Raw } from "#shared/jsx/jsx-runtime.ts"; import { type ListingFilter, diff --git a/src/ui/templates/admin/listings/types.ts b/src/ui/templates/admin/listings/types.ts index 45ed6e6b57..f37f3e0e52 100644 --- a/src/ui/templates/admin/listings/types.ts +++ b/src/ui/templates/admin/listings/types.ts @@ -1,6 +1,6 @@ import type { ListingMoneyTotals } from "#shared/accounting/listing-money-totals.ts"; import type { ListingAggregateRecalculation } from "#shared/db/listings/aggregates.ts"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import type { AdminSession, Attendee, diff --git a/test/integration/admin/attendee-notes.test.ts b/test/features/admin/attendee-notes.test.ts similarity index 89% rename from test/integration/admin/attendee-notes.test.ts rename to test/features/admin/attendee-notes.test.ts index 348425cc3b..dee370c05d 100644 --- a/test/integration/admin/attendee-notes.test.ts +++ b/test/features/admin/attendee-notes.test.ts @@ -1,7 +1,8 @@ import { expect } from "@std/expect"; import { it as test } from "@std/testing/bdd"; import { t } from "#i18n"; -import { getNoteRows, getNotesForAttendee } from "#shared/db/system-notes.ts"; +import { getNoteRows, getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { expectRedirectWithFlash } from "#test-utils/assertions.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; @@ -37,8 +38,8 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { // Redirects to the return target with a SUCCESS flash naming the action. expectRedirectWithFlash(returnUrl, t("notes.added"), true)(response); - const notes = await getNotesForAttendee( - attendee.id, + const notes = await getNotesFor( + attendeeNotes(attendee.id), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); @@ -56,7 +57,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { ); expect(response.status).toBe(400); expect(await response.text()).toContain("Enter a note"); - expect(await getNoteRows([attendee.id])).toEqual([]); + expect(await getNoteRows("attendee", [attendee.id])).toEqual([]); }); test("POST on a missing attendee is a 404", async () => { @@ -72,7 +73,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { await adminFormPost(`/admin/attendee/${attendee.id}/note`, { note: "deletable note", }); - const [row] = await getNoteRows([attendee.id]); + const [row] = await getNoteRows("attendee", [attendee.id]); const response = await adminGet( `/admin/attendee/${attendee.id}/note/${row!.id}/delete`, @@ -98,7 +99,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { await adminFormPost(`/admin/attendee/${attendee.id}/note`, { note: "to be deleted", }); - const [row] = await getNoteRows([attendee.id]); + const [row] = await getNoteRows("attendee", [attendee.id]); const returnUrl = `/admin/listing/${listing.id}`; const { response } = await adminFormPost( @@ -107,7 +108,7 @@ describeWithEnv("admin > attendee notes routes", { db: true }, () => { ); // Redirects to the return target with a SUCCESS flash confirming deletion. expectRedirectWithFlash(returnUrl, t("notes.deleted"), true)(response); - expect(await getNoteRows([attendee.id])).toEqual([]); + expect(await getNoteRows("attendee", [attendee.id])).toEqual([]); }); test("POST delete on a missing note is a 404", async () => { diff --git a/test/integration/migration-restore-verify.test.ts b/test/integration/migration-restore-verify.test.ts index 833ec20445..ed99a71e01 100644 --- a/test/integration/migration-restore-verify.test.ts +++ b/test/integration/migration-restore-verify.test.ts @@ -69,8 +69,11 @@ describeWithEnv( // absent-table checks cannot be rebuilt by a restore case. enabled_features // now owns the triggers that keep saved feature data and visibility in step. // The built-site marker drop is also removal-only. The activity backfill - // completion migration is data-only and covered by its direct tests. - expect(additiveMigrations.length).toBe(MIGRATIONS.length - 22); + // completion migration is data-only and covered by its direct tests. The + // note-entities migration rebuilds system_notes around its new + // entity_type/entity_id pair, owning no additive objects to drop and + // restore; it has its own migration test. + expect(additiveMigrations.length).toBe(MIGRATIONS.length - 23); }); test("restores triggers attached to a dropped table", async () => { diff --git a/test/integration/migration-round-trip-budget.test.ts b/test/integration/migration-round-trip-budget.test.ts index 98c87819fe..73aea37066 100644 --- a/test/integration/migration-round-trip-budget.test.ts +++ b/test/integration/migration-round-trip-budget.test.ts @@ -119,7 +119,7 @@ describeWithEnv("migration request round-trip budget", { db: true }, () => { } } expect(finished).toBe(true); - expect(continuations).toBe(4); + expect(continuations).toBe(5); const result = await getDb().execute({ args: pendingIds, diff --git a/test/integration/server/attendee-form-status.test.ts b/test/integration/server/attendee-form-status.test.ts index 19a6d19804..e058b7af93 100644 --- a/test/integration/server/attendee-form-status.test.ts +++ b/test/integration/server/attendee-form-status.test.ts @@ -393,8 +393,12 @@ describeWithEnv( "Noted", "noted@example.com", ); - const { createOwnerNote } = await import("#shared/db/system-notes.ts"); - await createOwnerNote(attendee.id, "Allergic to peanuts"); + const { createOwnerNote } = await import("#shared/db/notes/queries.ts"); + const { attendeeNotes } = await import("#shared/db/notes/target.ts"); + await createOwnerNote( + attendeeNotes(attendee.id), + "Allergic to peanuts", + ); const html = await ( await adminGet(`/admin/attendees/${attendee.id}/activity`) ).text(); diff --git a/test/integration/server/attendees-list.test.ts b/test/integration/server/attendees-list.test.ts index 58449d5135..421dd93dc5 100644 --- a/test/integration/server/attendees-list.test.ts +++ b/test/integration/server/attendees-list.test.ts @@ -1,7 +1,8 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { ATTENDEES_PAGE_SIZE } from "#shared/db/attendees/queries.ts"; -import { createSystemNote } from "#shared/db/system-notes.ts"; +import { createSystemNote } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { assertAdminHtml, expectHtml, @@ -382,7 +383,10 @@ describeWithEnv("server (admin attendees list)", { db: true }, () => { "Alice", "alice@example.com", ); - await createSystemNote(attendee.id, "Refunded — follow up tomorrow."); + await createSystemNote( + attendeeNotes(attendee.id), + "Refunded — follow up tomorrow.", + ); const response = await adminGet("/admin/attendees"); const html = await response.text(); diff --git a/test/integration/server/attendees/placeholder-refresh.test.ts b/test/integration/server/attendees/placeholder-refresh.test.ts index bd0b1d232f..2e923fb133 100644 --- a/test/integration/server/attendees/placeholder-refresh.test.ts +++ b/test/integration/server/attendees/placeholder-refresh.test.ts @@ -5,11 +5,9 @@ import { KIND } from "#shared/accounting/kinds.ts"; import { transfersByAccount } from "#shared/accounting/queries.ts"; import { attendeesApi } from "#shared/db/attendees/api.ts"; import { execute } from "#shared/db/client.ts"; +import { createSystemNote, getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { reserveSession } from "#shared/db/processed-payments.ts"; -import { - createSystemNote, - getNotesForAttendee, -} from "#shared/db/system-notes.ts"; import type { Attendee } from "#shared/types.ts"; import { expectFlash } from "#test-utils/assertions.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; @@ -109,7 +107,7 @@ describeWithEnv( ); const privateKey = await getTestPrivateKey(); await createSystemNote( - attendee.id, + attendeeNotes(attendee.id), "This booking was kept at quantity 0 but its payment could NOT be refunded automatically because the event filled up while they were paying. Payment reference: pi_stale_note (code: capacity_full). Please refund it manually and check the [ledger](/admin/ledger/attendee/" + attendee.id + ").", @@ -117,7 +115,7 @@ describeWithEnv( await submitRefreshPayment(attendee, () => Promise.resolve(true)); - const notes = await getNotesForAttendee(attendee.id, privateKey); + const notes = await getNotesFor(attendeeNotes(attendee.id), privateKey); expect( notes.some((note) => note.note.includes("could NOT be refunded")), ).toBe(false); @@ -152,7 +150,7 @@ describeWithEnv( // note cleanup failed — re-create the stale note afterward. await refreshAndVerifyRefundCash(attendee); await createSystemNote( - attendee.id, + attendeeNotes(attendee.id), "This booking was kept at quantity 0 but its payment could NOT be refunded.", ); @@ -164,7 +162,7 @@ describeWithEnv( expect.stringContaining("up to date"), ); - const notes = await getNotesForAttendee(attendee.id, privateKey); + const notes = await getNotesFor(attendeeNotes(attendee.id), privateKey); expect( notes.some((note) => note.note.includes("could NOT be refunded")), ).toBe(false); diff --git a/test/integration/server/listings/show-actions-and-activity.test.ts b/test/integration/server/listings/show-actions-and-activity.test.ts index 4359bfc399..55ef8eeebe 100644 --- a/test/integration/server/listings/show-actions-and-activity.test.ts +++ b/test/integration/server/listings/show-actions-and-activity.test.ts @@ -3,7 +3,8 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { attendeeAccount } from "#shared/accounting/accounts.ts"; import { addDays } from "#shared/dates.ts"; -import { createSystemNote } from "#shared/db/system-notes.ts"; +import { createSystemNote } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { todayInTz } from "#shared/timezone.ts"; import { createDailyListing } from "#test/integration/server/listings/_shared-setup.ts"; import { logActivity } from "#test-utils/activity-log.ts"; @@ -102,7 +103,10 @@ describeWithEnv( gross: 5000, listingId: listing.id, }); - await createSystemNote(confirmed.id, "Called ahead about access"); + await createSystemNote( + attendeeNotes(confirmed.id), + "Called ahead about access", + ); // An incomplete booking: a recognised sale that was never paid. const incomplete = await createPaidAttendeeWithoutLedger( listing.id, diff --git a/test/integration/server/payments-success-refunds.test.ts b/test/integration/server/payments-success-refunds.test.ts index cf440e16eb..35bf3f744d 100644 --- a/test/integration/server/payments-success-refunds.test.ts +++ b/test/integration/server/payments-success-refunds.test.ts @@ -284,12 +284,12 @@ describeWithEnv("server (payment flow: ticket success)", { db: true }, () => { const { getAttendeesRaw } = await import( "#shared/db/attendees/queries.ts" ); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); const ghost = (await getAttendeesRaw(listing.id)).find( (a) => a.quantity === 0, ); expect(ghost).toBeDefined(); - expect(await getNoteRows([ghost!.id])).toHaveLength(1); + expect(await getNoteRows("attendee", [ghost!.id])).toHaveLength(1); } finally { mockRetrieve.restore(); mockRefund.restore(); diff --git a/test/integration/server/payments/replay.test.ts b/test/integration/server/payments/replay.test.ts index b7b1e673bb..1eea4bd796 100644 --- a/test/integration/server/payments/replay.test.ts +++ b/test/integration/server/payments/replay.test.ts @@ -3,8 +3,8 @@ import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { handleRequest } from "#routes"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; +import { getNoteRows } from "#shared/db/notes/queries.ts"; import { isSessionProcessed } from "#shared/db/processed-payments.ts"; -import { getNoteRows } from "#shared/db/system-notes.ts"; import { expectHtmlResponse } from "#test-utils/assertions.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { @@ -146,7 +146,9 @@ describeWithEnv("server (payment flow)", { db: true, triggers: true }, () => { const attendees = await getAttendeesRaw(listing.id); expect(attendees.length).toBe(1); expect(attendees[0]?.quantity).toBe(0); - expect((await getNoteRows([attendees[0]!.id])).length).toBe(1); + expect( + (await getNoteRows("attendee", [attendees[0]!.id])).length, + ).toBe(1); expect(mockRefund.calls.length).toBe(1); // The session is recorded as a terminal failure. diff --git a/test/integration/server/webhooks/multi-ticket-refunds.test.ts b/test/integration/server/webhooks/multi-ticket-refunds.test.ts index 7edce3e7e4..3ea3e7c3e0 100644 --- a/test/integration/server/webhooks/multi-ticket-refunds.test.ts +++ b/test/integration/server/webhooks/multi-ticket-refunds.test.ts @@ -131,8 +131,8 @@ describeWithEnv( listing1.id, listing2.id, ); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); - expect((await getNoteRows([attendee.id])).length).toBe(1); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); + expect((await getNoteRows("attendee", [attendee.id])).length).toBe(1); await expectSessionFailed("cs_multi_mismatch"); // Verify refund was attempted exactly once diff --git a/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts b/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts index 3851400d5b..f4ec86a589 100644 --- a/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts +++ b/test/integration/server/webhooks/price-signature-stored-refund-and-ignore.test.ts @@ -4,8 +4,9 @@ import { stub } from "@std/testing/mock"; import { attendeeAccount } from "#shared/accounting/accounts.ts"; import { transfersByAccount } from "#shared/accounting/queries.ts"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; +import { getNoteRows, getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { isSessionProcessed } from "#shared/db/processed-payments.ts"; -import { getNoteRows, getNotesForAttendee } from "#shared/db/system-notes.ts"; import { balanceOf } from "#shared/ledger/project.ts"; import { expectAcknowledgedIgnore, @@ -74,8 +75,8 @@ describeWithEnv( expect(legs.some((leg) => leg.kind === "sale")).toBe(false); // The system note names the reason (PII-free) and links to the ledger. - const notes = await getNotesForAttendee( - attendee!.id, + const notes = await getNotesFor( + attendeeNotes(attendee!.id), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); @@ -344,7 +345,7 @@ describeWithEnv( expect(refund.calls.length).toBe(1); const [attendee] = await getAttendeesRaw(listing.id); expect(attendee?.quantity).toBe(0); - expect(await getNoteRows([attendee!.id])).toHaveLength(1); + expect(await getNoteRows("attendee", [attendee!.id])).toHaveLength(1); const record = await isSessionProcessed("cs_refund_retry"); expect(record?.failure_data).not.toBe(""); }, diff --git a/test/integration/servicing/purge-edge-cases.test.ts b/test/integration/servicing/purge-edge-cases.test.ts index 7b22ec5178..59c774ab69 100644 --- a/test/integration/servicing/purge-edge-cases.test.ts +++ b/test/integration/servicing/purge-edge-cases.test.ts @@ -15,6 +15,8 @@ import { expect } from "@std/expect"; import { it as test } from "@std/testing/bdd"; import { getDb } from "#shared/db/client.ts"; +import { createSystemNote, getNoteRows } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { countOrphanedAttendees, purgeOrphanedAttendees, @@ -33,14 +35,9 @@ import { // jscpd:ignore-end -/** Insert a system_notes row for an attendee (a dependent row the purge clears). */ -const attachSystemNote = async (attendeeId: number): Promise => { - await getDb().execute({ - args: [attendeeId], - sql: `INSERT INTO system_notes (attendee_id, type, note, created) - VALUES (?, 'system', 'x', '2026-01-01T00:00:00Z')`, - }); -}; +/** How many notes this attendee has — a dependent row the purge clears. */ +const noteCount = async (attendeeId: number): Promise => + (await getNoteRows("attendee", [attendeeId])).length; describeWithEnv("servicing edge cases — purge", { db: true }, () => { test("a servicing orphan created exactly at the cutoff survives; one ms earlier is swept", async () => { @@ -82,11 +79,11 @@ describeWithEnv("servicing edge cases — purge", { db: true }, () => { test("a servicing orphan's system_notes are swept with the attendee", async () => { const { id } = await createServicingHold(); await orphanServicingEvent(id); - await attachSystemNote(id); - expect(await childRowCount("system_notes", id)).toBe(1); + await createSystemNote(attendeeNotes(id), "swept with the attendee"); + expect(await noteCount(id)).toBe(1); await purgeOrphanedAttendees(nowIso()); - // system_notes is a dependent row, so the shared purge clears it. - expect(await childRowCount("system_notes", id)).toBe(0); + // A note is a dependent row, so the shared purge clears it. + expect(await noteCount(id)).toBe(0); }); test("the purge sweeps both a servicing orphan and an attendee orphan in one batch", async () => { diff --git a/test/integration/webhook-price-signature/helpers.ts b/test/integration/webhook-price-signature/helpers.ts index 0f509e35b6..38eba72376 100644 --- a/test/integration/webhook-price-signature/helpers.ts +++ b/test/integration/webhook-price-signature/helpers.ts @@ -3,7 +3,7 @@ import { stub } from "@std/testing/mock"; import { handleRequest } from "#routes"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; import { setGroupPackageMembers } from "#shared/db/groups.ts"; -import { getNoteRows } from "#shared/db/system-notes.ts"; +import { getNoteRows } from "#shared/db/notes/queries.ts"; import { stripeApi } from "#shared/stripe.ts"; import { assertJson } from "#test-utils/assertions.ts"; import { createTestGroup } from "#test-utils/db-helpers/groups.ts"; @@ -157,7 +157,7 @@ export const expectStoredRefundRecord = async ( ): Promise => { const [attendee] = await getAttendeesRaw(listingId); expect(attendee?.quantity).toBe(0); - expect(await getNoteRows([attendee!.id])).toHaveLength(1); + expect(await getNoteRows("attendee", [attendee!.id])).toHaveLength(1); }; export const expectStoredRefund = async (listingId: number): Promise => { diff --git a/test/scripts/mutation/expand.test.ts b/test/scripts/mutation/expand.test.ts new file mode 100644 index 0000000000..884428c370 --- /dev/null +++ b/test/scripts/mutation/expand.test.ts @@ -0,0 +1,78 @@ +import { join, relative } from "node:path"; +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { expand } from "#scripts/mutation/expand.ts"; +import { withTempDir } from "#test-utils/files.ts"; + +/** Globs are matched from a starting folder, so a test names its own without + * touching the working directory this whole process shares. */ +const expandFrom = (dir: string, globs: string[]): Promise => + expand(globs, dir); + +const writeFile = async ( + path: string, + body = "export {};\n", +): Promise => { + await Deno.mkdir(join(path, ".."), { recursive: true }); + await Deno.writeTextFile(path, body); +}; + +describe("the files a glob names", () => { + test("finds every match under a folder, sorted and without repeats", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "src", "b.ts")); + await writeFile(join(dir, "src", "a.ts")); + await writeFile(join(dir, "src", "deep", "c.ts")); + await writeFile(join(dir, "src", "notes.txt")); + + const found = await expandFrom(dir, ["src/**/*.ts", "src/a.ts"]); + + expect(found.map((path) => relative(dir, path))).toEqual([ + join("src", "a.ts"), + join("src", "b.ts"), + join("src", "deep", "c.ts"), + ]); + }); + }); + + test("names no files when the folder is not there", async () => { + await withTempDir(async (dir) => { + // A test file that moved is still named by a branch's committed diff, so + // this is asked for in a normal run. Reading a missing folder only fails + // once its entries are read, so a guard around opening it misses this. + const found = await expandFrom(dir, ["gone/**/*.test.ts"]); + + expect(found).toEqual([]); + }); + }); + + test("names no files when the path is a file, not a folder", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "a.ts")); + + expect(await expandFrom(dir, ["a.ts/*.ts"])).toEqual([]); + }); + }); + + test("matches from where it is asked when the glob starts with a star", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "top.ts")); + await writeFile(join(dir, "nested", "deep.ts")); + + const found = await expandFrom(dir, ["*.ts"]); + + expect(found.map((path) => relative(dir, path))).toEqual(["top.ts"]); + }); + }); + + test("names an exact file, and skips one that is not there", async () => { + await withTempDir(async (dir) => { + await writeFile(join(dir, "src", "only.ts")); + + expect(await expandFrom(dir, ["src/only.ts"])).toEqual([ + join(dir, "src", "only.ts"), + ]); + expect(await expandFrom(dir, ["src/missing.ts"])).toEqual([]); + }); + }); +}); diff --git a/test/shared/db/attendees/delete.test.ts b/test/shared/db/attendees/delete.test.ts index 37a3e52be3..61b4d6b75d 100644 --- a/test/shared/db/attendees/delete.test.ts +++ b/test/shared/db/attendees/delete.test.ts @@ -9,11 +9,12 @@ import { } from "#shared/db/listings/records.ts"; import { modifierUsedQuantities } from "#shared/db/modifier-usage.ts"; import { getAllModifiers, modifiersTable } from "#shared/db/modifiers.ts"; +import { createSystemNote, getNoteRows } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { isSessionProcessed, reserveSession, } from "#shared/db/processed-payments.ts"; -import { createSystemNote, getNoteRows } from "#shared/db/system-notes.ts"; import { insertCheckoutStage } from "#test-utils/checkout-stages.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; @@ -224,12 +225,15 @@ describeWithEnv("db > attendees > deleteAttendee", { db: true }, () => { "Noted Attendee", "noted@example.com", ); - await createSystemNote(attendee.id, "a note that should be purged"); - expect(await getNoteRows([attendee.id])).toHaveLength(1); + await createSystemNote( + attendeeNotes(attendee.id), + "a note that should be purged", + ); + expect(await getNoteRows("attendee", [attendee.id])).toHaveLength(1); await deleteAttendee(attendee.id); - expect(await getNoteRows([attendee.id])).toEqual([]); + expect(await getNoteRows("attendee", [attendee.id])).toEqual([]); }); test("succeeds when the attendee has no modifier usage", async () => { diff --git a/test/shared/db/migrations/2026-07-28_note_entities.test.ts b/test/shared/db/migrations/2026-07-28_note_entities.test.ts new file mode 100644 index 0000000000..a6d634f4eb --- /dev/null +++ b/test/shared/db/migrations/2026-07-28_note_entities.test.ts @@ -0,0 +1,252 @@ +import { expect } from "@std/expect"; +import { it as test } from "@std/testing/bdd"; +import { execute, getDb, queryAll } from "#shared/db/client.ts"; +import noteEntities from "#shared/db/migrations/2026-07-28_note_entities.ts"; +import { MIGRATION_IDS } from "#shared/db/migrations/registry.ts"; +import { + recreateTable, + syncIndexes, +} from "#shared/db/migrations/schema-sync.ts"; +import type { + Migration, + MigrationContext, +} from "#shared/db/migrations/types.ts"; +import { columnNames } from "#test/test-utils/db/migration-test-helpers.ts"; +import { describeWithEnv } from "#test-utils/db.ts"; +import { buildMigrationContext, indexExists } from "#test-utils/migrations.ts"; + +/** Put system_notes back the way it was before this migration: a note belonged + * to an attendee and nothing else. */ +const downgradeToAttendeeNotes = async (): Promise => { + await execute("DROP TABLE IF EXISTS system_notes"); + await execute(`CREATE TABLE system_notes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + attendee_id INTEGER NOT NULL, + type TEXT NOT NULL DEFAULT 'system' CHECK (type IN ('system', 'owner')), + note TEXT NOT NULL, + created TEXT NOT NULL DEFAULT (datetime('now')) + )`); + await execute("DROP INDEX IF EXISTS idx_system_notes_entity"); + await execute( + "CREATE INDEX idx_system_notes_attendee_id ON system_notes(attendee_id)", + ); +}; + +const migration = (): Migration => + noteEntities(buildMigrationContext({ recreateTable, syncIndexes })); + +const runMigration = (): Promise => migration().up(); + +/** Stop partway: the columns are added, but the rebuild never happened. */ +const addColumnsWithoutRebuilding = async ( + ...columns: string[] +): Promise => { + for (const column of columns) { + await execute( + `ALTER TABLE system_notes ADD COLUMN ${column} ${ + column === "entity_type" + ? "TEXT NOT NULL DEFAULT 'attendee'" + : "INTEGER NOT NULL DEFAULT 0" + }`, + ); + } +}; + +type StoredNote = { + entity_id: number; + entity_type: string; + id: number; + note: string; + type: string; +}; + +const storedNotes = (): Promise => + queryAll( + "SELECT id, entity_type, entity_id, type, note FROM system_notes ORDER BY id", + ); + +describeWithEnv( + "db > migrations > 2026-07-28_note_entities", + { db: true }, + () => { + test("turns an attendee's notes into notes about an attendee", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?), (?, ?, ?)", + [7, "system", "kept system", 9, "owner", "kept owner"], + ); + + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 7, + entity_type: "attendee", + id: 1, + note: "kept system", + type: "system", + }, + { + entity_id: 9, + entity_type: "attendee", + id: 2, + note: "kept owner", + type: "owner", + }, + ]); + expect(await columnNames("system_notes")).not.toContain("attendee_id"); + expect(await indexExists("idx_system_notes_entity")).toBe(true); + expect(await indexExists("idx_system_notes_attendee_id")).toBe(false); + }); + + test("leaves an already-migrated table alone", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?)", + [3, "system", "only once"], + ); + await runMigration(); + + // A verify retry, or a crash after the work but before the marker, runs + // up() a second time against the finished table. + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 3, + entity_type: "attendee", + id: 1, + note: "only once", + type: "system", + }, + ]); + }); + + test("carries on from a run that stopped after the first column", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?)", + [5, "system", "half-migrated"], + ); + await addColumnsWithoutRebuilding("entity_type"); + + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 5, + entity_type: "attendee", + id: 1, + note: "half-migrated", + type: "system", + }, + ]); + }); + + test("carries on from a run that stopped after both columns", async () => { + await downgradeToAttendeeNotes(); + await execute( + "INSERT INTO system_notes (attendee_id, type, note) VALUES (?, ?, ?)", + [6, "owner", "columns already there"], + ); + await addColumnsWithoutRebuilding("entity_type", "entity_id"); + + await runMigration(); + + expect(await storedNotes()).toEqual([ + { + entity_id: 6, + entity_type: "attendee", + id: 1, + note: "columns already there", + type: "owner", + }, + ]); + }); + + test("puts the index back when a run died right after the rebuild", async () => { + await downgradeToAttendeeNotes(); + await runMigration(); + // The state a run that stopped between the rebuild and the index leaves: + // the columns are moved, but nothing indexes them. + await execute("DROP INDEX idx_system_notes_entity"); + expect(await indexExists("idx_system_notes_entity")).toBe(false); + + await runMigration(); + + expect(await indexExists("idx_system_notes_entity")).toBe(true); + }); + + test("finishes when the work is done but the schema read is behind", async () => { + await downgradeToAttendeeNotes(); + // What a finished table answers to each step, when the read that chose + // to run them was a moment out of date. + const finishedDatabase = () => ({ + execute: (sql: string) => + Promise.reject( + new Error( + sql.startsWith("ALTER") + ? "duplicate column name: entity_type" + : "no such column: attendee_id", + ), + ), + }); + + await noteEntities( + buildMigrationContext({ + getDb: finishedDatabase as unknown as MigrationContext["getDb"], + recreateTable, + syncIndexes, + }), + ).up(); + + expect(await indexExists("idx_system_notes_entity")).toBe(true); + }); + + for (const failing of ["ALTER", "UPDATE"]) { + test(`stops when ${failing} fails for any other reason`, async () => { + await downgradeToAttendeeNotes(); + // Only this one statement fails, and not with an "already so" answer, + // so each step's tolerance has to be about its own answer alone. + const database = () => ({ + execute: (sql: string) => + sql.startsWith(failing) + ? Promise.reject(new Error("database or disk is full")) + : Promise.resolve({}), + }); + + await expect( + noteEntities( + buildMigrationContext({ + getDb: database as unknown as MigrationContext["getDb"], + recreateTable, + syncIndexes, + }), + ).up(), + ).rejects.toThrow("database or disk is full"); + }); + } + + test("is registered under the id it declares, and says what it does", () => { + const { description, id } = migration(); + + // The id is what the applied-marker is written under: a mismatch with the + // registry would run the migration again on every boot. + expect(MIGRATION_IDS).toContain(id); + expect(description).toContain("entity_type"); + expect(description).toContain("entity_id"); + }); + + test("refuses a note about a kind of record it does not know", async () => { + await downgradeToAttendeeNotes(); + await runMigration(); + + await expect( + getDb().execute({ + args: [1, "system", "who?"], + sql: "INSERT INTO system_notes (entity_type, entity_id, type, note) VALUES ('spaceship', ?, ?, ?)", + }), + ).rejects.toThrow(); + }); + }, +); diff --git a/test/shared/db/migrations/registry.test.ts b/test/shared/db/migrations/registry.test.ts index 8e46d109e5..b4130c0baa 100644 --- a/test/shared/db/migrations/registry.test.ts +++ b/test/shared/db/migrations/registry.test.ts @@ -31,12 +31,19 @@ describe("db > migration registry", () => { }); test("orders every scheduled-maintenance schema change", () => { - expect(MIGRATION_IDS.slice(-5)).toEqual([ + const maintenanceOrder = [ "2026-07-18_maintenance_tasks", "2026-07-18_drop_built_sites_last_pruned", "2026-07-19_maintenance_checkpoint", "2026-07-21_activity_backfill_complete", "2026-07-22_maintenance_completion", - ]); + ]; + // Taken from where the run starts rather than from the end of the list, so + // a later migration does not look like the maintenance run losing its order. + const start = MIGRATION_IDS.indexOf(maintenanceOrder[0]!); + + expect(MIGRATION_IDS.slice(start, start + maintenanceOrder.length)).toEqual( + maintenanceOrder, + ); }); }); diff --git a/test/shared/db/migrations/schema/tables-questions.test.ts b/test/shared/db/migrations/schema/tables-questions.test.ts index fab30dcd42..21ea3a3f83 100644 --- a/test/shared/db/migrations/schema/tables-questions.test.ts +++ b/test/shared/db/migrations/schema/tables-questions.test.ts @@ -5,7 +5,7 @@ import { jsonHash } from "#test-utils/hash.ts"; test("keeps the complete question and built-site schema exact", async () => { expect(await jsonHash(questionTables)).toBe( - "b2cb65571a95e6cad5cf42d2de7f88818719da14151f8eabbff2e761f8f52c85", + "2a24e32ee290fb0761fae15956b64c8246cc2b87845a5260c21943067131facb", ); }); diff --git a/test/shared/db/migrations/schema/version/guard.test.ts b/test/shared/db/migrations/schema/version/guard.test.ts index 03684a207f..af50a6f874 100644 --- a/test/shared/db/migrations/schema/version/guard.test.ts +++ b/test/shared/db/migrations/schema/version/guard.test.ts @@ -100,8 +100,9 @@ describe("db > migrations > schema change guard", () => { "2026-07-19_maintenance_checkpoint", "2026-07-21_activity_backfill_complete", "2026-07-22_maintenance_completion", + "2026-07-28_note_entities", ], - schemaHash: "qxxe41", + schemaHash: "w6e6g0", }); }); @@ -115,7 +116,7 @@ describe("db > migrations > schema change guard", () => { }).toEqual({ dbSchemaHash: "db_schema_hash", latestDbUpdate: "latest_db_update", - latestUpdate: "Keep completed maintenance work dormant.", + latestUpdate: "Let a note be about any record, not only an attendee.", migrationLock: "migration_lock", schemaMigrations: "schema_migrations", }); diff --git a/test/shared/db/system-notes.test.ts b/test/shared/db/notes/queries.test.ts similarity index 62% rename from test/shared/db/system-notes.test.ts rename to test/shared/db/notes/queries.test.ts index 9b30d2d907..4f6a476186 100644 --- a/test/shared/db/system-notes.test.ts +++ b/test/shared/db/notes/queries.test.ts @@ -5,20 +5,24 @@ import { queryOne } from "#shared/db/client.ts"; import { createOwnerNote, createSystemNote, - decryptNotes, - deleteAttendeeNote, - getAttendeeNote, + deleteNotes, + getNote, getNoteRows, getNoteRowsForListing, - getNotesForAttendee, - groupNotesByAttendee, + getNotesFor, loadNotesForAttendees, loadNotesForListing, -} from "#shared/db/system-notes.ts"; +} from "#shared/db/notes/queries.ts"; +import { openNotes } from "#shared/db/notes/sealing.ts"; +import { + attendeeNotes, + groupNotesByTargetId, +} from "#shared/db/notes/target.ts"; import { getTestPrivateKey } from "#test-utils/crypto.ts"; import { describeWithEnv } from "#test-utils/db.ts"; import { createTestAttendee } from "#test-utils/db-helpers/attendees.ts"; import { createTestListing } from "#test-utils/db-helpers/listings.ts"; +import { runAndCountRoundTrips } from "#test-utils/query-log.ts"; /** Create a listing + attendee and return the attendee id. */ const makeAttendee = async (name = "Note Target"): Promise => { @@ -37,11 +41,11 @@ const makeAttendee = async (name = "Note Target"): Promise => { const rawNote = (attendeeId: number): Promise<{ note: string } | null> => queryOne<{ note: string }>( - "SELECT note FROM system_notes WHERE attendee_id = ? ORDER BY id", + "SELECT note FROM system_notes WHERE entity_type = 'attendee' AND entity_id = ? ORDER BY id", [attendeeId], ); -describeWithEnv("db > system-notes", { db: true }, () => { +describeWithEnv("db > notes", { db: true }, () => { test("loads notes scoped to one listing's attendees only", async () => { const listing = await createTestListing({ maxAttendees: 50 }); const other = await createTestListing({ maxAttendees: 50 }); @@ -57,11 +61,11 @@ describeWithEnv("db > system-notes", { db: true }, () => { "Theirs", "theirs@example.com", ); - await createSystemNote(mine.id, "Note on this listing"); - await createSystemNote(theirs.id, "Note on another listing"); + await createSystemNote(attendeeNotes(mine.id), "Note on this listing"); + await createSystemNote(attendeeNotes(theirs.id), "Note on another listing"); const rows = await getNoteRowsForListing(listing.id); - expect(rows.map((r) => r.attendee_id)).toEqual([mine.id]); + expect(rows.map((r) => r.entity_id)).toEqual([mine.id]); const notes = await loadNotesForListing(listing.id, getTestPrivateKey); expect(notes).toHaveLength(1); @@ -84,15 +88,18 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("stores and reads back a decrypted system note", async () => { const attendeeId = await makeAttendee(); - await createSystemNote(attendeeId, "Refunded: price changed"); + await createSystemNote( + attendeeNotes(attendeeId), + "Refunded: price changed", + ); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); expect(notes[0]).toMatchObject({ - attendee_id: attendeeId, + entity_id: attendeeId, note: "Refunded: price changed", type: "system", }); @@ -100,10 +107,13 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("stores and reads back a decrypted owner note", async () => { const attendeeId = await makeAttendee(); - await createOwnerNote(attendeeId, "Called to confirm dietary needs"); + await createOwnerNote( + attendeeNotes(attendeeId), + "Called to confirm dietary needs", + ); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); @@ -115,14 +125,14 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("never stores note text in plaintext", async () => { const attendeeId = await makeAttendee(); - await createSystemNote(attendeeId, "system secret"); + await createSystemNote(attendeeNotes(attendeeId), "system secret"); // The symmetric encryption format is the enc:1: envelope, not the plaintext. const stored = await rawNote(attendeeId); expect(stored?.note.startsWith("enc:")).toBe(true); expect(stored?.note).not.toContain("system secret"); const ownerAttendee = await makeAttendee("Owner Target"); - await createOwnerNote(ownerAttendee, "owner secret"); + await createOwnerNote(attendeeNotes(ownerAttendee), "owner secret"); const ownerStored = await rawNote(ownerAttendee); // The owner hybrid-RSA envelope is hyb:1:, again never the plaintext. expect(ownerStored?.note.startsWith("hyb:")).toBe(true); @@ -131,12 +141,12 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("returns an attendee's notes oldest first", async () => { const attendeeId = await makeAttendee(); - await createSystemNote(attendeeId, "first"); - await createOwnerNote(attendeeId, "second"); - await createSystemNote(attendeeId, "third"); + await createSystemNote(attendeeNotes(attendeeId), "first"); + await createOwnerNote(attendeeNotes(attendeeId), "second"); + await createSystemNote(attendeeNotes(attendeeId), "third"); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes.map((n) => n.note)).toEqual(["first", "second", "third"]); @@ -145,27 +155,27 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("groups notes for several attendees by attendee id", async () => { const a = await makeAttendee("Alice Notes"); const b = await makeAttendee("Bob Notes"); - await createSystemNote(a, "a1"); - await createSystemNote(b, "b1"); - await createSystemNote(a, "a2"); + await createSystemNote(attendeeNotes(a), "a1"); + await createSystemNote(attendeeNotes(b), "b1"); + await createSystemNote(attendeeNotes(a), "a2"); - const notes = await decryptNotes( - await getNoteRows([a, b]), + const notes = await openNotes( + await getNoteRows("attendee", [a, b]), await getTestPrivateKey(), ); - const grouped = groupNotesByAttendee(notes); + const grouped = groupNotesByTargetId(notes); expect(grouped.get(a)?.map((n) => n.note)).toEqual(["a1", "a2"]); expect(grouped.get(b)?.map((n) => n.note)).toEqual(["b1"]); }); test("getNoteRows returns nothing for an empty attendee list", async () => { - expect(await getNoteRows([])).toEqual([]); + expect(await getNoteRows("attendee", [])).toEqual([]); }); test("loadNotesForAttendees derives the key only when notes exist", async () => { const withNotes = await makeAttendee("Has Notes"); const withoutNotes = await makeAttendee("No Notes"); - await createSystemNote(withNotes, "hi"); + await createSystemNote(attendeeNotes(withNotes), "hi"); const lazyKey = spy(() => getTestPrivateKey()); @@ -184,34 +194,48 @@ describeWithEnv("db > system-notes", { db: true }, () => { test("getAttendeeNote loads one note scoped to its attendee", async () => { const owner = await makeAttendee("Scoped Owner"); const other = await makeAttendee("Other Owner"); - await createSystemNote(owner, "scoped note"); - const [row] = await getNoteRows([owner]); + await createSystemNote(attendeeNotes(owner), "scoped note"); + const [row] = await getNoteRows("attendee", [owner]); const pk = await getTestPrivateKey(); - const found = await getAttendeeNote(owner, row!.id, pk); + const found = await getNote(attendeeNotes(owner), row!.id, pk); expect(found?.note).toBe("scoped note"); // The same note id under a different attendee must not resolve. - expect(await getAttendeeNote(other, row!.id, pk)).toBeNull(); + expect(await getNote(attendeeNotes(other), row!.id, pk)).toBeNull(); // A missing id resolves to null too. - expect(await getAttendeeNote(owner, 9_999_999, pk)).toBeNull(); + expect(await getNote(attendeeNotes(owner), 9_999_999, pk)).toBeNull(); }); test("deleteAttendeeNote removes only the scoped note", async () => { const owner = await makeAttendee("Delete Owner"); const other = await makeAttendee("Keep Owner"); - await createSystemNote(owner, "delete me"); - await createSystemNote(other, "keep me"); - const [ownerRow] = await getNoteRows([owner]); - const [otherRow] = await getNoteRows([other]); + await createSystemNote(attendeeNotes(owner), "delete me"); + await createSystemNote(attendeeNotes(other), "keep me"); + const [ownerRow] = await getNoteRows("attendee", [owner]); + const [otherRow] = await getNoteRows("attendee", [other]); // A wrong attendee id must not delete another attendee's note. - await deleteAttendeeNote(other, ownerRow!.id); - expect(await getNoteRows([owner])).toHaveLength(1); + await deleteNotes(attendeeNotes(other), [ownerRow!.id]); + expect(await getNoteRows("attendee", [owner])).toHaveLength(1); - await deleteAttendeeNote(owner, ownerRow!.id); - expect(await getNoteRows([owner])).toEqual([]); + await deleteNotes(attendeeNotes(owner), [ownerRow!.id]); + expect(await getNoteRows("attendee", [owner])).toEqual([]); // The other attendee's note is untouched. - expect((await getNoteRows([other]))[0]?.id).toBe(otherRow!.id); + expect((await getNoteRows("attendee", [other]))[0]?.id).toBe(otherRow!.id); + }); + + test("deletes nothing, and asks the database nothing, for an empty list", async () => { + const owner = await makeAttendee("Nothing To Delete"); + await createSystemNote(attendeeNotes(owner), "kept"); + + // The stale-note cleanup runs on every refresh, almost always with nothing + // to remove, so this path must not cost a round trip. + const { roundTrips } = await runAndCountRoundTrips(() => + deleteNotes(attendeeNotes(owner), []), + ); + + expect(roundTrips).toBe(0); + expect(await getNoteRows("attendee", [owner])).toHaveLength(1); }); }); diff --git a/test/shared/db/notes/sealing.test.ts b/test/shared/db/notes/sealing.test.ts new file mode 100644 index 0000000000..240ed096f2 --- /dev/null +++ b/test/shared/db/notes/sealing.test.ts @@ -0,0 +1,95 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { decrypt } from "#shared/crypto/encryption.ts"; +import { decryptWithOwnerKey } from "#shared/crypto/keys.ts"; +import type { + EnvKeyEncrypted, + OwnerKeyEncrypted, +} from "#shared/crypto/sealed.ts"; +import { openNote, openNotes, sealNote } from "#shared/db/notes/sealing.ts"; +import type { SystemNoteRow } from "#shared/db/notes/types.ts"; +import { getTestPrivateKey } from "#test-utils/crypto.ts"; +import { describeWithEnv } from "#test-utils/db.ts"; + +const row = ( + type: Type, + note: string, + id = 1, +): SystemNoteRow => + ({ + created: "2026-07-28T10:00:00.000Z", + entity_id: 4, + entity_type: "attendee", + id, + note, + type, + }) as SystemNoteRow; + +describeWithEnv("db > notes > sealing", { db: true }, () => { + describe("sealing a note", () => { + test("seals a system note so the data key alone opens it", async () => { + const sealed = await sealNote("system", "system secret"); + + expect(sealed.startsWith("enc:")).toBe(true); + expect(sealed).not.toContain("system secret"); + expect(await decrypt(sealed as EnvKeyEncrypted)).toBe("system secret"); + }); + + test("seals an owner note so only the owner key opens it", async () => { + const sealed = await sealNote("owner", "owner secret"); + + expect(sealed.startsWith("hyb:")).toBe(true); + expect(sealed).not.toContain("owner secret"); + expect( + await decryptWithOwnerKey( + sealed as OwnerKeyEncrypted, + await getTestPrivateKey(), + ), + ).toBe("owner secret"); + }); + }); + + describe("opening a note", () => { + test("opens each kind the way it was sealed", async () => { + const sealed = [ + row("system", await sealNote("system", "from the app"), 1), + row("owner", await sealNote("owner", "from the operator"), 2), + ]; + + const opened = await openNotes(sealed, await getTestPrivateKey()); + + expect(opened.map((note) => note.note)).toEqual([ + "from the app", + "from the operator", + ]); + }); + + test("keeps everything about the note except its sealed text", async () => { + const sealed = row("system", await sealNote("system", "kept"), 12); + + const opened = await openNote(sealed, await getTestPrivateKey()); + + expect(opened).toEqual({ ...sealed, note: "kept" }); + }); + + test("opens a batch in the order it was given", async () => { + const sealed = await Promise.all( + ["first", "second", "third"].map(async (text, index) => + row("system", await sealNote("system", text), index + 1), + ), + ); + + const opened = await openNotes(sealed, await getTestPrivateKey()); + + expect(opened.map((note) => note.note)).toEqual([ + "first", + "second", + "third", + ]); + }); + + test("opens nothing when given nothing", async () => { + expect(await openNotes([], await getTestPrivateKey())).toEqual([]); + }); + }); +}); diff --git a/test/shared/db/notes/target.test.ts b/test/shared/db/notes/target.test.ts new file mode 100644 index 0000000000..b8b793e6a5 --- /dev/null +++ b/test/shared/db/notes/target.test.ts @@ -0,0 +1,73 @@ +import { expect } from "@std/expect"; +import { describe, it as test } from "@std/testing/bdd"; +import { + attendeeNotes, + groupNotesByTargetId, + NOTE_ENTITIES, + targetsSelectedBy, + targetsWhere, + targetWhere, +} from "#shared/db/notes/target.ts"; + +describe("what a note is about", () => { + test("names a record by its kind and its id", () => { + expect(attendeeNotes(7)).toEqual({ entity: "attendee", id: 7 }); + }); + + test("lists the kinds of record a note can be about", () => { + // The database CHECK is built from this list, so it is also what a stored + // note is allowed to say. + expect([...NOTE_ENTITIES]).toEqual(["attendee"]); + }); + + test("asks for one record by both of its parts", () => { + expect(targetWhere(attendeeNotes(4))).toEqual({ + args: ["attendee", 4], + sql: "entity_type = ? AND entity_id = ?", + }); + }); + + test("asks for several records of one kind in one go", () => { + expect(targetsWhere("attendee", [4, 5])).toEqual({ + args: ["attendee", 4, 5], + sql: "entity_type = ? AND entity_id IN (?, ?)", + }); + }); + + test("refuses to ask about no records at all", () => { + // Building `IN ()` would be SQL no database accepts, and a caller with + // nothing to ask about should not have reached here. + expect(() => targetsWhere("attendee", [])).toThrow( + "Asked for the notes of no attendee records", + ); + }); + + test("keeps the subquery's own arguments after the kind", () => { + expect( + targetsSelectedBy("attendee", { + args: [12], + sql: "SELECT attendee_id FROM listing_attendees WHERE listing_id = ?", + }), + ).toEqual({ + args: ["attendee", 12], + sql: "entity_type = ? AND entity_id IN (SELECT attendee_id FROM listing_attendees WHERE listing_id = ?)", + }); + }); + + test("groups notes by the record they are about, keeping their order", () => { + const notes = [ + { entity_id: 1, note: "first" }, + { entity_id: 2, note: "other" }, + { entity_id: 1, note: "second" }, + ]; + + const grouped = groupNotesByTargetId(notes); + + expect(grouped.get(1)?.map((note) => note.note)).toEqual([ + "first", + "second", + ]); + expect(grouped.get(2)?.map((note) => note.note)).toEqual(["other"]); + expect(grouped.get(3)).toBeUndefined(); + }); +}); diff --git a/test/shared/db/orphan-attendees.test.ts b/test/shared/db/orphan-attendees.test.ts index 48241b2010..b1b70c0df3 100644 --- a/test/shared/db/orphan-attendees.test.ts +++ b/test/shared/db/orphan-attendees.test.ts @@ -10,11 +10,12 @@ import { assertExists } from "@std/assert"; import { expect } from "@std/expect"; import { describe, it as test } from "@std/testing/bdd"; import { getDb, insert, queryOne, requireOne } from "#shared/db/client.ts"; +import { createSystemNote, getNoteRows } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { countOrphanedAttendees, purgeOrphanedAttendees, } from "#shared/db/orphan-attendees.ts"; -import { createSystemNote, getNoteRows } from "#shared/db/system-notes.ts"; import { nowIso, nowMs } from "#shared/now.ts"; import { insertCheckoutStage } from "#test-utils/checkout-stages.ts"; import { describeWithEnv } from "#test-utils/db.ts"; @@ -148,13 +149,13 @@ describeWithEnv("db > orphan-attendees", { db: true }, () => { processed_at: nowIso(), }), ); - await createSystemNote(id, "orphan note"); + await createSystemNote(attendeeNotes(id), "orphan note"); await purgeOrphanedAttendees(nowIso()); expect(await childCount("attendee_answers", id)).toBe(0); expect(await childCount("processed_payments", id)).toBe(0); - expect(await getNoteRows([id])).toEqual([]); + expect(await getNoteRows("attendee", [id])).toEqual([]); }); test("removes the orphan's checkout stage", async () => { diff --git a/test/specs/steps/payment-capacity.ts b/test/specs/steps/payment-capacity.ts index bb0ddbedd4..3d8bbfdabb 100644 --- a/test/specs/steps/payment-capacity.ts +++ b/test/specs/steps/payment-capacity.ts @@ -4,8 +4,9 @@ import { Given, Then, When } from "@cucumber/cucumber"; import { expect } from "@std/expect"; import { handleRequest } from "#routes"; import { getAttendeesRaw } from "#shared/db/attendees/queries.ts"; +import { getNotesFor } from "#shared/db/notes/queries.ts"; +import { attendeeNotes } from "#shared/db/notes/target.ts"; import { isSessionProcessed } from "#shared/db/processed-payments.ts"; -import { getNotesForAttendee } from "#shared/db/system-notes.ts"; import { requiredWorldValue, type TicketsWorld, @@ -147,8 +148,8 @@ Then( async function (this: TicketsWorld): Promise { const attendeeId = requiredWorldValue(this.placeholderId, "placeholder id"); const sessionId = requiredWorldValue(this.sessionId, "session id"); - const notes = await getNotesForAttendee( - attendeeId, + const notes = await getNotesFor( + attendeeNotes(attendeeId), await getTestPrivateKey(), ); expect(notes).toHaveLength(1); diff --git a/test/test-utils/reservation/helpers.ts b/test/test-utils/reservation/helpers.ts index af1e9f420f..95ddb47697 100644 --- a/test/test-utils/reservation/helpers.ts +++ b/test/test-utils/reservation/helpers.ts @@ -239,7 +239,7 @@ export const expectRefundedPlaceholder = async ( expect(await modifierUsageCount(addOnId)).toBe(0); expect(refund.calls[0]!.args).toEqual([paymentIntentId]); expect(refund.calls.length).toBe(1); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); - expect((await getNoteRows([attendees[0]!.id])).length).toBe(1); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); + expect((await getNoteRows("attendee", [attendees[0]!.id])).length).toBe(1); return attendees; }; diff --git a/test/test-utils/webhooks.ts b/test/test-utils/webhooks.ts index c50c8a80f0..c6f15f6716 100644 --- a/test/test-utils/webhooks.ts +++ b/test/test-utils/webhooks.ts @@ -213,8 +213,8 @@ export const expectRefundedWithNote = async ( mockRefund: { calls: unknown[] }, ): Promise => { expect(mockRefund.calls.length).toBe(1); - const { getNoteRows } = await import("#shared/db/system-notes.ts"); - expect((await getNoteRows([attendeeId])).length).toBe(1); + const { getNoteRows } = await import("#shared/db/notes/queries.ts"); + expect((await getNoteRows("attendee", [attendeeId])).length).toBe(1); }; /** diff --git a/test/ui/templates/admin/attendee-notes.test.tsx b/test/ui/templates/admin/attendee-notes.test.tsx index 277b7d2fbe..e6b20f2108 100644 --- a/test/ui/templates/admin/attendee-notes.test.tsx +++ b/test/ui/templates/admin/attendee-notes.test.tsx @@ -1,6 +1,6 @@ import { expect } from "@std/expect"; import { beforeAll, describe, it as test } from "@std/testing/bdd"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { AddNoteLink, AttendeeNotesSection, @@ -15,8 +15,9 @@ import { import { withEnv } from "#test-utils/env.ts"; const note = (overrides: Partial = {}): SystemNote => ({ - attendee_id: 5, created: "2026-06-23T10:00:00.000Z", + entity_id: 5, + entity_type: "attendee", id: 1, note: "Refunded — see the [ledger](/admin/ledger?attendee=5).", type: "system", @@ -117,9 +118,9 @@ describe("AttendeeNotesSummary", () => { isOwner={false} names={names} notes={[ - note({ attendee_id: 5, id: 1, note: "first" }), - note({ attendee_id: 5, id: 2, note: "second" }), - note({ attendee_id: 6, id: 3, note: "other" }), + note({ entity_id: 5, id: 1, note: "first" }), + note({ entity_id: 5, id: 2, note: "second" }), + note({ entity_id: 6, id: 3, note: "other" }), ]} />, ); @@ -136,7 +137,7 @@ describe("AttendeeNotesSummary", () => { , ); expect(html).toContain("#9"); @@ -149,7 +150,7 @@ describe("AttendeeNotesSummary", () => { , ); expect(staffHtml).toContain("see the ledger"); @@ -159,7 +160,7 @@ describe("AttendeeNotesSummary", () => { , ); expect(ownerHtml).toContain('href="/admin/ledger?attendee=5"'); diff --git a/test/ui/templates/admin/attendee-page.test.ts b/test/ui/templates/admin/attendee-page.test.ts index d56925dd83..4e15c31ca4 100644 --- a/test/ui/templates/admin/attendee-page.test.ts +++ b/test/ui/templates/admin/attendee-page.test.ts @@ -1,7 +1,7 @@ import { expect } from "@std/expect"; import { beforeAll, describe, it as test } from "@std/testing/bdd"; import type { AttendeeStatus } from "#shared/db/attendee-statuses.ts"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import { attendeeBanner, ContactHistory, @@ -24,8 +24,9 @@ const attendeeStatus = ( }); const OWNER_NOTE: SystemNote = { - attendee_id: 1, created: "2026-07-11T10:00:00.000Z", + entity_id: 1, + entity_type: "attendee", id: 1, note: "Bring identification", type: "owner", diff --git a/test/ui/templates/admin/attendees-list.test.ts b/test/ui/templates/admin/attendees-list.test.ts index d7f24ed202..b1cc96cb0d 100644 --- a/test/ui/templates/admin/attendees-list.test.ts +++ b/test/ui/templates/admin/attendees-list.test.ts @@ -1,6 +1,6 @@ import { expect } from "@std/expect"; import { beforeAll, describe, it as test } from "@std/testing/bdd"; -import type { SystemNote } from "#shared/db/system-notes.ts"; +import type { SystemNote } from "#shared/db/notes/types.ts"; import type { AttendeeTableRow } from "#shared/types.ts"; import { type AttendeesListPageProps, @@ -87,8 +87,9 @@ describe("adminAttendeesListPage", () => { test("surfaces a red notes summary for attendees that have notes", () => { const noteRow: SystemNote = { - attendee_id: 1, created: "2026-06-23T10:00:00.000Z", + entity_id: 1, + entity_type: "attendee", id: 1, note: "needs a follow-up call", type: "system",