{
  "name": "Flow 3 - User Registration Automation",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "n8n/new-user",
        "responseMode": "responseNode",
        "options": {}
      },
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Extract user data from webhook body\nconst webhookData = items[0].json;\n\n// Debug: log what we received\nconsole.log('Received webhookData:', JSON.stringify(webhookData));\nconsole.log('Keys:', Object.keys(webhookData));\n\n// n8n webhook wraps POST data in 'body' key\n// Check if data is in body (from webhook) or at root level (from test)\nconst userData = webhookData.body || webhookData;\n\nconsole.log('Extracted userData:', JSON.stringify(userData));\n\n// Return array format for Function node v1\nreturn [{\n  json: {\n    user_id: userData.user_id || 0,\n    email: userData.email || '',\n    username: userData.username || '',\n    full_name: userData.full_name || userData.username || 'User',\n    token: userData.token || ''\n  }\n}];"
      },
      "name": "Function: Extract User Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.email}}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "name": "IF: Has Valid Email",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "fromEmail": "={{$env.EMAIL_FROM}}",
        "toEmail": "={{$json.email}}",
        "subject": "Ch\u00e0o m\u1eebng \u0111\u1ebfn v\u1edbi AI Deadline! \ud83c\udf89",
        "emailType": "html",
        "text": "=Xin ch\u00e0o {{$json.full_name}}!\n\nCh\u00fac m\u1eebng b\u1ea1n \u0111\u00e3 \u0111\u0103ng k\u00fd th\u00e0nh c\u00f4ng t\u00e0i kho\u1ea3n tr\u00ean h\u1ec7 th\u1ed1ng AI Deadline Forecasting Agent.\n\nCh\u00fang t\u00f4i \u0111\u00e3 t\u1ef1 \u0111\u1ed9ng t\u1ea1o s\u1eb5n 1 d\u1ef1 \u00e1n m\u1eabu \u0111\u1ec3 b\u1ea1n kh\u00e1m ph\u00e1 c\u00e1c t\u00ednh n\u0103ng c\u1ee7a h\u1ec7 th\u1ed1ng:\n\u2705 Dashboard tr\u1ef1c quan v\u1edbi bi\u1ec3u \u0111\u1ed3 th\u1ed1ng k\u00ea\n\ud83e\udd16 D\u1ef1 \u0111o\u00e1n nguy c\u01a1 tr\u1ec5 deadline b\u1eb1ng AI\n\ud83d\udcca Qu\u1ea3n l\u00fd d\u1ef1 \u00e1n v\u00e0 tasks hi\u1ec7u qu\u1ea3\n\ud83d\udd2e M\u00f4 ph\u1ecfng k\u1ecbch b\u1ea3n What-if\n\u2699\ufe0f T\u1ef1 \u0111\u1ed9ng h\u00f3a v\u1edbi n8n workflows\n\nH\u00e3y b\u1eaft \u0111\u1ea7u tr\u1ea3i nghi\u1ec7m ngay: http://localhost:5173/dashboard\n\nTh\u00f4ng tin \u0111\u0103ng nh\u1eadp:\n- T\u00ean \u0111\u0103ng nh\u1eadp: {{$json.username}}\n- Email: {{$json.email}}\n\n---\n\u00a9 2024 AI Deadline Forecasting Agent. All rights reserved.",
        "html": "=<html><head><style>body{font-family:Arial,sans-serif;line-height:1.6;color:#333}.container{max-width:600px;margin:0 auto;padding:20px}.header{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);color:white;padding:30px;text-align:center;border-radius:10px 10px 0 0}.content{background:#f9f9f9;padding:30px;border-radius:0 0 10px 10px}.button{display:inline-block;background:#667eea;color:white;padding:12px 30px;text-decoration:none;border-radius:5px;margin-top:20px}.footer{text-align:center;margin-top:20px;color:#666;font-size:12px}</style></head><body><div class='container'><div class='header'><h1>\ud83c\udf89 Ch\u00e0o m\u1eebng \u0111\u1ebfn v\u1edbi AI Deadline!</h1></div><div class='content'><h2>Xin ch\u00e0o {{$json.full_name}}!</h2><p>Ch\u00fac m\u1eebng b\u1ea1n \u0111\u00e3 \u0111\u0103ng k\u00fd th\u00e0nh c\u00f4ng t\u00e0i kho\u1ea3n tr\u00ean h\u1ec7 th\u1ed1ng <strong>AI Deadline Forecasting Agent</strong>.</p><p>Ch\u00fang t\u00f4i \u0111\u00e3 t\u1ef1 \u0111\u1ed9ng t\u1ea1o s\u1eb5n <strong>1 d\u1ef1 \u00e1n m\u1eabu</strong> \u0111\u1ec3 b\u1ea1n kh\u00e1m ph\u00e1 c\u00e1c t\u00ednh n\u0103ng c\u1ee7a h\u1ec7 th\u1ed1ng:</p><ul><li>\u2705 Dashboard tr\u1ef1c quan v\u1edbi bi\u1ec3u \u0111\u1ed3 th\u1ed1ng k\u00ea</li><li>\ud83e\udd16 D\u1ef1 \u0111o\u00e1n nguy c\u01a1 tr\u1ec5 deadline b\u1eb1ng AI</li><li>\ud83d\udcca Qu\u1ea3n l\u00fd d\u1ef1 \u00e1n v\u00e0 tasks hi\u1ec7u qu\u1ea3</li><li>\ud83d\udd2e M\u00f4 ph\u1ecfng k\u1ecbch b\u1ea3n What-if</li><li>\u2699\ufe0f T\u1ef1 \u0111\u1ed9ng h\u00f3a v\u1edbi n8n workflows</li></ul><p>H\u00e3y b\u1eaft \u0111\u1ea7u tr\u1ea3i nghi\u1ec7m ngay:</p><a href='http://localhost:5173/dashboard' class='button'>Truy c\u1eadp Dashboard</a><p style='margin-top:20px'><strong>Th\u00f4ng tin \u0111\u0103ng nh\u1eadp:</strong></p><ul><li>T\u00ean \u0111\u0103ng nh\u1eadp: <strong>{{$json.username}}</strong></li><li>Email: <strong>{{$json.email}}</strong></li></ul></div><div class='footer'><p>\u00a9 2024 AI Deadline Forecasting Agent. All rights reserved.</p><p>N\u1ebfu b\u1ea1n c\u1ea7n h\u1ed7 tr\u1ee3, vui l\u00f2ng li\u00ean h\u1ec7: admin@ai-deadline.io.vn</p></div></div></body></html>",
        "options": {}
      },
      "name": "Send Welcome Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2,
      "position": [
        900,
        200
      ],
      "credentials": {
        "smtp": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "url": "={{$env.BACKEND_API_URL}}/api/projects/demo/internal?user_id={{$node['Function: Extract User Data'].json.user_id}}",
        "authentication": "none",
        "method": "POST",
        "sendHeaders": false,
        "options": {}
      },
      "name": "HTTP: Create Demo Project",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "url": "={{$env.BACKEND_API_URL}}/api/webhooks/n8n/automation-log",
        "authentication": "none",
        "method": "POST",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={\n  \"workflow_name\": \"User Registration Automation\",\n  \"status\": \"success\",\n  \"input_data\": {{JSON.stringify({user_id: $node['Function: Extract User Data'].json.user_id, email: $node['Function: Extract User Data'].json.email})}},\n  \"output_data\": {{JSON.stringify({email_sent: true, demo_project_created: true, project_id: $json.id || 0})}}\n}",
        "options": {}
      },
      "name": "HTTP: Log Automation",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1340,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{JSON.stringify({success: true, message: 'User setup completed', demo_project_id: $json.id || 0})}}",
        "options": {}
      },
      "name": "Webhook Response Success",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1560,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{JSON.stringify({success: false, message: 'Invalid email address'})}}",
        "options": {
          "responseCode": 400
        }
      },
      "name": "Webhook Response Error",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        900,
        400
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Function: Extract User Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function: Extract User Data": {
      "main": [
        [
          {
            "node": "IF: Has Valid Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: Has Valid Email": {
      "main": [
        [
          {
            "node": "Send Welcome Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Webhook Response Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Welcome Email": {
      "main": [
        [
          {
            "node": "HTTP: Create Demo Project",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP: Create Demo Project": {
      "main": [
        [
          {
            "node": "HTTP: Log Automation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP: Log Automation": {
      "main": [
        [
          {
            "node": "Webhook Response Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "versionId": "3",
  "id": "3",
  "tags": []
}