Files
Convivo/docs/architecture.md
00c89c1b48 feat(printer): badge print queue, Pi worker, captive-portal setup, admin printing view
- migration 20261018170000: pgmq badge_print/badge_print_dlq, print_stations,
  token-gated printer RPCs (claim/complete/fail/heartbeat), admin RPCs,
  welcome_checkin enqueue; badge_print_tests.sql (101 assertions)
- apps/printer: Python worker (Playwright render -> brother_ql via pyusb),
  comitup captive-portal (Wi-Fi + HTTPS endpoint + anon key + token),
  arm64 image build + idempotent Pi setup/install scripts, 77 pytest
- apps/admin: Printing view, station management/DLQ, global printer status
  pill, Print button on Registrations, standalone print renderer entry
  (print.html -> dist-print) with bundled fonts
- docs: printing.md + architecture/schema/rpcs/tests/admin-app updates,
  apps/printer/AGENT.md operating manual
- e2e: printing.spec.ts

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-18 22:25:38 +02:00

13 KiB

Convivo — Architecture

High-level system overview, tech stack, environment setup, and system-wide data flows. For component-level detail, see backend/README.md and frontend/README.md.

1. The product in one paragraph

Convivo is a mobile-first web app (PWA) for conference attendees. People log in with the QR code on their lanyard + the email they used to buy their ticket (no email is ever sent). They pick one session per slot, scan peers' lanyards to make connections, get checked in by staff, and rate sessions. Every action earns points, and a live leaderboard turns networking into a game.

2. System diagram

flowchart LR
    subgraph Browser["Browser"]
        A["Convivo PWA<br/>React 18 + Vite 5 + TS<br/>mobile-first, installable"]
        AD["APPE Admin<br/>React 18 + Vite 5 + TS<br/>desktop-first console"]
    end

    subgraph Docker["Docker (local)"]
        subgraph AppC["convivo-app container (:3000)"]
            N["nginx<br/>serves SPA + proxies Supabase paths"]
        end
        subgraph AdminC["convivo-admin container (:3001)"]
            NA["nginx<br/>serves SPA + proxies Supabase paths"]
        end
        subgraph SideC["convivo-sidecar container (:3002)"]
            SC["node<br/>Sessionize sync (on demand)"]
        end
        subgraph Core["Supabase core (Supabase CLI)"]
            K["Kong API gateway<br/>:54321"]
            K --> G["GoTrue (Auth)"]
            K --> R["PostgREST"]
            K --> RT["Realtime"]
            K --> S["Storage"]
            R --> P[("Postgres 17<br/>schema · RLS · points RPCs")]
            RT --> P
            G --> P
        end
    end

    A -->|"same-origin /auth /rest /realtime /storage /functions"| N
    AD -->|"same-origin /auth /rest /realtime /storage /functions"| NA
    AD -->|"same-origin /sidecar/*"| SC
    N -->|"proxy (supabase_kong_Convivo:8000)"| K
    NA -->|"proxy (supabase_kong_Convivo:8000)"| K
    SC -->|"service-role key"| K

    subgraph Edge["Event venue"]
        PI["Raspberry Pi print station<br/>Docker: Python worker +<br/>headless Chromium + Brother QL"]
    end
    PI -->|"token-gated printer RPCs<br/>(anon key + station token)"| K
    P -->|"badge_print pgmq queue<br/>(enqueue on check-in)"| PI

The sidecar (:3002) is the one server-side component that holds the service-role key. It is driven by the admin's Interfaces menu and runs on demand only — see backend/sidecar.md.

The Raspberry Pi print station is the one piece of on-premise hardware: when staff welcome-check-in an attendee, the badge is pushed onto a pgmq queue in Postgres and the Pi long-polls token-gated RPCs to render and print it. No custom backend service sits in between — see backend/printing.md.

Key idea: there is no custom backend. Supabase provides auth, data, business logic (as Postgres RPCs), and realtime. The app is a thin client that talks to Supabase through same-origin paths, so there is no CORS to manage.

3. Tech stack

Layer Choice Why
Core Supabase (local, Docker) via the Supabase CLI Postgres + Auth + REST + Realtime + Storage in one managed stack; RLS + RPCs keep all logic in the DB
App React 18 + Vite 5 + TypeScript (PWA) Fast, mobile-first, installable; same-origin proxy avoids CORS
QR html5-qrcode (scan) + qrcode.react (render) Camera scan with manual-entry fallback
Client @supabase/supabase-js Auth + REST + Realtime in one client
Packaging Docker (multi-stage build → nginx) The app is a sidecar that joins the Supabase network

