# Componentes backend

```mermaid
flowchart TD
  server["server.js<br/>arranque"] --> pool["Pool PostgreSQL<br/>src/db/pool.js"]
  server --> ensureAuth["ensureAuthTables<br/>ensureDefaultAdmin<br/>cleanupExpiredSessions"]
  server --> ensureInvima["ensureInvimaTables"]
  server --> app["Express app<br/>src/app.js"]

  app --> cors["cors<br/>CORS_ORIGINS"]
  app --> json["express.json<br/>limit 1mb"]
  app --> apiRouter["/api<br/>routes/index.js"]
  app --> errorMiddleware["errorMiddleware"]

  apiRouter --> health["GET /health"]
  apiRouter --> authRoutes["/auth<br/>auth.routes.js"]
  apiRouter --> invimaRoutes["/invima<br/>invima.routes.js"]

  authRoutes --> loginRoute["POST /login"]
  authRoutes --> meRoute["GET /me"]
  authRoutes --> logoutRoute["POST /logout"]

  loginRoute --> authService["auth.service.js<br/>login, createSession"]
  meRoute --> requireAuth["requireAuth<br/>auth.middleware.js"]
  logoutRoute --> requireAuth
  logoutRoute --> logoutService["auth.service.js<br/>logout"]

  requireAuth --> authenticate["authenticateToken"]
  authenticate --> pool
  authService --> pool
  logoutService --> pool

  invimaRoutes --> requireAuth
  invimaRoutes --> datasets["GET /datasets<br/>listInvimaDatasets"]
  invimaRoutes --> categorias["GET /categorias<br/>listInvimaCategories"]
  invimaRoutes --> filtros["GET /filtros<br/>listInvimaFilterOptions"]
  invimaRoutes --> stats["GET /sync/stats<br/>getInvimaSyncStats"]
  invimaRoutes --> job["GET /sync/job<br/>getInvimaSyncJob"]
  invimaRoutes --> sync["POST /sync<br/>startInvimaSyncJob"]
  invimaRoutes --> catalogo["GET /catalogo<br/>getInvimaCatalog"]
  invimaRoutes --> exportar["GET /catalogo/export<br/>csv o xlsx"]

  categorias --> invimaService["invima/service.js"]
  filtros --> invimaService
  stats --> invimaService
  catalogo --> invimaService
  exportar --> invimaService
  sync --> syncManager["sync-job-manager.js"]
  syncManager --> invimaService

  invimaService --> queries["Consultas SQL<br/>invima_api_catalog<br/>invima_api_raw"]
  queries --> pool
  invimaService --> adapters["adapters.js<br/>normalizacion"]
  invimaService --> apiInvima["APIinvima.js<br/>fetchInvimaDataset"]
  apiInvima --> config["APIinvima.json<br/>datasets Socrata"]
  apiInvima -- "HTTPS fetch" --> socrata["datos.gov.co/resource"]
```

## Archivos analizados

- `backend/src/server.js`
- `backend/src/app.js`
- `backend/src/routes/index.js`
- `backend/src/routes/auth.routes.js`
- `backend/src/routes/invima.routes.js`
- `backend/src/middlewares/auth.middleware.js`
- `backend/src/middlewares/error.middleware.js`
- `backend/src/auth/auth.service.js`
- `backend/src/db/pool.js`
- `backend/src/config/env.js`
- `backend/invima/service.js`
- `backend/invima/sync-job-manager.js`
- `backend/invima/adapters.js`
- `backend/APIinvima.js`
- `backend/APIinvima.json`
- `backend/package.json`
