{
  "name": "ManageCallAI \u2014 Approval Review Queue",
  "meta": {
    "templateId": "managecallai-approval-review",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000001",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "managecall-approvals",
        "responseMode": "onReceived",
        "options": {
          "rawBody": true
        }
      },
      "notes": "Subscribe to: approval.requested"
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000003",
      "name": "Verify Signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ],
      "parameters": {
        "jsCode": "const crypto = require('crypto');\nfunction verify(item, secret, tol = 300) {\n  const h = item.headers ?? {};\n  const ts = parseInt(h['x-managecall-timestamp'] ?? '0', 10);\n  if (Math.abs(Date.now() / 1000 - ts) > tol) throw new Error('timestamp_expired');\n  const body = typeof item.body === 'string' ? item.body : JSON.stringify(item.body ?? {});\n  const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(`${ts}.${body}`).digest('hex');\n  if (!crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(h['x-managecall-signature-256'] ?? ''))) throw new Error('invalid_signature');\n}\nverify($input.first(), $env.MANAGECALL_WEBHOOK_SECRET);\nreturn $input.all();"
      }
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000004",
      "name": "Fetch Approval Details",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "={{ $env.MANAGECALL_API_URL }}/api/v1/approvals/{{ $json.body.data.approval_id }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000005",
      "name": "Post Decision Request to Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "blocks": [
            {
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": ":bell: *Approval Required*\n*Object:* {{ $('Fetch Approval Details').item.json.data.flow_name ?? $json.body.data.object_type }}\n*Action:* {{ $('Fetch Approval Details').item.json.data.action_type }}\n*Approval ID:* `{{ $json.body.data.approval_id }}`"
              }
            },
            {
              "type": "actions",
              "elements": [
                {
                  "type": "button",
                  "text": {
                    "type": "plain_text",
                    "text": "Approve"
                  },
                  "style": "primary",
                  "value": "approve:{{ $json.body.data.approval_id }}"
                },
                {
                  "type": "button",
                  "text": {
                    "type": "plain_text",
                    "text": "Reject"
                  },
                  "style": "danger",
                  "value": "reject:{{ $json.body.data.approval_id }}"
                }
              ]
            }
          ]
        },
        "options": {}
      },
      "notes": "Uses Slack Block Kit interactive buttons. Requires a Slack app with interactive components enabled."
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000006",
      "name": "Wait for Slack Response",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        1120,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "managecall-approval-decision",
        "responseMode": "onReceived",
        "options": {}
      },
      "notes": "Set this URL as the Slack interactive components Request URL."
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000008",
      "name": "Parse Decision",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1340,
        300
      ],
      "parameters": {
        "jsCode": "const payload = JSON.parse($input.first().body.payload ?? '{}');\nconst action = (payload.actions?.[0]?.value ?? '').split(':');\nconst [decision, approvalId] = action;\nif (!decision || !approvalId) throw new Error('Invalid Slack payload');\nreturn [{ json: { decision, approvalId } }];"
      }
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000009",
      "name": "Route Decision",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1560,
        300
      ],
      "parameters": {
        "mode": "rules",
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": false
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.decision }}",
                    "rightValue": "approve",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": false
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.decision }}",
                    "rightValue": "reject",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000010",
      "name": "POST Approve",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1780,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $env.MANAGECALL_API_URL }}/api/v1/approvals/{{ $json.approvalId }}/approve",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "json",
        "body": {},
        "options": {}
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "notes": "API key must have tenant.approvals.decide capability."
    },
    {
      "id": "b2c3d4e5-0002-0002-0002-000000000011",
      "name": "POST Reject",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1780,
        400
      ],
      "parameters": {
        "method": "POST",
        "url": "={{ $env.MANAGECALL_API_URL }}/api/v1/approvals/{{ $json.approvalId }}/reject",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "contentType": "json",
        "body": {
          "note": "Rejected via Slack review workflow"
        },
        "options": {}
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Verify Signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify Signature": {
      "main": [
        [
          {
            "node": "Fetch Approval Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Approval Details": {
      "main": [
        [
          {
            "node": "Post Decision Request to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Post Decision Request to Slack": {
      "main": [
        [
          {
            "node": "Wait for Slack Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for Slack Response": {
      "main": [
        [
          {
            "node": "Parse Decision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Decision": {
      "main": [
        [
          {
            "node": "Route Decision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route Decision": {
      "main": [
        [
          {
            "node": "POST Approve",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "POST Reject",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}