# Secuencia consulta INVIMA

```mermaid
sequenceDiagram
  actor Usuario
  participant Catalog as CatalogPageComponent
  participant InvimaFE as InvimaService Angular
  participant Interceptor as authInterceptor
  participant Route as GET /api/invima/catalogo
  participant AuthMW as requireAuth
  participant Service as invima/service.js
  participant DB as PostgreSQL

  Usuario->>Catalog: Aplica filtros
  Catalog->>Catalog: applyFilters() reinicia offset
  Catalog->>Catalog: buildQuery()
  Catalog->>InvimaFE: searchCatalog(query)
  InvimaFE->>Interceptor: GET /api/invima/catalogo con HttpParams
  Interceptor->>Route: Authorization Bearer token
  Route->>AuthMW: router.use(requireAuth)
  AuthMW->>DB: SELECT app_sessions JOIN app_users
  DB-->>AuthMW: usuario valido
  AuthMW-->>Route: continua
  Route->>Service: getInvimaCatalog(pool, req.query)
  Service->>Service: parseCatalogFilters()
  Service->>Service: buildCatalogWhere()
  Service->>DB: SELECT COUNT(*) FROM invima_api_catalog WHERE filtros
  DB-->>Service: total
  Service->>DB: SELECT columnas catalogo FROM invima_api_catalog WHERE filtros ORDER BY fecha_vencimiento, producto LIMIT OFFSET
  DB-->>Service: rows
  alt busqueda textual sin resultados y varios tokens
    Service->>Service: fallback_like
    Service->>DB: SELECT COUNT y SELECT con ILIKE por tokens
    DB-->>Service: rows fallback
  end
  Service-->>Route: { items, total, limit, offset }
  Route-->>InvimaFE: respuesta JSON
  InvimaFE-->>Catalog: InvimaCatalogResponse
  Catalog->>Catalog: items.set(), total.set(), loading false
  Catalog-->>Usuario: Presenta registros y paginacion
```

## Archivos analizados

- `semillero/src/app/features/invima/catalog-page.component.ts`
- `semillero/src/app/core/services/invima.service.ts`
- `semillero/src/app/core/interceptors/auth.interceptor.ts`
- `backend/src/routes/invima.routes.js`
- `backend/src/middlewares/auth.middleware.js`
- `backend/src/auth/auth.service.js`
- `backend/invima/service.js`
- `backend/src/db/pool.js`
