{
  "name": "SIGE \u2014 Proposta Rejeitada (Notifica\u00e7\u00e3o interna por Email + Slack)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "sige-proposta-rejeitada",
        "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.rejeitada`.\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    proposta_numero:  d.proposta_numero  || '',\n    proposta_versao:  d.proposta_versao  || 1,\n    valor_total:      d.valor_total      || 0,\n    rejeitada_por:    d.rejeitada_por    || 'cliente',\n    motivo:           d.motivo           || '',\n    data_rejeicao:    d.data_rejeicao    || '',\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": {
        "fromEmail": "={{ $env.SIGE_FROM_EMAIL }}",
        "toEmail": "={{ $env.SIGE_COMERCIAL_EMAIL }}",
        "subject": "[Comercial] Proposta {{ $json[\"proposta_numero\"] }} v{{ $json[\"proposta_versao\"] }} REJEITADA por {{ $json[\"rejeitada_por\"] }}",
        "text": "Cliente: {{ $json[\"cliente_nome\"] }}\nProposta: {{ $json[\"proposta_numero\"] }} (v{{ $json[\"proposta_versao\"] }})\nValor: R$ {{ $json[\"valor_total\"] }}\nRejeitada por: {{ $json[\"rejeitada_por\"] }} em {{ $json[\"data_rejeicao\"] }}\n\nMotivo registrado:\n{{ $json[\"motivo\"] }}\n\nPortal: {{ $json[\"portal_url\"] }}\n\n\u2014 SIGE"
      },
      "id": "send-email-comercial",
      "name": "Aviso Comercial (E-mail)",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        900,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "sendBody": true,
        "bodyContentType": "json",
        "jsonBody": "={\n  \"text\": \":x: Proposta *{{ $json[\\\"proposta_numero\\\"] }}* (v{{ $json[\\\"proposta_versao\\\"] }}) \u2014 *{{ $json[\\\"cliente_nome\\\"] }}* \u2014 REJEITADA por *{{ $json[\\\"rejeitada_por\\\"] }}*. Valor R$ {{ $json[\\\"valor_total\\\"] }}. Motivo: {{ $json[\\\"motivo\\\"] }}\"\n}"
      },
      "id": "send-slack",
      "name": "Aviso Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        900,
        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": "Aviso Comercial (E-mail)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Aviso Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "tags": [
    {
      "name": "sige"
    },
    {
      "name": "propostas"
    }
  ],
  "_meta": {
    "description": "Workflow exemplo para o evento `proposta.rejeitada` do SIGE \u2014 notifica equipe comercial por e-mail e Slack.",
    "env_obrigatorias": [
      "N8N_WEBHOOK_SECRET",
      "SIGE_FROM_EMAIL",
      "SIGE_COMERCIAL_EMAIL",
      "SLACK_WEBHOOK_URL"
    ]
  }
}