# carlosdb25/bookgym architecture

> Architecture diagram of the GitHub repository carlosdb25/bookgym, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/carlosdb25/bookgym
- Repository: https://github.com/carlosdb25/bookgym

## Overview

1. **Top-level shape**
   - The repository is a two-tier application: a **Node.js/Express backend** and a **React/Vite frontend**.
   - All business rules live in the backend; the frontend is a thin consumer of REST endpoints.
   - Persistence is centralized in **PostgreSQL** via **Prisma**.

2. **Backend runtime boundary**
   - `backend/src/server.js` is the process entrypoint.
   - `backend/src/app.js` assembles the Express app, mounts routes, middleware, and Swagger docs.
   - Shared infrastructure lives under `backend/src/shared`, including config, Prisma client, and request guards.

3. **Data layer and schema management**
   - Prisma is the sole data access layer (`backend/prisma/schema.prisma`, migrations, seed script).
   - The database is not just storage for bookings; it also holds operational rules in the `configuracion` table.
   - This makes reservation behavior configurable without changing the frontend.

4. **Auth and access control**
   - The backend has a dedicated `auth` module for login and token issuance.
   - `auth.middleware.js` enforces authenticated access, and `roles.middleware.js` separates student vs. admin capabilities.
   - Role checks are a hard boundary: admin-only endpoints are not just hidden in the UI, they are enforced server-side.

5. **Core booking domain**
   - `franjas` exposes the gym’s schedule/capacity view.
   - `reservas` is the transaction-critical module that creates, cancels, and lists reservations.
   - The key architectural point is that reservation creation must be atomic to prevent overbooking.

6. **Business rules as backend configuration**
   - Reservation constraints are read from runtime configuration keys such as:
     - `limite_reservas_activas`
     - `max_reservas_por_dia`
     - `anticipacion_reserva_min`
     - `anticipacion_cancelacion_min`
   - These rules are part of the backend’s decision flow, not client-side validation.
   - The UI can display them, but the backend is the source of truth.

7. **Admin analytics and operational views**
   - `metricas` provides aggregated weekly summaries for administrators.
   - These metrics are derived from reservation and schedule data, with cancelled or non-active slots excluded as described in the README.
   - `configuracion` likely acts as the admin-facing surface for inspecting or adjusting rule parameters.

8. **Background processing**
   - `backend/src/scheduler/noshow.scheduler.js` indicates a periodic background job inside the API service.
   - This scheduler is the non-request path in the architecture, used for time-based state changes such as no-show handling or closing past reservations.
   - It is part of the backend process rather than a separate worker service.

9. **Frontend structure**
   - The frontend is a single-page app built with **React + Vite**.
   - Pages are split by user intent: `Login`, `Disponibilidad`, `MisReservas`, and `AdminDashboard`.
   - Shared UI components (`FranjaCard`, `ReservaItem`, `SaturacionBadge`, `ActionModal`, `Toast`) encapsulate booking and feedback interactions.

10. **Frontend data access pattern**
    - API calls are centralized in `frontend/src/api/client.js`.
    - Feature hooks (`useAuth`, `useFranjas`, `useReservas`, `useMetricas`, `useReglasReserva`) isolate server interaction from components.
    - TanStack Query is used for fetching/cache synchronization, so the UI is reactive to backend state rather than maintaining its own source of truth.

11. **End-to-end student flow**
    - Student logs in through `/api/auth/login`.
    - Frontend fetches weekly availability from `/api/franjas/semana`.
    - User confirms a reservation in a modal; backend checks active limits, daily limits, time windows, suspension status, and available capacity.
    - If valid, the reservation is committed atomically and then appears in “Mis reservas”.

12. **End-to-end admin flow**
    - Admin logs in through the same auth path but receives admin-scoped access.
    - The dashboard reads weekly metrics and schedule saturation from backend endpoints.
    - Admin views are read-oriented: they observe occupancy and summary data, while the backend still enforces all operational rules.

13. **API documentation and operational endpoints**
    - Swagger/OpenAPI is integrated directly into the backend, not as a separate service.
    - The docs are mounted from `backend/src/docs/swagger.js` and exposed under `/api/docs` and `/api/docs.json`.
    - A `/health` endpoint exists for basic service monitoring.

