{
  "name": "Panel Mensajes",
  "nodes": [
    {
      "parameters": {
        "path": "panel-mensajes",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "fec2e74d-53e4-41db-a1d3-917c752d3f90",
      "name": "Webhook Mensajes",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "tableId": "conversaciones",
        "options": {
          "limit": 500
        }
      },
      "id": "61774111-ab34-4fc9-8567-3029e66f8e54",
      "name": "Traer Conversaciones",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 2,
      "position": [
        224,
        0
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const q = ($('Webhook Mensajes').item.json.query || {});\nif (q.secret !== 'jeancrg2026panel') return [{ json: { error: 'no-auth' } }];\nconst cid = q.contacto;\nconst rows = $('Traer Conversaciones').all().flatMap(i => i.json).flat(1).filter(x => x && typeof x === 'object' && x.contacto_id === cid);\nrows.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));\nconst mensajes = rows.map(m => ({ role: m.role, content: m.content, derivado_a_humano: !!m.derivado_a_humano, fecha: (m.created_at || '').slice(0, 16) }));\nreturn [{ json: { mensajes } }];"
      },
      "id": "465cf1d2-c009-4329-8402-fa6f1a4ab802",
      "name": "Filtrar y Ordenar",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        448,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {}
      },
      "id": "8f4127b3-6c78-4662-bff8-9bf6642b5262",
      "name": "Responder JSON",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.2,
      "position": [
        672,
        0
      ]
    }
  ],
  "connections": {
    "Webhook Mensajes": {
      "main": [
        [
          {
            "node": "Traer Conversaciones",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Traer Conversaciones": {
      "main": [
        [
          {
            "node": "Filtrar y Ordenar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filtrar y Ordenar": {
      "main": [
        [
          {
            "node": "Responder JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  }
}