diff --git a/.env.example b/.env.example index 335394a..7afa2d3 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,7 @@ # ----- PostgreSQL (Docker) ----- POSTGRES_USER=forum -POSTGRES_PASSWORD=YOUR_DB_PASSWORD_HERE +POSTGRES_PASSWORD=forum_password POSTGRES_DB=the_forum POSTGRES_PORT=5434 @@ -40,3 +40,6 @@ NEXT_PUBLIC_CAMPUS_MAP_STYLE=YOUR_CAMPUS_MAP_STYLE_HERE # Publish active, exact-time, high-confidence events without a location (default: false). # PUBLISH_LOCATIONLESS_EVENTS=true +# Demo seed scripts refuse remote databases unless explicitly enabled. +# ALLOW_REMOTE_SEED=1 + diff --git a/AGENTS.md b/AGENTS.md index 9b0d254..9e2f6ea 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,4 +1,4 @@ -# The Forum — Codex Rules +# The Forum — Agent Rules ## Project Overview @@ -9,6 +9,20 @@ Turborepo monorepo with: --- +## Workflow + +- Before starting work, read the relevant Linear issue and project guidance when Linear access is available. Use the issue ID and scope to guide the branch name and implementation. If Linear is unavailable, say so and work from the provided context. + +- Start branches for changes from the latest `staging`. `main` is the production branch. +- Name branches `__[-optional-descriptors]`, using the date the branch is created. Example: `alex_10-27_TIG-123-update-feed`. Linear's generated Git branch name is also acceptable. For work without a Linear issue, use a descriptive name in place of the ticket ID, such as `alex_10-27_agent-workflow`. +- Keep `AGENTS.md` and `CLAUDE.md` identical. They are separate regular files for portability across Windows checkouts; whenever either file changes, repeat the same changes in the other file in the same commit. + +### Linear description edits + +When proposing a change to a Linear issue description, preserve the full original description in the issue itself. Append a horizontal separator (`---`), then a clearly labeled `Proposed description` section containing the full new description. Keep the original intact so others can review the proposal before accepting the change; do not replace the original with the proposal before acceptance. + +--- + ## Package Manager **Always use `bun`.** Never use `npm`, `yarn`, or `pnpm`. @@ -45,9 +59,9 @@ Never use `process.env.*` directly in Next.js application code.** - **Client-side** (must start with `NEXT_PUBLIC_`) → goes in the `client:` block - Add a matching entry in `runtimeEnv:` pointing to `process.env.VAR_NAME` -2. Add it to `apps/web/.env.local.example` and to the root `.env.example` +2. Add it to the root `.env.example` -3. Add it to your local `apps/web/.env.local` +3. Add it to your local root `.env`. Keep local configuration in this single file; workspace scripts explicitly load it. ### Usage in code diff --git a/CLAUDE.md b/CLAUDE.md index 59b4a34..9e2f6ea 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,4 +1,4 @@ -# The Forum — Claude Code Rules +# The Forum — Agent Rules ## Project Overview @@ -9,6 +9,20 @@ Turborepo monorepo with: --- +## Workflow + +- Before starting work, read the relevant Linear issue and project guidance when Linear access is available. Use the issue ID and scope to guide the branch name and implementation. If Linear is unavailable, say so and work from the provided context. + +- Start branches for changes from the latest `staging`. `main` is the production branch. +- Name branches `__[-optional-descriptors]`, using the date the branch is created. Example: `alex_10-27_TIG-123-update-feed`. Linear's generated Git branch name is also acceptable. For work without a Linear issue, use a descriptive name in place of the ticket ID, such as `alex_10-27_agent-workflow`. +- Keep `AGENTS.md` and `CLAUDE.md` identical. They are separate regular files for portability across Windows checkouts; whenever either file changes, repeat the same changes in the other file in the same commit. + +### Linear description edits + +When proposing a change to a Linear issue description, preserve the full original description in the issue itself. Append a horizontal separator (`---`), then a clearly labeled `Proposed description` section containing the full new description. Keep the original intact so others can review the proposal before accepting the change; do not replace the original with the proposal before acceptance. + +--- + ## Package Manager **Always use `bun`.** Never use `npm`, `yarn`, or `pnpm`. @@ -45,9 +59,9 @@ Never use `process.env.*` directly in Next.js application code.** - **Client-side** (must start with `NEXT_PUBLIC_`) → goes in the `client:` block - Add a matching entry in `runtimeEnv:` pointing to `process.env.VAR_NAME` -2. Add it to `apps/web/.env.local.example` and to the root `.env.example` +2. Add it to the root `.env.example` -3. Add it to your local `apps/web/.env.local` +3. Add it to your local root `.env`. Keep local configuration in this single file; workspace scripts explicitly load it. ### Usage in code diff --git a/README.md b/README.md index 4d0e67a..b1c021e 100644 --- a/README.md +++ b/README.md @@ -47,18 +47,26 @@ bun install # installs every workspace package + sets up Husky pre-commit hook ### 2. Environment variables -Copy the example files: +Copy the single root example file: ```bash -cp .env.example .env # root — used by docker-compose -cp apps/web/.env.local.example apps/web/.env.local # Next.js app -cp apps/database/.env.example apps/database/.env # drizzle-kit CLI +cp .env.example .env ``` -Then fill in `apps/web/.env.local`. Env vars are validated at startup by +Then fill in the root `.env`. Docker, the web app, and database commands share this +file. Env vars are validated at startup by [`apps/web/src/env.ts`](apps/web/src/env.ts) — the app won't boot if a required one is missing, and that file is the source of truth for what's required. +Workspace package scripts explicitly load the root `.env`, so commands work from +the repo root or their app directory. Deployment can continue supplying variables +through the process environment without a local file. Existing checkouts can keep +using `apps/web/.env.local` and `apps/database/.env` as legacy fallbacks. Exported +environment variables take priority over the root `.env`, which takes priority +over duplicate legacy values. To migrate to one file, move your app-specific +values into the root `.env`, then remove the legacy files. +Restart the dev server after editing the root `.env`. + > **Can't obtain a value yourself? Ask Ibraheem.** He is the contact for all > credentials that aren't self-serve (Mapbox tokens, AWS, etc.). @@ -77,19 +85,34 @@ Clicking "Log in" goes to `/api/auth/cas/login`, which redirects to `fed.princeton.edu/cas`; CAS sends you back to `/api/auth/cas/callback`, where the ticket is validated server-side and your user row is created from your NetID. -### 3. Start the database +### 3. Start the database and app Make sure **Docker Desktop is running**, then from the repo root: ```bash -bun run db:up # starts Postgres 17 in Docker (container: the-forum-db, host port 5434) -bun run db:push # push the Drizzle schema into the fresh database +bun run dev ``` +This starts Postgres 17 in Docker (container: `-db`, host port **5434**), +waits until it is healthy, pushes the Drizzle schema, and runs the `apps/web` dev +script from the repo root. Open . + +On a fresh database volume, Docker enables the `vector` extension before reporting +Postgres ready. You can also run `bun run db:up` followed by `bun run db:push` +separately. Before comparing schemas, `db:push` also ensures the `vector` extension +exists in its target database, including existing volumes. This requires a database +role allowed to enable extensions. Drizzle compares the live schema on each run +and applies only differences; it does not recreate unchanged tables. +In Turbo's terminal UI, select the database task and press `i` to +interact with Drizzle's confirmation menu, then use the arrow keys and Enter. + +Use the same command on every startup. It runs `db:push` to keep the local schema +up to date; Docker preserves existing data between restarts. + Sanity checks: ```bash -docker compose ps # the-forum-db should show "Up (healthy)" +docker compose ps # the postgres service should show "Up (healthy)" bun run db:logs # tail the Postgres logs if something looks wrong ``` @@ -102,21 +125,19 @@ Optionally fill the database with realistic demo data: bun run db:seed ``` -### 4. Run the app +To start only the web app, use `bun run --cwd apps/web dev` from the repo root. -```bash -cd apps/web && bun run dev -``` - -Open . You're set up. - -To run the dev server through Turborepo from the repo root: `bun run dev`. +Local development uses Turbopack. Its persistent development cache is disabled to +avoid a recurring hot-reload panic (`Next.js package not found`) observed with +Bun workspace dependencies on Windows. Compilation is still reused in memory +during a running session; restarting the dev server starts with a cold cache. --- ## Everyday commands ```bash +bun run dev # start Postgres, wait for health, push schema, and start apps/web bun run check # Biome lint + format with auto-fix (run before pushing) bun run format # format only bun run build # build all packages @@ -136,7 +157,7 @@ if your commit fails, read the Biome output, fix, and re-commit. ### Conventions - **Env vars in `apps/web`:** always `import { env } from "~/env"` — never `process.env.*` directly. - New vars get added to `apps/web/src/env.ts` *and* the `.env.example` files. + New vars get added to `apps/web/src/env.ts` *and* the root `.env.example`. - **UI components:** use [shadcn/ui](https://ui.shadcn.com). Add new ones from `apps/web`: `bunx shadcn@latest add `. - **Linting:** Biome only (no ESLint/Prettier). @@ -158,7 +179,7 @@ It ingests MyPrincetonU's official events feed and residential/FreeFood listserv resolves the hosting organization, extracts time and place, and exposes a revisioned change feed. ```bash -# apps/database/.env +# Root .env INBOX_ENGINE_URL=https://inbox-engine.tigerapps.org INBOX_ENGINE_TOKEN=… # ask a TigerApps admin @@ -274,24 +295,59 @@ To change runtime configuration, update the SSM parameter and redeploy (or re-ru `main` is protected — you cannot push to it directly, and pull requests into `main` are only accepted from `staging`. -1. Branch off `main`: `git checkout -b feat/my-feature origin/main` +1. Branch off the latest `staging`: `git fetch origin`, then + `git checkout -b alex_11-07_TIG-123-update-feed origin/staging`. 2. Open a PR **into `staging`** and merge it there 3. When `staging` is ready to ship, open a PR from `staging` into `main` +Name branches `__[-optional-descriptors]`, using +the branch creation date. Linear's generated Git branch name is also acceptable. +Without a Linear issue, replace the ticket ID with a description, for example +`alex_11-07_local-startup`. Read the relevant Linear issue and project guidance +before starting when access is available. + +Agent instructions live in `AGENTS.md` and `CLAUDE.md`; keep these files identical +when updating either one. + --- ## Troubleshooting **The app crashes on startup with "Invalid environment variables"** -A required var in `apps/web/.env.local` is missing or malformed — compare against -`apps/web/.env.local.example` and the table above. +A required var in the root `.env` is missing or malformed — compare against +the root `.env.example` and the table above. **`ECONNREFUSED` / `DATABASE_URL` errors** The database container isn't running (`bun run db:up`), or your `DATABASE_URL` uses the wrong port — the Docker database listens on **5434**, not 5432. **Port 5434 already in use** -Change `POSTGRES_PORT` in the root `.env` and update `DATABASE_URL` everywhere to match. +Change `POSTGRES_PORT` in the root `.env` and update `DATABASE_URL` in that file to match. + +**Multiple Git worktrees** + +Compose uses the worktree directory name as its default project name. Each project +gets its own database container (`forum-1-db`, `forum-2-db`, etc.) and data volume. +Use a different `POSTGRES_PORT` in each worktree's root `.env` when running them +simultaneously, and match that port in its `DATABASE_URL` (for example, port 5435 +in `forum-2`). The database name inside each container can remain `the_forum`. +You can override the project name with `COMPOSE_PROJECT_NAME` or Compose's `-p` +flag. Keep the project name stable to continue using the same data volume; +renaming a container does not move it or its data into another project. + +**`type "vector" does not exist` on an existing database** + +First-time Docker initialization scripts only run for an empty data volume. +`bun run db:push` now enables the extension for existing databases too. If the +configured database role lacks permission, have an administrator enable it: + +```bash +docker compose exec postgres psql -U forum -d the_forum -c "CREATE EXTENSION IF NOT EXISTS vector;" +``` + +Replace the user and database if you customized `POSTGRES_USER` or `POSTGRES_DB`, +then retry `bun run db:push`. For a hosted database, enable the extension in the +database referenced by `DATABASE_URL` instead. **Husky hooks not running** Re-run `bun install` from the repo root (the `prepare` script reinstalls hooks). diff --git a/apps/database/.env.example b/apps/database/.env.example deleted file mode 100644 index b3c596a..0000000 --- a/apps/database/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -# Copy to .env for local development (used by drizzle-kit) -DATABASE_URL=postgresql://forum:forum_password@localhost:5434/the_forum - -# InboxEngine sync (`bun run db:sync-engine`) -# INBOX_ENGINE_URL=https://inbox-engine.tigerapps.org -# INBOX_ENGINE_TOKEN= diff --git a/apps/database/drizzle/0009_large_electro.sql b/apps/database/drizzle/0009_large_electro.sql new file mode 100644 index 0000000..01c8695 --- /dev/null +++ b/apps/database/drizzle/0009_large_electro.sql @@ -0,0 +1,6 @@ +CREATE INDEX "organizations_name_id_idx" ON "organizations" USING btree ("name","id");--> statement-breakpoint +CREATE INDEX "organizations_members_id_idx" ON "organizations" USING btree (coalesce("member_count", '-2147483649'::bigint) desc nulls last,"id" DESC NULLS LAST);--> statement-breakpoint +CREATE INDEX "organizations_created_at_id_idx" ON "organizations" USING btree ("created_at" DESC NULLS LAST,"id" DESC NULLS LAST);--> statement-breakpoint +CREATE INDEX "organizations_category_name_id_idx" ON "organizations" USING btree ("category","name","id");--> statement-breakpoint +CREATE INDEX "organizations_category_members_id_idx" ON "organizations" USING btree ("category",coalesce("member_count", '-2147483649'::bigint) desc nulls last,"id" DESC NULLS LAST);--> statement-breakpoint +CREATE INDEX "organizations_category_created_at_id_idx" ON "organizations" USING btree ("category","created_at" DESC NULLS LAST,"id" DESC NULLS LAST); \ No newline at end of file diff --git a/apps/database/drizzle/meta/0009_snapshot.json b/apps/database/drizzle/meta/0009_snapshot.json new file mode 100644 index 0000000..7a9115b --- /dev/null +++ b/apps/database/drizzle/meta/0009_snapshot.json @@ -0,0 +1,2276 @@ +{ + "id": "c6695fcb-c4c9-4f5d-b84b-d580b6666f4c", + "prevId": "7ee6f932-228f-429c-8e1e-0db3acdc1a0a", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.campus_locations": { + "name": "campus_locations", + "schema": "", + "columns": { + "area": { + "name": "area", + "type": "campus_region", + "typeSchema": "public", + "primaryKey": false, + "notNull": false + }, + "id": { + "name": "id", + "type": "varchar(100)", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "latitude": { + "name": "latitude", + "type": "double precision", + "primaryKey": false, + "notNull": true + }, + "longitude": { + "name": "longitude", + "type": "double precision", + "primaryKey": false, + "notNull": true + }, + "category": { + "name": "category", + "type": "location_category", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.event_tag_embeddings": { + "name": "event_tag_embeddings", + "schema": "", + "columns": { + "tag_name": { + "name": "tag_name", + "type": "event_tag", + "typeSchema": "public", + "primaryKey": true, + "notNull": true + }, + "embedding": { + "name": "embedding", + "type": "vector(1536)", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.event_tags": { + "name": "event_tags", + "schema": "", + "columns": { + "event_id": { + "name": "event_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "tag": { + "name": "tag", + "type": "event_tag", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "event_tags_tag_idx": { + "name": "event_tags_tag_idx", + "columns": [ + { + "expression": "tag", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "event_tags_event_id_events_id_fk": { + "name": "event_tags_event_id_events_id_fk", + "tableFrom": "event_tags", + "tableTo": "events", + "columnsFrom": [ + "event_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "event_tags_event_id_tag_pk": { + "name": "event_tags_event_id_tag_pk", + "columns": [ + "event_id", + "tag" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.events": { + "name": "events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "title": { + "name": "title", + "type": "varchar(200)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "datetime": { + "name": "datetime", + "type": "timestamp", + "primaryKey": false, + "notNull": true + }, + "end_datetime": { + "name": "end_datetime", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "location_id": { + "name": "location_id", + "type": "varchar(100)", + "primaryKey": false, + "notNull": true + }, + "org_id": { + "name": "org_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "creator_id": { + "name": "creator_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "flyer_url": { + "name": "flyer_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cover_preset": { + "name": "cover_preset", + "type": "varchar(50)", + "primaryKey": false, + "notNull": false + }, + "external_link": { + "name": "external_link", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_public": { + "name": "is_public", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "status": { + "name": "status", + "type": "event_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'published'" + }, + "source": { + "name": "source", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'manual'" + }, + "source_message_id": { + "name": "source_message_id", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "source_updated_at": { + "name": "source_updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "source_url": { + "name": "source_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "location_detail": { + "name": "location_detail", + "type": "varchar(200)", + "primaryKey": false, + "notNull": false + }, + "location_text": { + "name": "location_text", + "type": "varchar(300)", + "primaryKey": false, + "notNull": false + }, + "location_resolution": { + "name": "location_resolution", + "type": "location_resolution", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "announcement_count": { + "name": "announcement_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "announced_at": { + "name": "announced_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "events_published_datetime_idx": { + "name": "events_published_datetime_idx", + "columns": [ + { + "expression": "datetime", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"events\".\"status\" = 'published'", + "concurrently": false, + "method": "btree", + "with": {} + }, + "events_org_id_idx": { + "name": "events_org_id_idx", + "columns": [ + { + "expression": "org_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "events_creator_id_idx": { + "name": "events_creator_id_idx", + "columns": [ + { + "expression": "creator_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "events_location_id_idx": { + "name": "events_location_id_idx", + "columns": [ + { + "expression": "location_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "events_location_id_campus_locations_id_fk": { + "name": "events_location_id_campus_locations_id_fk", + "tableFrom": "events", + "tableTo": "campus_locations", + "columnsFrom": [ + "location_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "events_org_id_organizations_id_fk": { + "name": "events_org_id_organizations_id_fk", + "tableFrom": "events", + "tableTo": "organizations", + "columnsFrom": [ + "org_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "events_creator_id_users_id_fk": { + "name": "events_creator_id_users_id_fk", + "tableFrom": "events", + "tableTo": "users", + "columnsFrom": [ + "creator_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "events_source_message_id_unique": { + "name": "events_source_message_id_unique", + "nullsNotDistinct": false, + "columns": [ + "source_message_id" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.friendships": { + "name": "friendships", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "friend_id": { + "name": "friend_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "friendship_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "friendships_friend_id_status_idx": { + "name": "friendships_friend_id_status_idx", + "columns": [ + { + "expression": "friend_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "friendships_user_id_users_id_fk": { + "name": "friendships_user_id_users_id_fk", + "tableFrom": "friendships", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "friendships_friend_id_users_id_fk": { + "name": "friendships_friend_id_users_id_fk", + "tableFrom": "friendships", + "tableTo": "users", + "columnsFrom": [ + "friend_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "friendships_user_id_friend_id_pk": { + "name": "friendships_user_id_friend_id_pk", + "columns": [ + "user_id", + "friend_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.interactions": { + "name": "interactions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "item_id": { + "name": "item_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "item_type": { + "name": "item_type", + "type": "item_type", + "typeSchema": "public", + "primaryKey": false, + "notNull": true, + "default": "'event'" + }, + "interaction_type": { + "name": "interaction_type", + "type": "interaction_type", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "interaction_value": { + "name": "interaction_value", + "type": "double precision", + "primaryKey": false, + "notNull": true + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "interactions_user_id_created_at_idx": { + "name": "interactions_user_id_created_at_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "interactions_item_id_type_idx": { + "name": "interactions_item_id_type_idx", + "columns": [ + { + "expression": "item_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "interaction_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "interactions_user_id_users_id_fk": { + "name": "interactions_user_id_users_id_fk", + "tableFrom": "interactions", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.listserv_configs": { + "name": "listserv_configs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "address": { + "name": "address", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "label": { + "name": "label", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "org_id": { + "name": "org_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "gmail_label": { + "name": "gmail_label", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "enabled": { + "name": "enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "listserv_configs_org_id_organizations_id_fk": { + "name": "listserv_configs_org_id_organizations_id_fk", + "tableFrom": "listserv_configs", + "tableTo": "organizations", + "columnsFrom": [ + "org_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "listserv_configs_address_unique": { + "name": "listserv_configs_address_unique", + "nullsNotDistinct": false, + "columns": [ + "address" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.listserv_emails": { + "name": "listserv_emails", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "message_id": { + "name": "message_id", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "listserv": { + "name": "listserv", + "type": "varchar(100)", + "primaryKey": false, + "notNull": true + }, + "subject": { + "name": "subject", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "author_name": { + "name": "author_name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "author_email": { + "name": "author_email", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "date": { + "name": "date", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "body_text": { + "name": "body_text", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "body_html": { + "name": "body_html", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_hoagiemail": { + "name": "is_hoagiemail", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "hoagiemail_sender_name": { + "name": "hoagiemail_sender_name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "hoagiemail_sender_email": { + "name": "hoagiemail_sender_email", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "links": { + "name": "links", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "images": { + "name": "images", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "attachments": { + "name": "attachments", + "type": "jsonb", + "primaryKey": false, + "notNull": false, + "default": "'[]'::jsonb" + }, + "listserv_url": { + "name": "listserv_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "listserv_emails_message_id_unique": { + "name": "listserv_emails_message_id_unique", + "nullsNotDistinct": false, + "columns": [ + "message_id" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.location_aliases": { + "name": "location_aliases", + "schema": "", + "columns": { + "alias_normalized": { + "name": "alias_normalized", + "type": "varchar(250)", + "primaryKey": true, + "notNull": true + }, + "location_id": { + "name": "location_id", + "type": "varchar(100)", + "primaryKey": false, + "notNull": true + }, + "created_by": { + "name": "created_by", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "location_aliases_location_id_campus_locations_id_fk": { + "name": "location_aliases_location_id_campus_locations_id_fk", + "tableFrom": "location_aliases", + "tableTo": "campus_locations", + "columnsFrom": [ + "location_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "location_aliases_created_by_users_id_fk": { + "name": "location_aliases_created_by_users_id_fk", + "tableFrom": "location_aliases", + "tableTo": "users", + "columnsFrom": [ + "created_by" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.notifications": { + "name": "notifications", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "notification_type", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "payload": { + "name": "payload", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "read": { + "name": "read", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "notifications_user_id_created_at_idx": { + "name": "notifications_user_id_created_at_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "notifications_user_id_users_id_fk": { + "name": "notifications_user_id_users_id_fk", + "tableFrom": "notifications", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.org_blocks": { + "name": "org_blocks", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "org_id": { + "name": "org_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "org_blocks_user_id_idx": { + "name": "org_blocks_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "org_blocks_user_id_users_id_fk": { + "name": "org_blocks_user_id_users_id_fk", + "tableFrom": "org_blocks", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "org_blocks_org_id_organizations_id_fk": { + "name": "org_blocks_org_id_organizations_id_fk", + "tableFrom": "org_blocks", + "tableTo": "organizations", + "columnsFrom": [ + "org_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "org_blocks_user_id_org_id_pk": { + "name": "org_blocks_user_id_org_id_pk", + "columns": [ + "user_id", + "org_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.org_followers": { + "name": "org_followers", + "schema": "", + "columns": { + "org_id": { + "name": "org_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "org_followers_user_id_idx": { + "name": "org_followers_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "org_followers_org_id_organizations_id_fk": { + "name": "org_followers_org_id_organizations_id_fk", + "tableFrom": "org_followers", + "tableTo": "organizations", + "columnsFrom": [ + "org_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "org_followers_user_id_users_id_fk": { + "name": "org_followers_user_id_users_id_fk", + "tableFrom": "org_followers", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "org_followers_org_id_user_id_pk": { + "name": "org_followers_org_id_user_id_pk", + "columns": [ + "org_id", + "user_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.org_members": { + "name": "org_members", + "schema": "", + "columns": { + "org_id": { + "name": "org_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "role": { + "name": "role", + "type": "org_role", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "org_members_user_id_idx": { + "name": "org_members_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "org_members_org_id_organizations_id_fk": { + "name": "org_members_org_id_organizations_id_fk", + "tableFrom": "org_members", + "tableTo": "organizations", + "columnsFrom": [ + "org_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "org_members_user_id_users_id_fk": { + "name": "org_members_user_id_users_id_fk", + "tableFrom": "org_members", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "org_members_org_id_user_id_pk": { + "name": "org_members_org_id_user_id_pk", + "columns": [ + "org_id", + "user_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.organizations": { + "name": "organizations", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "logo_url": { + "name": "logo_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "category": { + "name": "category", + "type": "org_category", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "creator_id": { + "name": "creator_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "source": { + "name": "source", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true, + "default": "'manual'" + }, + "external_id": { + "name": "external_id", + "type": "varchar(64)", + "primaryKey": false, + "notNull": false + }, + "acronym": { + "name": "acronym", + "type": "varchar(40)", + "primaryKey": false, + "notNull": false + }, + "tagline": { + "name": "tagline", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "group_type": { + "name": "group_type", + "type": "varchar(120)", + "primaryKey": false, + "notNull": false + }, + "group_url": { + "name": "group_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "website": { + "name": "website", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "contact_email": { + "name": "contact_email", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "socials": { + "name": "socials", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "member_count": { + "name": "member_count", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "synced_at": { + "name": "synced_at", + "type": "timestamp", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "organizations_name_id_idx": { + "name": "organizations_name_id_idx", + "columns": [ + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "organizations_members_id_idx": { + "name": "organizations_members_id_idx", + "columns": [ + { + "expression": "coalesce(\"member_count\", '-2147483649'::bigint) desc nulls last", + "asc": true, + "isExpression": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "organizations_created_at_id_idx": { + "name": "organizations_created_at_id_idx", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "organizations_category_name_id_idx": { + "name": "organizations_category_name_id_idx", + "columns": [ + { + "expression": "category", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "organizations_category_members_id_idx": { + "name": "organizations_category_members_id_idx", + "columns": [ + { + "expression": "category", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "coalesce(\"member_count\", '-2147483649'::bigint) desc nulls last", + "asc": true, + "isExpression": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "organizations_category_created_at_id_idx": { + "name": "organizations_category_created_at_id_idx", + "columns": [ + { + "expression": "category", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": false, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "organizations_creator_id_users_id_fk": { + "name": "organizations_creator_id_users_id_fk", + "tableFrom": "organizations", + "tableTo": "users", + "columnsFrom": [ + "creator_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "organizations_name_unique": { + "name": "organizations_name_unique", + "nullsNotDistinct": false, + "columns": [ + "name" + ] + }, + "organizations_external_id_unique": { + "name": "organizations_external_id_unique", + "nullsNotDistinct": false, + "columns": [ + "external_id" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.pipeline_logs": { + "name": "pipeline_logs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "message_id": { + "name": "message_id", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "listserv_config_id": { + "name": "listserv_config_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "pipeline_log_status", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "error_text": { + "name": "error_text", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "extracted_event_id": { + "name": "extracted_event_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "pipeline_logs_listserv_config_id_listserv_configs_id_fk": { + "name": "pipeline_logs_listserv_config_id_listserv_configs_id_fk", + "tableFrom": "pipeline_logs", + "tableTo": "listserv_configs", + "columnsFrom": [ + "listserv_config_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "pipeline_logs_extracted_event_id_events_id_fk": { + "name": "pipeline_logs_extracted_event_id_events_id_fk", + "tableFrom": "pipeline_logs", + "tableTo": "events", + "columnsFrom": [ + "extracted_event_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.rsvps": { + "name": "rsvps", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "event_id": { + "name": "event_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "rsvps_event_id_idx": { + "name": "rsvps_event_id_idx", + "columns": [ + { + "expression": "event_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "rsvps_user_id_users_id_fk": { + "name": "rsvps_user_id_users_id_fk", + "tableFrom": "rsvps", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "rsvps_event_id_events_id_fk": { + "name": "rsvps_event_id_events_id_fk", + "tableFrom": "rsvps", + "tableTo": "events", + "columnsFrom": [ + "event_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "rsvps_user_id_event_id_pk": { + "name": "rsvps_user_id_event_id_pk", + "columns": [ + "user_id", + "event_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.saved_events": { + "name": "saved_events", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "event_id": { + "name": "event_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "saved_events_event_id_idx": { + "name": "saved_events_event_id_idx", + "columns": [ + { + "expression": "event_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "saved_events_user_id_users_id_fk": { + "name": "saved_events_user_id_users_id_fk", + "tableFrom": "saved_events", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "saved_events_event_id_events_id_fk": { + "name": "saved_events_event_id_events_id_fk", + "tableFrom": "saved_events", + "tableTo": "events", + "columnsFrom": [ + "event_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "saved_events_user_id_event_id_pk": { + "name": "saved_events_user_id_event_id_pk", + "columns": [ + "user_id", + "event_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.sync_state": { + "name": "sync_state", + "schema": "", + "columns": { + "key": { + "name": "key", + "type": "varchar(100)", + "primaryKey": true, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user_interests": { + "name": "user_interests", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "tag": { + "name": "tag", + "type": "event_tag", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "user_interests_user_id_users_id_fk": { + "name": "user_interests_user_id_users_id_fk", + "tableFrom": "user_interests", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "user_interests_user_id_tag_pk": { + "name": "user_interests_user_id_tag_pk", + "columns": [ + "user_id", + "tag" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user_preference_vectors": { + "name": "user_preference_vectors", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "tag_weights": { + "name": "tag_weights", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "latent_vector": { + "name": "latent_vector", + "type": "double precision[]", + "primaryKey": false, + "notNull": false + }, + "interaction_count": { + "name": "interaction_count", + "type": "double precision", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "user_preference_vectors_user_id_users_id_fk": { + "name": "user_preference_vectors_user_id_users_id_fk", + "tableFrom": "user_preference_vectors", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user_regions": { + "name": "user_regions", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "region": { + "name": "region", + "type": "campus_region", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "user_regions_user_id_users_id_fk": { + "name": "user_regions_user_id_users_id_fk", + "tableFrom": "user_regions", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "user_regions_user_id_region_pk": { + "name": "user_regions_user_id_region_pk", + "columns": [ + "user_id", + "region" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "net_id": { + "name": "net_id", + "type": "varchar(50)", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "display_name": { + "name": "display_name", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "class_year": { + "name": "class_year", + "type": "varchar(10)", + "primaryKey": false, + "notNull": false + }, + "major": { + "name": "major", + "type": "varchar(255)", + "primaryKey": false, + "notNull": false + }, + "avatar_url": { + "name": "avatar_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_org_leader": { + "name": "is_org_leader", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "onboarded": { + "name": "onboarded", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "calendar_token": { + "name": "calendar_token", + "type": "varchar(64)", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_net_id_unique": { + "name": "users_net_id_unique", + "nullsNotDistinct": false, + "columns": [ + "net_id" + ] + }, + "users_email_unique": { + "name": "users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + }, + "users_calendar_token_unique": { + "name": "users_calendar_token_unique", + "nullsNotDistinct": false, + "columns": [ + "calendar_token" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.campus_region": { + "name": "campus_region", + "schema": "public", + "values": [ + "central", + "east", + "west", + "south", + "north", + "off-campus" + ] + }, + "public.event_status": { + "name": "event_status", + "schema": "public", + "values": [ + "draft", + "published" + ] + }, + "public.event_tag": { + "name": "event_tag", + "schema": "public", + "values": [ + "free food", + "career", + "research", + "academics", + "tech", + "entrepreneurship", + "politics", + "visual arts", + "performing arts", + "literature", + "culture", + "music", + "gaming", + "athletics", + "religion", + "sustainability", + "outdoors", + "wellness", + "community service", + "speaker event", + "social event", + "stem" + ] + }, + "public.friendship_status": { + "name": "friendship_status", + "schema": "public", + "values": [ + "pending", + "accepted", + "declined" + ] + }, + "public.interaction_type": { + "name": "interaction_type", + "schema": "public", + "values": [ + "view", + "click", + "rsvp", + "save", + "share", + "hide" + ] + }, + "public.item_type": { + "name": "item_type", + "schema": "public", + "values": [ + "event", + "organization" + ] + }, + "public.location_category": { + "name": "location_category", + "schema": "public", + "values": [ + "academic", + "residential", + "athletic", + "social", + "administrative", + "library", + "dining", + "other" + ] + }, + "public.location_resolution": { + "name": "location_resolution", + "schema": "public", + "values": [ + "engine", + "online", + "alias", + "gazetteer", + "fuzzy", + "manual", + "unresolved" + ] + }, + "public.notification_type": { + "name": "notification_type", + "schema": "public", + "values": [ + "friend_request", + "event_reminder", + "org_new_event" + ] + }, + "public.org_category": { + "name": "org_category", + "schema": "public", + "values": [ + "career", + "affinity", + "performing arts", + "academics", + "athletics", + "social event", + "culture", + "religion", + "politics", + "community service" + ] + }, + "public.org_role": { + "name": "org_role", + "schema": "public", + "values": [ + "owner", + "officer", + "member" + ] + }, + "public.pipeline_log_status": { + "name": "pipeline_log_status", + "schema": "public", + "values": [ + "success", + "skipped_not_event", + "duplicate", + "error", + "needs_review" + ] + } + }, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/apps/database/drizzle/meta/_journal.json b/apps/database/drizzle/meta/_journal.json index ad19c3f..fdaf342 100644 --- a/apps/database/drizzle/meta/_journal.json +++ b/apps/database/drizzle/meta/_journal.json @@ -64,6 +64,13 @@ "when": 1794772434896, "tag": "0008_event_location_resolution", "breakpoints": true + }, + { + "idx": 9, + "version": "7", + "when": 1795310572356, + "tag": "0009_large_electro", + "breakpoints": true } ] } \ No newline at end of file diff --git a/apps/database/package.json b/apps/database/package.json index adae2ad..b9fa03c 100644 --- a/apps/database/package.json +++ b/apps/database/package.json @@ -6,16 +6,17 @@ ".": "./src/index.ts" }, "scripts": { - "db:generate": "drizzle-kit generate", - "db:migrate": "drizzle-kit migrate", - "db:push": "drizzle-kit push", - "db:studio": "drizzle-kit studio", - "db:setup-recommendations": "bun run src/setup-recommendations.ts", - "db:seed": "bun run src/seed.ts", + "db:generate": "bun --env-file=../../.env run drizzle-kit generate", + "db:migrate": "bun --env-file=../../.env run drizzle-kit migrate", + "db:push": "bun --env-file=../../.env run src/ensure-extensions.ts && bun --env-file=../../.env run drizzle-kit push", + "db:studio": "bun --env-file=../../.env run drizzle-kit studio", + "db:seed": "bun --env-file=../../.env run src/seed.ts", + "db:seed:org-review": "bun --env-file=../../.env run src/seed-org-review.ts", "lint": "biome check .", "check-types": "tsc --noEmit", - "db:sync-engine": "bun run src/sync-inbox-engine.ts", - "db:resolve-locations": "bun run src/resolve-locations.ts" + "db:setup-recommendations": "bun --env-file=../../.env run src/setup-recommendations.ts", + "db:resolve-locations": "bun --env-file=../../.env run src/resolve-locations.ts", + "db:sync-engine": "bun --env-file=../../.env run src/sync-inbox-engine.ts" }, "dependencies": { "drizzle-orm": "^0.45.2", diff --git a/apps/database/src/ensure-extensions.ts b/apps/database/src/ensure-extensions.ts new file mode 100644 index 0000000..1352427 --- /dev/null +++ b/apps/database/src/ensure-extensions.ts @@ -0,0 +1,15 @@ +import postgres from "postgres"; + +// CLI prerequisite: push compares schemas but does not run SQL migration files. +const url = process.env.DATABASE_URL; +if (!url) { + throw new Error("DATABASE_URL is required to enable database extensions"); +} + +const client = postgres(url, { max: 1 }); +try { + await client`CREATE EXTENSION IF NOT EXISTS vector`; + console.log("Database vector extension is ready."); +} finally { + await client.end(); +} diff --git a/apps/database/src/index.ts b/apps/database/src/index.ts index 72b5fa6..afee7b8 100644 --- a/apps/database/src/index.ts +++ b/apps/database/src/index.ts @@ -1,6 +1,8 @@ export { db } from "./db"; export type { Database } from "./db"; export * from "./schema"; +export { loadOrganizationPage } from "./organization-page"; +export type { OrganizationSort } from "./organization-page"; export { eq, and, diff --git a/apps/database/src/organization-page.test.ts b/apps/database/src/organization-page.test.ts new file mode 100644 index 0000000..bb3cc09 --- /dev/null +++ b/apps/database/src/organization-page.test.ts @@ -0,0 +1,279 @@ +/** Run against a migrated local database with ORG_TEST_DATABASE_URL=postgres://… bun test. */ +import { beforeAll, describe, expect, test } from "bun:test"; +import { asc, desc, eq, ilike, sql } from "drizzle-orm"; +import type { Database } from "./db"; +import type { OrganizationCursor, OrganizationQuery, OrganizationSort } from "./organization-page"; +import { organizations } from "./schema"; + +const url = process.env.ORG_TEST_DATABASE_URL; +type Transaction = Parameters[0]>[0]; +let db: Database; +let pageSize: number; +let loadPage: typeof import("./organization-page").loadOrganizationPage; + +describe.skipIf(!url)("organization keyset pagination (Postgres)", () => { + beforeAll(async () => { + const target = new URL(url as string); + if (!["localhost", "127.0.0.1", "[::1]"].includes(target.hostname)) { + throw new Error("Pagination tests require a local database"); + } + process.env.DATABASE_URL = url; + db = (await import("./db")).db; + const pagination = await import("./organization-page"); + loadPage = pagination.loadOrganizationPage; + pageSize = pagination.ORGANIZATION_PAGE_SIZE; + }); + + // Each test owns temporary rows inside a transaction that always rolls back. + async function withFixtures(run: (tx: Transaction, search: string) => Promise) { + const rollback = new Error("rollback test fixtures"); + try { + await db.transaction(async (tx) => { + const search = `pagination-test-${crypto.randomUUID()}`; + await tx.insert(organizations).values( + Array.from({ length: 47 }, (_, i) => ({ + name: `${search} ${String(i).padStart(3, "0")}`, + category: i % 2 ? ("academics" as const) : ("culture" as const), + source: "seed", + memberCount: [100, 0, null, -2147483648][i % 4], + // Both tied timestamps and distinct microseconds within one JS millisecond. + createdAt: sql`timestamp '2026-11-08 10:00:00' + ${i % 23} * interval '1 microsecond'`, + })), + ); + await run(tx, search); + throw rollback; + }); + } catch (error) { + if (error !== rollback) throw error; + } + } + + const combinations = (["name", "members", "newest"] satisfies OrganizationSort[]).flatMap( + (sort) => (["asc", "desc"] as const).map((direction) => ({ sort, direction })), + ); + for (const { sort, direction } of combinations) { + test(`${sort} ${direction}: all rows exactly once, ranges, and previous round trip`, async () => { + await withFixtures(async (tx, search) => { + const order = + sort === "name" + ? [asc(organizations.name), asc(organizations.id)] + : sort === "members" + ? [ + sql`coalesce(${organizations.memberCount}, '-2147483649'::bigint) desc nulls last`, + desc(organizations.id), + ] + : [desc(organizations.createdAt), desc(organizations.id)]; + const expected = await tx + .select({ id: organizations.id }) + .from(organizations) + .where(ilike(organizations.name, `%${search}%`)) + .orderBy(...order); + if ((sort === "name") !== (direction === "asc")) expected.reverse(); + const pages: Awaited>[] = []; + let cursor: OrganizationCursor | undefined; + do { + const page = await loadPage({ search, sort, order: direction, cursor }, tx); + expect(page.total).toBe(47); + expect(page.items.length).toBeLessThanOrEqual(pageSize); + pages.push(page); + cursor = page.nextCursor ?? undefined; + if (pages.length > Math.ceil(47 / pageSize)) + throw new Error("Pagination did not terminate"); + } while (cursor); + const ranges = Array.from({ length: Math.ceil(47 / pageSize) }, (_, i) => [ + i * pageSize + 1, + Math.min((i + 1) * pageSize, 47), + ]); + expect(pages.map((page) => page.items.length)).toEqual( + ranges.map(([start, end]) => end - start + 1), + ); + expect(pages.map((page) => [page.rangeStart, page.rangeEnd])).toEqual(ranges); + expect(pages.flatMap((page) => page.items.map((org) => org.id))).toEqual( + expected.map((org) => org.id), + ); + expect(pages[0].previousCursor).toBeNull(); + for (let i = pages.length - 1; i > 0; i--) { + const previous = await loadPage( + { + search, + sort, + order: direction, + cursor: pages[i].previousCursor as OrganizationCursor, + }, + tx, + ); + expect(previous.items.map((org) => org.id)).toEqual( + pages[i - 1].items.map((org) => org.id), + ); + expect([previous.rangeStart, previous.rangeEnd]).toEqual([ + pages[i - 1].rangeStart, + pages[i - 1].rangeEnd, + ]); + } + if (sort === "members" && direction === "desc") { + const counts = pages.flatMap((page) => page.items.map((org) => org.memberCount)); + const firstUnknown = counts.indexOf(null); + expect(firstUnknown).toBeGreaterThan(0); + expect(counts.slice(firstUnknown).every((count) => count === null)).toBe(true); + } + }); + }); + } + + test("category/search remain applied; filter changes without a boundary start at page one", async () => { + await withFixtures(async (tx, search) => { + const first = await loadPage({ search, category: "culture" }, tx); + const next = await loadPage( + { search, category: "culture", cursor: first.nextCursor as OrganizationCursor }, + tx, + ); + expect(first.total).toBe(24); + expect(next.items).toHaveLength(Math.min(pageSize, first.total - pageSize)); + expect([...first.items, ...next.items].every((org) => org.category === "culture")).toBe(true); + for (const changed of [ + { search, category: "academics" }, + { search, category: "culture", sort: "members" }, + { search, category: "culture", order: "desc" }, + { search: `${search} 00`, category: "culture" }, + ] satisfies OrganizationQuery[]) { + const reset = await loadPage(changed, tx); + const expected = await loadPage(changed, tx); + expect(reset).toEqual(expected); + expect(reset.hasCursor).toBe(false); + } + }); + }); + + test("malformed cursors, empty results, single page, and literal search wildcards", async () => { + await withFixtures(async (tx, search) => { + const first = await loadPage({ search }, tx); + for (const cursor of [ + { id: "invalid", direction: "next" }, + { id: "00000000-0000-0000-0000-000000000000", direction: "next" }, + { id: first.items[0].id, direction: "invalid" }, + ]) { + expect(await loadPage({ search, cursor: cursor as OrganizationCursor }, tx)).toEqual(first); + } + const single = await loadPage({ search: `${search} 000` }, tx); + expect(single.items).toHaveLength(1); + expect(single.previousCursor).toBeNull(); + expect(single.nextCursor).toBeNull(); + for (const suffix of [" missing", "%", "_", "\\"]) { + const empty = await loadPage({ search: search + suffix }, tx); + expect(empty.items).toHaveLength(0); + expect(empty.total).toBe(0); + } + }); + }); + + test("a stale cursor keeps the total and exposes first-page recovery", async () => { + await withFixtures(async (tx, search) => { + const page = await loadPage( + { + search, + sort: "name", + cursor: { + direction: "next", + id: "ffffffff-ffff-ffff-ffff-ffffffffffff", + value: `${search} zzzz`, + }, + }, + tx, + ); + expect(page.items).toHaveLength(0); + expect(page.total).toBe(47); + expect(page.hasCursor).toBe(true); + expect(page.previousCursor).toBeNull(); + expect(page.nextCursor).toBeNull(); + const first = await loadPage({ search }, tx); + expect(first.items).toHaveLength(pageSize); + expect(first.hasCursor).toBe(false); + }); + }); + + test("deleting the boundary row preserves the next page", async () => { + await withFixtures(async (tx, search) => { + const first = await loadPage({ search, sort: "newest" }, tx); + const expected = await loadPage( + { search, sort: "newest", cursor: first.nextCursor as OrganizationCursor }, + tx, + ); + await tx.delete(organizations).where(eq(organizations.id, first.items[pageSize - 1].id)); + const next = await loadPage( + { search, sort: "newest", cursor: first.nextCursor as OrganizationCursor }, + tx, + ); + expect(next.items).toEqual(expected.items); + expect(next.total).toBe(46); + }); + }); + + test("inserting ahead of the cursor does not shift or duplicate the next page", async () => { + await withFixtures(async (tx, search) => { + const first = await loadPage({ search }, tx); + const expected = await loadPage( + { search, cursor: first.nextCursor as OrganizationCursor }, + tx, + ); + await tx.insert(organizations).values({ name: `${search} ! new`, category: "culture" }); + const next = await loadPage({ search, cursor: first.nextCursor as OrganizationCursor }, tx); + expect(next.items).toEqual(expected.items); + expect(next.total).toBe(48); + }); + }); + + test("all six ordering indexes exist on public.organizations", async () => { + const indexes = await db.execute(sql`select indexname from pg_indexes + where schemaname = 'public' and tablename = 'organizations'`); + const names = indexes.map((row) => row.indexname); + for (const suffix of [ + "name_id", + "members_id", + "created_at_id", + "category_name_id", + "category_members_id", + "category_created_at_id", + ]) { + expect(names).toContain(`organizations_${suffix}_idx`); + } + }); + + test("all sorts can seek through an ordering index", async () => { + await db.transaction(async (tx) => { + // Small test databases may prefer a sequential scan; check index eligibility. + await tx.execute(sql`set local enable_seqscan = off`); + await tx.execute(sql`set local enable_bitmapscan = off`); + for (const filtered of [false, true]) { + for (const sort of ["name", "members", "newest"]) { + const key = + sort === "name" + ? sql`name` + : sort === "members" + ? sql`coalesce(member_count, '-2147483649'::bigint)` + : sql`created_at`; + const value = + sort === "name" ? sql`'M'` : sort === "members" ? sql`0` : sql`timestamp '2026-11-08'`; + const direction = sort === "name" ? sql`>` : sql`<`; + const order = sort === "name" ? sql`asc` : sql`desc nulls last`; + const rows = await tx.execute(sql`explain (format json) + select id, name from public.organizations + where ${filtered ? sql`category = 'culture' and` : sql``} + (${key}, id) ${direction} (${value}, '00000000-0000-0000-0000-000000000000'::uuid) + order by ${key} ${order}, id ${order} limit ${pageSize}`); + const plan = JSON.stringify(rows[0]["QUERY PLAN"]); + const suffix = sort === "newest" ? "created_at" : sort; + // With few matching rows Postgres may prefer the unfiltered ordering + // index and apply category as a filter. The unique name index may + // also use Incremental Sort for the id tie-breaker. + expect(plan).toMatch( + new RegExp( + `organizations_(?:${filtered ? "(?:category_)?" : ""}${suffix}_id_idx${sort === "name" ? "|name_unique" : ""})`, + ), + ); + expect(plan).toContain("Index Cond"); + expect(plan).not.toContain('"Node Type":"Sort"'); + } + } + }); + }); +}); diff --git a/apps/database/src/organization-page.ts b/apps/database/src/organization-page.ts new file mode 100644 index 0000000..15ca082 --- /dev/null +++ b/apps/database/src/organization-page.ts @@ -0,0 +1,146 @@ +import { type SQL, and, asc, desc, eq, ilike, or, sql } from "drizzle-orm"; +import { type Database, db } from "./db"; +import { organizations } from "./schema"; + +export const ORGANIZATION_PAGE_SIZE = 10; +export type OrganizationSort = "name" | "members" | "newest"; +export interface OrganizationQuery { + search?: string; + category?: typeof organizations.$inferSelect.category; + sort?: OrganizationSort; + order?: "asc" | "desc"; + cursor?: OrganizationCursor; +} + +export interface OrganizationCursor { + direction: "next" | "previous"; + id: string; + value: string | number; +} +// Below the minimum int4 value: unknown counts come after every known count. +// Keep this expression identical to the schema's expression index. +const members = sql`coalesce(${organizations.memberCount}, '-2147483649'::bigint)`.mapWith( + Number, +); + +export async function loadOrganizationPage( + query: OrganizationQuery = {}, + database: Pick = db, +) { + const sort = query.sort ?? "name"; + const sortOrder = query.order ?? (sort === "name" ? "asc" : "desc"); + const ascending = sortOrder === "asc"; + const key = + sort === "name" ? organizations.name : sort === "members" ? members : organizations.createdAt; + + const filters: (SQL | undefined)[] = []; + if (query.search) { + const pattern = `%${query.search.replace(/[\\%_]/g, "\\$&")}%`; + filters.push(or(ilike(organizations.name, pattern), ilike(organizations.description, pattern))); + } + if (query.category) filters.push(eq(organizations.category, query.category)); + const base = and(...filters); + const requested = query.cursor; + let valid = Boolean( + requested && + ["next", "previous"].includes(requested.direction) && + typeof requested.id === "string" && + /^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(requested.id), + ); + if (valid && requested) { + if (sort === "members") { + valid = + typeof requested.value === "number" && + Number.isInteger(requested.value) && + requested.value >= -2147483649 && + requested.value <= 2147483647; + } else { + valid = typeof requested.value === "string" && requested.value.length <= 255; + if (valid && sort === "newest") { + const value = String(requested.value).replace("T", " "); + const date = new Date(`${value.replace(" ", "T")}Z`); + valid = + /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,6})?$/.test(value) && + Number.isFinite(date.getTime()) && + date.toISOString().slice(0, 19) === value.replace(" ", "T").slice(0, 19) && + !value.startsWith("0000"); + } + } + } + const cursor = valid && requested ? requested : null; + const reverse = cursor?.direction === "previous"; + const order = ascending !== reverse ? asc : desc; + + function boundary(value: string | number, id: string, direction: "next" | "previous") { + const greater = ascending === (direction === "next"); + const operator = greater ? sql`>` : sql`<`; + const parameter = sort === "newest" ? sql`${value}::timestamp` : sql`${value}`; + return sql`(${key}, ${organizations.id}) ${operator} (${parameter}, ${id}::uuid)`; + } + + const [rows, [count]] = await Promise.all([ + database + .select({ + id: organizations.id, + name: organizations.name, + description: organizations.description, + logoUrl: organizations.logoUrl, + category: organizations.category, + memberCount: organizations.memberCount, + // Do not round Postgres microseconds through a JavaScript Date cursor. + createdAt: sql`${organizations.createdAt}::text`, + memberKey: members, + }) + .from(organizations) + .where(and(base, cursor ? boundary(cursor.value, cursor.id, cursor.direction) : undefined)) + .orderBy( + sort === "name" + ? order(key) + : sql`${key} ${ascending !== reverse ? sql`asc nulls first` : sql`desc nulls last`}`, + sort === "name" + ? order(organizations.id) + : sql`${organizations.id} ${ascending !== reverse ? sql`asc nulls first` : sql`desc nulls last`}`, + ) + .limit(ORGANIZATION_PAGE_SIZE), + database.select({ total: sql`count(*)::int` }).from(organizations).where(base), + ]); + const items = rows.slice(0, ORGANIZATION_PAGE_SIZE); + if (reverse) items.reverse(); + type Row = (typeof items)[number]; + const cursorValue = (row: Row) => + sort === "name" ? row.name : sort === "members" ? row.memberKey : row.createdAt; + const cursorFor = (row: Row, direction: OrganizationCursor["direction"]): OrganizationCursor => ({ + id: row.id, + value: cursorValue(row), + direction, + }); + + const first = items[0]; + const last = items.at(-1); + // Check both edges against live data, including when the cursor's row was deleted. + const [before, after] = await Promise.all([ + first + ? database + .select({ count: sql`count(*)::int` }) + .from(organizations) + .where(and(base, boundary(cursorValue(first), first.id, "previous"))) + : [], + last + ? database + .select({ id: organizations.id }) + .from(organizations) + .where(and(base, boundary(cursorValue(last), last.id, "next"))) + .limit(1) + : [], + ]); + return { + items: items.map(({ memberKey: _, createdAt: __, ...item }) => item), + total: count.total, + rangeStart: first ? (before[0]?.count ?? 0) + 1 : 0, + rangeEnd: first ? (before[0]?.count ?? 0) + items.length : 0, + previousCursor: first && (before[0]?.count ?? 0) > 0 ? cursorFor(first, "previous") : null, + nextCursor: last && after.length ? cursorFor(last, "next") : null, + // Allows a stale link beyond the remaining results to recover without a dead end. + hasCursor: cursor !== null, + }; +} diff --git a/apps/database/src/schema/index.ts b/apps/database/src/schema/index.ts index bc8fc49..8751ed9 100644 --- a/apps/database/src/schema/index.ts +++ b/apps/database/src/schema/index.ts @@ -170,33 +170,56 @@ export const locationAliases = pgTable("location_aliases", { createdAt: timestamp("created_at").defaultNow().notNull(), }); -export const organizations = pgTable("organizations", { - id: uuid("id").defaultRandom().primaryKey(), - name: varchar("name", { length: 255 }).notNull().unique(), - description: text("description"), - logoUrl: text("logo_url"), - category: orgCategoryEnum("category").notNull(), - /** Null for organizations imported from MyPrincetonU (no Forum user created them). */ - creatorId: uuid("creator_id").references(() => users.id), - /** 'myprincetonu' (synced from InboxEngine) or 'manual' (created in The Forum). */ - source: varchar("source", { length: 20 }).default("manual").notNull(), - /** Stable InboxEngine organization ID, e.g. "mpu:52941" for MyPrincetonU group 52941. */ - externalId: varchar("external_id", { length: 64 }).unique(), - acronym: varchar("acronym", { length: 40 }), - tagline: text("tagline"), - groupType: varchar("group_type", { length: 120 }), - /** The organization's MyPrincetonU group page. */ - groupUrl: text("group_url"), - website: text("website"), - contactEmail: varchar("contact_email", { length: 255 }), - /** { instagram?, facebook?, linkedin?, twitter?, youtube? } → URLs */ - socials: jsonb("socials").$type>().default({}).notNull(), - /** Member count reported by MyPrincetonU (not Forum users). */ - memberCount: integer("member_count"), - syncedAt: timestamp("synced_at"), - createdAt: timestamp("created_at").defaultNow().notNull(), - updatedAt: timestamp("updated_at").defaultNow().notNull(), -}); +export const organizations = pgTable( + "organizations", + { + id: uuid("id").defaultRandom().primaryKey(), + name: varchar("name", { length: 255 }).notNull().unique(), + description: text("description"), + logoUrl: text("logo_url"), + category: orgCategoryEnum("category").notNull(), + /** Null for organizations imported from MyPrincetonU (no Forum user created them). */ + creatorId: uuid("creator_id").references(() => users.id), + /** 'myprincetonu' (synced from InboxEngine) or 'manual' (created in The Forum). */ + source: varchar("source", { length: 20 }).default("manual").notNull(), + /** Stable InboxEngine organization ID, e.g. "mpu:52941" for MyPrincetonU group 52941. */ + externalId: varchar("external_id", { length: 64 }).unique(), + acronym: varchar("acronym", { length: 40 }), + tagline: text("tagline"), + groupType: varchar("group_type", { length: 120 }), + /** The organization's MyPrincetonU group page. */ + groupUrl: text("group_url"), + website: text("website"), + contactEmail: varchar("contact_email", { length: 255 }), + /** { instagram?, facebook?, linkedin?, twitter?, youtube? } → URLs */ + socials: jsonb("socials").$type>().default({}).notNull(), + /** Member count reported by MyPrincetonU (not Forum users). */ + memberCount: integer("member_count"), + syncedAt: timestamp("synced_at"), + createdAt: timestamp("created_at").defaultNow().notNull(), + updatedAt: timestamp("updated_at").defaultNow().notNull(), + }, + (t) => [ + index("organizations_name_id_idx").on(t.name, t.id), + index("organizations_members_id_idx").on( + sql`coalesce(${t.memberCount}, '-2147483649'::bigint) desc nulls last`, + t.id.desc(), + ), + index("organizations_created_at_id_idx").on(t.createdAt.desc(), t.id.desc()), + // Category-filtered pages can seek without scanning unrelated categories. + index("organizations_category_name_id_idx").on(t.category, t.name, t.id), + index("organizations_category_members_id_idx").on( + t.category, + sql`coalesce(${t.memberCount}, '-2147483649'::bigint) desc nulls last`, + t.id.desc(), + ), + index("organizations_category_created_at_id_idx").on( + t.category, + t.createdAt.desc(), + t.id.desc(), + ), + ], +); export const orgMembers = pgTable( "org_members", diff --git a/apps/database/src/seed-org-review.ts b/apps/database/src/seed-org-review.ts new file mode 100644 index 0000000..3113c62 --- /dev/null +++ b/apps/database/src/seed-org-review.ts @@ -0,0 +1,150 @@ +/** TIG-308 organization review fixtures. See seeddata/org-review.md. */ +import { eq } from "drizzle-orm"; +import { + events, + campusLocations, + eventTags, + orgFollowers, + organizations, + userInterests, + users, +} from "./schema"; +import { reviewOrganizations } from "./seeddata/org-review"; + +if (Bun.argv.includes("--dry-run")) { + console.log({ + organizations: reviewOrganizations.length, + categories: new Set(reviewOrganizations.map((org) => org.category)).size, + missingDescriptions: reviewOrganizations.filter((org) => !org.description).length, + missingLogos: reviewOrganizations.filter((org) => !org.logoUrl).length, + offices: reviewOrganizations.filter((org) => org.groupType === "University Office").length, + users: 24, + events: 12, + pagesAt10: Math.ceil(reviewOrganizations.length / 10), + }); + process.exit(0); +} + +// Check before importing the lazy database client. No remote override for this fixture. +const target = new URL(process.env.DATABASE_URL ?? "postgres://missing"); +if ( + process.env.NODE_ENV === "production" || + !["localhost", "127.0.0.1", "[::1]"].includes(target.hostname) +) { + throw new Error("TIG-308 fixtures require a local, non-production DATABASE_URL."); +} + +const { db } = await import("./db"); +try { + await db.transaction(async (tx) => { + const demoUsers = []; + for (let i = 0; i < 24; i++) { + const netId = `tig308_demo_${String(i + 1).padStart(2, "0")}`; + await tx + .insert(users) + .values({ + netId, + email: `${netId}@example.invalid`, + displayName: `TIG-308 Demo ${i + 1}`, + classYear: "2029", + onboarded: true, + }) + .onConflictDoNothing(); + const [user] = await tx.select().from(users).where(eq(users.netId, netId)); + if (!user || user.email !== `${netId}@example.invalid`) { + throw new Error(`Demo user collision: ${netId}`); + } + demoUsers.push(user); + } + const viewer = demoUsers[0]; + await tx + .insert(userInterests) + .values([ + { userId: viewer.id, tag: "academics" }, + { userId: viewer.id, tag: "culture" }, + ]) + .onConflictDoNothing(); + await tx + .insert(campusLocations) + .values({ + id: "seed-tig-308", + name: "TIG-308 Demo Meeting Room", + latitude: 40.3469, + longitude: -74.6551, + category: "social", + }) + .onConflictDoNothing(); + + for (const [index, fixture] of reviewOrganizations.entries()) { + const [existing] = await tx + .select() + .from(organizations) + .where(eq(organizations.externalId, fixture.externalId as string)); + if (existing && existing.source !== "seed") { + throw new Error(`Non-seed organization collision: ${fixture.externalId}`); + } + const [org] = existing + ? await tx + .update(organizations) + .set(fixture) + .where(eq(organizations.id, existing.id)) + .returning() + : await tx.insert(organizations).values(fixture).returning(); + + // Most rows intentionally have zero followers; others have 1, 3, 8, or 24. + const followerCount = [0, 0, 0, 1, 3, 8, 24][index % 7]; + if (followerCount) { + await tx + .insert(orgFollowers) + .values( + demoUsers.slice(0, followerCount).map((user) => ({ orgId: org.id, userId: user.id })), + ) + .onConflictDoNothing(); + } + if (index < 12) { + const sourceMessageId = `seed:tig-308:event:${index}`; + const datetime = new Date(Date.now() + (index - 4) * 86400000); + const [existingEvent] = await tx + .select() + .from(events) + .where(eq(events.sourceMessageId, sourceMessageId)); + if (existingEvent && existingEvent.source !== "seed") { + throw new Error(`Non-seed event collision: ${sourceMessageId}`); + } + const values = { + sourceMessageId, + source: "seed", + orgId: org.id, + creatorId: viewer.id, + title: `TIG-308 Demo Meeting ${index + 1}`, + description: "Fictional activity for organization design review.", + datetime, + endDatetime: new Date(datetime.getTime() + 3600000), + locationId: "seed-tig-308", + locationResolution: "manual" as const, + }; + let eventId = existingEvent?.id; + if (existingEvent) { + await tx.update(events).set(values).where(eq(events.id, existingEvent.id)); + } else { + const [inserted] = await tx.insert(events).values(values).returning({ id: events.id }); + eventId = inserted.id; + } + if (eventId) { + await tx + .insert(eventTags) + .values([ + { eventId, tag: "academics" }, + { eventId, tag: "culture" }, + ]) + .onConflictDoNothing(); + } + } + } + }); + console.log("TIG-308: seeded 312 organizations, 24 demo users, and 12 events."); + process.exit(0); +} catch (error) { + console.error("TIG-308 seed rolled back:", error); + process.exit(1); +} diff --git a/apps/database/src/seeddata/org-review.md b/apps/database/src/seeddata/org-review.md new file mode 100644 index 0000000..f383d3c --- /dev/null +++ b/apps/database/src/seeddata/org-review.md @@ -0,0 +1,71 @@ +# TIG-308 organization review fixtures + +TIG-308 requests a Figma redesign for overlooked suggestions, the long organization +list, inconsistent row heights and description truncation, zero-follower noise, +small logos, mixed offices and clubs, and inconsistent category labels. +This command supplies review data. The directory now supports keyset pagination +with 10 rows per page, Name/Member count/Date created sorts, and an independent +Ascending/Descending control. The remaining +redesign work is separate. + +Set `DATABASE_URL` in the root `.env` to your local database, following the root +`.env.example`. From the repository root, with the database running and schema applied: + +```powershell +bun run db:seed:org-review --dry-run +bun run db:seed:org-review +``` + +The command loads the root `.env`, like the other database scripts. It refuses remote hosts and +production mode. It runs independently of the full seed and InboxEngine, in one +transaction. Stable external IDs and names ending in `[TIG-308 demo]` keep imported +and real organizations separate. Reruns refresh these fixtures and event dates; +they restore the specified demo follows, retain additional follows, and do not duplicate fixture rows. +There is no need to reset the database. Additional existing orgs increase the total. + +| Review case | Fixture / action | +| --- | --- | +| Many pages | 312 demo organizations: 32 pages at the fixed size of 10, including a partial last page. Names span A–Z. First/Previous/Next are grouped on the right. The result range reads 1–10, 16–30, 31–45, etc. Sort, direction, filters, and keyset cursors are kept in the URL for Back and shared links. | +| Description heights and clipping | 52 null descriptions, 52 empty strings, short descriptions, and 52 long paragraphs. | +| Logo readability and fallback | 104 each with no logo, a simple logo, and a dense logo. Assets are local and work offline. | +| Zero and nonzero followers | Most start at zero; other rows have 1, 3, 8, or 24 followers. Counts come from actual follow records, independently of imported member counts. | +| Offices mixed with clubs | Academic Advising, AccessAbility Center, Office of International Programs, and Research Computing have `groupType = University Office`. Others are student organizations. | +| Category mismatch | All ten categories are represented. Social Event and Community Service expose chip/row wording differences. AccessAbility Center and Active Minds deliberately retain Politics to reproduce the issue. | +| Long suggestion names | The International Association entry has a long name and Academics category. Suggestions remain selected by the existing ranking logic, so this exact entry is not guaranteed to appear. | +| Followed and unfollowed states | Demo viewer `tig308_demo_01` follows some orgs and has Academics/Culture interests, leaving others available for suggestions. These are fictional database users, not CAS accounts; use an existing local auth workflow or sign in normally and follow a few demo orgs. | +| Activity and event counts | First 12 orgs each have one event, with past and upcoming dates; remaining demo orgs have none. | +| Single page | Search `Research Computing` (one demo match), or `Collective 300` (one generated demo match). | +| No results | Search `tig308-no-match-xyz`. Clear the search to restore results. | +| Loading, mobile, URL/back behavior | Use browser network throttling, narrow the viewport, and navigate between pages. Search/category/sort/direction changes reset pagination. Name sorts by `(name, id)`; Member count by `(coalesce(member_count, -2147483649), id)`; Date created by `(created_at, id)`. Each supports ascending or descending order, including the ID tie-breaker. Unknown member counts come last descending and first ascending. Member counts differ from Forum follower counts. | +| No suggestions / wholly empty directory | Requires a separate clean database or a viewer with no eligible suggestions; this seed intentionally populates the directory and does not delete existing data. | + +The dry run reports fixture coverage without requiring a database connection. + +Pagination indexes are defined in the Drizzle schema and migration +`0007_organization_keyset_indexes.sql`: one tuple index per sort, plus +category-prefixed versions for filtered lists. There are no offset queries or +numbered page jumps. Cursors contain the boundary values, so a deleted boundary +row still works. Changing an organization's sort value can move it between pages; +the directory shows live data rather than a frozen snapshot. +Ranges are computed from the count of matching rows before the first displayed +row, so they remain accurate on refreshed links and after inserts/deletes. This +adds a count query; the rows themselves are still fetched by tuple seek without +an offset. Existing indexes also support reverse scans for the opposite direction. + +Pagination URLs use ordinary query parameters rather than encoded JSON: +`direction=next&after_id=` plus `after_name`, `after_member_count`, or +`after_created_at`, according to the selected sort. The timestamp uses `T` between +date and time and retains all six fractional digits when present. The server +validates the tuple and queries directly, without looking up the boundary row. +Changing a filter, sort, or order removes all boundary parameters. + +Run the database pagination tests against a migrated local database: + +```powershell +$env:ORG_TEST_DATABASE_URL = 'postgresql://forum:forum_password@localhost:5434/the_forum' +bun test apps/database/src/organization-page.test.ts +``` + +Tests insert temporary data in transactions and roll it back. They cover all +sorts, tied values, timestamp microseconds, unknown counts, filters, invalid +cursors, reverse navigation, concurrent inserts/deletes, and index query plans. diff --git a/apps/database/src/seeddata/org-review.ts b/apps/database/src/seeddata/org-review.ts new file mode 100644 index 0000000..c0016f8 --- /dev/null +++ b/apps/database/src/seeddata/org-review.ts @@ -0,0 +1,51 @@ +import { orgCategoryEnum, type organizations } from "../schema"; + +type Organization = typeof organizations.$inferInsert; + +const examples = [ + ["Academic Advising", "academics", "University Office"], + ["AccessAbility Center", "politics", "University Office"], + ["Active Minds", "politics", "Student Organization"], + ["Office of International Programs", "culture", "University Office"], + ["Research Computing", "academics", "University Office"], + ["Community Service Coalition", "community service", "Student Organization"], + ["Social Activities Council", "social event", "Student Organization"], + [ + "International Association for Interdisciplinary Research, Community Engagement, and Student Leadership", + "academics", + "Student Organization", + ], +] as const; + +// Deliberately fictional, namespaced data; no imported organizations are changed. +export const reviewOrganizations: Organization[] = Array.from({ length: 312 }, (_, i) => { + const example = examples[i]; + const category = example?.[1] ?? orgCategoryEnum.enumValues[i % 10]; + const number = String(i + 1).padStart(3, "0"); + const name = + example?.[0] ?? `${String.fromCharCode(65 + (i % 26))} ${category} Collective ${number}`; + const description = + i % 6 === 0 + ? null + : i % 6 === 1 + ? "" + : i % 6 === 2 + ? "A student community." + : i % 6 === 3 + ? "We bring students together for weekly workshops, peer mentoring, performances, and community projects. Everyone is welcome, whether you are trying something new or bringing years of experience. Join our open meetings to meet collaborators, learn practical skills, and help shape next semester's activities. ".repeat( + 3, + ) + : `Explore ${category} through open meetings, projects, and campus events.`; + return { + externalId: `seed:tig-308:org:${number}`, + source: "seed", + name: `${name} [TIG-308 demo]`, + category, + description, + groupType: example?.[2] ?? "Student Organization", + acronym: i % 4 === 0 ? `D${number}` : null, + logoUrl: i % 3 === 0 ? null : `/seed/tig-308-${i % 3 === 1 ? "simple" : "detailed"}.svg`, + memberCount: i % 5 === 0 ? null : (i * 17) % 1200, + updatedAt: new Date(Date.UTC(2026, 0, 1 + (i % 90))), + }; +}); diff --git a/apps/web/.env.local.example b/apps/web/.env.local.example deleted file mode 100644 index e809c30..0000000 --- a/apps/web/.env.local.example +++ /dev/null @@ -1,34 +0,0 @@ -# Copy to .env.local for local development. -# All required vars are validated at startup by src/env.ts — see the root README -# for where to get each value. - -# ----- Database (Docker default — see `bun run db:up`) ----- -DATABASE_URL=postgresql://forum:forum_password@localhost:5434/the_forum - -# ----- Auth.js + Princeton CAS login ----- -# Generate AUTH_SECRET yourself: openssl rand -base64 32 -AUTH_SECRET=YOUR_AUTH_SECRET_HERE -# Login uses Princeton CAS (no client ID/secret needed). CAS redirects back to -# http://localhost:3000/api/auth/cas/callback in dev. -# Canonical public URL — set this in production (e.g. https://forum.example.edu). -# AUTH_URL=http://localhost:3000 -# Only needed behind a reverse proxy without AUTH_URL (not needed on Vercel). -# AUTH_TRUST_HOST=true -# CAS server (default shown) -# CAS_BASE_URL=https://fed.princeton.edu/cas/ - -# ----- Mapbox (ask Ibraheem for these) ----- -NEXT_PUBLIC_MAPBOX_TOKEN=YOUR_MAPBOX_TOKEN_HERE -NEXT_PUBLIC_CAMPUS_MAP_TOKEN=YOUR_CAMPUS_MAP_TOKEN_HERE -NEXT_PUBLIC_CAMPUS_MAP_STYLE=YOUR_CAMPUS_MAP_STYLE_HERE - -# ----- Optional ----- -# Public origin for metadata / Open Graph / sitemap (defaults to https://forum.tigerapps.org) -# NEXT_PUBLIC_SITE_URL=http://localhost:3000 -# AWS S3 (image uploads) -# AWS_S3_BUCKET=the-forum-uploads -# AWS_REGION=us-east-1 - -# InboxEngine API — powers "Recent emails" on org pages (optional) -# INBOX_ENGINE_URL=http://127.0.0.1:8300 -# INBOX_ENGINE_TOKEN=local-forum-token-0123456789abcdef diff --git a/apps/web/next.config.ts b/apps/web/next.config.ts index fd72621..6230407 100644 --- a/apps/web/next.config.ts +++ b/apps/web/next.config.ts @@ -9,6 +9,15 @@ const nextConfig: NextConfig = { output: "standalone", // Monorepo: trace workspace packages from the repo root. outputFileTracingRoot: path.join(import.meta.dirname, "../.."), + // Keep Bun's workspace symlinks and shared packages inside Turbopack's root. + turbopack: { + root: path.join(import.meta.dirname, "../.."), + }, + experimental: { + // Avoid a cached-resolution HMR panic with Bun workspaces on Windows. + // Turbopack still caches in memory during each dev session. + turbopackFileSystemCacheForDev: false, + }, // Allow Next.js to transpile the workspace database package (TypeScript sources) transpilePackages: ["@the-forum/database"], images: { diff --git a/apps/web/package.json b/apps/web/package.json index c152d92..d4f2e13 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -3,11 +3,11 @@ "version": "0.1.0", "private": true, "scripts": { - "dev": "next dev", - "build": "next build", - "start": "next start", + "dev": "bun --env-file=../../.env run next dev --turbopack", + "build": "bun --env-file=../../.env run next build", + "start": "bun --env-file=../../.env run next start", "lint": "biome check .", - "test": "bun test", + "test": "bun --env-file=../../.env test", "check-types": "tsc --noEmit" }, "dependencies": { diff --git a/apps/web/public/seed/tig-308-detailed.svg b/apps/web/public/seed/tig-308-detailed.svg new file mode 100644 index 0000000..ea55639 --- /dev/null +++ b/apps/web/public/seed/tig-308-detailed.svg @@ -0,0 +1 @@ +INTERDISCIPLINARY SOCIETYRESEARCH · SERVICE · COMMUNITY diff --git a/apps/web/public/seed/tig-308-simple.svg b/apps/web/public/seed/tig-308-simple.svg new file mode 100644 index 0000000..a9832d0 --- /dev/null +++ b/apps/web/public/seed/tig-308-simple.svg @@ -0,0 +1 @@ + diff --git a/apps/web/src/actions/org-boundaries.test.ts b/apps/web/src/actions/org-boundaries.test.ts index 83d8cad..1bb7b5d 100644 --- a/apps/web/src/actions/org-boundaries.test.ts +++ b/apps/web/src/actions/org-boundaries.test.ts @@ -11,6 +11,7 @@ async function runIsolated(source: string) { mock.module("@the-forum/database", () => ({ ...schema, ...orm, + loadOrganizationPage: async () => { throw new Error("Unexpected pagination query"); }, db: { select() { throw new Error("Invalid IDs must not reach the database"); } }, })); mock.module("~/auth", () => ({ auth: async () => ({ user: { id: "fixture" } }) })); @@ -59,3 +60,33 @@ test("upload action rejects organization logos and still signs avatar and flyer assert.ok(commands.every(command => command.Bucket === "fixture-bucket")); `); }); + +test("organization page discards oversized cursor strings instead of failing action validation", async () => { + await runIsolated(` + const calls = []; + mock.module("~/actions/orgs", () => ({ + getOrgs: async (params) => { + if (typeof params.cursor?.value === "string" && params.cursor.value.length > 255) { + throw new Error("Oversized cursor reached the action"); + } + calls.push(params); + return { items: [], total: 0, rangeStart: 0, rangeEnd: 0, previousCursor: null, nextCursor: null, hasCursor: false }; + }, + getBlockedOrgs: async () => [], + getRecommendedOrgs: async () => [], + })); + mock.module("~/components/layout/page-shell", () => ({ PageShell: () => null, PageHeading: () => null })); + mock.module("./src/app/(app)/orgs/orgs-client", () => ({ OrgsClient: () => null })); + const { default: Page } = await import("./src/app/(app)/orgs/page"); + const id = "ffffffff-ffff-ffff-ffff-ffffffffffff"; + for (const [sort, key] of [["name", "after_name"], ["newest", "after_created_at"]]) { + for (const length of [255, 256, 1000]) { + const value = "a".repeat(length); + await Page({ searchParams: Promise.resolve({ sort, after_id: id, direction: "next", [key]: value }) }); + assert.deepEqual(calls.at(-1).cursor, length <= 255 ? { id, direction: "next", value } : undefined); + } + } + await Page({ searchParams: Promise.resolve({ sort: "members", after_id: id, direction: "next", after_member_count: "42" }) }); + assert.deepEqual(calls.at(-1).cursor, { id, direction: "next", value: 42 }); + `); +}); diff --git a/apps/web/src/actions/orgs.ts b/apps/web/src/actions/orgs.ts index 998ddd5..79b4bde 100644 --- a/apps/web/src/actions/orgs.ts +++ b/apps/web/src/actions/orgs.ts @@ -2,6 +2,7 @@ import { events, + type OrganizationSort, and, campusLocations, db, @@ -10,6 +11,7 @@ import { eventTags, ilike, inArray, + loadOrganizationPage, or, orgBlocks, orgFollowers, @@ -38,6 +40,8 @@ export interface OrgListItem { logoUrl: string | null; category: string; followerCount: number; + /** Imported membership count; independent of Forum followers. */ + memberCount?: number | null; isFollowing: boolean; /** The viewer hid this org's events from their feed. */ isHidden: boolean; @@ -96,47 +100,49 @@ export interface OrgDetail { const getOrgsSchema = z.object({ search: z.string().trim().max(100).optional(), category: z.union([orgCategorySchema, z.literal("")]).optional(), + sort: z.enum(["name", "members", "newest"]).default("name"), + order: z.enum(["asc", "desc"]).optional(), + cursor: z + .object({ + id: idSchema, + direction: z.enum(["next", "previous"]), + value: z.union([z.string().max(255), z.number().finite()]), + }) + .optional(), }); +export interface OrgCursor { + id: string; + value: string | number; + direction: "next" | "previous"; +} + +export interface OrgPage { + items: OrgListItem[]; + total: number; + rangeStart: number; + rangeEnd: number; + previousCursor: OrgCursor | null; + nextCursor: OrgCursor | null; + hasCursor: boolean; +} + export async function getOrgs(params?: { search?: string; category?: string; -}): Promise { + sort?: OrganizationSort; + order?: "asc" | "desc"; + cursor?: OrgCursor; +}): Promise { const session = await auth(); if (!session?.user?.id) throw new Error("Unauthorized"); - const userId = session.user.id; const input = parseInput(getOrgsSchema, params ?? {}); - const conditions = []; + const page = await loadOrganizationPage({ ...input, category: input.category || undefined }); + if (page.items.length === 0) return { ...page, items: [] }; - if (input.search) { - const pattern = containsPattern(input.search); - conditions.push( - or(ilike(organizations.name, pattern), ilike(organizations.description, pattern)), - ); - } - - if (input.category) { - conditions.push(eq(organizations.category, input.category)); - } - - const orgs = await db - .select({ - id: organizations.id, - name: organizations.name, - description: organizations.description, - logoUrl: organizations.logoUrl, - category: organizations.category, - }) - .from(organizations) - .where(conditions.length > 0 ? and(...conditions) : undefined) - .orderBy(organizations.name); - - if (orgs.length === 0) return []; - - // Follower counts + the viewer's follow state: two grouped queries for the - // whole list, instead of two queries per org. - const orgIds = orgs.map((o) => o.id); + // Enrich only the rows on this page, never the full directory. + const orgIds = page.items.map((o) => o.id); const [countRows, myFollows, myHidden] = await Promise.all([ db .select({ orgId: orgFollowers.orgId, count: sql`count(*)::int` }) @@ -147,18 +153,23 @@ export async function getOrgs(params?: { .select({ orgId: orgFollowers.orgId }) .from(orgFollowers) .where(and(eq(orgFollowers.userId, userId), inArray(orgFollowers.orgId, orgIds))), - db.select({ orgId: orgBlocks.orgId }).from(orgBlocks).where(eq(orgBlocks.userId, userId)), + db + .select({ orgId: orgBlocks.orgId }) + .from(orgBlocks) + .where(and(eq(orgBlocks.userId, userId), inArray(orgBlocks.orgId, orgIds))), ]); const countByOrg = new Map(countRows.map((r) => [r.orgId, r.count])); const followed = new Set(myFollows.map((r) => r.orgId)); const hidden = new Set(myHidden.map((r) => r.orgId)); - - return orgs.map((org) => ({ - ...org, - followerCount: countByOrg.get(org.id) ?? 0, - isFollowing: followed.has(org.id), - isHidden: hidden.has(org.id), - })); + return { + ...page, + items: page.items.map((org) => ({ + ...org, + followerCount: countByOrg.get(org.id) ?? 0, + isFollowing: followed.has(org.id), + isHidden: hidden.has(org.id), + })), + }; } export async function getOrg(orgId: string): Promise { diff --git a/apps/web/src/app/(app)/orgs/orgs-client.tsx b/apps/web/src/app/(app)/orgs/orgs-client.tsx index 1f6e454..290a289 100644 --- a/apps/web/src/app/(app)/orgs/orgs-client.tsx +++ b/apps/web/src/app/(app)/orgs/orgs-client.tsx @@ -2,12 +2,14 @@ import { Check, Eye, EyeOff, Users } from "lucide-react"; import Link from "next/link"; -import { useCallback, useEffect, useRef, useState, useTransition } from "react"; +import { useRouter } from "next/navigation"; +import { useEffect, useRef, useState, useTransition } from "react"; import { toast } from "sonner"; import { type HiddenOrg, + type OrgCursor, type OrgListItem, - getOrgs, + type OrgPage, toggleFollowOrg, unblockOrg, } from "~/actions/orgs"; @@ -16,6 +18,13 @@ import { OrgAvatar } from "~/components/common/org-avatar"; import { SearchInput } from "~/components/common/search-input"; import { EmptyState } from "~/components/common/states"; import { Button } from "~/components/ui/button"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "~/components/ui/select"; import { formatTimeAgo } from "~/lib/date-format"; import { cn } from "~/lib/utils"; @@ -37,7 +46,11 @@ const ORG_CATEGORIES = [ const HIDDEN = "hidden"; interface OrgsClientProps { - initialOrgs: OrgListItem[]; + initialOrgs: OrgPage; + initialSearch: string; + initialCategory: string; + initialSort: "name" | "members" | "newest"; + initialOrder: "asc" | "desc"; recommendedOrgs: OrgListItem[]; /** Orgs the viewer hid, most recent first. */ initialHidden: HiddenOrg[]; @@ -47,16 +60,21 @@ interface OrgsClientProps { export function OrgsClient({ initialOrgs, + initialSearch, + initialCategory, + initialSort, + initialOrder, recommendedOrgs, initialHidden, showHidden = false, }: OrgsClientProps) { const [isPending, startTransition] = useTransition(); - const [orgs, setOrgs] = useState(initialOrgs); + const router = useRouter(); + const [orgs, setOrgs] = useState(initialOrgs.items); const [hidden, setHidden] = useState(initialHidden); - const [searchQuery, setSearchQuery] = useState(""); + const [searchQuery, setSearchQuery] = useState(initialSearch); const [activeCategory, setActiveCategory] = useState( - showHidden && initialHidden.length > 0 ? HIDDEN : "all", + showHidden ? HIDDEN : initialCategory || "all", ); const viewingHidden = activeCategory === HIDDEN; const hiddenChipRef = useRef(null); @@ -70,33 +88,75 @@ export function OrgsClient({ if (showHidden && row) row.scrollLeft = row.scrollWidth; }, [showHidden]); const searchTimeout = useRef>(null); + const resultsRef = useRef(null); + const movingPage = useRef(false); - const refreshOrgs = useCallback((search?: string, category?: string) => { - startTransition(async () => { - const result = await getOrgs({ - search: search || undefined, - category: category === "all" ? undefined : category, - }); - setOrgs(result); + useEffect(() => { + setOrgs(initialOrgs.items); + setHidden(initialHidden); + setActiveCategory(showHidden ? HIDDEN : initialCategory || "all"); + // Preserve newer typing while a debounced search is still waiting to submit. + if (!searchTimeout.current) setSearchQuery(initialSearch); + if (movingPage.current) { + resultsRef.current?.focus({ preventScroll: true }); + resultsRef.current?.scrollIntoView({ block: "start" }); + movingPage.current = false; + } + }, [initialOrgs, initialHidden, initialCategory, initialSearch, showHidden]); + + useEffect( + () => () => { + if (searchTimeout.current) clearTimeout(searchTimeout.current); + }, + [], + ); + + const navigate = ( + search: string, + category: string, + sort = initialSort, + cursor?: OrgCursor, + order = initialOrder, + ) => { + if (searchTimeout.current) clearTimeout(searchTimeout.current); + searchTimeout.current = null; + const params = new URLSearchParams(); + if (search.trim()) params.set("search", search.trim()); + if (category === HIDDEN) params.set("view", HIDDEN); + else if (category !== "all") params.set("category", category); + if (sort !== "name") params.set("sort", sort); + params.set("order", order); + if (cursor) { + params.set("direction", cursor.direction); + params.set("after_id", cursor.id); + const valueParam = + sort === "name" + ? "after_name" + : sort === "members" + ? "after_member_count" + : "after_created_at"; + params.set( + valueParam, + sort === "newest" ? String(cursor.value).replace(" ", "T") : String(cursor.value), + ); + } + movingPage.current = Boolean(cursor); + startTransition(() => { + router.push(`/orgs${params.size ? `?${params}` : ""}`, { scroll: false }); }); - }, []); + }; const handleSearch = (query: string) => { setSearchQuery(query); if (searchTimeout.current) clearTimeout(searchTimeout.current); - // Searching always searches every org — leave the Hidden list for it. const category = viewingHidden ? "all" : activeCategory; if (viewingHidden) setActiveCategory("all"); - searchTimeout.current = setTimeout(() => { - refreshOrgs(query, category); - }, 300); + searchTimeout.current = setTimeout(() => navigate(query, category), 300); }; - const handleCategoryChange = (cat: string) => { - setActiveCategory(cat); - if (cat !== HIDDEN) refreshOrgs(searchQuery, cat); + const handleCategoryChange = (category: string) => { + navigate(searchQuery, category); }; - const handleUnhide = (org: HiddenOrg) => { setHidden((prev) => prev.filter((o) => o.id !== org.id)); setOrgs((prev) => prev.map((o) => (o.id === org.id ? { ...o, isHidden: false } : o))); @@ -150,6 +210,7 @@ export function OrgsClient({ value={searchQuery} onChange={(e) => handleSearch(e.target.value)} className="flex-1" + maxLength={100} /> @@ -181,27 +242,87 @@ export function OrgsClient({ ) : ( <> - {recommendedOrgs.length > 0 && ( -

