{
  "name": "Boulangerie — Custom Order Manager",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "commande-personnalisee",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a1b2c3d4-b001-b002-b003-b00400000001",
      "name": "Webhook — Commande personnalisée",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "values": {
          "string": [
            { "name": "orderId", "value": "={{ 'CMD-' + Date.now() }}" },
            { "name": "clientNom", "value": "={{ $json.body.nom }}" },
            { "name": "clientEmail", "value": "={{ $json.body.email }}" },
            { "name": "clientTel", "value": "={{ $json.body.telephone }}" },
            { "name": "produit", "value": "={{ $json.body.produit }}" },
            { "name": "description", "value": "={{ $json.body.description }}" },
            { "name": "nombrePersonnes", "value": "={{ $json.body.personnes }}" },
            { "name": "dateRetrait", "value": "={{ $json.body.date_retrait }}" },
            { "name": "heureRetrait", "value": "={{ $json.body.heure_retrait }}" },
            { "name": "dateCommande", "value": "={{ new Date().toLocaleDateString('fr-FR') }}" },
            { "name": "dateAlerte", "value": "={{ new Date(new Date($json.body.date_retrait).getTime() - 2*86400000).toLocaleDateString('fr-FR') }}" }
          ]
        },
        "options": {}
      },
      "id": "b2c3d4e5-b001-b002-b003-b00400000002",
      "name": "Extraire la commande",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [450, 300]
    },
    {
      "parameters": {
        "authentication": "serviceAccount",
        "operation": "append",
        "documentId": { "__rl": true, "mode": "id", "value": "VOTRE_GOOGLE_SHEET_ID" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Commandes sur mesure" },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "ID": "={{ $json.orderId }}",
            "Client": "={{ $json.clientNom }}",
            "Email": "={{ $json.clientEmail }}",
            "Téléphone": "={{ $json.clientTel }}",
            "Produit": "={{ $json.produit }}",
            "Description": "={{ $json.description }}",
            "Personnes": "={{ $json.nombrePersonnes }}",
            "Date retrait": "={{ $json.dateRetrait }}",
            "Heure retrait": "={{ $json.heureRetrait }}",
            "Date commande": "={{ $json.dateCommande }}",
            "Statut": "En attente"
          }
        },
        "options": {}
      },
      "id": "c3d4e5f6-b001-b002-b003-b00400000003",
      "name": "Enregistrer dans Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [650, 300]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.clientEmail }}",
        "subject": "Commande confirmée — {{ $json.produit }} — {{ $json.dateRetrait }}",
        "message": "Bonjour {{ $json.clientNom }},\n\nVotre commande a bien été enregistrée !\n\nRéférence : {{ $json.orderId }}\nProduit : {{ $json.produit }}\nDescription : {{ $json.description }}\nNombre de personnes : {{ $json.nombrePersonnes }}\n\nDate de retrait : {{ $json.dateRetrait }} à {{ $json.heureRetrait }}\n\nNous vous contacterons si nous avons des questions. En cas d'empêchement, merci de nous prévenir au moins 48h à l'avance.\n\nÀ bientôt,\nVotre boulangerie",
        "options": {}
      },
      "id": "d4e5f6a7-b001-b002-b003-b00400000004",
      "name": "Confirmation client",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [850, 200]
    },
    {
      "parameters": {
        "rule": { "interval": [{ "field": "cronExpression", "expression": "0 7 * * *" }] }
      },
      "id": "e5f6a7b8-b001-b002-b003-b00400000005",
      "name": "Vérification quotidienne 7h",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [250, 560]
    },
    {
      "parameters": {
        "authentication": "serviceAccount",
        "operation": "readRows",
        "documentId": { "__rl": true, "mode": "id", "value": "VOTRE_GOOGLE_SHEET_ID" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Commandes sur mesure" },
        "filters": {
          "conditions": [{ "leftValue": "={{ $json['Statut'] }}", "rightValue": "En attente", "operator": { "type": "string", "operation": "equals" } }]
        },
        "options": {}
      },
      "id": "f6a7b8c9-b001-b002-b003-b00400000006",
      "name": "Lire commandes en attente",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [450, 560]
    },
    {
      "parameters": {
        "jsCode": "const today = new Date().toLocaleDateString('fr-FR');\nconst rows = $input.all();\nreturn rows.filter(r => {\n  const d = new Date(r.json['Date retrait']);\n  const alert = new Date(d.getTime() - 2*86400000);\n  return alert.toLocaleDateString('fr-FR') === today;\n});"
      },
      "id": "a7b8c9d0-b001-b002-b003-b00400000007",
      "name": "Retrait dans 48h ?",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [650, 560]
    },
    {
      "parameters": {
        "sendTo": "boulangerie@votre-boulangerie.fr",
        "subject": "⏰ Rappel — Commande à préparer : {{ $json['Produit'] }}",
        "message": "Rappel : une commande doit être retirée dans 2 jours !\n\nCommande : {{ $json['ID'] }}\nClient : {{ $json['Client'] }} ({{ $json['Téléphone'] }})\nProduit : {{ $json['Produit'] }}\nDescription : {{ $json['Description'] }}\nPersonnes : {{ $json['Personnes'] }}\nRetrait : {{ $json['Date retrait'] }} à {{ $json['Heure retrait'] }}",
        "options": {}
      },
      "id": "b8c9d0e1-b001-b002-b003-b00400000008",
      "name": "Alerte boulangerie",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [850, 560]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"success\": true, \"orderId\": \"{{ $json.orderId }}\" }",
        "options": {}
      },
      "id": "c9d0e1f2-b001-b002-b003-b00400000009",
      "name": "Réponse",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [1050, 200]
    }
  ],
  "connections": {
    "Webhook — Commande personnalisée": { "main": [[{ "node": "Extraire la commande", "type": "main", "index": 0 }]] },
    "Extraire la commande": { "main": [[{ "node": "Enregistrer dans Sheets", "type": "main", "index": 0 }]] },
    "Enregistrer dans Sheets": { "main": [[{ "node": "Confirmation client", "type": "main", "index": 0 }]] },
    "Confirmation client": { "main": [[{ "node": "Réponse", "type": "main", "index": 0 }]] },
    "Vérification quotidienne 7h": { "main": [[{ "node": "Lire commandes en attente", "type": "main", "index": 0 }]] },
    "Lire commandes en attente": { "main": [[{ "node": "Retrait dans 48h ?", "type": "main", "index": 0 }]] },
    "Retrait dans 48h ?": { "main": [[{ "node": "Alerte boulangerie", "type": "main", "index": 0 }]] }
  },
  "active": false,
  "settings": { "executionOrder": "v1" },
  "tags": [{ "name": "boulangerie" }, { "name": "commandes" }],
  "meta": {
    "templateId": "likyly-boulangerie-custom-order",
    "templateVersion": "1.0",
    "description": "Gère les commandes sur mesure (gâteaux d'anniversaire, pièces montées) avec confirmation client et rappel automatique 48h avant le retrait.",
    "instructions": "1. Créez la feuille 'Commandes sur mesure' avec les colonnes listées\n2. Remplacez VOTRE_GOOGLE_SHEET_ID et les emails\n3. Configurez Gmail\n4. Activez le webhook et la vérification quotidienne\n5. Intégrez l'URL webhook dans votre formulaire de commande"
  }
}
