{
  "name": "Zapp Compartilhado \u2014 Disparo",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "zapp-disparo",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-1",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { \"accepted\": $json.body.recipients.length } }}",
        "options": {}
      },
      "id": "respond-1",
      "name": "Respond 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        400,
        200
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.recipients",
        "options": {}
      },
      "id": "split-1",
      "name": "Split Recipients",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        400,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "// Itera todos os items recebidos (do Split Recipients) e renderiza\n// o template pra cada recipient. Mode-agnostic \u2014 funciona em\n// 'Run Once for All Items' (default) sem depender de parameters.mode.\nconst body = $('Webhook').first().json.body;\nconst conteudo = body.conteudoTemplate;\nconst disparoVars = body.disparoVars || {};\nconst items = $input.all();\nreturn items.map(item => {\n  const rendered = conteudo.replace(/\\{\\{\\s*(\\w+)\\s*\\}\\}/g, (_, k) => {\n    const key = k.toLowerCase();\n    if (key === 'destinatario' || key === 'nome') return item.json.nome;\n    const v = disparoVars[key];\n    return v != null ? String(v) : `{{${k}}}`;\n  });\n  return { json: { ...item.json, conteudoFinal: rendered } };\n});\n"
      },
      "id": "render-1",
      "name": "Render Template",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        400
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "has-media",
              "leftValue": "={{ $('Webhook').first().json.body.attachmentBase64 }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-1",
      "name": "Tem anexo?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        800,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://chat.cejusc.com/message/sendMedia/{{ $('Webhook').first().json.body.instanceName }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "apikey",
              "value": "={{ $env.EVOLUTION_API_KEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ number: $json.telefone, mediatype: $('Webhook').first().json.body.attachmentType || 'document', media: $('Webhook').first().json.body.attachmentBase64, mimetype: $('Webhook').first().json.body.attachmentMime, caption: $json.conteudoFinal, fileName: $('Webhook').first().json.body.attachmentFileName || undefined }) }}",
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "id": "send-media",
      "name": "Evolution sendMedia",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://chat.cejusc.com/message/sendText/{{ $('Webhook').first().json.body.instanceName }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "apikey",
              "value": "={{ $env.EVOLUTION_API_KEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ number: $json.telefone, text: $json.conteudoFinal }) }}",
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "id": "send-text",
      "name": "Evolution sendText",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1000,
        500
      ]
    },
    {
      "parameters": {
        "jsCode": "// Itera o response de cada Evolution sendText/sendMedia e produz\n// um shape padronizado por recipient. Match com Split Recipients\n// por \u00edndice (paralelismo \u00e9 1:1).\nconst splitItems = $('Split Recipients').all();\nreturn $input.all().map((item, idx) => {\n  const ok = item.json.statusCode >= 200 && item.json.statusCode < 300;\n  const body = item.json.body || {};\n  return {\n    json: {\n      recipientId: splitItems[idx]?.json?.id,\n      templateId: $('Webhook').first().json.body.templateId,\n      status: ok ? 'sent' : 'failed',\n      evolutionMsgId: body?.key?.id || null,\n      errorMessage: ok ? null : (typeof body === 'string' ? body : JSON.stringify(body))\n    }\n  };\n});\n"
      },
      "id": "shape-1",
      "name": "Shape Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1200,
        400
      ]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "agg-1",
      "name": "Aggregate Results",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        1400,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://vara.brotto.io/api/disparo/callback",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "X-Callback-Secret",
              "value": "={{ $('Webhook').first().json.body.callbackSecret }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ varaSlug: $('Webhook').first().json.body.varaSlug, results: $json.data }) }}",
        "options": {}
      },
      "id": "callback-1",
      "name": "Callback App",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1600,
        400
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Respond 200",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Recipients",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Recipients": {
      "main": [
        [
          {
            "node": "Render Template",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Render Template": {
      "main": [
        [
          {
            "node": "Tem anexo?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tem anexo?": {
      "main": [
        [
          {
            "node": "Evolution sendMedia",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Evolution sendText",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evolution sendMedia": {
      "main": [
        [
          {
            "node": "Shape Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evolution sendText": {
      "main": [
        [
          {
            "node": "Shape Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Shape Result": {
      "main": [
        [
          {
            "node": "Aggregate Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Results": {
      "main": [
        [
          {
            "node": "Callback App",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}