# API CartecWeb (Node) — Referencia rápida

**Base:** `http://localhost:3000/api`  
**Content-Type:** `application/json`

---

## Endpoints

| Método | Ruta | Params / Body | Respuesta |
|--------|------|----------------|-----------|
| GET | `/health` | query: `db=1` (opcional, prueba BD) | `{ success, message, version, timestamp, db }` |
| GET | `/ot/list` | query: `estado`, `rut_tecnico`, `limit` (1-100, default 10) | `{ success, count, data[] }` |
| GET | `/ot/latest` | — | Últimas 10 OT: `{ success, count, data[] }` |
| GET | `/ot/:id` | id en URL | `{ success, data: { ot, cliente, vehiculos } }` |
| POST | `/ot/create` | body abajo | `{ success, message, id_ot }` |
| PATCH | `/ot/:id/complete` | body: `hora_llegada`, `hora_salida`, `observaciones`, `firma{ rut, nombre, imagen }` | `{ success, message }` |
| GET | `/cliente/:rut` | rut en URL | `{ success, data }` |
| GET | `/gps/position/:imei` | imei en URL *(requiere GPS_DB)* | `{ success, data }` |
| GET | `/gps/info/:id` | id en URL *(requiere GPS_DB)* | `{ success, data }` |
| GET | `/tecnicos` | — *(requiere GPS_DB)* | `{ success, count, data[] }` |
| GET | `/vehiculo/vin/:vin` | vin 11-17 chars | `{ success, data }` (Carcloud) |

---

## POST /ot/create — Body

```json
{
  "cliente": {
    "rut": "12345678-5",
    "nombre": "string",
    "direccion": "string",
    "comuna": "string",
    "telefono": "opcional",
    "email": "opcional"
  },
  "tecnico": { "rut": "11111111-1" },
  "servicio": {
    "presGPS": "gpsimple|entel|tattersall|cemin|carflex",
    "tiposerv": "instalacion_gps|desinstalacion_gps|visita_tecnica|visita_fallida|servicio_mecanico",
    "taller_install": "cartec|domicilio|taller_externo"
  },
  "vehiculos": [
    { "patente": "OBLIGATORIO", "marca", "modelo", "color", "anio": "opcionales" }
  ]
}
```

---

## cURL

```bash
# Health
curl "http://localhost:3000/api/health"

# Listar OT
curl "http://localhost:3000/api/ot/list?limit=20"
curl "http://localhost:3000/api/ot/latest"

# Detalle OT
curl "http://localhost:3000/api/ot/1"

# Crear OT
curl -X POST "http://localhost:3000/api/ot/create" \
  -H "Content-Type: application/json" \
  -d '{"cliente":{"rut":"12345678-5","nombre":"Test","direccion":"Av. Test 123","comuna":"Santiago"},"tecnico":{"rut":"11111111-1"},"servicio":{"presGPS":"gpsimple","tiposerv":"instalacion_gps","taller_install":"domicilio"},"vehiculos":[{"patente":"TEST99"}]}'

# Completar OT
curl -X PATCH "http://localhost:3000/api/ot/1/complete" \
  -H "Content-Type: application/json" \
  -d '{"hora_llegada":"09:00","hora_salida":"10:30","observaciones":"OK"}'

# Cliente por RUT
curl "http://localhost:3000/api/cliente/12345678-5"
```

---

## Códigos HTTP

`200` OK · `201` Creado · `400` Datos inválidos · `404` No encontrado · `422` Validación · `500` Error servidor · `503` GPS DB no configurada

---

## Notas

- RUT: con o sin guión; se valida módulo 11.
- Cliente: se crea o actualiza (UPSERT) por `rut_cliente`.
- Sin auth en esta versión.
