{
  "name": "SIGE \u2014 Proposta Expirando (Lembrete por Email + WhatsApp)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "sige-proposta-expirando",
        "responseMode": "onReceived",
        "responseData": "allEntries",
        "options": {
          "rawBody": true
        }
      },
      "id": "webhook-in",
      "name": "Webhook (POST)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Valida\u00e7\u00e3o HMAC-SHA256 (X-Signature) \u2014 segredo em $env.N8N_WEBHOOK_SECRET.\nconst crypto = require('crypto');\nconst secret = $env.N8N_WEBHOOK_SECRET || '';\nconst given  = ($input.first().json.headers || {})['x-signature'] || '';\nlet body = $input.first().json.body ? JSON.stringify($input.first().json.body) : '';\ntry {\n  if ($input.first().binary && $input.first().binary.data) {\n    body = Buffer.from($input.first().binary.data, 'base64').toString('utf8');\n  }\n} catch (e) {}\nif (!secret) return [{ json: { ...$input.first().json, _signature_skipped: 'no_secret' } }];\nconst expected = crypto.createHmac('sha256', secret).update(body, 'utf8').digest('hex');\nconst ok = expected.length === given.length && crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(given));\nif (!ok) throw new Error('Assinatura HMAC inv\u00e1lida (X-Signature n\u00e3o confere)');\nreturn [{ json: { ...$input.first().json, _signature_ok: true } }];"
      },
      "id": "validate-hmac",
      "name": "Validar Assinatura HMAC",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Normaliza payload do evento `proposta.expirando`.\nconst body = $input.first().json.body || $input.first().json;\nconst d = (body && body.data) || {};\nreturn [{\n  json: {\n    cliente_nome:      d.cliente_nome      || 'Cliente',\n    cliente_email:     d.cliente_email     || '',\n    cliente_telefone:  d.cliente_telefone  || '',\n    proposta_numero:   d.proposta_numero   || '',\n    proposta_versao:   d.proposta_versao   || 1,\n    valor_total:       d.valor_total       || 0,\n    data_validade:     d.data_validade     || '',\n    dias_restantes:    d.dias_restantes    || 0,\n    validade_dias:     d.validade_dias     || null,\n    portal_url:        d.portal_url        || '',\n    admin_id:          body.admin_id       || null\n  }\n}];"
      },
      "id": "extract",
      "name": "Extrair Dados",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json[\"cliente_email\"] }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "if-has-email",
      "name": "Tem e-mail?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json[\"cliente_telefone\"] }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "id": "if-has-phone",
      "name": "Tem WhatsApp?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        900,
        420
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{ $env.SIGE_FROM_EMAIL }}",
        "toEmail": "={{ $json[\"cliente_email\"] }}",
        "subject": "Sua proposta {{ $json[\"proposta_numero\"] }} expira em {{ $json[\"dias_restantes\"] }} dia(s)",
        "text": "Ol\u00e1 {{ $json[\"cliente_nome\"] }},\n\nA proposta n\u00ba {{ $json[\"proposta_numero\"] }} (vers\u00e3o {{ $json[\"proposta_versao\"] }}) que enviamos expira em {{ $json[\"dias_restantes\"] }} dia(s) \u2014 {{ $json[\"data_validade\"] }}.\n\nValor: R$ {{ $json[\"valor_total\"] }}\n\nVoc\u00ea pode revisar e aprovar diretamente no portal do cliente:\n{{ $json[\"portal_url\"] }}\n\nQualquer d\u00favida, \u00e9 s\u00f3 responder este e-mail."
      },
      "id": "send-email",
      "name": "Enviar E-mail",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1140,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.EVOLUTION_API_URL }}/message/sendText/{{ $env.EVOLUTION_INSTANCE }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.EVOLUTION_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyContentType": "json",
        "jsonBody": "={\n  \"number\": \"{{ $json[\"cliente_telefone\"] }}\",\n  \"text\":   \"Ol\u00e1 *{{ $json[\\\"cliente_nome\\\"] }}*!\\n\\nLembrete: sua proposta *{{ $json[\\\"proposta_numero\\\"] }}* (v{{ $json[\\\"proposta_versao\\\"] }}) expira em *{{ $json[\\\"dias_restantes\\\"] }} dia(s)* ({{ $json[\\\"data_validade\\\"] }}).\\n\\n\ud83d\udcb0 Valor: R$ {{ $json[\\\"valor_total\\\"] }}\\n\ud83d\udc49 {{ $json[\\\"portal_url\\\"] }}\"\n}"
      },
      "id": "send-whatsapp",
      "name": "Enviar WhatsApp",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1140,
        420
      ]
    }
  ],
  "connections": {
    "Webhook (POST)": {
      "main": [
        [
          {
            "node": "Validar Assinatura HMAC",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validar Assinatura HMAC": {
      "main": [
        [
          {
            "node": "Extrair Dados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extrair Dados": {
      "main": [
        [
          {
            "node": "Tem e-mail?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Tem WhatsApp?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tem e-mail?": {
      "main": [
        [
          {
            "node": "Enviar E-mail",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Tem WhatsApp?": {
      "main": [
        [
          {
            "node": "Enviar WhatsApp",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {},
  "tags": [
    {
      "name": "sige"
    },
    {
      "name": "propostas"
    }
  ],
  "_meta": {
    "description": "Workflow exemplo para o evento `proposta.expirando` (disparado pelo job di\u00e1rio `flask emitir-propostas-expirando`).",
    "env_obrigatorias": [
      "N8N_WEBHOOK_SECRET",
      "SIGE_FROM_EMAIL",
      "EVOLUTION_API_URL",
      "EVOLUTION_INSTANCE",
      "EVOLUTION_API_KEY"
    ]
  }
}