{
  "name": "Leonex — Pipeline diario (fire-and-forget, 08:00 NY = 12:00 UTC L-V)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 8 * * 1-5"
            }
          ]
        }
      },
      "id": "trigger-daily-cron",
      "name": "Cron — 08:00 NY L-V (= 12:00 UTC)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [200, 320],
      "notes": "Dispara cada dia laborable a las 08:00 hora NY (= 12:00 UTC = 14:00 hora España), 90 minutos antes de la apertura del mercado US (09:30 NY / 13:30 UTC). El pipeline tarda ~40-50 min, asi termina antes del cron intraday de las 10:00 NY. IMPORTANTE: n8n self-hosted ejecuta crones en el timezone de la instancia (America/New_York), NO en el timezone del workflow setting. Por eso el cron va en hora NY."
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://leonex.187.127.86.200.nip.io/api/refresh-async",
        "options": {
          "redirect": {"redirect": {"followRedirects": true}},
          "response": {"response": {"neverError": true, "responseFormat": "json"}},
          "timeout": 30000
        },
        "sendHeaders": false,
        "sendBody": false
      },
      "id": "call-refresh-async",
      "name": "POST /api/refresh-async",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [460, 320],
      "notes": "Llama al endpoint fire-and-forget. El servidor responde {ok: true, status: 'started'} en <1s y luego ejecuta el pipeline ENTERO en background (~40-50 min). n8n NO espera al pipeline — solo confirma que arranco. Timeout 30s es mas que suficiente para recibir el 200 inicial. Esto evita el error que tenia el workflow anterior (timeout 30m por intentar esperar al sincrono /api/refresh). URL HARDCODED porque n8n self-hosted bloquea acceso a $env vars con 'access to env vars denied'."
    },
    {
      "parameters": {
        "conditions": {
          "options": {"caseSensitive": false, "leftValue": "", "typeValidation": "loose"},
          "conditions": [
            {
              "id": "is-started",
              "leftValue": "={{ $json.status }}",
              "rightValue": "started",
              "operator": {"type": "string", "operation": "equals"}
            }
          ],
          "combinator": "and"
        }
      },
      "id": "check-started",
      "name": "If status=started",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [720, 320],
      "notes": "Verifica que el servidor confirme que el pipeline arranco en background. Si no, hay algo mal (lock atascado, fallo de import, etc.) y registramos para alerta."
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "log",
              "value": "=Pipeline diario lanzado OK at {{ $now.toISO() }}. Background job running. Esperado terminar en ~45 min."
            }
          ]
        }
      },
      "id": "log-started",
      "name": "Log: pipeline lanzado",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, 200]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "log",
              "value": "=ERROR: Pipeline diario no arranco. Respuesta del server: {{ JSON.stringify($json) }}. Verifica /api/lock-status y los logs del container."
            }
          ]
        }
      },
      "id": "log-failed",
      "name": "Log: error arranque",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, 440]
    }
  ],
  "connections": {
    "Cron — 08:00 NY L-V (= 12:00 UTC)": {
      "main": [[{"node": "POST /api/refresh-async", "type": "main", "index": 0}]]
    },
    "POST /api/refresh-async": {
      "main": [[{"node": "If status=started", "type": "main", "index": 0}]]
    },
    "If status=started": {
      "main": [
        [{"node": "Log: pipeline lanzado", "type": "main", "index": 0}],
        [{"node": "Log: error arranque", "type": "main", "index": 0}]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "saveManualExecutions": true,
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "all",
    "timezone": "UTC"
  },
  "tags": ["leonex", "trading", "daily", "fire-and-forget"],
  "meta": {
    "instanceId": "leonex-daily-pipeline-async"
  },
  "pinData": {},
  "staticData": null
}