4. The two halves

Backend — the Supabase core

Everything server-side lives in Postgres. See backend/README.md:

  • Schema — 13 tables (people, the event, participation, schedule, and the event's activity). → backend/schema.md
  • Business logic — transactional security definer RPCs that award points and keep the leaderboard consistent. → backend/rpcs.md
  • Security — Row Level Security on every table; the app only ever uses the anon key + the user's session JWT. → backend/rls.md
  • Data — seed with 1 real event (Power Platform Bootcamp AT): 10 slots, 22 sessions, 54 contacts (50 attendees + 4 staff). Speakers are added via the Excel import, not the seed. → backend/seed.md

Frontend — the attendee PWA

A single-page React app with a tab shell and a few full-screen takeovers. See frontend/README.md:

Frontend — the admin console (APPE Admin)

A separate desktop-first React app for organizers, guarded to existing contacts with is_admin or an @appe.at email. See frontend/admin-app.md:

  • Views — Dashboard (active event + aggregates), Contacts, Registrations, Sponsors, Schedule (slots/sessions/speakers/material), Activity (manual event and session AI summaries), Raffle (attendee prize draws), Badges (per-event WYSIWYG badge layout editor), Printing (badge-print stations + queue), Events, Interfaces, Reports and Settings.
  • Auth guard — email-only magic links; non-admin sessions are rejected.
  • Key RPCs — reset_registration_qr_token (regenerate a registration's lanyard token + sync the Auth password), adjust_points, upsert_contact, create_registration, set_registration_type, set_session_material, and the raffle functions get_raffle_pool / draw_raffle_winner / reset_raffle, the badge-layout functions save_badge_layout / set_default_badge_layout / delete_badge_layout, and the badge-print functions get_print_overview / enqueue_badge_print / create_print_station (see backend/rpcs.md). The deprecated AI stub is no longer client-callable; generation uses authenticated sidecar routes.

5. How a request flows (example: staff check-in)

sequenceDiagram
    participant U as Staff (PWA)
    participant N as nginx (app container)
    participant K as Kong
    participant R as PostgREST
    participant DB as Postgres (RPC)

    U->>N: POST /rest/v1/rpc/checkin_attendee {session, attendeeQr}
    N->>K: proxy (same-origin)
    K->>R: route /rest
    R->>DB: execute checkin_attendee() as `authenticated`
    Note over DB: security definer → bypasses RLS<br/>1. insert checkin (ON CONFLICT do nothing)<br/>2. _award_points(+10) → registrations.points + points_log<br/>3. _sync_leaderboard()
    DB-->>R: {checkin_id, attendee_name, points, already}
    R-->>K: JSON
    K-->>N: JSON
    N-->>U: result sheet ("+10 pts · now 62")
    Note over DB: leaderboard row changed → Realtime broadcasts to all open apps

The same pattern applies to every mutation: the app calls an RPC, Postgres does the work atomically, and the leaderboard table change fans out over Realtime.

6. Authentication model

  • Attendees: the QR token is the Supabase Auth password; the email is the identifier. Login is a standard signInWithPassword({ email, password: qrToken }).
  • Staff: log in with QR + their admin password (event-independent). In the Convivo app, scanning a staff QR shows a password field (not the email field); the email is taken from the resolved contact. The password is recorded in contacts.password; a trigger (trg_contact_password_sync) auto-syncs it as the bcrypt hash in auth.users.encrypted_password, so setting it in the Supabase Table Editor makes it work with signInWithPassword immediately. (Staff can also still log in with their email + QR token, their seeded Auth password.)
  • @appe.at admins: any contact on the @appe.at domain is an admin (is_admin() is domain-aware). The Admin console uses only email magic links, without password login or a password-change prompt. The trg_contacts_appe_at_admin trigger sets contacts.password to the default (when null) and the auth-user creation helpers hash it into auth.users.encrypted_password; these existing passwords and shared password operations remain available for Convivo staff login. A custom password set on the contact row is always respected.
  • Normal attendee/staff QR login sends no email. enable_confirmations = false disables signup confirmation, not explicit Admin/speaker magic-link requests.
  • After login, the app holds a session JWT (in localStorage) that lasts 24 hours (jwt_expiry = 86400). Every request sends the anon key (public) + the session JWT (identity).
  • The service-role key is never in the app — it bypasses RLS and stays server-side. The sidecar is its first consumer (the Sessionize sync). See backend/rls.md for what each role can do and backend/sidecar.md for the sidecar.

7. Environment & ports

Service Port Notes
Convivo app 3000 Vite dev server (dev) or nginx (Docker)
Convivo admin 3001 Vite dev server (dev) or nginx (Docker)
Sync sidecar 3002 Node service (Docker) or npm run sidecar:dev (dev)
Supabase API (Kong) 54321 The single gateway the apps proxy to
Supabase Studio 54323 UI — login postgres / postgres
Postgres 54322 Direct DB access (postgres / postgres)
SMTP (Mailpit) 54324 Captures emails (none are sent in this flow)

Run it (full instructions in the root README.md):

npm run db:start          # start the Supabase core (Docker)
npm run db:reset          # (re)apply migrations + seed
npm run app:dev           # attendee dev server on :3000
npm run admin:dev         # admin dev server on :3001
npm run sidecar:dev       # Sessionize sync sidecar on :3002
# — or —
docker compose up --build # apps + sidecar as Docker sidecars (:3000 + :3001 + :3002)

8. The demo clock

The app uses a simulated "now" of 2026-10-23 10:20 (see frontend/app-structure.md) so that Slot 1 is done and Slot 2 is live — matching the mockup. This is a frontend concern only; the database stores real slot times.

9. Repository layout

Convivo/
├─ apps/convivo/          # Attendee web PWA (React + Vite + TS)
│  ├─ Dockerfile          # Multi-stage: build → nginx
│  ├─ nginx.conf.template # SPA + same-origin proxy to the Supabase core
│  └─ src/                # screens, components, lib, context
├─ apps/admin/            # Admin console (React + Vite + TS, desktop-first)
│  ├─ Dockerfile          # Multi-stage: build → nginx
│  ├─ nginx.conf.template # SPA + same-origin proxy to the Supabase core + /sidecar
│  └─ src/                # views, components, lib, context
├─ apps/sidecar/          # Sessionize sync sidecar (Node, on demand)
│  ├─ Dockerfile          # node:20-alpine + xlsx (parses the Excel export)
│  └─ src/server.js       # /health, /status, /run, /import-excel (blocking)
├─ apps/printer/          # Raspberry Pi badge-print worker (Python + Docker)
│  ├─ Dockerfile          # repo-root context: node print-renderer stage + Playwright Chromium runtime
│  ├─ worker/             # claim → render (headless Chromium) → compose → print (Brother QL) → report
│  └─ pi/                 # setup.sh / install.sh: Pi provisioning (Docker, comitup Wi-Fi portal)
│                         # → backend/printing.md
├─ supabase/
│  ├─ config.toml         # Local stack config (ports, auth, RLS defaults)
│  ├─ migrations/
│  │  ├─ 20260822000000_init.sql     # original schema (replaced by the Redesign)
│  │  ├─ 20260823000000_admin_app.sql # admin read RLS + reset_qr_token (superseded)
│  │  ├─ 20260824000000_redesign.sql  # multi-event model, RPCs, RLS, materials
│  │  ├─ 20260825000000_interfaces.sql # external_id (sync objects later dropped)
│  │  ├─ 20260826000000_registration_qr_token.sql # QR token on registration
│  │  ├─ 20260827000000_admin_password_profile.sql # staff password + self-service profile
│  │  ├─ 20260828000000_staff_exclusions_password_sync.sql # staff leaderboard exclusion + password sync
│  │  ├─ 20260829000000_registration_type_optionset_speaker_promote_welcome_checkin.sql
│  │  ├─ 20260830000000_remove_interfaces.sql # ← drops the sync RPCs + sync_runs
│  │  ├─ 20260831000000_sessionize_sidecar.sql # sessionize_endpoint + sync_runs + sync RPC
│  │  └─ 20260832000000_slots_enum_excel_import.sql # slot_type enum + slots.id default + Excel RPC
│  └─ seed.sql            # 1 real event, 10 slots, 22 sessions, 54 contacts (50 attendees + 4 staff)
├─ docker-compose.yml     # Runs the app sidecars + sync sidecar on the Supabase network
├─ docs/                  # ← this documentation
└─ mockup/index.html      # Original interactive mockup (design reference)