{
  "name": "SmartMart Reassessment",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "reassess",
        "responseMode": "responseNode",
        "responseData": "",
        "rawJson": "{\"incident_id\": \"INC-0001\"}",
        "options": {}
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222222",
      "name": "Reassess webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const body = $input.first().json.body || $input.first().json;\nreturn [{\n  json: { incidentId: body.incident_id || body.incidentId || '' }\n}];"
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222223",
      "name": "Read incident id",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SMARTMART_BASE_URL }}/api/v1/ai-operations/snapshot",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SMARTMART_N8N_TOKEN }}"
            },
            {
              "name": "Cookie",
              "value": "tunnel_phishing_protection=$env.TUNNEL_COOKIE"
            }
          ]
        },
        "sendQuery": false,
        "sendBody": false,
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222224",
      "name": "Collect current snapshot",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const snapBody = $input.first().json;\nconst snap = snapBody.data ?? snapBody;\nconst incident = $('Read incident id').first().json;\nconst risk = snap.risk || { score: 0, severity: 'NORMAL' };\nreturn [{\n  json: {\n    incidentId: incident.incidentId || '',\n    newScore: Number(risk.score || 0),\n    newSeverity: (risk.severity || 'NORMAL').toUpperCase(),\n    recovered: Number(risk.score || 0) < 50\n  }\n}];"
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222225",
      "name": "Compute new risk",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": {
            "boolean": [
              {
                "value1": "={{ $json.recovered }}",
                "operation": "true"
              }
            ]
          }
        },
        "options": {}
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222226",
      "name": "Recovered?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.1,
      "position": [880, 0]
    },
    {
      "parameters": {
        "url": "={{ $env.SMARTMART_BASE_URL }}/api/v1/ai-operations/incidents/{{ $json.incidentId }}",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SMARTMART_N8N_TOKEN }}"
            },
            {
              "name": "Cookie",
              "value": "tunnel_phishing_protection=$env.TUNNEL_COOKIE"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"status\": \"resolved\",\n  \"risk_score\": {{ $json.newScore }},\n  \"severity\": \"{{ $json.newSeverity }}\"\n}",
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222227",
      "name": "Resolve incident",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1100, -160]
    },
    {
      "parameters": {
        "url": "={{ $env.SMARTMART_BASE_URL }}/api/v1/ai-operations/incidents/{{ $json.incidentId }}",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.SMARTMART_N8N_TOKEN }}"
            },
            {
              "name": "Cookie",
              "value": "tunnel_phishing_protection=$env.TUNNEL_COOKIE"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"risk_score\": {{ $json.newScore }},\n  \"severity\": \"{{ $json.newSeverity }}\",\n  \"status\": \"reassessing\"\n}",
        "sendPreflight": false,
        "options": {
          "timeout": 10000
        }
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222228",
      "name": "Update incident risk",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1100, 160]
    },
    {
      "parameters": {
        "mode": "combine",
        "combineMode": "append"
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-222222222229",
      "name": "Merge outcome",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2.2,
      "position": [1320, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const body = $input.first().json;\nconst data = body.data ?? body;\nreturn [{\n  json: {\n    incidentId: data.incident_id || '',\n    status: data.status || '',\n    severity: data.severity || '',\n    riskScore: data.risk_score ?? null\n  }\n}];"
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-22222222222a",
      "name": "Outcome summary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1540, 0]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, data: $json }) }}",
        "options": {}
      },
      "id": "a3b9e8f2-3333-4d5e-bf60-22222222222b",
      "name": "Respond to webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1760, 0]
    }
  ],
  "connections": {
    "Reassess webhook": {
      "main": [[{ "node": "Read incident id", "type": "main", "index": 0 }]]
    },
    "Read incident id": {
      "main": [[{ "node": "Compute new risk", "type": "main", "index": 0 }]]
    },
    "Collect current snapshot": {
      "main": [[{ "node": "Compute new risk", "type": "main", "index": 1 }]]
    },
    "Compute new risk": {
      "main": [[{ "node": "Recovered?", "type": "main", "index": 0 }]]
    },
    "Recovered?": {
      "main": [
        [{ "node": "Resolve incident", "type": "main", "index": 0 }],
        [{ "node": "Update incident risk", "type": "main", "index": 0 }]
      ]
    },
    "Resolve incident": {
      "main": [[{ "node": "Merge outcome", "type": "main", "index": 0 }]]
    },
    "Update incident risk": {
      "main": [[{ "node": "Merge outcome", "type": "main", "index": 1 }]]
    },
    "Merge outcome": {
      "main": [[{ "node": "Outcome summary", "type": "main", "index": 0 }]]
    },
    "Outcome summary": {
      "main": [[{ "node": "Respond to webhook", "type": "main", "index": 0 }]]
    }
  },
  "settings": {
    "errorWorkflow": "SmartMart Error Handler",
    "timezone": "Africa/Lagos",
    "executionOrder": "v1"
  }
}