{
  "name": "VIP Escalation — Gorgias Webhook → Slack Alert",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "gorgias-vip-alert",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Gorgias Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "gorgias-vip-alert"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const ticket = $input.first().json;\nreturn [{\n  json: {\n    ticketId: ticket.id,\n    subject: ticket.subject || '(no subject)',\n    body: ticket.messages?.[0]?.body_text?.slice(0, 400) || '',\n    customerId: ticket.requester?.id,\n    customerEmail: ticket.requester?.email,\n    customerName: ticket.requester?.name || ticket.requester?.email,\n    existingTags: (ticket.requester?.meta?.tags || []).map(t => t.name),\n  }\n}];"
      },
      "id": "extract-data",
      "name": "Extract Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.existingTags.includes('vip') }}",
              "value2": true
            }
          ]
        }
      },
      "id": "check-vip-tag",
      "name": "Has VIP Tag?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://your-store.gorgias.com/api/customers/{{ $('Extract Data').item.json.customerId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "options": {}
      },
      "id": "fetch-customer",
      "name": "Fetch Customer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [660, 200],
      "credentials": {
        "httpBasicAuth": {
          "id": "credential-id",
          "name": "Gorgias API"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const customer = $input.first().json;\nconst stats = customer.meta || {};\n\nconst totalSpent = parseFloat(stats.shopify_total_spent || '0');\nconst orderCount = parseInt(stats.shopify_orders_count || '0', 10);\n\nconst isVip = totalSpent >= 500 || orderCount >= 5;\n\nreturn [{\n  json: {\n    isVip,\n    totalSpent,\n    orderCount,\n    customerEmail: customer.email,\n    customerName: customer.name || customer.email,\n  }\n}];"
      },
      "id": "ltv-check",
      "name": "LTV Check",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [880, 200]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isVip }}",
              "value2": true
            }
          ]
        }
      },
      "id": "is-vip",
      "name": "Is VIP?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1100, 200]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "support-vip",
          "mode": "name"
        },
        "messageType": "block",
        "blocksUi": "={\n  \"blocks\": [\n    {\n      \"type\": \"header\",\n      \"text\": {\n        \"type\": \"plain_text\",\n        \"text\": \"\\ud83d\\udea8 VIP Customer \\u2014 New Support Ticket\"\n      }\n    },\n    {\n      \"type\": \"section\",\n      \"fields\": [\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Customer*\\n\" + $('Extract Data').item.json.customerName\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Lifetime Value*\\n$\" + ($('LTV Check').item.json.totalSpent || 0).toFixed(0)\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Total Orders*\\n\" + ($('LTV Check').item.json.orderCount || 0)\n        },\n        {\n          \"type\": \"mrkdwn\",\n          \"text\": \"*Subject*\\n\" + $('Extract Data').item.json.subject\n        }\n      ]\n    },\n    {\n      \"type\": \"section\",\n      \"text\": {\n        \"type\": \"mrkdwn\",\n        \"text\": \"*Message preview*\\n\" + $('Extract Data').item.json.body\n      }\n    },\n    {\n      \"type\": \"actions\",\n      \"elements\": [\n        {\n          \"type\": \"button\",\n          \"text\": { \"type\": \"plain_text\", \"text\": \"Open in Gorgias\" },\n          \"url\": \"https://your-store.gorgias.com/app/ticket/\" + $('Extract Data').item.json.ticketId,\n          \"style\": \"primary\"\n        }\n      ]\n    }\n  ]\n}",
        "otherOptions": {
          "unfurl_links": false
        }
      },
      "id": "post-slack",
      "name": "Post Slack Alert",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [1320, 0],
      "credentials": {
        "slackApi": {
          "id": "credential-id",
          "name": "Slack API"
        }
      }
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://your-store.gorgias.com/api/tickets/{{ $('Extract Data').item.json.ticketId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{ \"tags\": [{ \"name\": \"vip-escalation\" }] }",
        "options": {}
      },
      "id": "tag-ticket",
      "name": "Tag as VIP",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1540, 0],
      "credentials": {
        "httpBasicAuth": {
          "id": "credential-id",
          "name": "Gorgias API"
        }
      }
    }
  ],
  "connections": {
    "Gorgias Webhook": {
      "main": [
        [
          {
            "node": "Extract Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Data": {
      "main": [
        [
          {
            "node": "Has VIP Tag?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has VIP Tag?": {
      "main": [
        [
          {
            "node": "Post Slack Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fetch Customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Customer": {
      "main": [
        [
          {
            "node": "LTV Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LTV Check": {
      "main": [
        [
          {
            "node": "Is VIP?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is VIP?": {
      "main": [
        [
          {
            "node": "Post Slack Alert",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Post Slack Alert": {
      "main": [
        [
          {
            "node": "Tag as VIP",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "active": false,
  "meta": {
    "instanceId": "",
    "templateId": "revi-gorgias-vip-escalation"
  }
}
