{
  "name": "Corrigé 4 - Résilience & Erreur HubSpot AgroTech",
  "nodes": [
    {
      "parameters": {},
      "id": "d12770e5-ad84-4012-b592-ab9a7c5d24d3",
      "name": "When clicking 'Test workflow'",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        112,
        512
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "out_source",
              "name": "out_source",
              "value": "Station Météo Web",
              "type": "string"
            },
            {
              "id": "out_subject",
              "name": "out_subject",
              "value": "Risque de Gel",
              "type": "string"
            },
            {
              "id": "out_message",
              "name": "out_message",
              "value": "Chute de température brutale sous 0°C",
              "type": "string"
            },
            {
              "id": "out_device_email",
              "name": "out_device_email",
              "value": "emailmaria@hubspot.com",
              "type": "string"
            },
            {
              "id": "out_priority",
              "name": "out_priority",
              "value": "high",
              "type": "string"
            },
            {
              "id": "out_created_at",
              "name": "out_created_at",
              "value": "2026-03-15T06:00:00Z",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "e8cb7050-6aa0-4b61-b363-14e6c522ec16",
      "name": "Simuler relevé normalisé",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        400,
        512
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.hubspot.com/crm/v3/objects/contacts/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer INVALIDE_POUR_TEST"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { \"query\": $json.out_device_email } }}",
        "options": {}
      },
      "id": "5b449b9b-b875-4979-9d86-2ea3397989c9",
      "name": "Rechercher Exploitation HubSpot",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        704,
        512
      ],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "out_farm_owner",
              "name": "out_farm_owner",
              "value": "={{ ($json.results[0].properties.firstname || '') + ' ' + ($json.results[0].properties.lastname || '') }}",
              "type": "string"
            },
            {
              "id": "out_farm_name",
              "name": "out_farm_name",
              "value": "={{ $json.results[0].properties.company || null }}",
              "type": "string"
            },
            {
              "id": "out_farm_crop_type",
              "name": "out_farm_crop_type",
              "value": "={{ $json.results[0].properties.jobtitle || 'Viticulture Standard' }}",
              "type": "string"
            },
            {
              "id": "out_farm_registered_since",
              "name": "out_farm_registered_since",
              "value": "={{ $json.results[0].properties.createdate }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "fade5172-9486-44fc-a717-4d695a0f6067",
      "name": "Enrichir Relevé (Succès)",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1104,
        208
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "out_farm_owner",
              "name": "out_farm_owner",
              "value": "={{ null }}",
              "type": "string"
            },
            {
              "id": "out_farm_name",
              "name": "out_farm_name",
              "value": "={{ null }}",
              "type": "string"
            },
            {
              "id": "out_farm_crop_type",
              "name": "out_farm_crop_type",
              "value": "={{ null }}",
              "type": "string"
            },
            {
              "id": "out_farm_registered_since",
              "name": "out_farm_registered_since",
              "value": "={{ null }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "a5fb820d-e963-47e5-89ef-598f4713cb95",
      "name": "Fallback Sans Enrichissement",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1104,
        512
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "notification_payload",
              "name": "notification_payload",
              "value": "={{ $json.slack_message }}",
              "type": "string"
            },
            {
              "id": "sent_at",
              "name": "sent_at",
              "value": "={{ new Date().toISOString() }}",
              "type": "string"
            },
            {
              "id": "channel",
              "name": "channel",
              "value": "#alerts-agrotech",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "1d36d469-c6d9-4f3b-b335-62a56932fc12",
      "name": "Envoyer Alerte Slack/Discord",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1504,
        800
      ]
    },
    {
      "parameters": {
        "jsCode": "const statusCode = $json.error?.status || $json.error?.statusCode || 500;\nconst is4xx = statusCode >= 400 && statusCode < 500;\nconst errorCategory = is4xx ? '🔴 [4xx] Erreur de Configuration / Auth' : '🟠 [5xx/Timeout] Panne Serveur HubSpot';\n\nconst prevData = $('Simuler relevé normalisé').item.json;\n\nreturn {\n  workflow_name: 'Corrigé 4 - Résilience HubSpot Agrotech',\n  timestamp: new Date().toISOString(),\n  device_email: prevData.out_device_email,\n  status_code: statusCode,\n  error_category: errorCategory,\n  error_message: $json.error?.message || 'HubSpot indisponible',\n  slack_message: `🚨 *Alerte Pipeline Agrotech*\\n• *Workflow*: Corrigé 4\\n• *Capteur*: ${prevData.out_device_email}\\n• *Type d'erreur*: ${errorCategory} (Code ${statusCode})\\n• *Action*: Passage automatique en mode dégradé.`\n};"
      },
      "id": "6be8b6e7-030f-4f79-98cb-4a5e58734f76",
      "name": "Formater Alerte Erreur",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1104,
        800
      ]
    },
    {
      "parameters": {
        "content": "### Corrigé 4 - Résilience HubSpot AgroTech\n\n**Objectif**\nGérer l'indisponibilité de l'API HubSpot lors de la recherche d'une exploitation.\n\n**Comportement**\n- **Succès** : enrichissement avec les champs `out_farm_*`.\n- **Erreur** :\n  1. Mode dégradé : `out_farm_*` forcés à `null`.\n  2. Alerte structurée : catégorisation 4xx vs 5xx/timeout.\n  3. Simulation d'envoi Slack/Discord.\n\n**Test**\nLe nœud HTTP utilise `Authorization: Bearer INVALIDE_POUR_TEST` pour forcer une erreur 401 et valider la résilience.",
        "height": 866,
        "width": 1796,
        "color": 3
      },
      "id": "02865bf3-993c-4222-aeec-222f76249117",
      "name": "Sticky Note 241822f2",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -32,
        96
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "When clicking 'Test workflow'": {
      "main": [
        [
          {
            "node": "Simuler relevé normalisé",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simuler relevé normalisé": {
      "main": [
        [
          {
            "node": "Rechercher Exploitation HubSpot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rechercher Exploitation HubSpot": {
      "main": [
        [
          {
            "node": "Enrichir Relevé (Succès)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Fallback Sans Enrichissement",
            "type": "main",
            "index": 0
          },
          {
            "node": "Formater Alerte Erreur",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Formater Alerte Erreur": {
      "main": [
        [
          {
            "node": "Envoyer Alerte Slack/Discord",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": true
  },
  "versionId": "cbfdee6a-a302-4a95-9e25-f15bd1371dd0",
  "meta": {
    "aiBuilderAssisted": true,
    "builderVariant": "mcp",
    "instanceId": "ee4605bad6c4b5003dc41d3ea7bedaa9c80198c8426442908a1e821a57858a1d"
  },
  "nodeGroups": [],
  "id": "WwcVR6j9lryZKoNd",
  "tags": []
}