{
  "name": "Approvals \u2014 Telegram Callback Handler (GAL-109)",
  "nodes": [
    {
      "id": "tg-webhook",
      "name": "Telegram Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "telegram-approvals-callback",
        "responseMode": "onReceived",
        "responseCode": 200,
        "responseData": "noData",
        "options": {}
      }
    },
    {
      "id": "is-callback",
      "name": "Is Callback Query?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "2423c845-ac1d-409f-b1e8-bfa61b772c09",
              "leftValue": "={{ !!($json.body && $json.body.callback_query && $json.body.callback_query.data) }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true"
              }
            }
          ],
          "combinator": "and"
        }
      }
    },
    {
      "id": "parse",
      "name": "Parse Callback",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        640,
        300
      ],
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "970bae25-22b1-4867-9133-19cc9a9bdf75",
              "name": "action",
              "value": "={{ ($json.body.callback_query.data || '').split(':')[0] }}",
              "type": "string"
            },
            {
              "id": "7b95656f-cd81-4df7-a881-41222e8d0470",
              "name": "row_id",
              "value": "={{ ($json.body.callback_query.data || '').split(':').slice(1).join(':') }}",
              "type": "string"
            },
            {
              "id": "ea3c044f-273a-49f5-9395-102fc96d91b2",
              "name": "callback_query_id",
              "value": "={{ $json.body.callback_query.id }}",
              "type": "string"
            },
            {
              "id": "f3ab0f27-fa8c-4b78-8f78-c1a38d030593",
              "name": "chat_id",
              "value": "={{ $json.body.callback_query.message.chat.id }}",
              "type": "string"
            },
            {
              "id": "2964baa4-c9cd-4801-be38-2f5759abbf16",
              "name": "message_id",
              "value": "={{ $json.body.callback_query.message.message_id }}",
              "type": "number"
            },
            {
              "id": "8a5962cf-ce6d-4434-b3cc-8e6fa386a464",
              "name": "approver",
              "value": "={{ $json.body.callback_query.from.username || $json.body.callback_query.from.first_name }}",
              "type": "string"
            },
            {
              "id": "9e98ab91-1ea2-4936-ab56-96226aa37f89",
              "name": "new_status",
              "value": "={{ $json.body.callback_query.data.startsWith('approve:') ? 'approved' : 'rejected' }}",
              "type": "string"
            }
          ]
        }
      }
    },
    {
      "id": "update-row",
      "name": "Update Row Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        840,
        300
      ],
      "parameters": {
        "method": "PATCH",
        "url": "=https://jiidzeympaalzljyqvjq.supabase.co/rest/v1/approval_requests?id=eq.{{ $json.row_id }}&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": "={{ $json.new_status }}"
            },
            {
              "name": "approver",
              "value": "={{ $json.approver }}"
            },
            {
              "name": "resolved_at",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      }
    },
    {
      "id": "answer-cbq",
      "name": "Answer Callback Query",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://api.telegram.org/bot{{ $env.TELEGRAM_BOT_TOKEN_OPS }}/answerCallbackQuery",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({callback_query_id: $('Parse Callback').item.json.callback_query_id, text: `Marked as ${$('Parse Callback').item.json.new_status}`, show_alert: false}) }}",
        "options": {}
      }
    },
    {
      "id": "edit-msg",
      "name": "Edit Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        400
      ],
      "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: $('Parse Callback').item.json.chat_id, message_id: $('Parse Callback').item.json.message_id, text: $('Telegram Webhook').item.json.body.callback_query.message.text + `\\n\\n${$('Parse Callback').item.json.new_status === 'approved' ? '\\u2705 APPROVED' : '\\u274c REJECTED'} by ${$('Parse Callback').item.json.approver} at ${new Date().toISOString()}`}) }}",
        "options": {}
      }
    }
  ],
  "connections": {
    "Telegram Webhook": {
      "main": [
        [
          {
            "node": "Is Callback Query?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Callback Query?": {
      "main": [
        [
          {
            "node": "Parse Callback",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Parse Callback": {
      "main": [
        [
          {
            "node": "Update Row Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Row Status": {
      "main": [
        [
          {
            "node": "Answer Callback Query",
            "type": "main",
            "index": 0
          },
          {
            "node": "Edit Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "callerPolicy": "workflowsFromSameOwner"
  }
}