14. **Deployment boundaries**
    - The repo is structured for separate deployment of backend and frontend, each with its own environment variables.
    - Backend needs `DATABASE_URL`, `JWT_SECRET`, and `PORT`; frontend needs `VITE_API_URL`.
    - Railway deployment is supported as two services: one API service rooted at `backend/`, and one web service rooted at `frontend/`.

Main entry points: [`backend/src/app.js`](https://github.com/carlosdb25/bookgym/blob/main/backend/src/app.js) (App), [`backend/src/shared/prisma/client.js`](https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/prisma/client.js) (Prisma), [`frontend/src/api/client.js`](https://github.com/carlosdb25/bookgym/blob/main/frontend/src/api/client.js) (API client), [`backend/src/modules/reservas/reservas.service.js`](https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/reservas/reservas.service.js) (Reservas)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_backend_server["Server<br/>entrypoint<br/>[server.js]"]
  node_backend_app["App<br/>express setup<br/>[app.js]"]
  node_backend_config["Config<br/>[index.js]"]
  node_backend_prisma["Prisma<br/>db client<br/>[client.js]"]
  node_backend_auth["Auth<br/>[auth.service.js]"]
  node_backend_guard_auth["Auth guard<br/>middleware<br/>[auth.middleware.js]"]
  node_backend_guard_roles["Roles guard<br/>middleware"]
  node_backend_franjas["Franjas<br/>[franjas.service.js]"]
  node_backend_reservas["Reservas"]
  node_backend_configuracion["Configuración"]
  node_backend_metricas["Métricas"]
  node_backend_scheduler["No-show job<br/>scheduler"]
  node_backend_swagger["Swagger<br/>docs<br/>[swagger.js]"]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["SPA<br/>react app<br/>[main.jsx]"]
  node_frontend_api["API client<br/>http client<br/>[client.js]"]
  node_frontend_hooks["Hooks<br/>[useReservas.js]"]
  node_frontend_pages["Pages<br/>views<br/>[Login.jsx]"]
  node_frontend_components["Components<br/>[ActionModal.jsx]"]
end

subgraph group_data["Data"]
  node_backend_schema["Schema<br/>[schema.prisma]"]
  node_backend_migrations["Migrations<br/>[migration.sql]"]
  node_backend_seed["Seed<br/>[seed.js]"]
end

node_backend_server -->|"starts"| node_backend_app
node_backend_app -->|"uses"| node_backend_guard_auth
node_backend_app -->|"uses"| node_backend_guard_roles
node_backend_app -->|"mounts"| node_backend_auth
node_backend_app -->|"mounts"| node_backend_franjas
node_backend_app -->|"mounts"| node_backend_reservas
node_backend_app -->|"mounts"| node_backend_configuracion
node_backend_app -->|"mounts"| node_backend_metricas
node_backend_app -->|"exposes"| node_backend_swagger
node_backend_app -->|"starts"| node_backend_scheduler
node_backend_auth -->|"reads/writes"| node_backend_prisma
node_backend_franjas -->|"reads"| node_backend_prisma
node_backend_reservas -->|"transactions"| node_backend_prisma
node_backend_reservas -->|"enforces"| node_backend_config
node_backend_configuracion -->|"reads/writes"| node_backend_prisma
node_backend_metricas -->|"aggregates"| node_backend_prisma
node_backend_scheduler -->|"updates"| node_backend_prisma
node_backend_prisma -->|"generated from"| node_backend_schema
node_backend_schema -->|"evolves via"| node_backend_migrations
node_backend_seed -->|"initializes"| node_backend_prisma
node_frontend_app -->|"calls"| node_frontend_api
node_frontend_app -->|"uses"| node_frontend_hooks
node_frontend_app -->|"renders"| node_frontend_pages
node_frontend_pages -->|"composes"| node_frontend_components
node_frontend_hooks -->|"wraps"| node_frontend_api
node_frontend_api -.->|"authenticates with"| node_backend_auth
node_frontend_api -.->|"fetches"| node_backend_franjas
node_frontend_api -.->|"fetches/mutates"| node_backend_reservas
node_frontend_api -.->|"fetches"| node_backend_metricas
node_frontend_api -.->|"fetches"| node_backend_configuracion

click node_backend_server "https://github.com/carlosdb25/bookgym/blob/main/backend/src/server.js"
click node_backend_app "https://github.com/carlosdb25/bookgym/blob/main/backend/src/app.js"
click node_backend_config "https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/config/index.js"
click node_backend_prisma "https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/prisma/client.js"
click node_backend_auth "https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/auth/auth.service.js"
click node_backend_guard_auth "https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/middlewares/auth.middleware.js"
click node_backend_guard_roles "https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/middlewares/roles.middleware.js"
click node_backend_franjas "https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/franjas/franjas.service.js"
click node_backend_reservas "https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/reservas/reservas.service.js"
click node_backend_configuracion "https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/configuracion/configuracion.service.js"
click node_backend_metricas "https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/metricas/metricas.service.js"
click node_backend_scheduler "https://github.com/carlosdb25/bookgym/blob/main/backend/src/scheduler/noshow.scheduler.js"
click node_backend_swagger "https://github.com/carlosdb25/bookgym/blob/main/backend/src/docs/swagger.js"
click node_backend_schema "https://github.com/carlosdb25/bookgym/blob/main/backend/prisma/schema.prisma"
click node_backend_migrations "https://github.com/carlosdb25/bookgym/blob/main/backend/prisma/migrations/20260325043000_init/migration.sql"
click node_backend_seed "https://github.com/carlosdb25/bookgym/blob/main/backend/prisma/seed.js"
click node_frontend_app "https://github.com/carlosdb25/bookgym/blob/main/frontend/src/main.jsx"
click node_frontend_api "https://github.com/carlosdb25/bookgym/blob/main/frontend/src/api/client.js"
click node_frontend_hooks "https://github.com/carlosdb25/bookgym/blob/main/frontend/src/hooks/useReservas.js"
click node_frontend_pages "https://github.com/carlosdb25/bookgym/blob/main/frontend/src/pages/Login.jsx"
click node_frontend_components "https://github.com/carlosdb25/bookgym/blob/main/frontend/src/components/ActionModal.jsx"

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_backend_server,node_backend_app,node_backend_config,node_backend_prisma,node_backend_auth,node_backend_guard_auth,node_backend_guard_roles,node_backend_franjas,node_backend_reservas,node_backend_configuracion,node_backend_metricas,node_backend_scheduler,node_backend_swagger toneBlue
class node_frontend_app,node_frontend_api,node_frontend_hooks,node_frontend_pages,node_frontend_components toneAmber
class node_backend_schema,node_backend_migrations,node_backend_seed toneMint
```

## Components

### Backend

Node/Express API, business rules, and persistence

- [Server](https://github.com/carlosdb25/bookgym/blob/main/backend/src/server.js): entrypoint · Boots the API process · `backend/src/server.js`
- [App](https://github.com/carlosdb25/bookgym/blob/main/backend/src/app.js): express setup · Assembles middleware, routes, and docs · `backend/src/app.js`
- [Config](https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/config/index.js): shared config · Loads runtime settings and environment variables · `backend/src/shared/config/index.js`
- [Prisma](https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/prisma/client.js): db client · Shared Prisma client used by backend modules · `backend/src/shared/prisma/client.js`
- [Auth](https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/auth/auth.service.js): module · Login and token issuance · `backend/src/modules/auth/auth.service.js`
- [Auth guard](https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/middlewares/auth.middleware.js): middleware · Enforces authenticated access · `backend/src/shared/middlewares/auth.middleware.js`
- [Roles guard](https://github.com/carlosdb25/bookgym/blob/main/backend/src/shared/middlewares/roles.middleware.js): middleware · Separates student and admin access · `backend/src/shared/middlewares/roles.middleware.js`
- [Franjas](https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/franjas/franjas.service.js): module · Weekly schedule and capacity view · `backend/src/modules/franjas/franjas.service.js`
- [Reservas](https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/reservas/reservas.service.js): module · Create, cancel, and list bookings atomically · `backend/src/modules/reservas/reservas.service.js`
- [Configuración](https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/configuracion/configuracion.service.js): module · Operational booking rules and admin-facing settings · `backend/src/modules/configuracion/configuracion.service.js`
- [Métricas](https://github.com/carlosdb25/bookgym/blob/main/backend/src/modules/metricas/metricas.service.js): module · Weekly admin summaries and occupancy aggregates · `backend/src/modules/metricas/metricas.service.js`
- [No-show job](https://github.com/carlosdb25/bookgym/blob/main/backend/src/scheduler/noshow.scheduler.js): scheduler · Periodic time-based reservation handling · `backend/src/scheduler/noshow.scheduler.js`
- [Swagger](https://github.com/carlosdb25/bookgym/blob/main/backend/src/docs/swagger.js): docs · OpenAPI docs mounted in the API · `backend/src/docs/swagger.js`

### Frontend

React/Vite SPA consuming the API

- [SPA](https://github.com/carlosdb25/bookgym/blob/main/frontend/src/main.jsx): react app · Vite entry and application shell · `frontend/src/main.jsx`
- [API client](https://github.com/carlosdb25/bookgym/blob/main/frontend/src/api/client.js): http client · Centralized backend request layer · `frontend/src/api/client.js`
- [Hooks](https://github.com/carlosdb25/bookgym/blob/main/frontend/src/hooks/useReservas.js): data hooks · Query hooks for auth, franjas, reservas, métricas, and rules · `frontend/src/hooks/useReservas.js`
- [Pages](https://github.com/carlosdb25/bookgym/blob/main/frontend/src/pages/Login.jsx): views · Login, availability, reservations, and admin dashboard screens · `frontend/src/pages/Login.jsx`
- [Components](https://github.com/carlosdb25/bookgym/blob/main/frontend/src/components/ActionModal.jsx): ui components · Shared booking and feedback widgets · `frontend/src/components/ActionModal.jsx`

### Data

Prisma schema, migrations, and seed data

- [Schema](https://github.com/carlosdb25/bookgym/blob/main/backend/prisma/schema.prisma): prisma schema · Database models and relations · `backend/prisma/schema.prisma`
- [Migrations](https://github.com/carlosdb25/bookgym/blob/main/backend/prisma/migrations/20260325043000_init/migration.sql): db migrations · Applied database change history · `backend/prisma/migrations/20260325043000_init/migration.sql`
- [Seed](https://github.com/carlosdb25/bookgym/blob/main/backend/prisma/seed.js): db seed · Initial data population script · `backend/prisma/seed.js`

## Connections

- Server → App: starts · Process entrypoint boots the Express app
- App → Auth guard: uses · Request authentication is applied in the app
- App → Roles guard: uses · Role checks protect admin and student paths
- App → Auth: mounts · Auth routes are attached to the API
- App → Franjas: mounts · Schedule and capacity endpoints
- App → Reservas: mounts · Reservation endpoints
- App → Configuración: mounts · Operational rule endpoints
- App → Métricas: mounts · Admin metrics endpoints
- App → Swagger: exposes · API documentation and spec
- App → No-show job: starts · Background job runs inside the API process
- Auth → Prisma: reads/writes · User lookup and token-related persistence
- Franjas → Prisma: reads · Builds availability from booking data
- Reservas → Prisma: transactions · Atomic reservation create/cancel/list operations
- Reservas → Config: enforces · Uses runtime booking constraints from config
- Configuración → Prisma: reads/writes · Persists operational rule parameters
- Métricas → Prisma: aggregates · Derives weekly summaries from reservation data
- No-show job → Prisma: updates · Time-based state changes and no-show handling
- Prisma → Schema: generated from · Client is based on the Prisma schema
- Schema → Migrations: evolves via · Schema changes are applied through migrations
- Seed → Prisma: initializes · Seeds baseline database data
- SPA → API client: calls · UI reads and mutates state through a central client
- SPA → Hooks: uses · Hooks isolate server interaction from components
- SPA → Pages: renders · Page-level views compose the experience
- Pages → Components: composes · Screens reuse booking and feedback widgets
- Hooks → API client: wraps · Hooks are built on the shared client layer
- API client → Auth: authenticates with · Login and token issuance flow
- API client → Franjas: fetches · Weekly availability and saturation data
- API client → Reservas: fetches/mutates · Student booking lifecycle
- API client → Métricas: fetches · Admin weekly summaries
- API client → Configuración: fetches · Reservation rules exposed to the UI