- - Suggested - - {recommendedOrgs.map((org, i) => ( - - - {org.name} - - {i < recommendedOrgs.length - 1 && · } + {!initialOrgs.previousCursor && + orgs.length > 0 && + !initialSearch && + !initialCategory && + recommendedOrgs.length > 0 && ( +

+ + Suggested - ))} -

- )} + {recommendedOrgs.map((org, i) => ( + + + {org.name} + + {i < recommendedOrgs.length - 1 && · } + + ))} +

+ )} +
+ + {isPending + ? "Loading organizations…" + : `${initialOrgs.rangeStart}–${initialOrgs.rangeEnd} of ${initialOrgs.total} organizations`} + + + +
{orgs.length > 0 ? (
    Hidden · )} - {org.category} · {org.followerCount}{" "} - {org.followerCount === 1 ? "follower" : "followers"} + {org.category} ·{" "} + {initialSort === "members" + ? org.memberCount == null + ? "Members unknown" + : `${org.memberCount} ${org.memberCount === 1 ? "member" : "members"}` + : `${org.followerCount} ${org.followerCount === 1 ? "follower" : "followers"}`} {org.description && ( · {org.description} )} @@ -253,14 +378,78 @@ export function OrgsClient({ ) : ( 0 ? ( + + ) : undefined + } + title={ + initialOrgs.hasCursor && initialOrgs.total > 0 + ? "This page is no longer available" + : "No organizations found" + } description={ - searchQuery || activeCategory !== "all" - ? "Try adjusting your search or filters." - : "Campus organizations will appear here as they are synced." + initialOrgs.hasCursor && initialOrgs.total > 0 + ? "This page is no longer available. Return to the first page to browse organizations." + : searchQuery || activeCategory !== "all" + ? "Try adjusting your search or filters." + : "Campus organizations will appear here as they are synced." } /> )} + + {(initialSearch || initialCategory) && ( + + )} )} diff --git a/apps/web/src/app/(app)/orgs/page.tsx b/apps/web/src/app/(app)/orgs/page.tsx index 5bfa778..a3d4642 100644 --- a/apps/web/src/app/(app)/orgs/page.tsx +++ b/apps/web/src/app/(app)/orgs/page.tsx @@ -1,6 +1,8 @@ +import { orgCategoryEnum } from "@the-forum/database"; import type { Metadata } from "next"; -import { getBlockedOrgs, getOrgs, getRecommendedOrgs } from "~/actions/orgs"; +import { type OrgCursor, getBlockedOrgs, getOrgs, getRecommendedOrgs } from "~/actions/orgs"; import { PageHeading, PageShell } from "~/components/layout/page-shell"; +import { idSchema } from "~/lib/validation"; import { OrgsClient } from "./orgs-client"; export const metadata: Metadata = { title: "Organizations" }; @@ -8,11 +10,39 @@ export const metadata: Metadata = { title: "Organizations" }; export default async function OrgsPage({ searchParams, }: { - searchParams: Promise<{ view?: string | string[] }>; + searchParams: Promise>; }) { - const { view } = await searchParams; + const params = await searchParams; + const single = (key: string) => (typeof params[key] === "string" ? (params[key] as string) : ""); + const search = single("search").trim().slice(0, 100); + const categoryValue = single("category"); + const category = orgCategoryEnum.enumValues.find((value) => value === categoryValue) ?? ""; + const sort = + single("sort") === "members" ? "members" : single("sort") === "newest" ? "newest" : "name"; + const afterId = single("after_id"); + const direction = single("direction"); + const valueParam = + sort === "name" ? "after_name" : sort === "members" ? "after_member_count" : "after_created_at"; + const rawValue = single(valueParam); + const value = sort === "members" ? Number(rawValue) : rawValue; + const cursor: OrgCursor | undefined = + idSchema.safeParse(afterId).success && + typeof params[valueParam] === "string" && + (typeof value === "number" ? Number.isFinite(value) : value.length <= 255) && + (direction === "next" || direction === "previous") + ? { id: afterId, direction, value } + : undefined; + const order = + single("order") === "asc" + ? "asc" + : single("order") === "desc" + ? "desc" + : sort === "name" + ? "asc" + : "desc"; + const view = single("view"); const [orgs, recommended, hidden] = await Promise.all([ - getOrgs(), + getOrgs({ search, category, sort, order, cursor }), getRecommendedOrgs(), getBlockedOrgs(), ]); @@ -22,6 +52,10 @@ export default async function OrgsPage({ Organizations