Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions docs/features/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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.

Expand All @@ -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).

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/features/hackathons.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down Expand Up @@ -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`). |
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/.internal-tests/bootcamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down
295 changes: 295 additions & 0 deletions packages/api/src/.internal-tests/derived-counts.db.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading
Loading