{
  "name": "DARDO · WordPress · Crear borrador supervisado",
  "nodes": [
    {
      "name": "Inicio manual",
      "id": "dardo-start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {}
    },
    {
      "name": "Preparar borrador",
      "id": "dardo-config",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const site = 'https://example.com'; // Cambia por un WordPress de pruebas con HTTPS.\nconst key = 'dardo-n8n-prueba-001'; // Conserva esta clave al repetir la misma prueba.\nif (!/^https:\\/\\/[^/?#]+(?:\\/[^?#]*)?$/.test(site) || site.includes('example.com')) {\n  throw new Error('Configura la URL HTTPS de tu WordPress de pruebas.');\n}\nif (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(key)) throw new Error('La clave debe ser un slug simple.');\nreturn [{ json: {\n  site: site.replace(/\\/$/, ''),\n  key,\n  payload: {\n    title: 'Prueba de borrador desde n8n',\n    content: '<!-- wp:paragraph --><p>Borrador de prueba. Revisión humana pendiente.</p><!-- /wp:paragraph -->',\n    slug: key,\n    status: 'draft'\n  }\n} }];"
      }
    },
    {
      "name": "Buscar clave",
      "id": "dardo-lookup",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        0
      ],
      "parameters": {
        "method": "GET",
        "url": "={{ $json.site + '/wp-json/wp/v2/posts' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "slug",
              "value": "={{ $json.key }}"
            },
            {
              "name": "status",
              "value": "any"
            },
            {
              "name": "context",
              "value": "edit"
            },
            {
              "name": "per_page",
              "value": "1"
            },
            {
              "name": "_fields",
              "value": "id,slug,status"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "json",
              "neverError": false
            }
          },
          "timeout": 30000
        }
      },
      "retryOnFail": false,
      "onError": "stopWorkflow"
    },
    {
      "name": "Evitar duplicado",
      "id": "dardo-guard",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const response = $input.first().json;\nif (response.statusCode !== 200 || !Array.isArray(response.body)) {\n  throw new Error('La búsqueda no devolvió una lista válida. No se crea contenido.');\n}\nif (response.body.length > 0) {\n  throw new Error('La clave ya existe en WordPress. Revisa la entrada y no repitas el alta.');\n}\nconst data = $('Preparar borrador').first().json;\nif (data.payload.status !== 'draft') throw new Error('Solo se permiten borradores.');\nreturn [{ json: data }];"
      }
    },
    {
      "name": "Crear borrador",
      "id": "dardo-create",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        960,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $json.site + '/wp-json/wp/v2/posts' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.payload) }}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "json",
              "neverError": false
            }
          },
          "timeout": 30000
        }
      },
      "retryOnFail": false,
      "onError": "stopWorkflow"
    },
    {
      "name": "Comprobar resultado",
      "id": "dardo-result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        0
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const response = $input.first().json;\nif (response.statusCode !== 201 || !response.body || !Number.isInteger(response.body.id) || response.body.id <= 0 || response.body.status !== 'draft') {\n  throw new Error('Resultado inesperado: revisa la ejecución y WordPress antes de reintentar.');\n}\nreturn [{ json: { id: response.body.id, status: response.body.status, resultado: 'Borrador creado; revisión humana pendiente.' } }];"
      }
    }
  ],
  "connections": {
    "Inicio manual": {
      "main": [
        [
          {
            "node": "Preparar borrador",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Preparar borrador": {
      "main": [
        [
          {
            "node": "Buscar clave",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar clave": {
      "main": [
        [
          {
            "node": "Evitar duplicado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evitar duplicado": {
      "main": [
        [
          {
            "node": "Crear borrador",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crear borrador": {
      "main": [
        [
          {
            "node": "Comprobar resultado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "none",
    "saveManualExecutions": true
  },
  "pinData": {},
  "tags": []
}