How cb-proyecto-sistemas-2026/pr2-26-totem works
1. Repo shape
This is a monorepo with two independently built apps: backend/ is an Express API and realtime server, and frontend/ is a React + TypeScript admin/client UI. A separate top-level database/ folder contains an SQL schema/seed snapshot, while the canonical app schema lives in Sequelize migrations under backend/database/.
2. Three user-facing surfaces
The system is split into: an admin panel at /admin, a public totem client at /client/totem, and the backend API under /api plus /client routes. The admin panel manages users, campuses, totems, content, notifications, and assignments; the client renders assigned media, reports device status, and enters question mode.
3. Backend request pipeline
The backend follows a clear layered flow: routes define endpoints and middleware, controllers handle HTTP shape, services contain business rules, repositories isolate data access, and Sequelize models/migrations define persistence. This separation is important because most features are implemented as a service/repository pair rather than directly in controllers.
4. Authentication and identity
Admin access uses session-based auth with express-session, HTTP-only cookies, rate-limited login/reset flows, password reset tokens, and user invitations. User accounts have role and status distinctions (Admin, SuperAdmin, active, inactive, invited), and the frontend reflects that through protected routes and role-gated screens.
5. Core business domains in the database
The main persisted entities are app_user, campuses, totems, contents, totem_contents, notifications, notification_targets, totem_device_sessions, and totem_question_sessions, plus PDF-specific tables like pdf_documents, pdf_chunks, and pdf_question_images. These tables show the project is centered on device registration, content assignment, notification targeting, and question-session state.
6. Content and file handling
Content is not just metadata; it includes uploaded assets for images, videos, ads, news, and PDFs. File validation and upload handling live in middleware/services, and storage is abstracted so the app can write either to local disk under backend/uploads or to Supabase Storage. Public file URLs are then consumed by the frontend and the client display.
7. Totem device lifecycle
The totem client is a separate operational flow from admin users. A device is linked using a generated code, receives access/refresh tokens, sends heartbeat and device-status reports, and can be unlinked. Device sessions are tracked in totem_device_sessions, which is the boundary between a physical screen and the platform.
8. Realtime synchronization
Socket.IO is used to keep the totem client synchronized with backend changes, especially for content, notifications, and client state updates. The frontend has dedicated client realtime services, and the backend has a matching realtime service layer, indicating this is not a purely polling-based system.
9. Question mode over PDFs
A distinctive subsystem ingests PDFs, extracts question/answer chunks, and supports answer search using PostgreSQL full-text search plus trigram similarity. The backend also stores question sessions and image attachments per question, while the client exposes question-mode entry/activity/exit and per-session question answering endpoints.
10. Notifications and campus scoping
Notifications are a separate domain from content assignment. They can target all devices, specific campuses, or selected totems, and there is dedicated refactoring around targets and soft deletion. This means notification delivery is stateful and scoped, not just a broadcast message table.
11. Frontend structure
The React app is organized by feature rather than by technical layer: features/ contains domain-specific UI, hooks, services, and validators for auth, campuses, contents, notifications, totems, totem contents, users, and the client screen. Shared UI lives in components/, routing and access control in routes/, and a global auth context coordinates session state.
12. Operational dependencies
PostgreSQL is the primary datastore and requires migrations, including trigram/FTS support for PDF search. SMTP is required for password recovery and invitations. The backend also depends on environment-driven CORS/session settings and a configurable public file base URL, so deployment boundaries matter to both the admin UI and the client.
13. Architecture-level boundaries to draw
For an architecture diagram, the important boundaries are: admin browser vs totem browser/device, frontend vs backend API, backend vs PostgreSQL, backend vs file storage, and backend vs SMTP/realtime channel. The totem client deserves special treatment because it has its own /client API surface, token lifecycle, heartbeat, and websocket relationship distinct from the admin panel.
Main entry points: auth.routes.js Route Layer · index.js Sequelize Models · AppRouter.tsx App Router · TotemClientPage.tsx Totem Client
Components 22
Frontend
React admin UI and public totem client
- App Router · Top-level route composition and access control for admin and client surfaces
frontend/src/routes/AppRouter.tsx - Auth State · Global session state and guarded navigation for protected screens
frontend/src/context/AuthContext.tsx - Admin UI · Admin panel pages and feature modules for users, campuses, totems, contents, notifications, and assignments
frontend/src/pages/DashboardPage.tsx - Totem Client · Public client experience for linked devices, display panels, notifications, and question mode
frontend/src/pages/client/TotemClientPage.tsx - API Layer · Shared frontend API services and request helpers used by feature modules
frontend/src/services/api.ts - Realtime Client · Frontend-side realtime integration for synchronizing the totem client
frontend/src/features/totemClient/services/totemClientRealtime.service.ts
Backend
Express API, realtime, and domain services
- Express Server · Backend application entrypoint, middleware stack, and route mounting
backend/src/server.js - Auth Flow · Session login, password reset, invitations, and role-aware access control
backend/src/services/auth.service.js - User Mgmt · Admin users, invitations, and status lifecycle management
backend/src/services/users.service.js - Content Pipeline · Content CRUD, uploads, storage abstraction, and content-to-totem assignment
backend/src/services/content.service.js - Notifications · Notification creation, campus/totem targeting, and soft-delete lifecycle
backend/src/services/notification.service.js - Totem Mgmt · Totem registration, linking, and assignment boundary for physical devices
backend/src/services/totem.service.js - Client Session · Totem client authentication, heartbeat/state sync, and device-session lifecycle
backend/src/services/totemClientSession.service.js - Question Mode · PDF ingestion, chunking, question search, answer flow, and question sessions
backend/src/services/pdfDocumentIngestion.service.js - Realtime Hub · Backend realtime synchronization for client state, content, and notifications
backend/src/services/totemClientRealtime.service.js - Route Layer · Endpoint grouping for auth, users, campuses, contents, notifications, totems, and client APIs
backend/src/routes/auth.routes.js
Data
Primary persistence and schema assets
- Sequelize Models · ORM models for campuses, users, contents, notifications, devices, sessions, and PDF artifacts
backend/database/models/index.js - Migrations · Canonical schema changes for users, devices, assignments, notifications, and question search
backend/database/migrations/20260309230558-create-totems.cjs - SQL Snapshot · Top-level SQL schema and seed snapshot used as a separate database reference
database/01_schema.sql
External
Runtime dependencies outside the repo
- PostgreSQL · Main relational database with full-text and trigram support
- File Storage · Local uploads or Supabase Storage for media and PDF question images
- SMTP · Outbound email for invitations and password recovery
Connections 27
- App Router → Auth State: guards · Protected and public-only navigation depends on session state
- App Router → Admin UI: routes to · Admin screens are organized behind the app router
- App Router → Totem Client: routes to · Public client screen is mounted from the shared router
- Admin UI → API Layer: calls · Admin feature pages use the shared API layer
- Totem Client → API Layer: calls · Client experience fetches device, content, and session data
- Totem Client → Realtime Client: syncs · Client screen listens for realtime state updates
- API Layer → Express Server: http · Frontend requests are served by the backend API
- Realtime Client → Realtime Hub: socket · Frontend and backend realtime services synchronize the client
- Express Server → Route Layer: mounts · The server composes the route surface
- Route Layer → Auth Flow: delegates · Auth routes invoke the session/login service flow
- Route Layer → User Mgmt: delegates · User management routes are handled by the users domain
- Route Layer → Content Pipeline: delegates · Content routes are handled by the content pipeline
- Route Layer → Notifications: delegates · Notification routes are handled by the notification domain
- Route Layer → Totem Mgmt: delegates · Totem management routes are handled by device services
- Route Layer → Client Session: delegates · Client-facing routes manage linking, heartbeat, and session state
- Route Layer → Question Mode: delegates · Question-mode routes and ingestion/search flow live here
- Auth Flow → SMTP: emails · Password recovery and invitations send email
- Content Pipeline → File Storage: files · Uploaded media and PDFs are written to storage
- Question Mode → File Storage: question images · PDF question images are persisted in storage
- Express Server → PostgreSQL: reads/writes · API services persist and query operational data in PostgreSQL
- Sequelize Models → PostgreSQL: maps to · ORM models represent the database tables
- Migrations → PostgreSQL: evolves · Migrations define and update the canonical schema
- SQL Snapshot → PostgreSQL: snapshot of · SQL snapshot documents the database structure
- Content Pipeline → Sequelize Models: persists via · Content logic is backed by Sequelize models
- Notifications → Sequelize Models: persists via · Notification logic uses the ORM layer for targets and status
- Client Session → Sequelize Models: persists via · Device sessions and client state are stored through models
- Question Mode → Sequelize Models: persists via · PDF documents, chunks, images, and question sessions use models