{
  "name": "etiquetas",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "internal/etiquetas",
        "responseMode": "responseNode"
      },
      "id": "wh",
      "name": "Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// body: { tipo: 'producto'|'envio', items: [{sku?, ov_numero?}], copias?: 1 }\nconst { tipo, items: list = [], copias = 1 } = items[0].json.body;\nconst out = [];\nfor (const it of list) for (let i=0;i<copias;i++) out.push({ json: { tipo, ...it } });\nreturn out;"
      },
      "id": "expand",
      "name": "Expandir copias",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT v.sku, p.nombre AS producto,\n       (SELECT string_agg(va.valor, ' / ') FROM variante_valores vv\n        JOIN valores_atributo va ON va.id = vv.valor_atributo_id\n        WHERE vv.variante_id = v.id) AS variante,\n       v.barcode\nFROM variantes v JOIN productos p ON p.id = v.producto_id\nWHERE v.sku = $1;",
        "options": {
          "queryReplacement": "={{ $json.sku }}"
        }
      },
      "id": "data",
      "name": "Datos variante",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        680,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "http://labels:3001/barcode/dataurl",
        "method": "POST",
        "sendBody": true,
        "jsonBody": "={ \"text\": \"{{ $json.barcode || $json.sku }}\", \"type\": \"code128\" }"
      },
      "id": "bar",
      "name": "Generar barcode",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Junta todas las etiquetas en un array para renderizar el grid.\nconst etiquetas = items.map(it => ({\n  sku: it.json.sku, producto: it.json.producto,\n  variante: it.json.variante, barcode_data_url: it.json.dataUrl\n}));\nconst fs = require('fs');\nconst tpl = fs.readFileSync('/templates/etiqueta_producto.html','utf8');\nfunction render(tpl, ctx) {\n  tpl = tpl.replace(/\\{\\{#each (\\w+)\\}\\}([\\s\\S]*?)\\{\\{\\/each\\}\\}/g, (_, k, body) => (ctx[k]||[]).map(it => render(body, { ...ctx, ...it })).join(''));\n  tpl = tpl.replace(/\\{\\{\\s*([\\w.]+)\\s*\\}\\}/g, (_, p) => { const v = p.split('.').reduce((o,k)=>o==null?o:o[k], ctx); return v==null?'':String(v); });\n  return tpl;\n}\nreturn [{ json: { html: render(tpl, { etiquetas }) } }];"
      },
      "id": "compose",
      "name": "Componer HTML",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://gotenberg:3000/forms/chromium/convert/html",
        "method": "POST",
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "parameterType": "formBinaryData",
              "name": "files",
              "inputDataFieldName": "html_file"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "id": "pdf",
      "name": "PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "binary",
        "responseBody": "={{$binary.data}}"
      },
      "id": "resp",
      "name": "Devolver PDF",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1560,
        300
      ]
    }
  ],
  "connections": {
    "Trigger": {
      "main": [
        [
          {
            "node": "Expandir copias",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Expandir copias": {
      "main": [
        [
          {
            "node": "Datos variante",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Datos variante": {
      "main": [
        [
          {
            "node": "Generar barcode",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generar barcode": {
      "main": [
        [
          {
            "node": "Componer HTML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Componer HTML": {
      "main": [
        [
          {
            "node": "PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PDF": {
      "main": [
        [
          {
            "node": "Devolver PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}