# cb-proyecto-sistemas-2026/pr2-26-totem architecture

> Architecture diagram of the GitHub repository cb-proyecto-sistemas-2026/pr2-26-totem, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cb-proyecto-sistemas-2026/pr2-26-totem
- Repository: https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem

## Overview

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: [`backend/src/routes/auth.routes.js`](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/routes/auth.routes.js) (Route Layer), [`backend/database/models/index.js`](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/database/models/index.js) (Sequelize Models), [`frontend/src/routes/AppRouter.tsx`](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/routes/AppRouter.tsx) (App Router), [`frontend/src/pages/client/TotemClientPage.tsx`](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/pages/client/TotemClientPage.tsx) (Totem Client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_router["App Router<br/>routing/access<br/>[AppRouter.tsx]"]
  node_frontend_auth(("Auth State<br/>session/context<br/>[AuthContext.tsx]"))
  node_frontend_admin["Admin UI<br/>feature shell<br/>[DashboardPage.tsx]"]
  node_frontend_client["Totem Client<br/>client UI"]
  node_frontend_api["API Layer<br/>HTTP client<br/>[api.ts]"]
  node_frontend_realtime(("Realtime Client<br/>socket client"))
end

subgraph group_backend["Backend"]
  node_backend_server["Express Server<br/>http runtime<br/>[server.js]"]
  node_backend_auth["Auth Flow<br/>session auth<br/>[auth.service.js]"]
  node_backend_users["User Mgmt<br/>admin domain<br/>[users.service.js]"]
  node_backend_content["Content Pipeline<br/>media domain<br/>[content.service.js]"]
  node_backend_notifications["Notifications<br/>delivery domain"]
  node_backend_totems["Totem Mgmt<br/>device domain<br/>[totem.service.js]"]
  node_backend_client["Client Session<br/>device session"]
  node_backend_questions["Question Mode<br/>pdf/qna"]
  node_backend_realtime(("Realtime Hub<br/>socket service"))
  node_backend_routes["Route Layer<br/>http endpoints<br/>[auth.routes.js]"]
end

subgraph group_data["Data"]
  node_backend_models[("Sequelize Models<br/>persistence models<br/>[index.js]")]
  node_backend_migrations["Migrations<br/>schema evolution"]
  node_snapshot_schema["SQL Snapshot<br/>schema snapshot<br/>[01_schema.sql]"]
end

subgraph group_external["External"]
  node_postgres[("PostgreSQL<br/>primary datastore")]
  node_storage[("File Storage<br/>object storage")]
  node_smtp["SMTP<br/>email service"]
end

node_frontend_router -->|"guards"| node_frontend_auth
node_frontend_router -->|"routes to"| node_frontend_admin
node_frontend_router -->|"routes to"| node_frontend_client
node_frontend_admin -->|"calls"| node_frontend_api
node_frontend_client -->|"calls"| node_frontend_api
node_frontend_client -->|"syncs"| node_frontend_realtime
node_frontend_api -->|"http"| node_backend_server
node_frontend_realtime -->|"socket"| node_backend_realtime
node_backend_server -->|"mounts"| node_backend_routes
node_backend_routes -->|"delegates"| node_backend_auth
node_backend_routes -->|"delegates"| node_backend_users
node_backend_routes -->|"delegates"| node_backend_content
node_backend_routes -->|"delegates"| node_backend_notifications
node_backend_routes -->|"delegates"| node_backend_totems
node_backend_routes -->|"delegates"| node_backend_client
node_backend_routes -->|"delegates"| node_backend_questions
node_backend_auth -->|"emails"| node_smtp
node_backend_content -->|"files"| node_storage
node_backend_questions -->|"question images"| node_storage
node_backend_server -->|"reads/writes"| node_postgres
node_backend_models -->|"maps to"| node_postgres
node_backend_migrations -->|"evolves"| node_postgres
node_snapshot_schema -.->|"snapshot of"| node_postgres
node_backend_content -->|"persists via"| node_backend_models
node_backend_notifications -->|"persists via"| node_backend_models
node_backend_client -->|"persists via"| node_backend_models
node_backend_questions -->|"persists via"| node_backend_models

click node_frontend_router "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/routes/AppRouter.tsx"
click node_frontend_auth "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/context/AuthContext.tsx"
click node_frontend_admin "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/pages/DashboardPage.tsx"
click node_frontend_client "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/pages/client/TotemClientPage.tsx"
click node_frontend_api "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/services/api.ts"
click node_frontend_realtime "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/features/totemClient/services/totemClientRealtime.service.ts"
click node_backend_server "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/server.js"
click node_backend_auth "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/auth.service.js"
click node_backend_users "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/users.service.js"
click node_backend_content "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/content.service.js"
click node_backend_notifications "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/notification.service.js"
click node_backend_totems "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/totem.service.js"
click node_backend_client "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/totemClientSession.service.js"
click node_backend_questions "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/pdfDocumentIngestion.service.js"
click node_backend_realtime "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/totemClientRealtime.service.js"
click node_backend_routes "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/routes/auth.routes.js"
click node_backend_models "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/database/models/index.js"
click node_backend_migrations "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/database/migrations/20260309230558-create-totems.cjs"
click node_snapshot_schema "https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/database/01_schema.sql"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_router,node_frontend_auth,node_frontend_admin,node_frontend_client,node_frontend_api,node_frontend_realtime toneBlue
class node_backend_server,node_backend_auth,node_backend_users,node_backend_content,node_backend_notifications,node_backend_totems,node_backend_client,node_backend_questions,node_backend_realtime,node_backend_routes toneAmber
class node_backend_models,node_backend_migrations,node_snapshot_schema toneMint
class node_postgres,node_storage,node_smtp toneRose
```

## Components

### Frontend

React admin UI and public totem client

- [App Router](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/routes/AppRouter.tsx): routing/access · Top-level route composition and access control for admin and client surfaces · `frontend/src/routes/AppRouter.tsx`
- [Auth State](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/context/AuthContext.tsx): session/context · Global session state and guarded navigation for protected screens · `frontend/src/context/AuthContext.tsx`
- [Admin UI](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/pages/DashboardPage.tsx): feature shell · Admin panel pages and feature modules for users, campuses, totems, contents, notifications, and assignments · `frontend/src/pages/DashboardPage.tsx`
- [Totem Client](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/pages/client/TotemClientPage.tsx): client UI · Public client experience for linked devices, display panels, notifications, and question mode · `frontend/src/pages/client/TotemClientPage.tsx`
- [API Layer](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/services/api.ts): HTTP client · Shared frontend API services and request helpers used by feature modules · `frontend/src/services/api.ts`
- [Realtime Client](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/frontend/src/features/totemClient/services/totemClientRealtime.service.ts): socket 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](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/server.js): http runtime · Backend application entrypoint, middleware stack, and route mounting · `backend/src/server.js`
- [Auth Flow](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/auth.service.js): session auth · Session login, password reset, invitations, and role-aware access control · `backend/src/services/auth.service.js`
- [User Mgmt](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/users.service.js): admin domain · Admin users, invitations, and status lifecycle management · `backend/src/services/users.service.js`
- [Content Pipeline](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/content.service.js): media domain · Content CRUD, uploads, storage abstraction, and content-to-totem assignment · `backend/src/services/content.service.js`
- [Notifications](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/notification.service.js): delivery domain · Notification creation, campus/totem targeting, and soft-delete lifecycle · `backend/src/services/notification.service.js`
- [Totem Mgmt](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/totem.service.js): device domain · Totem registration, linking, and assignment boundary for physical devices · `backend/src/services/totem.service.js`
- [Client Session](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/totemClientSession.service.js): device session · Totem client authentication, heartbeat/state sync, and device-session lifecycle · `backend/src/services/totemClientSession.service.js`
- [Question Mode](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/pdfDocumentIngestion.service.js): pdf/qna · PDF ingestion, chunking, question search, answer flow, and question sessions · `backend/src/services/pdfDocumentIngestion.service.js`
- [Realtime Hub](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/services/totemClientRealtime.service.js): socket service · Backend realtime synchronization for client state, content, and notifications · `backend/src/services/totemClientRealtime.service.js`
- [Route Layer](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/src/routes/auth.routes.js): http endpoints · 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](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/database/models/index.js): persistence models · ORM models for campuses, users, contents, notifications, devices, sessions, and PDF artifacts · `backend/database/models/index.js`
- [Migrations](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/backend/database/migrations/20260309230558-create-totems.cjs): schema evolution · Canonical schema changes for users, devices, assignments, notifications, and question search · `backend/database/migrations/20260309230558-create-totems.cjs`
- [SQL Snapshot](https://github.com/cb-proyecto-sistemas-2026/pr2-26-totem/blob/main/database/01_schema.sql): schema 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**: primary datastore · Main relational database with full-text and trigram support
- **File Storage**: object storage · Local uploads or Supabase Storage for media and PDF question images
- **SMTP**: email service · Outbound email for invitations and password recovery

## Connections

- 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
