{
  "id": "invatturS2Lovabl",
  "name": "S2 · Demo docente Lovable -> n8n -> Calendar + Sheets (sin credenciales)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "consulta-excursion-aula",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "s2-demo-webhook",
      "name": "Aviso desde Lovable (Webhook)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "webhookId": "20261120-0002-4000-8000-000000000003",
      "position": [
        0,
        160
      ]
    },
    {
      "parameters": {
        "operation": "getAll",
        "calendar": {
          "__rl": true,
          "value": "CAMBIAR_POR_CALENDARIO_DE_AULA",
          "mode": "id"
        },
        "returnAll": false,
        "limit": 10,
        "options": {
          "singleEvents": true,
          "orderBy": "startTime"
        }
      },
      "id": "s2-demo-calendar",
      "name": "Consultar horario (Google Calendar)",
      "type": "n8n-nodes-base.googleCalendar",
      "typeVersion": 1.2,
      "position": [
        280,
        160
      ]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "CAMBIAR_POR_ID_HOJA_EXCURSIONES_DE_AULA",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Excursiones",
          "mode": "name"
        },
        "options": {}
      },
      "id": "s2-demo-sheets",
      "name": "Leer punto de encuentro (Google Sheets)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        560,
        160
      ]
    },
    {
      "parameters": {
        "jsCode": "const webhookData = $('Aviso desde Lovable (Webhook)').first().json;\nconst body = webhookData.body && typeof webhookData.body === 'object' ? webhookData.body : webhookData;\nconst idVisita = String(body.id_visita || 'MOR-01').trim().toUpperCase();\nconst pregunta = String(body.pregunta || '¿A qué hora sale el paseo del sábado y dónde quedamos?').trim();\n\nconst eventos = $('Consultar horario (Google Calendar)').all().map((i) => i.json);\nconst filas = $('Leer punto de encuentro (Google Sheets)').all().map((i) => i.json);\n\nconst evento = eventos.find((ev) => {\n  const texto = `${ev.summary || ''} ${ev.description || ''}`.toUpperCase();\n  return texto.includes(idVisita);\n});\n\nconst fila = filas.find((row) => String(row.id_visita || '').trim().toUpperCase() === idVisita);\n\nlet horaSalida = 'Pendiente de confirmar en el calendario de aula';\nif (evento && evento.start && evento.start.dateTime) {\n  const match = String(evento.start.dateTime).match(/T(\\d{2}:\\d{2})/);\n  horaSalida = match ? match[1] : String(evento.start.dateTime);\n}\n\nconst nombreVisita = (fila && fila.nombre_visita) || (evento && evento.summary) || idVisita;\nconst puntoEncuentro = (fila && String(fila.punto_encuentro || '').trim()) || 'Pendiente de confirmar en la hoja Excursiones';\n\nconst respuesta = `La visita «${nombreVisita}» (${idVisita}) sale a las ${horaSalida} y el punto de encuentro es: ${puntoEncuentro}.`;\n\nreturn [{\n  json: {\n    id_visita: idVisita,\n    pregunta_recibida: pregunta,\n    nombre_visita: nombreVisita,\n    hora_salida: horaSalida,\n    punto_encuentro: puntoEncuentro,\n    fuente_horario: evento ? 'Google Calendar (calendario de aula)' : 'Sin evento coincidente',\n    fuente_punto_encuentro: fila ? 'Google Sheets (hoja Excursiones)' : 'Sin fila coincidente',\n    respuesta,\n    estado: 'RESPUESTA_CON_DOS_FUENTES'\n  }\n}];"
      },
      "id": "s2-demo-combinar",
      "name": "Combinar horario y punto de encuentro",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        840,
        160
      ]
    },
    {
      "parameters": {
        "respondWith": "firstIncomingItem",
        "options": {}
      },
      "id": "s2-demo-respond",
      "name": "Responder a Lovable",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1120,
        160
      ]
    }
  ],
  "connections": {
    "Aviso desde Lovable (Webhook)": {
      "main": [
        [
          {
            "node": "Consultar horario (Google Calendar)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Consultar horario (Google Calendar)": {
      "main": [
        [
          {
            "node": "Leer punto de encuentro (Google Sheets)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Leer punto de encuentro (Google Sheets)": {
      "main": [
        [
          {
            "node": "Combinar horario y punto de encuentro",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combinar horario y punto de encuentro": {
      "main": [
        [
          {
            "node": "Responder a Lovable",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
