{
  "name": "Corrigé 3 - Webhook Monitoring AgroTech",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agri-monitoring-alert",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "3c405570-5033-46d6-9951-7af479a346b9",
      "name": "Webhook Alerte IoT",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        112,
        400
      ],
      "webhookId": "1ae30c41-9ab6-4eb0-bdb3-b1cc40bcf591"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "out_source",
              "name": "out_source",
              "value": "monitoring_iot",
              "type": "string"
            },
            {
              "id": "out_subject",
              "name": "out_subject",
              "value": "={{ $json.body.alert_title || $json.body.subject || 'Alerte Système IoT' }}",
              "type": "string"
            },
            {
              "id": "out_message",
              "name": "out_message",
              "value": "={{ $json.body.incident_details || $json.body.message || 'Aucun détail fourni' }}",
              "type": "string"
            },
            {
              "id": "out_priority",
              "name": "out_priority",
              "value": "={{ ['critical', '1', 'high'].includes(String($json.body.severity || '').toLowerCase()) ? 'high' : (['warning', '2', 'medium'].includes(String($json.body.severity || '').toLowerCase()) ? 'medium' : ($json.body.severity ? 'low' : 'medium')) }}",
              "type": "string"
            },
            {
              "id": "out_device_email",
              "name": "out_device_email",
              "value": "={{ null }}",
              "type": "string"
            },
            {
              "id": "out_created_at",
              "name": "out_created_at",
              "value": "={{ $json.body.triggered_at ? new Date($json.body.triggered_at).toISOString() : new Date().toISOString() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "361df7fd-b9f2-4f4a-88b1-23a66774826e",
      "name": "Normalisation Alerte IoT - Edit Fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        496,
        256
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "d7ce5016-02ba-4c06-8c43-97436c19429f",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.5,
      "position": [
        912,
        400
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body || {};\n\nlet out_priority = \"medium\";\nif (body.severity) {\n  const sev = String(body.severity).toLowerCase();\n  if (['critical', '1', 'high'].includes(sev)) {\n    out_priority = \"high\";\n  } else if (['warning', '2', 'medium'].includes(sev)) {\n    out_priority = \"medium\";\n  } else {\n    out_priority = \"low\";\n  }\n}\n\nlet out_created_at = new Date().toISOString();\nif (body.triggered_at) {\n  const parsed = new Date(body.triggered_at);\n  if (!isNaN(parsed.getTime())) {\n    out_created_at = parsed.toISOString();\n  }\n}\n\nreturn {\n  out_source: \"monitoring_iot\",\n  out_subject: body.alert_title || body.subject || \"Alerte Système IoT\",\n  out_message: body.incident_details || body.message || \"Aucun détail fourni\",\n  out_priority,\n  out_device_email: null,\n  out_created_at\n};"
      },
      "id": "1f2f0e2b-83f7-40b7-b829-862f33c96cec",
      "name": "Normalisation Alerte IoT - Code JS",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        512,
        512
      ]
    },
    {
      "parameters": {
        "content": "### Corrigé 3 - Webhook Monitoring AgroTech\n\n**Déclencheur**\nWebhook `POST` sur `/webhook/agri-monitoring-alert` (mode test : `/webhook-test/agri-monitoring-alert`).\n\n**Normalisation**\nDeux variantes side-by-side :\n1. Edit Fields (expressions ternaires)\n2. Code JS (JavaScript impératif)\n\n**Réponse**\nRenvoie l'objet `out_*` normalisé via `Respond to Webhook`.\n\n**Commande de test**\n```bash\ncurl -X POST \"http://localhost:5678/webhook-test/agri-monitoring-alert\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\\\n    \"alert_title\": \"Alerte Gel - Sonde Parcelle Nord\",\\\n    \"incident_details\": \"Chute de température sous -2°C détectée à 05:00\",\\\n    \"severity\": \"critical\",\\\n    \"triggered_at\": \"2026-03-15T05:00:00Z\"\\\n  }'\n```",
        "height": 1162,
        "width": 1764,
        "color": 2
      },
      "id": "dec06b96-34c3-4b1b-b218-f6ee3f37cfc9",
      "name": "Sticky Note 494e0278",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -416,
        -384
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook Alerte IoT": {
      "main": [
        [
          {
            "node": "Normalisation Alerte IoT - Edit Fields",
            "type": "main",
            "index": 0
          },
          {
            "node": "Normalisation Alerte IoT - Code JS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalisation Alerte IoT - Edit Fields": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalisation Alerte IoT - Code JS": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": true
  },
  "versionId": "5a1db66f-7bf6-4503-9ccc-bcb9390228d2",
  "meta": {
    "aiBuilderAssisted": true,
    "builderVariant": "mcp",
    "instanceId": "ee4605bad6c4b5003dc41d3ea7bedaa9c80198c8426442908a1e821a57858a1d"
  },
  "nodeGroups": [],
  "id": "hq7wI1GbvU7pJweP",
  "tags": []
}