{
  "name": "Bunche Logger",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "bunche-logger",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "wf11-trigger",
      "name": "Webhook: Bunche Logger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "functionCode": "// Normalize incoming log event to match customer_audit_log schema\nconst body = $input.first().json || {};\n\n// Build the details JSONB object with all extra fields\nconst details = {\n    source: body.source || 'n8n-webhook',\n    workflow_id: body.workflow_id || null,\n    customer_hash: body.customer_hash || null,\n    duration_ms: body.duration_ms || null,\n    correlation_id: body.correlation_id || null,\n    n8n_execution_id: body.n8n_execution_id || null,\n    metadata: typeof body.metadata === 'object' ? body.metadata : {}\n};\n\n// request_id comes from the incoming request or is generated\nconst request_id = body.request_id || body.correlation_id || null;\n\nreturn [{ json: {\n    request_id,\n    event_type: body.event_type || 'unknown',\n    order_id: body.order_id || null,\n    workflow: body.workflow || 'unknown',\n    status: body.status || 'success',\n    details\n}}];"
      },
      "id": "wf11-normalize",
      "name": "Normalize to Audit Schema",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "customer_audit_log",
        "columns": "request_id, event_type, order_id, workflow, status, details",
        "values": "'{{ $json.request_id }}', '{{ $json.event_type }}', '{{ $json.order_id }}', '{{ $json.workflow }}', '{{ $json.status }}', '{{ JSON.stringify($json.details) }}'"
      },
      "id": "wf11-insert",
      "name": "Insert Audit Log",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        400,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\"status\":\"logged\",\"event_type\":\"{{ $json.event_type }}\"}"
      },
      "id": "wf11-respond",
      "name": "Respond 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        600,
        0
      ]
    }
  ],
  "connections": {
    "Webhook: Bunche Logger": {
      "main": [
        [
          {
            "node": "Normalize to Audit Schema",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize to Audit Schema": {
      "main": [
        [
          {
            "node": "Insert Audit Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Audit Log": {
      "main": [
        [
          {
            "node": "Respond 200",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": false,
    "timezone": "Africa/Lagos"
  },
  "staticData": null,
  "tags": [
    "logger",
    "audit",
    "webhook"
  ]
}