diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8920a4ee..15e8f6a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,6 +82,9 @@ jobs: run: pnpm test env: DATABASE_URL: postgresql://postgres:postgres@localhost:5434/neondb + # Opts in the tests that write to a real database. Only the throwaway + # CI database is ever set here. + TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:5434/neondb - name: Stop test database if: always() diff --git a/docs/features/events.md b/docs/features/events.md index 91381fda..87ea07f6 100644 --- a/docs/features/events.md +++ b/docs/features/events.md @@ -35,7 +35,6 @@ sequenceDiagram API->>API: enabled? within 24h of start? bootcamp seat? API->>DB: lock event row if capped, insert event_check_in DB-->>API: unique violation means already checked in - API->>DB: current_check_ins + 1 API-->>Door: success with event title ``` @@ -48,7 +47,7 @@ sequenceDiagram | Event started more than 24 hours ago | `BAD_REQUEST` "Check-in for this event has closed" | | `bootcampOnly` and the caller's `bootcampTerm` is not the session's term | `FORBIDDEN` | | Already has a row for this event | `CONFLICT` "Already checked in" | -| `currentCheckIns >= maxCheckIns` (row locked `FOR UPDATE`, only when capped) | `BAD_REQUEST` "Event is full" | +| Count of `event_check_in` rows `>= maxCheckIns` (event row locked `FOR UPDATE`, only when capped) | `BAD_REQUEST` "Event is full" | There is no earliest time: a scan is accepted before the event starts. Membership is not checked. The member row is looked up only so a member's check-in carries `member_id`. The `events.membersOnly` column is no longer read. @@ -63,7 +62,7 @@ Officers need the scanner gate (`isScanner`: any active, unexpired admin row, vo | Scan a member's pass | `scanMemberPass` | Looks up `member.pass_code`. `NOT_FOUND` "Unrecognised pass. It may have been rotated." Returns whether the membership is active. Does not check `checkInEnabled` or the 24 hour limit | | Check in by email | `manualCheckIn` | The person must already have an account. Method is stored as `manual` | | See the roster | `attendees` | 50 per page by default, max 200, newest first | -| Undo | `removeAttendance` | Deletes the row and decrements `current_check_ins` (never below zero) | +| Undo | `removeAttendance` | Deletes the row; the next capacity check counts one fewer | The three check-in writes (`checkIn`, `manualCheckIn`, `scanMemberPass`) share the capacity lock and the unique `(event_id, user_id)` index, so a double tap or two stations scanning the same person produce one row. A member's pass is a UUID rotated with `member.rotatePass`; see [Membership](./membership.md). @@ -117,10 +116,10 @@ The scanner hook (`components/portal/EventCheckIn.tsx`) also recognises a hackat | Table | Key columns | | --- | --- | -| `event` | `title`, `event_date`, `qr_code` (unique), `check_in_enabled`, `max_check_ins`, `current_check_ins`, `bootcamp_week`, `bootcamp_term`, `bootcamp_only`, `kind` (generated), `created_by_id` | +| `event` | `title`, `event_date`, `qr_code` (unique), `check_in_enabled`, `max_check_ins`, `bootcamp_week`, `bootcamp_term`, `bootcamp_only`, `kind` (generated), `created_by_id` | | `event_check_in` | `event_id`, `user_id`, `member_id` (null for non-members), `check_in_method` (`qr_code` or `manual`), `checked_in_at`. Unique on `(event_id, user_id)` | -`current_check_ins` is a counter kept in step with the rows. It is the capacity check, so every insert and delete moves it in the same transaction. +The check-in count is `count(*)` of `event_check_in` rows, taken under the event row lock for the capacity check and as a subquery for `currentCheckIns` in event reads. The old `current_check_ins` column is no longer read or written and will be dropped in a later change. ## Code and tests diff --git a/docs/features/hackathons.md b/docs/features/hackathons.md index f58ecb9f..16760ddd 100644 --- a/docs/features/hackathons.md +++ b/docs/features/hackathons.md @@ -82,7 +82,7 @@ Roles come from `packages/api/src/middleware/procedures.ts`. `isAdmin` is full s | `sendMassAcceptanceEmails` | Mails accepted people in batches. Resumable per person through `acceptance_email_sent_at`. | | `analytics` | Counts for the dashboard. | -`max_participants` caps acceptances, not applications. `current_participants` is re-derived from `approved` plus `checked_in` rows by `syncCurrentParticipants`, which locks the hackathon row first. +`max_participants` caps acceptances, not applications. Seats taken are counted from `approved` plus `checked_in` rows on every read (`currentParticipants` in hackathon reads) and under the hackathon row lock in `assertSeats`. The `current_participants` and team `current_members` columns are no longer read or written and will be dropped in a later change. ### Interest list (`interest.ts`) @@ -127,7 +127,7 @@ Schema: `packages/db/src/schemas/hackathons.ts`. | Table | Holds | | --- | --- | -| `hackathon` | Edition: dates, deadline, status, `max_participants`, `current_participants`, prizes, tracks, challenges, `is_public`, `judging_active`. | +| `hackathon` | Edition: dates, deadline, status, `max_participants`, prizes, tracks, challenges, `is_public`, `judging_active`. | | `hackathon_participant` | One per user per edition: status, form answers, consent, `acceptance_wave`, `attendance_confirmed_at`, `checked_in_at`, team link. | | `hackathon_interest` | Interest list, with per-recipient email sent and failed stamps. | | `hackathon_team`, `hackathon_project` | Teams (max 4 by default) and submissions (`draft`, `submitted`, `judging`, `winner`). | diff --git a/packages/api/package.json b/packages/api/package.json index 0990fc20..c7ea81d2 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -16,6 +16,7 @@ "./pricing": "./src/services/pricing.ts", "./email-limits": "./src/services/email-limits.ts", "./eastern-time": "./src/services/eastern-time.ts", + "./derived-counts": "./src/services/derived-counts.ts", "./metrics": "./src/services/metrics.ts", "./judge-interest": "./src/services/judge-interest.ts", "./portal-context": "./src/types/portal-context.ts", diff --git a/packages/api/src/.internal-tests/bootcamp.test.ts b/packages/api/src/.internal-tests/bootcamp.test.ts index 2f91a4ed..dd3711a6 100644 --- a/packages/api/src/.internal-tests/bootcamp.test.ts +++ b/packages/api/src/.internal-tests/bootcamp.test.ts @@ -54,6 +54,7 @@ vi.mock("@query/db", async () => { leftJoin: () => node, where: () => node, orderBy: () => node, + groupBy: () => node, limit: () => rows(), then: (ok: any, err: any) => rows().then(ok, err), }; diff --git a/packages/api/src/.internal-tests/derived-counts.db.test.ts b/packages/api/src/.internal-tests/derived-counts.db.test.ts new file mode 100644 index 00000000..834e42f6 --- /dev/null +++ b/packages/api/src/.internal-tests/derived-counts.db.test.ts @@ -0,0 +1,295 @@ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import type * as DbModule from "@query/db"; +import type * as DrizzleOps from "drizzle-orm"; +import type * as CountHelpers from "../services/derived-counts"; + +/** + * Real-Postgres test for the derived counts that replaced the stored counters + * events.current_check_ins, hackathon_team.current_members and + * hackathon.current_participants. + * + * The mocked suites cannot see SQL: a correlated subquery in a relational + * query's extras bound its outer id to the wrong table and returned 0 for every + * row, and no mock noticed. This file runs the real procedures and helpers + * against a real database. + * + * Needs TEST_DATABASE_URL (a local, disposable database; never Neon or + * production) and skips without it. It inserts its own rows under a random + * suffix and removes them afterwards. The stored counters are set to wrong + * numbers on purpose, so a read that still used them would fail. + */ +const URL = process.env.TEST_DATABASE_URL; + +describe.skipIf(!URL)("derived counts against a real database", () => { + const suffix = Math.random().toString(36).slice(2, 10); + const userIds = Array.from({ length: 6 }, (_, i) => `dc-${suffix}-u${i}`); + + let ctx: any; + let appRouter: any; + let S: typeof DbModule; + let helpers: typeof CountHelpers; + let drizzleOps: typeof DrizzleOps; + let eventId: string; + let cappedEventId: string; + let cappedQr: string; + let hackathonId: string; + let teamId: string; + let emptyTeamId: string; + + beforeAll(async () => { + process.env.DATABASE_URL = URL; + S = await import("@query/db"); + drizzleOps = await import("drizzle-orm"); + helpers = await import("../services/derived-counts"); + ({ appRouter } = await import("../root")); + const { cache } = await import("../middleware/cache"); + const db = S.db!; + + ctx = (userId?: string) => ({ + db, + session: userId ? { user: { id: userId } } : null, + userId, + cache, + clientIp: "127.0.0.1", + req: { headers: { get: () => null } }, + }); + + await db.insert(S.users).values( + userIds.map((id) => ({ id, email: `${id}@example.test`, name: id })), + ); + + // Event with 3 check-ins and a stored counter of 99. + const [event] = await db + .insert(S.events) + .values({ + title: `dc event ${suffix}`, + eventDate: new Date(), + qrCode: crypto.randomUUID(), + createdById: userIds[0]!, + currentCheckIns: 99, + }) + .returning({ id: S.events.id }); + eventId = event!.id; + await db.insert(S.eventCheckIns).values( + userIds.slice(0, 3).map((userId) => ({ eventId, userId })), + ); + + // Capped event: cap 2, two real rows, stored counter 0. + cappedQr = crypto.randomUUID(); + const [capped] = await db + .insert(S.events) + .values({ + title: `dc capped ${suffix}`, + eventDate: new Date(), + qrCode: cappedQr, + createdById: userIds[0]!, + maxCheckIns: 2, + currentCheckIns: 0, + }) + .returning({ id: S.events.id }); + cappedEventId = capped!.id; + await db.insert(S.eventCheckIns).values( + userIds.slice(0, 2).map((userId) => ({ eventId: cappedEventId, userId })), + ); + + // Hackathon: 2 approved + 1 checked_in + 2 pending/rejected = 3 seats, + // stored counter 42. + const [hackathon] = await db + .insert(S.hackathons) + .values({ + name: `dc hackathon ${suffix}`, + startDate: new Date(Date.now() + 86_400_000), + endDate: new Date(Date.now() + 2 * 86_400_000), + status: "open", + maxParticipants: 10, + currentParticipants: 42, + }) + .returning({ id: S.hackathons.id }); + hackathonId = hackathon!.id; + + const [team, emptyTeam] = await db + .insert(S.hackathonTeams) + .values([ + { + hackathonId, + name: `dc team ${suffix}`, + captainId: userIds[0]!, + currentMembers: 7, + }, + { + hackathonId, + name: `dc empty ${suffix}`, + captainId: userIds[5]!, + currentMembers: 3, + }, + ]) + .returning({ id: S.hackathonTeams.id }); + teamId = team!.id; + emptyTeamId = emptyTeam!.id; + + const statuses = [ + "approved", + "approved", + "checked_in", + "pending", + "rejected", + ] as const; + await db.insert(S.hackathonParticipants).values( + statuses.map((registrationStatus, i) => ({ + hackathonId, + userId: userIds[i]!, + registrationStatus, + // Three of the five are on the team. + teamId: i < 3 ? teamId : null, + })), + ); + }, 60_000); + + afterAll(async () => { + const db = S?.db; + if (!db) return; + const { inArray } = drizzleOps; + await db + .delete(S.events) + .where(inArray(S.events.id, [eventId, cappedEventId].filter(Boolean))); + if (hackathonId) { + await db + .delete(S.hackathons) + .where(drizzleOps.eq(S.hackathons.id, hackathonId)); + } + await db.delete(S.users).where(inArray(S.users.id, userIds)); + }, 60_000); + + it("counts check-ins per event in one grouped read", async () => { + const counts = await helpers.checkInCounts(S.db!, [ + eventId, + cappedEventId, + null, + ]); + expect(counts.get(eventId)).toBe(3); + expect(counts.get(cappedEventId)).toBe(2); + }); + + it("counts team members and gives an empty team no entry", async () => { + const counts = await helpers.teamMemberCounts(S.db!, [teamId, emptyTeamId]); + expect(counts.get(teamId)).toBe(3); + expect(counts.get(emptyTeamId)).toBeUndefined(); + const merged = await helpers.withTeamMemberCounts(S.db!, [ + { id: teamId }, + { id: emptyTeamId }, + ]); + expect(merged.map((t) => t.currentMembers)).toEqual([3, 0]); + }); + + it("counts only approved and checked_in participants as seats", async () => { + const counts = await helpers.seatedCounts(S.db!, [hackathonId]); + expect(counts.get(hackathonId)).toBe(3); + }); + + it("does not query for an empty id list", async () => { + const counts = await helpers.checkInCounts( + { select: () => { throw new Error("queried"); } } as any, + [], + ); + expect(counts.size).toBe(0); + }); + + it("events.list returns the row count, not the stored counter", async () => { + const { cache } = await import("../middleware/cache"); + cache.clear(); + const list = await appRouter.createCaller(ctx()).events.list(); + const mine = list.find((e: any) => e.id === eventId); + expect(mine?.currentCheckIns).toBe(3); + expect(list.find((e: any) => e.id === cappedEventId)?.currentCheckIns).toBe( + 2, + ); + }); + + it("hackathon.getById returns the seats held by rows, not the stored counter", async () => { + const { cache } = await import("../middleware/cache"); + cache.clear(); + const row = await appRouter + .createCaller(ctx()) + .hackathon.getById({ id: hackathonId }); + expect(row.currentParticipants).toBe(3); + }); + + it("hackathon.list returns the seats held by rows", async () => { + const { cache } = await import("../middleware/cache"); + cache.clear(); + const rows = await appRouter.createCaller(ctx()).hackathon.list({}); + expect( + rows.find((h: any) => h.id === hackathonId)?.currentParticipants, + ).toBe(3); + }); + + it("team.list returns the roster size, not the stored counter", async () => { + const { cache } = await import("../middleware/cache"); + cache.clear(); + const teams = await appRouter + .createCaller(ctx(userIds[0])) + .team.list({ hackathonId }); + expect(teams.find((t: any) => t.id === teamId)?.currentMembers).toBe(3); + expect(teams.find((t: any) => t.id === emptyTeamId)?.currentMembers).toBe( + 0, + ); + }); + + it("refuses a check-in when rows fill the event although the stored counter reads 0", async () => { + await expect( + appRouter + .createCaller(ctx(userIds[4])) + .events.checkIn({ qrCode: cappedQr }), + ).rejects.toMatchObject({ message: "Event is full" }); + }); + + it("admits a check-in once the rows leave room although the stored counter reads full", async () => { + const db = S.db!; + const { and, eq } = drizzleOps; + // Free a seat by deleting a row, and set the counter to read full. + await db + .delete(S.eventCheckIns) + .where( + and( + eq(S.eventCheckIns.eventId, cappedEventId), + eq(S.eventCheckIns.userId, userIds[1]!), + ), + ); + await db + .update(S.events) + .set({ currentCheckIns: 2 }) + .where(eq(S.events.id, cappedEventId)); + + await expect( + appRouter + .createCaller(ctx(userIds[4])) + .events.checkIn({ qrCode: cappedQr }), + ).resolves.toMatchObject({ success: true }); + + const counts = await helpers.checkInCounts(db, [cappedEventId]); + expect(counts.get(cappedEventId)).toBe(2); + // And the next one is refused again, by rows. + await expect( + appRouter + .createCaller(ctx(userIds[5])) + .events.checkIn({ qrCode: cappedQr }), + ).rejects.toMatchObject({ message: "Event is full" }); + }); + + it("a deleted user's rows leave every count with nothing to maintain", async () => { + const db = S.db!; + const { eq } = drizzleOps; + // Cascades their check-in and participant rows, the drift that motivated + // deriving these numbers. + await db.delete(S.users).where(eq(S.users.id, userIds[2]!)); + expect( + (await helpers.checkInCounts(db, [eventId])).get(eventId), + ).toBe(2); + expect( + (await helpers.teamMemberCounts(db, [teamId])).get(teamId), + ).toBe(2); + expect( + (await helpers.seatedCounts(db, [hackathonId])).get(hackathonId), + ).toBe(2); + }); +}); diff --git a/packages/api/src/.internal-tests/hackathon-admin-edge.test.ts b/packages/api/src/.internal-tests/hackathon-admin-edge.test.ts index 7b29b33b..e1af3395 100644 --- a/packages/api/src/.internal-tests/hackathon-admin-edge.test.ts +++ b/packages/api/src/.internal-tests/hackathon-admin-edge.test.ts @@ -1003,9 +1003,10 @@ describe("Hackathon admin management edge cases", () => { ).resolves.toMatchObject({ success: true }); }); - // BUG: hackathons.currentParticipants is incremented on register and never - // decremented, so rejected applicants permanently consume capacity. - it("frees a seat when an admin rejects a registration", async () => { + // Seats are counted from the participant rows, so rejecting one frees the + // seat by changing that row alone; no stored counter on the hackathon is + // written. + it("frees a seat when an admin rejects a registration without writing a counter", async () => { const caller = adminCaller({ hackathonParticipants: { id: PART_A1, @@ -1024,7 +1025,10 @@ describe("Hackathon admin management edge cases", () => { const capacityWrite = mockUpdate.mock.calls.find( (c) => c[1][0] === hackathons, ); - expect(capacityWrite).toBeDefined(); + expect(capacityWrite).toBeUndefined(); + expect( + mockUpdate.mock.calls.some((c) => c[1][0] === hackathonParticipants), + ).toBe(true); }); }); diff --git a/packages/api/src/.internal-tests/hackathon-flow.test.ts b/packages/api/src/.internal-tests/hackathon-flow.test.ts index 37c4844e..e449df2f 100644 --- a/packages/api/src/.internal-tests/hackathon-flow.test.ts +++ b/packages/api/src/.internal-tests/hackathon-flow.test.ts @@ -665,7 +665,28 @@ describe("Hackathon end-to-end flow", () => { ); }); + // The stored counter reads 0 here on purpose: capacity is the count of + // participant rows pointing at the team, whatever the column holds. it("refuses to overfill a team at capacity", async () => { + const caller = joinCaller({ + team: { + id: "team_1", + hackathonId: HACK_A, + isOpen: true, + currentMembers: 0, + maxMembers: 4, + }, + }); + mockWhereRows.mockImplementation(() => [{ n: 4 }]); + + try { + await expect(caller.team.joinTeam(join)).rejects.toThrow(/full/); + } finally { + mockWhereRows.mockImplementation(() => [{ n: 1 }]); + } + }); + + it("lets a join through when the rows leave room even though the stored counter reads full", async () => { const caller = joinCaller({ team: { id: "team_1", @@ -675,8 +696,15 @@ describe("Hackathon end-to-end flow", () => { maxMembers: 4, }, }); + mockWhereRows.mockImplementation(() => [{ n: 2 }]); - await expect(caller.team.joinTeam(join)).rejects.toThrow(/full/); + try { + await expect(caller.team.joinTeam(join)).resolves.toMatchObject({ + success: true, + }); + } finally { + mockWhereRows.mockImplementation(() => [{ n: 1 }]); + } }); it("refuses to join a closed team", async () => { diff --git a/packages/api/src/.internal-tests/participant-edge.test.ts b/packages/api/src/.internal-tests/participant-edge.test.ts index d73a221e..175cdc39 100644 --- a/packages/api/src/.internal-tests/participant-edge.test.ts +++ b/packages/api/src/.internal-tests/participant-edge.test.ts @@ -776,18 +776,20 @@ describe("Participant edge cases", () => { // ===================================================================== describe("5. Concurrent team mutations", () => { - // The seat is taken by `update ... where currentMembers < maxMembers`, so - // the count each caller read is only a hint; the UPDATE re-tests it. A - // plain `team.currentMembers + 1` written from the snapshot would be a lost - // update and would seat five people in a team of four. - it("admits only one of two racing joins into the last free seat", async () => { - // A team with 3 of 4 seats taken. findFirst hands out snapshots, exactly - // as an unlocked SELECT inside a transaction would. + // The seat count is count(*) of participant rows taken under the team row + // lock (select ... for update), so the second joiner waits for the first to + // commit and then counts the first one's row. This file's transaction + // stand-in does not model that select lock, so the joins run one after the + // other, which is the order the lock enforces, and the lock is asserted. + it("admits only one of two joins into the last free seat", async () => { + // 3 of 4 seats held by rows. The stored counter is stale on purpose: it + // must play no part. + let seatedRows = 3; const teamRow = { id: TEAM_A, hackathonId: HACK_A, captainId: "captain_user", - currentMembers: 3, + currentMembers: 0, maxMembers: 4, isOpen: true, }; @@ -807,36 +809,35 @@ describe("Participant edge cases", () => { if (table === "hackathonTeams") return { ...teamRow }; return undefined; }); + mockSelect.mockImplementation((_trace: any, selectArgs: any[]) => + Object.keys(selectArgs?.[0] ?? {}).includes("n") + ? [{ n: seatedRows }] + : [{ id: TEAM_A }], + ); mockUpdate.mockImplementation((_op, updateArgs) => { - // `where currentMembers < maxMembers` is re-evaluated against the row - // as it stands when the statement runs — not against the snapshot the - // caller read — and rowCount reports whether it matched. That is the - // whole reason the capacity test lives in the UPDATE, so the stand-in - // has to re-check it too rather than blindly applying the write. - if (updateArgs[0] === hackathonTeams) { - if (teamRow.currentMembers >= teamRow.maxMembers) - return { rowCount: 0 }; - teamRow.currentMembers += 1; - return { rowCount: 1 }; - } - // Each caller claims their own participant row, and both start with - // teamId null, so `where teamId is null` matches for both. + // Claiming the participant row seats them: the next count sees it. + if (updateArgs[0] === hackathonParticipants) seatedRows += 1; return { rowCount: 1 }; }); - const results = await Promise.allSettled([ - callerFor("user_a").team.joinTeam({ hackathonId: HACK_A, teamId: TEAM_A }), - callerFor("user_b").team.joinTeam({ hackathonId: HACK_A, teamId: TEAM_A }), - ]); + const first = await callerFor("user_a").team.joinTeam({ + hackathonId: HACK_A, + teamId: TEAM_A, + }); + const second = callerFor("user_b").team.joinTeam({ + hackathonId: HACK_A, + teamId: TEAM_A, + }); - const rejected = results.filter((r) => r.status === "rejected"); - expect(rejected).toHaveLength(1); - expect(String((rejected[0] as PromiseRejectedResult)?.reason)).toMatch( - /full/, + expect(first).toMatchObject({ success: true }); + await expect(second).rejects.toThrow(/full/); + expect(seatedRows).toBe(4); + // The retired counter is never written. + expect(updatedTables()).not.toContain(hackathonTeams); + const lockedForUpdate = mockSelect.mock.calls.some((call) => + (call[0] as [string, unknown[]][]).some(([method]) => method === "for"), ); - // Both writers computed 3 + 1, so the counter reads 4 while five people - // actually hold teamId = TEAM_A. - expect(teamRow.currentMembers).toBe(4); + expect(lockedForUpdate).toBe(true); }); // Both clicks read teamId = null and both insert a team; what stops the @@ -1300,10 +1301,10 @@ describe("Participant edge cases", () => { expect(cache.get("hackathon:registrations:other_user")).not.toBeNull(); }); - // BUG: currentParticipants is only ever incremented (one write in the whole - // router tree, registration.ts:173). Rejected applicants hold a seat forever - // and there is no withdraw/cancelRegistration procedure at all. - it("frees a seat when an admin rejects a participant", async () => { + // Seats are counted from the participant rows (approved and checked_in), so + // rejecting one frees the seat by changing that row alone. There is no stored + // counter on the hackathon left to forget to decrement. + it("frees a seat when an admin rejects a participant without writing a counter", async () => { mockFindFirst.mockImplementation((table) => { if (table === "admins") return { userId: "admin_user_id", isActive: true, role: "admin" }; @@ -1323,7 +1324,8 @@ describe("Participant edge cases", () => { status: "rejected", }); - expect(updatedTables()).toContain(hackathons); + expect(updatedTables()).toContain(hackathonParticipants); + expect(updatedTables()).not.toContain(hackathons); }); it("refuses a check-in when a concurrent change unseated the participant", async () => { diff --git a/packages/api/src/.internal-tests/qr-checkin.test.ts b/packages/api/src/.internal-tests/qr-checkin.test.ts index 0ec98ad9..8c5d4783 100644 --- a/packages/api/src/.internal-tests/qr-checkin.test.ts +++ b/packages/api/src/.internal-tests/qr-checkin.test.ts @@ -157,6 +157,7 @@ vi.mock("@query/db", async () => { where: vi.fn().mockImplementation(() => Object.assign(Promise.resolve([{ count: 0 }]), { for: vi.fn().mockResolvedValue([{ count: 0 }]), + groupBy: vi.fn().mockResolvedValue([]), }), ), })), @@ -623,12 +624,12 @@ describe("QR check-in", () => { return io(checkIns.find((c) => c.userId === "member_user")); return io(undefined); }); - // Stands in for `select ... for update`: the locked read sees the row as - // it stands now, not the snapshot the caller opened with. + // Stands in for `select ... for update` and for the row count taken under + // it: capacity is the rows themselves, read as they stand now. mockSelect.mockImplementation((_op, _sel, fromArgs) => - fromArgs[0] === events - ? [{ currentCheckIns: row.currentCheckIns }] - : [{ count: 0 }], + fromArgs[0] === eventCheckIns + ? [{ n: checkIns.length }] + : [{ id: CLUB_EVENT }], ); mockInsert.mockImplementation((_op, insertArgs, valArgs) => { if (insertArgs[0] === eventCheckIns) { @@ -652,16 +653,6 @@ describe("QR check-in", () => { } return [{ id: `checkin_${checkIns.length}` }]; }); - mockUpdate.mockImplementation((_op, updateArgs, setArgs) => { - if (updateArgs[0] === events && setArgs[0].currentCheckIns) { - row.currentCheckIns += 1; - __onRollback(() => { - row.currentCheckIns -= 1; - }); - return [{ id: CLUB_EVENT }]; - } - return []; - }); const caller = appRouter.createCaller(createMockCtx("member_user")); const outcomes = await Promise.allSettled([ @@ -670,7 +661,7 @@ describe("QR check-in", () => { ]); expect(checkIns).toHaveLength(1); - expect(row.currentCheckIns).toBe(1); + expect(mockUpdate).not.toHaveBeenCalled(); const rejected = outcomes.filter((o) => o.status === "rejected"); expect(rejected).toHaveLength(1); expect((rejected[0] as PromiseRejectedResult).reason.code).toBe( @@ -678,12 +669,12 @@ describe("QR check-in", () => { ); }); - // The count the caller opened with is a snapshot; capacity holds because - // the locked re-read and the `currentCheckIns < maxCheckIns` on the - // increment both re-test it, so the second scanner is turned away rather - // than pushing the event one over. + // Capacity holds because the event row lock serialises the two scanners and + // the count of rows is taken under it, so the second scanner sees the first + // one's row and is turned away rather than pushing the event one over. it("never admits more people than maxCheckIns", async () => { const row = clubEvent({ maxCheckIns: 1 }); + const checkIns: any[] = []; mockFindFirst.mockImplementation((table: string) => { if (table === "events") return io({ ...row }); if (table === "hackathons") return { id: HACK_A }; @@ -691,24 +682,19 @@ describe("QR check-in", () => { return io(undefined); }); mockSelect.mockImplementation((_op, _sel, fromArgs) => - fromArgs[0] === events - ? [{ currentCheckIns: row.currentCheckIns }] - : [{ count: 0 }], + fromArgs[0] === eventCheckIns + ? [{ n: checkIns.length }] + : [{ id: CLUB_EVENT }], ); - mockInsert.mockReturnValue([{ id: "checkin" }]); - mockUpdate.mockImplementation((_op, updateArgs, setArgs) => { - if (updateArgs[0] === events && setArgs[0].currentCheckIns) { - // `where currentCheckIns < maxCheckIns` — the statement matches no - // row once the event is full, and RETURNING says so. - if (row.maxCheckIns && row.currentCheckIns >= row.maxCheckIns) - return []; - row.currentCheckIns += 1; + mockInsert.mockImplementation((_op, insertArgs, valArgs) => { + if (insertArgs[0] === eventCheckIns) { + const added = valArgs[0]; + checkIns.push(added); __onRollback(() => { - row.currentCheckIns -= 1; + checkIns.splice(checkIns.indexOf(added), 1); }); - return [{ id: CLUB_EVENT }]; } - return []; + return [{ id: "checkin" }]; }); const first = appRouter.createCaller(createMockCtx("member_one")); @@ -718,7 +704,8 @@ describe("QR check-in", () => { second.events.checkIn({ qrCode: QR_OLD }), ]); - expect(row.currentCheckIns).toBe(1); + expect(checkIns).toHaveLength(1); + expect(mockUpdate).not.toHaveBeenCalled(); expect(outcomes.filter((o) => o.status === "fulfilled")).toHaveLength(1); const rejected = outcomes.find((o) => o.status === "rejected"); expect((rejected as PromiseRejectedResult).reason.message).toMatch( @@ -726,6 +713,50 @@ describe("QR check-in", () => { ); }); + // events.current_check_ins is retired: a user deletion cascades their + // check-ins away and the stored number never moved. Capacity must follow + // the rows whatever that column holds. + it("refuses when the rows fill the event even though the stored counter reads low", async () => { + const row = clubEvent({ maxCheckIns: 2, currentCheckIns: 0 }); + mockFindFirst.mockImplementation((table: string) => { + if (table === "events") return io({ ...row }); + if (table === "members") return io(activeMember); + return io(undefined); + }); + mockSelect.mockImplementation((_op, _sel, fromArgs) => + fromArgs[0] === eventCheckIns ? [{ n: 2 }] : [{ id: CLUB_EVENT }], + ); + + await expect( + appRouter + .createCaller(createMockCtx("member_user")) + .events.checkIn({ qrCode: QR_OLD }), + ).rejects.toMatchObject({ message: "Event is full" }); + expect(mockInsert).not.toHaveBeenCalled(); + }); + + it("admits when the rows leave room even though the stored counter reads full", async () => { + const row = clubEvent({ maxCheckIns: 2, currentCheckIns: 2 }); + mockFindFirst.mockImplementation((table: string) => { + if (table === "events") return io({ ...row }); + if (table === "members") return io(activeMember); + return io(undefined); + }); + mockSelect.mockImplementation((_op, _sel, fromArgs) => + fromArgs[0] === eventCheckIns ? [{ n: 1 }] : [{ id: CLUB_EVENT }], + ); + mockInsert.mockReturnValue([{ id: "checkin" }]); + + await expect( + appRouter + .createCaller(createMockCtx("member_user")) + .events.checkIn({ qrCode: QR_OLD }), + ).resolves.toMatchObject({ success: true }); + expect(mockInsert).toHaveBeenCalledTimes(1); + // The counter is never written, so there is nothing to drift. + expect(mockUpdate).not.toHaveBeenCalled(); + }); + // BUG: checkIn's only temporal gate is checkInEnabled (events.ts:220); it // never reads eventDate, while events.ts:111-113 already advertises any // event older than 24h as "closed". @@ -1312,7 +1343,11 @@ describe("QR check-in", () => { * forever, and the counter read as over-full with nothing explaining it. */ it("refuses a capacity below the people already checked in", async () => { - asAdmin(clubEvent({ currentCheckIns: 40 })); + asAdmin(clubEvent()); + // The rows are what is counted; the stored counter is not consulted. + (db as any).select.mockImplementationOnce(() => ({ + from: () => ({ where: () => Promise.resolve([{ n: 40 }]) }), + })); await expect( appRouter diff --git a/packages/api/src/routers/bootcamp.ts b/packages/api/src/routers/bootcamp.ts index fa07cdae..70a33377 100644 --- a/packages/api/src/routers/bootcamp.ts +++ b/packages/api/src/routers/bootcamp.ts @@ -14,6 +14,7 @@ import { compareTerms, currentTerm } from "@query/db/services/membership"; import { createTRPCRouter, protectedProcedure } from "../trpc"; import { isAdmin } from "../middleware/procedures"; import { isUniqueViolation } from "../middleware/db-errors"; +import { withCheckInCounts } from "../services/derived-counts"; // Sessions remain events and attendance remains `event_check_in`. Workshop // rows hold only the material officers publish beside those sessions. @@ -143,16 +144,16 @@ async function sessionsForTerm(db: DrizzleDB, term: string): Promise // Staff only. Carries the door QR, which must never reach a member-facing // read: holding the code is enough to check in from anywhere. async function adminSessionsForTerm(db: DrizzleDB, term: string) { - return db + const rows = await db .select({ ...sessionColumns, qrCode: events.qrCode, - currentCheckIns: events.currentCheckIns, maxCheckIns: events.maxCheckIns, }) .from(events) .where(scheduledIn(term)) .orderBy(asc(events.bootcampWeek)); + return withCheckInCounts(db, rows); } export const bootcampRouter = createTRPCRouter({ diff --git a/packages/api/src/routers/events.ts b/packages/api/src/routers/events.ts index f5650443..2eec6ad3 100644 --- a/packages/api/src/routers/events.ts +++ b/packages/api/src/routers/events.ts @@ -6,7 +6,6 @@ import { events, eventCheckIns, members, users } from "@query/db"; import { eq, and, - lt, gt, gte, lte, @@ -18,6 +17,8 @@ import { import { randomUUID } from "crypto"; import { currentTerm } from "@query/db/services/membership"; import { isAdmin, isScanner } from "../middleware/procedures"; +import { withCheckInCounts } from "../services/derived-counts"; +import type { DrizzleDB } from "@query/db"; // Postgres unique_violation. Drizzle wraps every driver error in a // DrizzleQueryError, which carries no `code` — the pg error holding the @@ -42,6 +43,17 @@ const termStart = (now: Date) => { : new Date(`${year}-06-01T00:00:00-04:00`); }; +// The capacity is the rows themselves, counted under the event row lock the +// callers hold. events.currentCheckIns is a retired counter that drifted when a +// user's deletion cascaded their check-ins away. +const countCheckIns = async (tx: Pick, eventId: string) => { + const [row] = await tx + .select({ n: count() }) + .from(eventCheckIns) + .where(eq(eventCheckIns.eventId, eventId)); + return row?.n ?? 0; +}; + const ATTENDANCE_RANGES = ["term", "90d", "all"] as const; export const eventRouter = createTRPCRouter({ @@ -115,23 +127,26 @@ export const eventRouter = createTRPCRouter({ ctx.db as NonNullable ).query.events.findFirst({ where: eq(events.id, eventId), - columns: { currentCheckIns: true, bootcampTerm: true }, + columns: { bootcampTerm: true }, }); if (!existing) { throw new TRPCError({ code: "NOT_FOUND", message: "Event not found" }); } - // A cap below the number already scanned would make the counter read - // over-full forever and refuse everyone at the door, with nothing saying why. - if ( - typeof fields.maxCheckIns === "number" && - fields.maxCheckIns < existing.currentCheckIns - ) { - throw new TRPCError({ - code: "CONFLICT", - message: `${existing.currentCheckIns} people have already checked in, so the cap cannot be lower than that.`, - }); + // A cap below the number already scanned would read over-full forever and + // refuse everyone at the door, with nothing saying why. + if (typeof fields.maxCheckIns === "number") { + const checkedIn = await countCheckIns( + ctx.db as NonNullable, + eventId, + ); + if (fields.maxCheckIns < checkedIn) { + throw new TRPCError({ + code: "CONFLICT", + message: `${checkedIn} people have already checked in, so the cap cannot be lower than that.`, + }); + } } // Term follows the week, or an event moved out of the bootcamp keeps holding @@ -193,12 +208,19 @@ export const eventRouter = createTRPCRouter({ ctx.cache.deletePattern(`event:${input.eventId}`); ctx.cache.deletePattern("event*"); - return updatedEvent; + // The modal replaces its event with this result, so it carries the + // derived count rather than the retired stored one. + const [withCount] = await withCheckInCounts( + ctx.db as NonNullable, + [updatedEvent], + ); + return withCount; }), listAll: isAdmin.query(async ({ ctx }) => { - const fetchEvents = () => - (ctx.db as NonNullable).query.events.findMany({ + const fetchEvents = async () => { + const db = ctx.db as NonNullable; + const rows = await db.query.events.findMany({ orderBy: (events, { desc }) => [desc(events.eventDate)], with: { createdBy: { @@ -207,6 +229,8 @@ export const eventRouter = createTRPCRouter({ }, limit: 100, }); + return withCheckInCounts(db, rows); + }; const cacheKey = `events:list:all`; // Same shared-key stampede as `list` below, minus the public filter. @@ -218,11 +242,14 @@ export const eventRouter = createTRPCRouter({ }), list: publicProcedure.query(async ({ ctx }) => { - const fetchEvents = () => - (ctx.db as NonNullable).query.events.findMany({ + const fetchEvents = async () => { + const db = ctx.db as NonNullable; + const rows = await db.query.events.findMany({ orderBy: (events, { desc }) => [desc(events.eventDate)], limit: 50, }); + return withCheckInCounts(db, rows); + }; const cacheKey = `events:list:public`; // One key for every visitor, 30 seconds long: on a plain miss the whole @@ -285,7 +312,8 @@ export const eventRouter = createTRPCRouter({ }); } - return event; + // Every check-in row is loaded above, so that is the count. + return { ...event, currentCheckIns: event.checkIns.length }; }), toggleCheckIn: isAdmin @@ -416,13 +444,13 @@ export const eventRouter = createTRPCRouter({ // Held from the top it covered the two lookups above, serialising the // whole queue; a double tap is settled by unique(event_id, user_id). if (event.maxCheckIns) { - const [locked] = await tx - .select({ currentCheckIns: events.currentCheckIns }) + await tx + .select({ id: events.id }) .from(events) .where(eq(events.id, event.id)) .for("update"); - if (locked && locked.currentCheckIns >= event.maxCheckIns) { + if ((await countCheckIns(tx, event.id)) >= event.maxCheckIns) { throw new TRPCError({ code: "BAD_REQUEST", message: "Event is full", @@ -450,31 +478,6 @@ export const eventRouter = createTRPCRouter({ throw error; } - // The counter is the capacity, so the increment re-tests it in the same - // statement. A scanner past the gate on a stale count finds no row left to - // claim, and throwing takes the attendance row down with the transaction. - const claimed = await tx - .update(events) - .set({ - currentCheckIns: sql`${events.currentCheckIns} + 1`, - }) - .where( - event.maxCheckIns - ? and( - eq(events.id, event.id), - lt(events.currentCheckIns, event.maxCheckIns), - ) - : eq(events.id, event.id), - ) - .returning({ id: events.id }); - - if (event.maxCheckIns && claimed.length === 0) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Event is full", - }); - } - // Invalidate event and related caches after successful check-in ctx.cache.deletePattern(`event:${event.id}`); ctx.cache.deletePattern("event*"); @@ -535,16 +538,16 @@ export const eventRouter = createTRPCRouter({ columns: { id: true }, }); - // Only a capped event has a count worth locking; the guarded increment - // below and unique(event_id, user_id) carry the rest. + // only a capped event has a count worth locking; unique(event_id, user_id) + // carries the rest. if (event.maxCheckIns) { - const [locked] = await tx - .select({ currentCheckIns: events.currentCheckIns }) + await tx + .select({ id: events.id }) .from(events) .where(eq(events.id, event.id)) .for("update"); - if (locked && locked.currentCheckIns >= event.maxCheckIns) { + if ((await countCheckIns(tx, event.id)) >= event.maxCheckIns) { throw new TRPCError({ code: "BAD_REQUEST", message: "Event is full", @@ -569,26 +572,6 @@ export const eventRouter = createTRPCRouter({ throw error; } - const claimed = await tx - .update(events) - .set({ currentCheckIns: sql`${events.currentCheckIns} + 1` }) - .where( - event.maxCheckIns - ? and( - eq(events.id, event.id), - lt(events.currentCheckIns, event.maxCheckIns), - ) - : eq(events.id, event.id), - ) - .returning({ id: events.id }); - - if (event.maxCheckIns && claimed.length === 0) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Event is full", - }); - } - ctx.cache.deletePattern(`event:${event.id}`); ctx.cache.deletePattern("event*"); @@ -647,13 +630,13 @@ export const eventRouter = createTRPCRouter({ // Same rule as the other two doors, and this one is the burst path. if (event.maxCheckIns) { - const [locked] = await tx - .select({ currentCheckIns: events.currentCheckIns }) + await tx + .select({ id: events.id }) .from(events) .where(eq(events.id, event.id)) .for("update"); - if (locked && locked.currentCheckIns >= event.maxCheckIns) { + if ((await countCheckIns(tx, event.id)) >= event.maxCheckIns) { throw new TRPCError({ code: "BAD_REQUEST", message: "Event is full", @@ -680,26 +663,6 @@ export const eventRouter = createTRPCRouter({ throw error; } - const claimed = await tx - .update(events) - .set({ currentCheckIns: sql`${events.currentCheckIns} + 1` }) - .where( - event.maxCheckIns - ? and( - eq(events.id, event.id), - lt(events.currentCheckIns, event.maxCheckIns), - ) - : eq(events.id, event.id), - ) - .returning({ id: events.id }); - - if (event.maxCheckIns && claimed.length === 0) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: "Event is full", - }); - } - ctx.cache.deletePattern(`event:${event.id}`); ctx.cache.deletePattern("event*"); @@ -745,7 +708,7 @@ export const eventRouter = createTRPCRouter({ return { attendees: rows, matching: totals?.count ?? 0 }; }), - /** Undoes one check-in. The counter is the capacity, so it moves with it. */ + /** Undoes one check-in. Capacity is counted from the rows, so nothing else moves. */ removeAttendance: isScanner .input( z.object({ @@ -773,18 +736,6 @@ export const eventRouter = createTRPCRouter({ }); } - // Guarded so a double undo cannot drive the counter negative and hand out - // capacity the room does not have. - await tx - .update(events) - .set({ currentCheckIns: sql`${events.currentCheckIns} - 1` }) - .where( - and( - eq(events.id, input.eventId), - lt(sql`0`, events.currentCheckIns), - ), - ); - ctx.cache.deletePattern(`event:${input.eventId}`); ctx.cache.deletePattern("event*"); diff --git a/packages/api/src/routers/hackathon/admin.ts b/packages/api/src/routers/hackathon/admin.ts index 92ed25e5..fdd392c7 100644 --- a/packages/api/src/routers/hackathon/admin.ts +++ b/packages/api/src/routers/hackathon/admin.ts @@ -5,7 +5,7 @@ import { analyticsProcedures } from "./admin/analytics"; import { checkInProcedures } from "./admin/check-in"; // Registration calls these when it changes who holds a seat. -export { evictParticipantCaches, syncCurrentParticipants } from "./admin/seats"; +export { evictParticipantCaches } from "./admin/seats"; export const hackathonAdminRouter = createTRPCRouter({ ...rosterProcedures, diff --git a/packages/api/src/routers/hackathon/admin/seats.ts b/packages/api/src/routers/hackathon/admin/seats.ts index 3806fa0c..4809c8aa 100644 --- a/packages/api/src/routers/hackathon/admin/seats.ts +++ b/packages/api/src/routers/hackathon/admin/seats.ts @@ -11,29 +11,6 @@ import type { DrizzleDB } from "@query/db"; // than seats and fill them in waves. export const SEATED_STATUSES = ["approved", "checked_in"] as const; -// Re-derives currentParticipants from the rows that actually hold a seat. The -// hackathon row is locked first: `SET x = (subquery)` plans the subquery once -// per statement, so a register() committing mid-wait would be overwritten and -// the seat total would drift low enough to admit people past maxParticipants. -export const syncCurrentParticipants = ( - db: DrizzleDB, - hackathonId: string, -) => - db.transaction(async (tx) => { - await tx - .select({ id: hackathons.id }) - .from(hackathons) - .where(eq(hackathons.id, hackathonId)) - .for("update"); - - await tx - .update(hackathons) - .set({ - currentParticipants: sql`(select count(*)::int from ${hackathonParticipants} where ${hackathonParticipants.hackathonId} = ${hackathonId} and ${hackathonParticipants.registrationStatus} in ('approved', 'checked_in'))`, - }) - .where(eq(hackathons.id, hackathonId)); - }); - /** * Refuses an acceptance that would pass maxParticipants. Call inside the * transaction that writes the new statuses: it locks the hackathon row, so two @@ -88,8 +65,8 @@ export async function assertSeats( // wiped every attendee's cached registrations and the venue-wide events list. // Each affected user's own registration list goes too, or an acceptance lands // in their inbox while their dashboard still says pending. The hackathon row -// goes as well: every caller has just synced currentParticipants, and getById -// serves the public "spots taken" line from that cached row. +// goes as well: getById serves the public "spots taken" line, counted from the +// participant rows, from that cached row. export const evictParticipantCaches = ( cache: { delete: (key: string) => boolean }, hackathonId: string, diff --git a/packages/api/src/routers/hackathon/admin/status.ts b/packages/api/src/routers/hackathon/admin/status.ts index b9860809..48400f73 100644 --- a/packages/api/src/routers/hackathon/admin/status.ts +++ b/packages/api/src/routers/hackathon/admin/status.ts @@ -15,7 +15,6 @@ import { eq, and, inArray, ne, sql } from "drizzle-orm"; import type { DrizzleDB } from "@query/db"; import { SEATED_STATUSES, - syncCurrentParticipants, assertSeats, evictParticipantCaches, } from "./seats"; @@ -102,8 +101,6 @@ export const statusProcedures = { } }); - await syncCurrentParticipants(ctx.db as DrizzleDB, input.hackathonId); - evictParticipantCaches(ctx.cache, input.hackathonId, [ participant.userId, ]); @@ -261,8 +258,6 @@ export const statusProcedures = { }; } - await syncCurrentParticipants(db, input.hackathonId); - await recordAdminAction(db, { userId: ctx.userId, action: "hackathon.acceptWave", @@ -375,9 +370,6 @@ export const statusProcedures = { ); }); - // Approving a rejected or waitlisted applicant hands a seat back out. - await syncCurrentParticipants(db, hackathonId); - const { sendAcceptanceEmail } = await import("@query/auth/email"); let emailed = 0; @@ -527,8 +519,6 @@ export const statusProcedures = { }); }); - await syncCurrentParticipants(ctx.db as DrizzleDB, hackathonId); - evictParticipantCaches( ctx.cache, hackathonId, diff --git a/packages/api/src/routers/hackathon/crud.ts b/packages/api/src/routers/hackathon/crud.ts index 5b5c02e1..078fcc49 100644 --- a/packages/api/src/routers/hackathon/crud.ts +++ b/packages/api/src/routers/hackathon/crud.ts @@ -15,6 +15,11 @@ import { import { recordAdminAction } from "../../middleware/audit"; import { CacheKeys, VOLATILE_TTL } from "../../middleware/cache"; import type { DrizzleDB } from "@query/db"; +import { withSeatedCounts } from "../../services/derived-counts"; + +// Every hackathon read below returns currentParticipants counted from the +// participant rows (one grouped query per call). The stored column is retired: +// it drifted whenever a deleted user cascaded their registration away. // A draft has not been announced, so it does not belong in a participant's // browse list. "cancelled" is deliberately NOT hidden: both detail pages ship @@ -134,7 +139,7 @@ export const hackathonCrudRouter = createTRPCRouter({ async () => { const now = new Date(); - return await (ctx.db as DrizzleDB).query.hackathons.findMany({ + const rows = await (ctx.db as DrizzleDB).query.hackathons.findMany({ where: and( // Hidden means hidden from the public funnel, not from the people running the // event. Filtering it for staff too emptied the judging dashboard, which @@ -150,6 +155,7 @@ export const hackathonCrudRouter = createTRPCRouter({ offset: input.offset, orderBy: (hackathons, { desc }) => [desc(hackathons.startDate)], }); + return await withSeatedCounts(ctx.db as DrizzleDB, rows); }, VOLATILE_TTL, ); @@ -160,9 +166,10 @@ export const hackathonCrudRouter = createTRPCRouter({ const cacheKey = "hackathons:list:all"; const fetchAll = async () => { - return await (ctx.db as DrizzleDB).query.hackathons.findMany({ + const rows = await (ctx.db as DrizzleDB).query.hackathons.findMany({ orderBy: (hackathons, { desc }) => [desc(hackathons.startDate)], }); + return await withSeatedCounts(ctx.db as DrizzleDB, rows); }; // Five-second TTL on a key the whole instance shares, so every expiry @@ -218,12 +225,20 @@ export const hackathonCrudRouter = createTRPCRouter({ } } - const hackathon = isUuid + const found = isUuid ? await (ctx.db as DrizzleDB).query.hackathons.findFirst({ where: eq(hackathons.id, input.id), }) : await findByNameOrSlug(ctx.db as DrizzleDB, input.id); + if (!found) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "Hackathon not found", + }); + } + + const [hackathon] = await withSeatedCounts(ctx.db as DrizzleDB, [found]); if (!hackathon) { throw new TRPCError({ code: "NOT_FOUND", diff --git a/packages/api/src/routers/hackathon/registration.ts b/packages/api/src/routers/hackathon/registration.ts index 91d276da..8a1dbf2e 100644 --- a/packages/api/src/routers/hackathon/registration.ts +++ b/packages/api/src/routers/hackathon/registration.ts @@ -13,7 +13,8 @@ import { import { eq, and, inArray, isNull, ne } from "drizzle-orm"; import type { DrizzleDB } from "@query/db"; import { assertHackathonVisible } from "./visibility"; -import { evictParticipantCaches, syncCurrentParticipants } from "./admin"; +import { evictParticipantCaches } from "./admin"; +import { teamMemberCounts } from "../../services/derived-counts"; // Postgres unique_violation on hackathon_participant_hackathon_user_idx — a second // submission of the same form. Drizzle wraps every driver error in a @@ -317,9 +318,6 @@ export const hackathonRegistrationRouter = createTRPCRouter({ }); } - // An accepted withdrawal frees a seat for the next wave. - await syncCurrentParticipants(db, input.hackathonId); - evictParticipantCaches(ctx.cache, input.hackathonId, [ ctx.userId as string, ]); @@ -521,8 +519,8 @@ export const hackathonRegistrationRouter = createTRPCRouter({ // key two reads were in flight at once and each ran the query. return ctx.cache.getOrSet( `hackathon:registrations:${userId}`, - () => - (ctx.db as DrizzleDB).query.hackathonParticipants.findMany({ + async () => { + const rows = await (ctx.db as DrizzleDB).query.hackathonParticipants.findMany({ // What the portal reads from a registration: the sidebar, dashboard, // event pages and submit page. The rest of the application (phone, // demographics, emergency contact, essay) is the viewer's own data but @@ -555,7 +553,6 @@ export const hackathonRegistrationRouter = createTRPCRouter({ id: true, name: true, captainId: true, - currentMembers: true, maxMembers: true, }, }, @@ -563,7 +560,19 @@ export const hackathonRegistrationRouter = createTRPCRouter({ orderBy: (hackathonParticipants, { desc }) => [ desc(hackathonParticipants.registeredAt), ], - }), + }); + // Counted from the participant rows; the stored counter is retired. + const counts = await teamMemberCounts( + ctx.db as DrizzleDB, + rows.map((row) => row.team?.id), + ); + return rows.map((row) => ({ + ...row, + team: row.team + ? { ...row.team, currentMembers: counts.get(row.team.id) ?? 0 } + : row.team, + })); + }, 30, ); }), diff --git a/packages/api/src/routers/team/reads.ts b/packages/api/src/routers/team/reads.ts index ba2f1bf5..802eb80f 100644 --- a/packages/api/src/routers/team/reads.ts +++ b/packages/api/src/routers/team/reads.ts @@ -37,8 +37,10 @@ export const teamReadProcedures = { const cacheKey = `hackathon:${input.hackathonId}:teams`; - const fetchTeams = () => - (ctx.db as NonNullable).query.hackathonTeams.findMany({ + const fetchTeams = async () => { + const rows = await ( + ctx.db as NonNullable + ).query.hackathonTeams.findMany({ where: eq(hackathonTeams.hackathonId, input.hackathonId), with: { captain: { @@ -63,6 +65,13 @@ export const teamReadProcedures = { desc(hackathonTeams.createdAt), ], }); + // Every participant row of each team is loaded above, so that is the + // roster size; the stored counter is retired. + return rows.map((team) => ({ + ...team, + currentMembers: team.participants.length, + })); + }; const cached = ctx.cache.get>>(cacheKey); diff --git a/packages/api/src/routers/team/teams.ts b/packages/api/src/routers/team/teams.ts index 6dfa43ba..4dc7150f 100644 --- a/packages/api/src/routers/team/teams.ts +++ b/packages/api/src/routers/team/teams.ts @@ -9,11 +9,25 @@ import { hackathonParticipants, hackathonProjects, } from "@query/db"; -import { eq, and, isNull, lt, sql } from "drizzle-orm"; +import { eq, and, isNull, count } from "drizzle-orm"; import type { DrizzleDB } from "@query/db"; import { checkTeamEditWindow } from "./windows"; import { checkAdmitted, assertNoLiveSoloSubmission } from "./guards"; +// Roster size is the participant rows pointing at the team. hackathonTeams.currentMembers +// is a retired counter that drifted when a user or team deletion nulled or cascaded +// rows around it. Callers hold the team row lock, so the count cannot move under them. +const countMembers = async ( + tx: Pick, + teamId: string, +) => { + const [row] = await tx + .select({ n: count() }) + .from(hackathonParticipants) + .where(eq(hackathonParticipants.teamId, teamId)); + return row?.n ?? 0; +}; + export const teamProcedures = { createTeam: notHackathonBanned .input( @@ -70,7 +84,6 @@ export const teamProcedures = { name: input.name, description: input.description, captainId: ctx.userId as string, - currentMembers: 1, // The captain is the first member maxMembers: input.maxMembers, }) .returning(); @@ -176,7 +189,14 @@ export const teamProcedures = { }); } - if (team.currentMembers >= team.maxMembers) { + // Locked so two joiners cannot both count the last seat as free. + await tx + .select({ id: hackathonTeams.id }) + .from(hackathonTeams) + .where(eq(hackathonTeams.id, team.id)) + .for("update"); + + if ((await countMembers(tx, team.id)) >= team.maxMembers) { throw new TRPCError({ code: "FORBIDDEN", message: "This team is full.", @@ -202,27 +222,6 @@ export const teamProcedures = { }); } - // 5. Take the seat. The count above is a snapshot, so the increment carries - // the capacity test: whoever loses the last seat updates no row. - const seated = await tx - .update(hackathonTeams) - .set({ - currentMembers: sql`${hackathonTeams.currentMembers} + 1`, - }) - .where( - and( - eq(hackathonTeams.id, team.id), - lt(hackathonTeams.currentMembers, hackathonTeams.maxMembers), - ), - ); - - if (seated.rowCount === 0) { - throw new TRPCError({ - code: "FORBIDDEN", - message: "This team is full.", - }); - } - return { success: true }; }, ); @@ -292,7 +291,7 @@ export const teamProcedures = { // If captain is the only one left, they can "leave" which deletes the team if (team.captainId === (ctx.userId as string)) { - if (team.currentMembers <= 1) { + if ((await countMembers(tx, team.id)) <= 1) { const project = await tx.query.hackathonProjects.findFirst({ where: eq(hackathonProjects.teamId, team.id), }); @@ -332,9 +331,8 @@ export const teamProcedures = { }); } - // 1. Give up the seat. `teamId = team.id` makes a second Leave match no row - // instead of decrementing twice. - const left = await tx + // Give up the seat. A second Leave matches no row and is a no-op. + await tx .update(hackathonParticipants) .set({ teamId: null }) .where( @@ -344,21 +342,6 @@ export const teamProcedures = { ), ); - // Already out; idempotent for the caller, but must not decrement. - if (left.rowCount === 0) { - return { success: true }; - } - - // 2. Decrement in the database so a concurrent join is not overwritten. - // Drifting low to 1 makes the captain branch above delete the team and its - // project with members still on it. - await tx - .update(hackathonTeams) - .set({ - currentMembers: sql`greatest(${hackathonTeams.currentMembers} - 1, 0)`, - }) - .where(eq(hackathonTeams.id, team.id)); - return { success: true }; }, ); diff --git a/packages/api/src/services/derived-counts.ts b/packages/api/src/services/derived-counts.ts new file mode 100644 index 00000000..07aa4af5 --- /dev/null +++ b/packages/api/src/services/derived-counts.ts @@ -0,0 +1,126 @@ +// Counts for numbers that used to be stored counters. Each helper takes the ids +// a list is about to return and answers with one grouped query, never one query +// per row; an empty id list does not query at all. +// +// A grouped query rather than a correlated subquery in the select: in a +// relational query's `extras` drizzle prints the outer column unqualified, so +// the subquery's own `id` shadows it and every count comes back 0. +// +// The counters (events.currentCheckIns, hackathonTeams.currentMembers, +// hackathons.currentParticipants) drifted whenever an FK cascade removed rows +// without touching them; the rows cannot drift from themselves. + +import { and, count, inArray } from "drizzle-orm"; +import { + eventCheckIns, + hackathonParticipants, +} from "@query/db"; +import type { DrizzleDB } from "@query/db"; + +type Reader = Pick; + +const unique = (ids: (string | null | undefined)[]) => [ + ...new Set(ids.filter((id): id is string => !!id)), +]; + +export const checkInCounts = async ( + db: Reader, + eventIds: (string | null | undefined)[], +) => { + const ids = unique(eventIds); + const counts = new Map(); + if (ids.length === 0) return counts; + const rows = await db + .select({ id: eventCheckIns.eventId, n: count() }) + .from(eventCheckIns) + .where(inArray(eventCheckIns.eventId, ids)) + .groupBy(eventCheckIns.eventId); + for (const row of rows) counts.set(row.id, Number(row.n)); + return counts; +}; + +export const teamMemberCounts = async ( + db: Reader, + teamIds: (string | null | undefined)[], +) => { + const ids = unique(teamIds); + const counts = new Map(); + if (ids.length === 0) return counts; + const rows = await db + .select({ id: hackathonParticipants.teamId, n: count() }) + .from(hackathonParticipants) + .where(inArray(hackathonParticipants.teamId, ids)) + .groupBy(hackathonParticipants.teamId); + for (const row of rows) if (row.id) counts.set(row.id, Number(row.n)); + return counts; +}; + +// A seat belongs to somebody accepted: SEATED_STATUSES in +// routers/hackathon/admin/seats.ts, which this must agree with. +export const seatedCounts = async ( + db: Reader, + hackathonIds: (string | null | undefined)[], +) => { + const ids = unique(hackathonIds); + const counts = new Map(); + if (ids.length === 0) return counts; + const rows = await db + .select({ id: hackathonParticipants.hackathonId, n: count() }) + .from(hackathonParticipants) + .where( + and( + inArray(hackathonParticipants.hackathonId, ids), + inArray(hackathonParticipants.registrationStatus, [ + "approved", + "checked_in", + ]), + ), + ) + .groupBy(hackathonParticipants.hackathonId); + for (const row of rows) counts.set(row.id, Number(row.n)); + return counts; +}; + +// Merge helpers: same field names and types the stored columns had, so no +// reader needs to change. A parent with no child rows is 0. +export const withCheckInCounts = async ( + db: Reader, + rows: T[], +) => { + const counts = await checkInCounts( + db, + rows.map((row) => row.id), + ); + return rows.map((row) => ({ + ...row, + currentCheckIns: counts.get(row.id) ?? 0, + })); +}; + +export const withTeamMemberCounts = async ( + db: Reader, + rows: T[], +) => { + const counts = await teamMemberCounts( + db, + rows.map((row) => row.id), + ); + return rows.map((row) => ({ + ...row, + currentMembers: counts.get(row.id) ?? 0, + })); +}; + +export const withSeatedCounts = async ( + db: Reader, + rows: T[], +) => { + const counts = await seatedCounts( + db, + rows.map((row) => row.id), + ); + return rows.map((row) => ({ + ...row, + currentParticipants: counts.get(row.id) ?? 0, + })); +}; diff --git a/packages/db/scripts/seed-local.ts b/packages/db/scripts/seed-local.ts index 95693aa2..d3ac4819 100644 --- a/packages/db/scripts/seed-local.ts +++ b/packages/db/scripts/seed-local.ts @@ -254,11 +254,6 @@ async function main() { registrationDeadline: new Date(now.getTime() - 7 * DAY), hackingStartTime: new Date(now.getTime() - DAY), maxParticipants: 300, - // Seats are held by approved and checked-in rows only, as - // syncCurrentParticipants counts them. - currentParticipants: HACKERS.filter( - (h) => h.status === "approved" || h.status === "checked_in", - ).length, status: "in_progress", theme: "Data for good", tracks: ["Healthcare", "Sports", "Finance"], @@ -273,8 +268,8 @@ async function main() { const teams = await db .insert(S.hackathonTeams) .values([ - { hackathonId, name: "Gradient Descenders", captainId: "seed-hacker-1", currentMembers: 3 }, - { hackathonId, name: "Overfit Club", captainId: "seed-hacker-4", currentMembers: 2 }, + { hackathonId, name: "Gradient Descenders", captainId: "seed-hacker-1" }, + { hackathonId, name: "Overfit Club", captainId: "seed-hacker-4" }, ]) .returning({ id: S.hackathonTeams.id }); const teamA = one(teams, 0); diff --git a/packages/db/src/schemas/events.ts b/packages/db/src/schemas/events.ts index 9ee71dae..929372a0 100644 --- a/packages/db/src/schemas/events.ts +++ b/packages/db/src/schemas/events.ts @@ -47,6 +47,7 @@ export const events = pgTable( sql`case when bootcamp_term is null then 'club' else 'bootcamp' end`, ), maxCheckIns: integer("max_check_ins"), + // No longer read or written: derived from rows (see services/derived-counts.ts). Dropped in a later change. currentCheckIns: integer("current_check_ins").notNull().default(0), createdById: text("created_by_id") .notNull() diff --git a/packages/db/src/schemas/hackathons.ts b/packages/db/src/schemas/hackathons.ts index 5a355492..0663153d 100644 --- a/packages/db/src/schemas/hackathons.ts +++ b/packages/db/src/schemas/hackathons.ts @@ -35,6 +35,7 @@ export const hackathons = pgTable( registrationDeadline: timestamp("registration_deadline"), hackingStartTime: timestamp("hacking_start_time"), maxParticipants: integer("max_participants"), + // No longer read or written: derived from rows (see services/derived-counts.ts). Dropped in a later change. currentParticipants: integer("current_participants").notNull().default(0), // `announced` is the gap between hidden and open: the edition is public and // collects interest, but takes no registrations and is NOT the edition @@ -91,6 +92,7 @@ export const hackathonTeams = pgTable( name: text("name").notNull(), description: text("description"), maxMembers: integer("max_members").notNull().default(4), + // No longer read or written: derived from rows (see services/derived-counts.ts). Dropped in a later change. currentMembers: integer("current_members").notNull().default(0), captainId: text("captain_id") .notNull() @@ -197,8 +199,8 @@ export const hackathonParticipants = pgTable( // to choose from. A third only made every insert write another entry. index("participant_user_id_idx").on(table.userId), index("participant_team_id_idx").on(table.teamId), - // syncCurrentParticipants filters on the first two columns after every - // approve and check-in. registered_at is here for ordering, not filtering: + // The seat counts (assertSeats, seatedCounts) filter on the first two + // columns. registered_at is here for ordering, not filtering: // acceptWave takes the oldest pending and the attendee list pages // newest-first, so without it both read every matching row and sort. index("participant_hackathon_status_idx").on( @@ -451,9 +453,9 @@ export const hackathonEventAttendees = pgTable( checkedInById: text("checked_in_by_id"), }, (table) => [ - index("event_attendee_event_id_idx").on(table.eventId), index("event_attendee_participant_id_idx").on(table.participantId), - // Prevent duplicate check-ins + // Prevent duplicate check-ins. Leads with event_id, so it also serves the + // by-event reads. uniqueIndex("event_attendee_event_participant_idx").on(table.eventId, table.participantId), // Named explicitly: the generated name is 67 chars, Postgres truncates to 63, // and drizzle then sees a diff on every push and re-creates it forever. diff --git a/packages/db/src/schemas/initiatives.ts b/packages/db/src/schemas/initiatives.ts index 5ea20dd7..eb36f3f1 100644 --- a/packages/db/src/schemas/initiatives.ts +++ b/packages/db/src/schemas/initiatives.ts @@ -145,8 +145,8 @@ export const initiativeApplications = pgTable( }), }, (table) => [ - index("initiative_application_initiative_idx").on(table.initiativeId), index("initiative_application_user_idx").on(table.userId), + // Leads with initiative_id, so it also serves the by-initiative reads. uniqueIndex("initiative_application_initiative_user_idx").on( table.initiativeId, table.userId, diff --git a/packages/db/src/schemas/judge.ts b/packages/db/src/schemas/judge.ts index 0555369a..0f2fe96a 100644 --- a/packages/db/src/schemas/judge.ts +++ b/packages/db/src/schemas/judge.ts @@ -77,10 +77,10 @@ export const judgeAssignments = pgTable( track: text("track"), }, (table) => [ - index("assignment_judge_id_idx").on(table.judgeId), index("assignment_hackathon_id_idx").on(table.hackathonId), // judge.register enforces one assignment per judge per hackathon with a - // read before the insert. + // read before the insert. Leads with judge_id, so it also serves the + // by-judge reads. uniqueIndex("assignment_judge_hackathon_idx").on( table.judgeId, table.hackathonId, @@ -172,9 +172,9 @@ export const judgeVotes = pgTable( updatedAt: timestamp("updated_at").defaultNow().notNull(), }, (table) => [ - index("vote_judge_id_idx").on(table.judgeId), index("vote_project_id_idx").on(table.projectId), - // Unique constraint: one vote per judge per project (enforced at DB level) + // One vote per judge per project. Leads with judge_id, so it also serves + // the by-judge reads. uniqueIndex("vote_unique_idx").on(table.judgeId, table.projectId), ], ); @@ -217,9 +217,9 @@ export const hackathonResults = pgTable( computedAt: timestamp("computed_at").defaultNow().notNull(), }, (table) => [ - index("result_hackathon_idx").on(table.hackathonId), // One placing per project per prize. Recomputing upserts onto this rather - // than appending a second, contradictory ordering. + // than appending a second, contradictory ordering. Leads with hackathon_id, + // so it also serves the by-edition reads. uniqueIndex("result_unique_placing").on( table.hackathonId, table.projectId, @@ -256,7 +256,7 @@ export const judgeQueue = pgTable( arrivedAt: timestamp("arrived_at"), }, (table) => [ - index("queue_judge_id_idx").on(table.judgeId), + // No judge_id index: queue_todo_idx leads with it. index("queue_hackathon_id_idx").on(table.hackathonId), // Critical for "next table" logic index("queue_todo_idx").on( diff --git a/packages/db/src/schemas/judging.ts b/packages/db/src/schemas/judging.ts index 557dc445..4a8f8b45 100644 --- a/packages/db/src/schemas/judging.ts +++ b/packages/db/src/schemas/judging.ts @@ -876,7 +876,7 @@ export const judgingPlacements = pgTable( computedAt: timestamp("computed_at").defaultNow().notNull(), }, (table) => [ - index("judging_placement_hackathon_idx").on(table.hackathonId), + // Leads with hackathon_id, so it also serves the by-edition reads. uniqueIndex("judging_placement_unique_idx").on( table.hackathonId, table.scope, diff --git a/packages/db/src/schemas/members.ts b/packages/db/src/schemas/members.ts index e94f9852..5d813a43 100644 --- a/packages/db/src/schemas/members.ts +++ b/packages/db/src/schemas/members.ts @@ -31,7 +31,7 @@ export const userProfiles = pgTable( createdAt: timestamp("created_at").defaultNow().notNull(), updatedAt: timestamp("updated_at").defaultNow().notNull(), }, - (table) => [index("profile_user_id_idx").on(table.userId)], + // No user_id index: the unique constraint on it is one. ); export const members = pgTable( diff --git a/sites/mainweb/app/events/page.tsx b/sites/mainweb/app/events/page.tsx index 40dd7d52..e4474b2b 100644 --- a/sites/mainweb/app/events/page.tsx +++ b/sites/mainweb/app/events/page.tsx @@ -5,6 +5,7 @@ import Section from "@/components/Section"; import { db, events } from "@query/db"; import { and, eq, gte, lt } from "drizzle-orm"; import { startOfEasternDay } from "@query/api/eastern-time"; +import { withCheckInCounts } from "@query/api/derived-counts"; import Link from "next/link"; import { eventDateParts, eventWhenWhere } from "@/lib/public-events"; @@ -39,7 +40,6 @@ const listedColumns = { location: true, eventDate: true, maxCheckIns: true, - currentCheckIns: true, } as const; // From the start of today, so an event running this afternoon counts as @@ -54,7 +54,8 @@ async function loadUpcoming() { // Same as /projects: a database that is asleep or unreachable renders an // empty list instead of failing the build or the revalidation. try { - return await db.query.events.findMany({ + // Counted from the rows: events.current_check_ins is a retired counter. + const rows = await db.query.events.findMany({ where: and( eq(events.kind, "club"), gte(events.eventDate, startOfToday()), @@ -63,6 +64,7 @@ async function loadUpcoming() { limit: 20, columns: listedColumns, }); + return await withCheckInCounts(db, rows); } catch (error) { console.error("Failed to load upcoming events", error); return []; @@ -78,7 +80,7 @@ async function loadPast() { if (!db) return []; try { - return await db.query.events.findMany({ + const rows = await db.query.events.findMany({ where: and( eq(events.kind, "club"), lt(events.eventDate, startOfToday()), @@ -87,6 +89,7 @@ async function loadPast() { limit: 20, columns: listedColumns, }); + return await withCheckInCounts(db, rows); } catch (error) { console.error("Failed to load past events", error); return []; diff --git a/sites/mainweb/components/admin/hackathons/CreateHackathonForm.tsx b/sites/mainweb/components/admin/hackathons/CreateHackathonForm.tsx index de8d0308..7c837032 100644 --- a/sites/mainweb/components/admin/hackathons/CreateHackathonForm.tsx +++ b/sites/mainweb/components/admin/hackathons/CreateHackathonForm.tsx @@ -256,7 +256,7 @@ export function CreateHackathonForm({ className={input} /> {/* The cap counts SEATS: approved and checked-in people only - (syncCurrentParticipants). Pending applications do not use one. */} + (assertSeats). Pending applications do not use one. */}

Caps accepted people, not applications. Pending applications do not count, so you can take more applications than seats and diff --git a/sites/mainweb/components/admin/hackathons/EditHackathonForm.tsx b/sites/mainweb/components/admin/hackathons/EditHackathonForm.tsx index 3fad169c..adbca091 100644 --- a/sites/mainweb/components/admin/hackathons/EditHackathonForm.tsx +++ b/sites/mainweb/components/admin/hackathons/EditHackathonForm.tsx @@ -356,7 +356,7 @@ export function EditHackathonForm({ className={input} /> {/* The cap counts SEATS: approved and checked-in people only - (syncCurrentParticipants). Pending applications do not use one. */} + (assertSeats). Pending applications do not use one. */}

Caps accepted people, not applications. Pending applications do not count, so you can take more applications than seats and diff --git a/turbo.json b/turbo.json index 492eef8f..43587fac 100644 --- a/turbo.json +++ b/turbo.json @@ -64,6 +64,7 @@ "npm_lifecycle_event", "PORT", "DATABASE_URL", + "TEST_DATABASE_URL", "NEXTAUTH_SECRET", "AUTH_SECRET", "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY",