{
  "name": "Relances Devis Automatiques - IntuitionConcept",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * *"
            }
          ]
        }
      },
      "id": "trigger-cron",
      "name": "D\u00e9clencheur Quotidien",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "notes": "Ex\u00e9cution tous les jours \u00e0 9h"
    },
    {
      "parameters": {
        "url": "={{$env.VITE_API_BASE_URL}}/api/quotes/follow-up/pending",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "id": "get-quotes",
      "name": "R\u00e9cup\u00e9rer Devis \u00e0 Relancer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        450,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "notes": "Appel API pour r\u00e9cup\u00e9rer les devis n\u00e9cessitant une relance"
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.quotes.length}}",
              "operation": "larger",
              "value2": 0
            }
          ]
        }
      },
      "id": "check-quotes",
      "name": "V\u00e9rifier si Devis",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "notes": "Continuer seulement s'il y a des devis"
    },
    {
      "parameters": {
        "fieldToSplitOut": "quotes",
        "options": {}
      },
      "id": "split-quotes",
      "name": "S\u00e9parer Devis",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        850,
        200
      ],
      "notes": "Traiter chaque devis individuellement"
    },
    {
      "parameters": {
        "jsCode": "// G\u00e9n\u00e9rer le contenu de l'email de relance\nconst quote = $input.item.json;\nconst followUpNumber = (quote.followUpCount || 0) + 1;\n\n// Calculer jours restants avant expiration\nlet daysLeft = null;\nif (quote.expiresAt) {\n  const expiry = new Date(quote.expiresAt);\n  const now = new Date();\n  daysLeft = Math.ceil((expiry - now) / (1000 * 60 * 60 * 24));\n}\n\n// D\u00e9finir le sujet selon le num\u00e9ro de relance\nlet subject, urgency;\nif (followUpNumber === 1) {\n  subject = `Rappel : Votre devis pour ${quote.projectName}`;\n  urgency = 'Nous esp\u00e9rons que vous avez eu le temps de consulter notre proposition.';\n} else if (followUpNumber === 2) {\n  subject = `2\u00e8me rappel : Devis ${quote.projectName} - Besoin d'informations ?`;\n  urgency = 'Nous souhaitons nous assurer que vous avez toutes les informations n\u00e9cessaires.';\n} else {\n  subject = `Derni\u00e8re chance : Devis ${quote.projectName}`;\n  urgency = 'C\\'est votre derni\u00e8re opportunit\u00e9 de b\u00e9n\u00e9ficier de cette offre.';\n}\n\n// Texte d'expiration\nconst expiryText = daysLeft \n  ? `<p><strong>\u26a0\ufe0f Attention :</strong> Ce devis expire dans ${daysLeft} jour${daysLeft > 1 ? 's' : ''}.</p>`\n  : '';\n\n// G\u00e9n\u00e9rer le corps HTML\nconst body = `\n<!DOCTYPE html>\n<html>\n<head>\n    <meta charset=\"UTF-8\">\n    <style>\n        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }\n        .container { max-width: 600px; margin: 0 auto; padding: 20px; }\n        .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; border-radius: 10px 10px 0 0; }\n        .content { background: #f9f9f9; padding: 30px; border-radius: 0 0 10px 10px; }\n        .quote-details { background: white; padding: 20px; border-radius: 8px; margin: 20px 0; }\n        .amount { font-size: 32px; font-weight: bold; color: #667eea; }\n        .cta-button { display: inline-block; background: #667eea; color: white; padding: 15px 40px; text-decoration: none; border-radius: 8px; font-weight: bold; margin: 20px 0; }\n        .urgency { background: #fff3cd; border-left: 4px solid #ffc107; padding: 15px; margin: 20px 0; }\n        .footer { text-align: center; color: #666; font-size: 12px; margin-top: 30px; }\n    </style>\n</head>\n<body>\n    <div class=\"container\">\n        <div class=\"header\">\n            <h1>\ud83c\udfd7\ufe0f IntuitionConcept</h1>\n            <p>Votre partenaire BTP de confiance</p>\n        </div>\n        \n        <div class=\"content\">\n            <h2>Bonjour ${quote.clientName},</h2>\n            \n            <p>${urgency}</p>\n            \n            <div class=\"quote-details\">\n                <h3>\ud83d\udccb D\u00e9tails du devis</h3>\n                <p><strong>Projet :</strong> ${quote.projectName}</p>\n                <p><strong>Montant :</strong> <span class=\"amount\">${quote.amount.toLocaleString('fr-FR')} FCFA</span></p>\n                ${quote.sentAt ? `<p><strong>Envoy\u00e9 le :</strong> ${new Date(quote.sentAt).toLocaleDateString('fr-FR')}</p>` : ''}\n                ${quote.viewedAt ? `<p>\u2705 <em>Vous avez consult\u00e9 ce devis le ${new Date(quote.viewedAt).toLocaleDateString('fr-FR')}</em></p>` : ''}\n            </div>\n            \n            ${expiryText}\n            \n            <div class=\"urgency\">\n                <strong>\ud83d\udca1 Besoin d'aide ?</strong><br>\n                Notre \u00e9quipe est \u00e0 votre disposition pour r\u00e9pondre \u00e0 toutes vos questions :\n                <ul>\n                    <li>\ud83d\udcde Appelez-nous au +221 XX XXX XX XX</li>\n                    <li>\ud83d\udce7 R\u00e9pondez \u00e0 cet email</li>\n                    <li>\ud83d\udcac Contactez-nous via WhatsApp</li>\n                </ul>\n            </div>\n            \n            <div style=\"text-align: center;\">\n                <a href=\"https://intuitionconcept.com/quotes/${quote.id}\" class=\"cta-button\">\n                    \ud83d\udcc4 Consulter mon devis\n                </a>\n            </div>\n            \n            <p style=\"margin-top: 30px;\">\n                Nous serions ravis de collaborer avec vous sur ce projet !\n            </p>\n            \n            <p>\n                Cordialement,<br>\n                <strong>L'\u00e9quipe IntuitionConcept</strong>\n            </p>\n        </div>\n        \n        <div class=\"footer\">\n            <p>IntuitionConcept - Plateforme de Gestion BTP Intelligente</p>\n            <p>Dakar, S\u00e9n\u00e9gal | www.intuitionconcept.com</p>\n        </div>\n    </div>\n</body>\n</html>\n`;\n\nreturn {\n  quoteId: quote.id,\n  clientEmail: quote.clientEmail,\n  clientName: quote.clientName,\n  subject: subject,\n  body: body,\n  followUpNumber: followUpNumber,\n  shouldSendSMS: followUpNumber >= 3 || (daysLeft && daysLeft <= 2)\n};"
      },
      "id": "generate-email",
      "name": "G\u00e9n\u00e9rer Email",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1050,
        200
      ],
      "notes": "G\u00e9n\u00e8re le contenu personnalis\u00e9 de l'email"
    },
    {
      "parameters": {
        "fromEmail": "noreply@intuitionconcept.com",
        "toEmail": "={{$json.clientEmail}}",
        "subject": "={{$json.subject}}",
        "emailType": "html",
        "message": "={{$json.body}}",
        "options": {}
      },
      "id": "send-email",
      "name": "Envoyer Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        1250,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      },
      "notes": "Envoi de l'email via SMTP"
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.shouldSendSMS}}",
              "value2": true
            }
          ]
        }
      },
      "id": "check-sms",
      "name": "V\u00e9rifier SMS",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1450,
        200
      ],
      "notes": "Envoyer SMS seulement si urgent"
    },
    {
      "parameters": {
        "url": "https://api.twilio.com/2010-04-01/Accounts/{{$env.TWILIO_ACCOUNT_SID}}/Messages.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "={{$json.clientPhone}}"
            },
            {
              "name": "From",
              "value": "={{$env.TWILIO_PHONE_NUMBER}}"
            },
            {
              "name": "Body",
              "value": "\u26a0\ufe0f URGENT ${$json.clientName} ! Votre devis \"${$json.projectName}\" expire bient\u00f4t. Consultez-le : https://intuitionconcept.com/quotes/${$json.quoteId}"
            }
          ]
        },
        "options": {}
      },
      "id": "send-sms",
      "name": "Envoyer SMS",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1650,
        100
      ],
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        }
      },
      "notes": "Envoi SMS via Twilio"
    },
    {
      "parameters": {
        "url": "={{$env.VITE_API_BASE_URL}}/api/quotes/{{$json.quoteId}}/mark-followed-up",
        "method": "POST",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "followUpNumber",
              "value": "={{$json.followUpNumber}}"
            },
            {
              "name": "emailSent",
              "value": true
            },
            {
              "name": "smsSent",
              "value": "={{$json.shouldSendSMS}}"
            }
          ]
        },
        "options": {}
      },
      "id": "update-quote",
      "name": "Mettre \u00e0 Jour Devis",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1850,
        200
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "notes": "Marquer le devis comme relanc\u00e9"
    },
    {
      "parameters": {},
      "id": "loop-back",
      "name": "Retour Boucle",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        2050,
        200
      ],
      "notes": "Retour pour traiter le devis suivant"
    },
    {
      "parameters": {
        "url": "={{$env.VITE_API_BASE_URL}}/api/automation/log",
        "method": "POST",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "workflowName",
              "value": "quote-follow-up"
            },
            {
              "name": "status",
              "value": "completed"
            },
            {
              "name": "quotesProcessed",
              "value": "={{$json.quotes.length}}"
            },
            {
              "name": "timestamp",
              "value": "={{new Date().toISOString()}}"
            }
          ]
        },
        "options": {}
      },
      "id": "log-completion",
      "name": "Logger Ex\u00e9cution",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        850,
        400
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "notes": "Enregistrer les statistiques d'ex\u00e9cution"
    }
  ],
  "connections": {
    "D\u00e9clencheur Quotidien": {
      "main": [
        [
          {
            "node": "R\u00e9cup\u00e9rer Devis \u00e0 Relancer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "R\u00e9cup\u00e9rer Devis \u00e0 Relancer": {
      "main": [
        [
          {
            "node": "V\u00e9rifier si Devis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "V\u00e9rifier si Devis": {
      "main": [
        [
          {
            "node": "S\u00e9parer Devis",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Logger Ex\u00e9cution",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "S\u00e9parer Devis": {
      "main": [
        [
          {
            "node": "G\u00e9n\u00e9rer Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "G\u00e9n\u00e9rer Email": {
      "main": [
        [
          {
            "node": "Envoyer Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Envoyer Email": {
      "main": [
        [
          {
            "node": "V\u00e9rifier SMS",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "V\u00e9rifier SMS": {
      "main": [
        [
          {
            "node": "Envoyer SMS",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mettre \u00e0 Jour Devis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Envoyer SMS": {
      "main": [
        [
          {
            "node": "Mettre \u00e0 Jour Devis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mettre \u00e0 Jour Devis": {
      "main": [
        [
          {
            "node": "Retour Boucle",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retour Boucle": {
      "main": [
        [
          {
            "node": "S\u00e9parer Devis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1",
  "id": "quote-follow-up-workflow",
  "tags": [
    {
      "createdAt": "2025-01-18T00:00:00.000Z",
      "updatedAt": "2025-01-18T00:00:00.000Z",
      "id": "1",
      "name": "BTP"
    },
    {
      "createdAt": "2025-01-18T00:00:00.000Z",
      "updatedAt": "2025-01-18T00:00:00.000Z",
      "id": "2",
      "name": "Automatisation"
    },
    {
      "createdAt": "2025-01-18T00:00:00.000Z",
      "updatedAt": "2025-01-18T00:00:00.000Z",
      "id": "3",
      "name": "Devis"
    }
  ]
}