{
  "name": "Approvals \u2014 Revoke Handler (GAL-109)",
  "nodes": [
    {
      "id": "revoke-webhook",
      "name": "Revoke Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "approval-revoke",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "normalize-revoke",
      "name": "Normalize",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        440,
        300
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "6cd08cd6-8bf7-4562-8b34-e34a185d59ed",
              "name": "idempotency_key",
              "value": "={{ $json.body.idempotency_key }}",
              "type": "string"
            },
            {
              "id": "b5e407cf-7413-4814-a744-739e8a698154",
              "name": "reason",
              "value": "={{ $json.body.reason || 'no reason given' }}",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "fetch-row",
      "name": "Fetch Row",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        640,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "=https://jiidzeympaalzljyqvjq.supabase.co/rest/v1/approval_requests?idempotency_key=eq.{{ $json.idempotency_key }}&select=*",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SB_LSA_PRO_SR }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SB_LSA_PRO_SR }}"
            },
            {
              "name": "Accept-Profile",
              "value": "ops"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "alwaysOutputData": true
    },
    {
      "id": "wrap-fetch",
      "name": "Wrap Fetch Result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        740,
        300
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "// n8n's HTTP Request splits JSON arrays into items; each item's json is a single row.\n// With alwaysOutputData=true, a truly empty response still emits 1 item (possibly empty json).\n// Filter by idempotency_key presence to distinguish real rows from the phantom item.\nconst items = $input.all();\nconst rows = items.map(i => i.json).filter(r => r && typeof r === 'object' && r.idempotency_key);\nreturn [{ json: { rows, row_count: rows.length, first_row: rows[0] || null } }];\n"
      }
    },
    {
      "id": "is-pending",
      "name": "Row Exists & Pending?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        840,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "96ecb918-d48c-46ed-be00-25f8202de726",
              "leftValue": "={{ $json.row_count }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            },
            {
              "id": "929eb98f-c7ef-45c9-af74-e9eabfba29db",
              "leftValue": "={{ $json.first_row.status }}",
              "rightValue": "pending",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "update-revoked",
      "name": "Update to Revoked",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        200
      ],
      "parameters": {
        "method": "PATCH",
        "url": "=https://jiidzeympaalzljyqvjq.supabase.co/rest/v1/approval_requests?idempotency_key=eq.{{ $('Normalize').item.json.idempotency_key }}&status=eq.pending",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $env.SB_LSA_PRO_SR }}"
            },
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.SB_LSA_PRO_SR }}"
            },
            {
              "name": "Content-Profile",
              "value": "ops"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=representation"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "status",
              "value": "revoked"
            },
            {
              "name": "resolved_at",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "edit-revoke-msg",
      "name": "Edit Original: Revoke",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1240,
        120
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://api.telegram.org/bot{{ $env.TELEGRAM_BOT_TOKEN_OPS }}/editMessageText",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({chat_id: $env.TELEGRAM_CHAT_ID, message_id: $('Wrap Fetch Result').item.json.first_row.telegram_message_id, text: `\\ud83d\\udd04 Human Gate \u2014 ${$('Wrap Fetch Result').item.json.first_row.gate_id}\\n\\nAgent: ${$('Wrap Fetch Result').item.json.first_row.agent}\\nAction: ${$('Wrap Fetch Result').item.json.first_row.description}\\n\\nRisk: ${$('Wrap Fetch Result').item.json.first_row.risk_level}\\n\\n\\ud83d\\udd04 REVOKED by agent \u2014 ${$('Normalize').item.json.reason}`}) }}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      }
    },
    {
      "id": "send-revoke-notice",
      "name": "Send Revoke Notice",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1440,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://api.telegram.org/bot{{ $env.TELEGRAM_BOT_TOKEN_OPS }}/sendMessage",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({chat_id: $env.TELEGRAM_CHAT_ID, text: `\\ud83d\\udd04 Revoked by agent ${$('Wrap Fetch Result').item.json.first_row.agent}: ${$('Wrap Fetch Result').item.json.first_row.description}\\nReason: ${$('Normalize').item.json.reason}`}) }}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      }
    },
    {
      "id": "respond-revoked",
      "name": "Respond: Revoked",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1440,
        200
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({status: \"revoked\", idempotency_key: $('Normalize').item.json.idempotency_key}) }}",
        "options": {
          "responseCode": 200
        }
      }
    },
    {
      "id": "respond-conflict",
      "name": "Respond: Conflict",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1040,
        400
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({error: $json.row_count === 0 ? \"not_found\" : \"already_resolved\", current_status: $json.first_row ? $json.first_row.status : null}) }}",
        "options": {
          "responseCode": 409
        }
      }
    }
  ],
  "connections": {
    "Revoke Webhook": {
      "main": [
        [
          {
            "node": "Normalize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize": {
      "main": [
        [
          {
            "node": "Fetch Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Row": {
      "main": [
        [
          {
            "node": "Wrap Fetch Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wrap Fetch Result": {
      "main": [
        [
          {
            "node": "Row Exists & Pending?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Row Exists & Pending?": {
      "main": [
        [
          {
            "node": "Update to Revoked",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond: Conflict",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update to Revoked": {
      "main": [
        [
          {
            "node": "Edit Original: Revoke",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Original: Revoke": {
      "main": [
        [
          {
            "node": "Send Revoke Notice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Revoke Notice": {
      "main": [
        [
          {
            "node": "Respond: Revoked",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "callerPolicy": "workflowsFromSameOwner"
  }
}