Skip to main content

Architecture

Rendered from corpus/wiki/architecture.md · updated 2026-11-02

This page is generated from the project corpus and rewritten on every docs build. Edit the source in corpus/, not here.

Workspace layout​

npm workspaces with three code packages, plus docs/ (the Docusaurus site); dependency direction is strictly:

ui ──► shared ◄── backend

shared's one runtime dependency is Zod, and it compiles to ESM with declaration files. ui and backend each depend on it via the npm workspace link ("*" version — resolved locally by npm).

UI (ui/)​

  • Framework: React Router 8 (SPA mode — ssr: false), basename from PRM_BASE (/prm/ in the deploy, / in dev)
  • Build: Vite 7 + @react-router/dev
  • Styling: Tailwind CSS 4 (Vite plugin) over hex tokens in app.css, mapped to --color-*; Archivo from Google Fonts
  • Components: @base-ui/react primitives + lucide-react icons (the original Stitch brief specified shadcn/ui; replaced when it was built — see decisions-engineering.md → Code conventions)
  • Map: Leaflet 1.9 + react-leaflet 5; the whole city is loaded once and clustered client-side
  • Data: TanStack Query for server state, Zustand for city/filter/auth state
  • Language: Romanian by default, English switch, in lib/i18n.tsx (no framework)
  • Theme: dark/light/system — persisted to localStorage as "vite-ui-theme", toggled via ThemeProvider context
  • Sign-in: none in this app. Login, register and account are full navigations to Ward (/ward/login, /ward/register?app=prm, /ward/account), built in lib/authApi.ts
  • Alias: ~/* and @/* both resolve to ./app/*

Routes (app/routes.ts)​

PathModuleWhat
/routes/map.tsxthe map; home
/places/:idroutes/place.tsxchild of /, so the map stays mounted under the place panel
/whats-onroutes/whats-on.tsxthe city's upcoming events, on the map's filters
/archiveroutes/archive.tsxpast events, citywide or mine
/about-dataroutes/about-data.tsxsources, licences, takedown contact
/map, /events, /resources/:idroutes/legacy.*.tsxold URLs; a clientLoader redirects to /, /whats-on, /places/:id
/admin, /admin/sourcesroutes/admin/own shell (AdminLayout), code-split from the public pages

Current UI file map​

app/
root.tsx HTML shell (lang="ro"), font links, error boundary
routes.ts route config (table above)
app.css Tailwind + design tokens, both themes
components/
Layout.tsx AppProviders (Query, i18n, theme) + Navbar + main outlet + NoticeToast
Navbar.tsx top links / phone bottom tabs, city picker, bell, account menu (Ward links)
FavoriteStar.tsx follow a place / remind me of an event; signed out, offers Ward sign-in
ThemeProvider.tsx context + localStorage persistence
ThemeToggle.tsx light / dark / system
map/ PlaceMarkers (pins + clusters), FilterBar (chips, lens, draw entry),
AreaDraw (freehand + polygon), UserLocationMarker
place/ PlaceDetail (the one place view), PlacePanel (desktop), PlaceSheet (phone),
EventList, EventPlaceRow (what's-on and archive rows)
shell/ Bell (inbox), BrandMark, CityPicker, LangToggle, NoticeToast
ui/ Avatar, Button, CategoryBadge, Chip, DropdownMenu, SearchInput,
Segmented (timing lens), Skeleton, StateBlock (empty / zero results / error)
hooks/ usePlaces (+ usePlace, usePlaceEvents), useFavorites (+ inbox),
useArchive, useAdmin, useIsMobile, useUserLocation
lib/
api.ts typed fetch for the public API (whole-city places, what's-on, archive)
adminApi.ts admin routes
meApi.ts favourites + inbox
authApi.ts fetchMe (/api/me) and the Ward URLs
i18n.tsx RO/EN strings, Intl plurals and dates
cities.ts the two cities: id, name (= place.city), centre, zoom
categories.tsx category icon paths + hues, shared by the panel and the Leaflet pins
cluster.ts screen-space grid clustering
area.ts point-in-drawn-area
dates.ts Bucharest calendar days and the day groups
map.ts basemap tiles (CARTO with a key, filtered OSM without)
queryClient.ts, LocalStorage.ts, utils.ts (cn, normalizeText)
stores/
appStore.ts Zustand — city, chips, lens, drawn area: one store for map + what's-on
authStore.ts Zustand — who is signed in, from /api/me
locationStore.ts Zustand — geolocation, default centre
routes/
map.tsx full-bleed map, filter bar, zero-results chips; outlet for places/:id
place.tsx place panel/sheet over the live map; a cold visit centres on the place
whats-on.tsx citywide, day-grouped list
archive.tsx month-grouped past events, paged
about-data.tsx attribution and the data posture
redirects.tsx the redirects behind legacy.map / legacy.events / legacy.resource
admin/ AdminLayout, review.tsx (staged-event queue), sources.tsx

Backend (backend/)​

  • Server: Fastify 5 with @fastify/cors (credentials: true, so Ward's cookie arrives)
  • Database: SQLite via better-sqlite3 + Drizzle ORM
  • Validation: Zod (request bodies and query strings)
  • Identity: Ward; jose verifies its tokens locally (ward/)
  • DB file: backend/data/app.db (gitignored), or DATABASE_PATH, which the deploy points outside the rsynced tree
  • Migrations: Drizzle Kit → backend/drizzle/, applied with npm run db:migrate (drizzle-orm's migrator via tsx). Five: 0000 the consolidated place-centric schema (the schema consolidation), 0001 the Ward cutover (hand-written: drops the user tables, rebuilds the per-person ones empty and keyed on subject), 0002 ingestion keys on staged_event (the event ingestion pipeline), 0003 reconciles 0001 with schema.ts (the 0001 reconciliation), 0004 place.retired_at (OSM retirement). migrations.test.ts fails when the migrated database drifts from schema.ts.

Backend modules (src/)​

index.ts listen on PORT (3001); start the daily reminder sweep
app.ts buildApp(): CORS, Ward wiring, the mail sweeper, the route plugins under /api
routes/ places, events, whats-on, archive, favorites (+ inbox), me, admin-osm,
admin-ingest; event-mapper + event-window shared by the read routes
ward/ client (JWT verify + introspection, 30 s cache), plugin (request.ward,
requireAuth, requireAdmin), config, wire types
ingest/ pipeline (fetch → dedup → match → geocode → reconcile → staged diff),
adapters, iCal reader, venue matching, Nominatim geocoder
lib/ osm-sync (Overpass, city registry, retiring), osm-categories, notify (new-event
items at accept), notify-mail (mail via Ward), geo, time, fk
jobs/ reminder-sweep: day-before reminders at 09:00 Bucharest
db/ schema, index (createDb), migrate, seed (+ seed-data, seed-ids), capture-osm, fixtures/
test/ harness (in-memory DB + injected Ward), fake-ward, real-ward

API routes​

MethodPathDescription
GET/healthliveness check
GET/api/placesa city's places (or a bbox), category chips + timing lens, each with upcomingEventCount (public)
GET/api/places/:idsingle place (public)
GET/api/places/:id/eventswhat is on at a place in the lens window (public)
POST · DELETE/api/places, /api/places/:idcreate / delete a place — admin only (requireAdmin); 409 PLACE_IN_USE while anything references it
GET/api/eventsevents near a point (+ from/to date filters) (public)
GET/api/events/:idsingle event (public)
POST · DELETE/api/events, /api/events/:idcreate (publishes straight to live) / delete an event — admin only; 409 EVENT_IN_USE while referenced
GET/api/whats-onthe city's upcoming events with their places, on the map's filters (public)
GET/api/archivepast events citywide, newest first (public)
GET/api/archive/minepast events you saved or at places you follow — signed in
GET/api/methe signed-in Ward caller, narrowed
GET/api/favoritesyour followed places and saved events — signed in
POST · DELETE/api/favorites/places/:placeId, /api/favorites/events/:eventIdfollow / unfollow, idempotent — signed in
GET · POST/api/notifications, /api/notifications/readthe inbox; mark items (or all) read — signed in
POST/api/admin/osm/syncOSM import for a city — admin only; 409 while that city is syncing, 504 when Overpass does not answer in 100 s (the watch-list hardening)
GET · POST · PATCH/api/admin/sources, /api/admin/sources/:idlist / add / enable-disable event sources — admin only
POST/api/admin/sources/:id/refresh, /api/admin/sources/refresh-allrun one source, or every enabled one in turn, into a staged diff — admin only
GET/api/admin/staged-eventsthe review queue — admin only
POST/api/admin/staged-events/accept, …/reject, …/:id/placeaccept (goes live, notifies followers, kicks mail), reject, or set a staged event's place by hand — admin only

"Signed in" is requireAuth; "admin only" is requireAdmin (401 anonymous, 403 without prm:admin). Ward is asked only by those two guards and /api/me, through resolveWard, at most once per request. A public route never asks, even when a ward_session cookie comes in (Ward-only-where-needed).

Public place reads skip retired places. Proximity filtering uses a bounding-box approximation (not Haversine). Good enough for city-scale use.

Data layer — place-centric schema (the schema consolidation, then Ward)​

One Drizzle schema (backend/src/db/schema.ts). It was consolidated on 2026-07-02; the Ward cutover (2026-09-06) then dropped user, session, verification_token and reset_token. prm holds no credential, and the per-person rows are keyed on Ward's opaque subject with no foreign key. The table it would point at does not exist here. Conventions: text PK defaulting to randomUUID() (except notification_event, keyed on its pair); UTC ISO 8601 string dates (created/updated default to datetime('now')); no universal soft-delete — lifecycle via status enums and place.retiredAt; two category taxonomies (PlaceCategory, EventCategory); an index on every FK plus the spatial/natural-key indexes below.

9 tables:

TableOwner briefKey columnsNatural-key uniques / notable indexesFKs
place03source (osm|event-venue), osmType/osmId, isManualPin, category (PlaceCategory), city, lat/lng, address/website/phone/openingHours, retiredAt (OSM retirement)unique (osmType,osmId) where source='osm' (partial); index (lat,lng); index city—
event04placeId, title, normalizedTitle, category (EventCategory), status (live|stale|ended|past), startDate/endDate, buyUrl, source/price fieldsindex placeId; index status; dedup index (normalizedTitle,startDate,placeId)place
event_source04adapterKey, mechanism, url, city, enabled, health fields (lastStatus/lastEventCount/lastSuccessfulAt/lastRunAt)unique adapterKey—
staged_event04sourceId, nullable placeId, nullable eventId, matchStatus (auto-matched|ambiguous|unmatched|manual), status (new|changed|accepted|rejected|needs-attention), venueName, rawAddress, candidates (JSON), payload (JSON), externalKey, geocoded lat/lng, lastSeenAtindex sourceId, (sourceId,externalKey), placeId, eventId, statusevent_source, place, event
geocode_cache04normalizedAddress, city, nullable lat/lng (null = cached miss), importance, granularity, raw (JSON)unique normalizedAddress—
favorite_place05subject, placeIdunique (subject,placeId); indexes on bothplace
favorite_event05subject, eventIdunique (subject,eventId); indexes on bothevent
notification05subject, kind (new-event|reminder), nullable placeId/eventId, batchId (coalesces a new-event batch), readAt, emailedAtunique (subject,eventId,kind) (reminder idempotency; new-event rows have null eventId)place, event
notification_event05, 29join: notificationId, eventIdPK (notificationId,eventId); index eventId; deleted with its notificationnotification, event

Lifecycle: an event is never hard-deleted when it ends — it flips status to past (powers the archive page); OSM re-sync upserts only source='osm' places (never clobbers event-venue or manual pins) and sets retiredAt on the ones OSM no longer has (OSM retirement); new-event notifications are coalesced per (place, batch) and fan out via notification_event, reminders are per-event. Each row is mailed once through Ward's POST /notify by lib/notify-mail.ts, kicked after an accept and after the daily reminder run; emailedAt marks it (notification email).

Shared (shared/)​

Zod schemas + types derived via z.infer (not pure types — it has runtime code: the Zod schemas; backend imports them so shape + validation share one source of truth). Seven modules, re-exported from src/index.ts:

  • types/common.ts — coordinatesSchema/Coordinates, httpUrlSchema (http(s) only), PaginatedResponse<T>, ApiError, nearbyQuerySchema/NearbyQuery, the timing lens (eventLenses: today/weekend/all), parseCsv, and the query schemas for /api/places, /api/places/:id/events and /api/whats-on
  • types/place.ts — placeSchema/Place, placeCategorySchema/PlaceCategory (park/library/clinic/museum/townhall/community_center/education/theater/sports/cultural_center/other), placeSourceSchema/PlaceSource (osm|event-venue), createPlaceSchema/CreatePlaceInput. Place carries source, osmType/osmId, isManualPin, nullable address, city, coordinates.
  • types/event.ts — eventSchema/Event, eventCategorySchema/EventCategory (concert/theater/sport/community/festival/exhibition/workshop/other), eventStatusSchema/EventStatus (live/stale/ended/past), createEventSchema/CreateEventInput. Event has placeId, status, nullable buyUrl; no standalone address/coordinates (they live on the Place).
  • types/whats-on.ts — WhatsOnItem (an event with its place), archiveQuerySchema
  • types/ingest.ts — sources (SourceMechanism, createSourceSchema, health), RawEvent, staged-event statuses and match statuses, the admin DTOs
  • types/osm.ts — osmSyncRequestSchema/osmSyncResultSchema
  • types/notifications.ts — FavoritesDto, NotificationDto/InboxDto, markReadSchema

Imports use .js suffixes (Node ESM). Compiled with composite: true so downstream packages can use TypeScript project references.