{
  "name": "Precifique \u2014 Afiliada Cadastro",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "afiliado-cadastro",
        "responseMode": "onReceived",
        "responseData": "firstEntryJson",
        "options": {}
      },
      "id": "webhook-cadastro",
      "name": "Webhook Cadastro",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.body.whatsapp }}",
              "operation": "isNotEmpty"
            },
            {
              "value1": "={{ $json.body.nome }}",
              "operation": "isNotEmpty"
            },
            {
              "value1": "={{ $json.body.link_afiliado }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "validar-campos-cadastro",
      "name": "Validar Campos",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const raw = $input.first().json.body.whatsapp || '';\nconst digits = raw.replace(/\\D/g, '');\nlet number = digits;\nif (!number.startsWith('55')) {\n  number = '55' + number;\n}\nreturn [{ json: { ...$input.first().json.body, whatsapp_fmt: number } }];"
      },
      "id": "formatar-telefone-cadastro",
      "name": "Formatar Telefone",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.EVOLUTION_URL }}/message/sendText/{{ $env.EVOLUTION_INSTANCE }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.EVOLUTION_API_KEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"number\": \"{{ $json.whatsapp_fmt }}\",\n  \"text\": \"Ol\u00e1, {{ $json.nome }}! \ud83d\udc9c\\n\\nSeu cadastro como afiliada do *Precifique* foi confirmado!\\n\\n\ud83d\udd17 *Seu link:*\\n{{ $json.link_afiliado }}\\n\\nCompartilhe esse link e ganhe *R$ 12,90* por cada venda!\\n\\n\ud83d\udcca *Seu painel:*\\n{{ $json.link_painel }}\\n\\nQualquer d\u00favida, estamos aqui. Boas vendas! \ud83d\ude80\"\n}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "id": "whatsapp-cadastro",
      "name": "WhatsApp Boas-vindas",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.statusCode }}",
              "operation": "smallerEqual",
              "value2": 299
            }
          ]
        }
      },
      "id": "checar-resposta-cadastro",
      "name": "Enviou OK?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "console.error('[afiliado-cadastro] Falha ao enviar WhatsApp:', JSON.stringify($input.first().json));\nreturn [];"
      },
      "id": "log-erro-cadastro",
      "name": "Log Erro Envio",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconsole.error('[afiliado-cadastro] Campos inv\u00e1lidos:', JSON.stringify(item.body));\nreturn [];"
      },
      "id": "log-campos-invalidos-cadastro",
      "name": "Log Campos Inv\u00e1lidos",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        400
      ]
    }
  ],
  "connections": {
    "Webhook Cadastro": {
      "main": [
        [
          {
            "node": "Validar Campos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validar Campos": {
      "main": [
        [
          {
            "node": "Formatar Telefone",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log Campos Inv\u00e1lidos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Formatar Telefone": {
      "main": [
        [
          {
            "node": "WhatsApp Boas-vindas",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "WhatsApp Boas-vindas": {
      "main": [
        [
          {
            "node": "Enviou OK?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviou OK?": {
      "main": [
        [],
        [
          {
            "node": "Log Erro Envio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}