{
  "name": "storm_mode",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "mode",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "d8c03429-1a9c-4446-8dd4-25d734871dcc",
      "name": "Webhook (POST)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -848,
        112
      ]
    },
    {
      "parameters": {
        "jsCode": "// 1. R\u00e9cup\u00e9ration des param\u00e8tres envoy\u00e9s\nconst body = $input.item.json.body;\nconst id_alloc = body.allocation_id;\nconst mode_vise = body.target || 'storm';\n\n// 2. Validation ID (Obligatoire pour idempotence)\nif (!id_alloc) {\n  throw new Error(\"Allocation ID manquant.\");\n}\n\n// 3. D\u00e9finition du pourcentage selon le mode\n// Storm = 60%, Nominal = 100%\nconst percent = (mode_vise === 'nominal') ? 100 : 60;\n\n// 4. Pr\u00e9paration des donn\u00e9es (Noms exacts de tes colonnes Airtable)\nreturn {\n  json: {\n    ts: new Date().toISOString(),\n    sector_id: 'global_boiler',\n    percent: percent,\n    mode: mode_vise,\n    allocation_id: id_alloc,\n    correlation_id: body.correlation_id || 'manual-' + Math.floor(Math.random() * 1000)\n  }\n};"
      },
      "id": "0af247a7-d4de-4e42-86e0-77eb8c87ab9c",
      "name": "Prepare Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -608,
        112
      ]
    },
    {
      "parameters": {
        "operation": "search",
        "base": {
          "__rl": true,
          "value": "appA5xT82tXKbH7ST",
          "mode": "list",
          "cachedResultName": "Hearthops",
          "cachedResultUrl": "https://airtable.com/appA5xT82tXKbH7ST"
        },
        "table": {
          "mode": "list",
          "value": "allocations"
        },
        "filterByFormula": "={allocation_id} = '{{ $json.allocation_id }}'",
        "returnAll": false,
        "limit": 1,
        "options": {}
      },
      "id": "f024aea8-20af-43a5-b74f-155dd59d80be",
      "name": "Check Idempotence",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        -384,
        112
      ],
      "alwaysOutputData": true,
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.id }}",
              "operation": "notEqual",
              "value2": "={{ undefined }}"
            }
          ]
        }
      },
      "id": "016167ca-0948-45b5-8b18-b3d6de99c4ec",
      "name": "Already Exists?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        -176,
        112
      ]
    },
    {
      "parameters": {
        "operation": "create",
        "base": {
          "__rl": true,
          "value": "appA5xT82tXKbH7ST",
          "mode": "list",
          "cachedResultName": "Hearthops",
          "cachedResultUrl": "https://airtable.com/appA5xT82tXKbH7ST"
        },
        "table": {
          "mode": "list",
          "value": "allocations"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "9f70fad7-9da8-478f-9a31-049240101357",
      "name": "Execute Order",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 2,
      "position": [
        192,
        208
      ],
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n \"status\": \"executed\",\n \"mode\": \"{{ $json.mode }}\",\n \"id\": \"{{ $json.allocation_id }}\"\n}",
        "options": {}
      },
      "id": "82817e2b-545f-41cb-8af8-58e054bc3aed",
      "name": "Respond Executed",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        368,
        208
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n \"status\": \"ignored (idempotent)\",\n \"message\": \"Order already processed.\"\n}",
        "options": {}
      },
      "id": "9d035f11-f428-4e73-b9bf-67f9dfbee815",
      "name": "Respond Ignored",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// On r\u00e9cup\u00e8re les donn\u00e9es du n\u0153ud Prepare Data car le n\u0153ud If/Check a chang\u00e9 le contexte\nconst originalData = $('Prepare Data').first().json;\n\nreturn {\n  json: originalData\n};"
      },
      "id": "155d4ff4-df27-4691-9e93-e32a8f79d935",
      "name": "Restore Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        0,
        208
      ]
    }
  ],
  "connections": {
    "Webhook (POST)": {
      "main": [
        [
          {
            "node": "Prepare Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Data": {
      "main": [
        [
          {
            "node": "Check Idempotence",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Idempotence": {
      "main": [
        [
          {
            "node": "Already Exists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Exists?": {
      "main": [
        [
          {
            "node": "Respond Ignored",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Restore Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restore Data": {
      "main": [
        [
          {
            "node": "Execute Order",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Order": {
      "main": [
        [
          {
            "node": "Respond Executed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2fbab8d6-70af-4b3d-ab18-2ccb5ea593fd",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "OWeCbMs4d0KigreZ",
  "tags": []
}