{
  "nodes": [
    {
      "id": "4a32e11b-0b14-40d7-aa64-c3a9ca56835c",
      "name": "Wait until product get deliver",
      "type": "n8n-nodes-base.wait",
      "position": [
        1248,
        320
      ],
      "parameters": {
        "unit": "days",
        "amount": 7
      },
      "typeVersion": 1.1
    },
    {
      "id": "83e3c23e-3f20-4993-b5b7-d87f0dcfaabc",
      "name": "Wait for 2 weeks",
      "type": "n8n-nodes-base.wait",
      "position": [
        2688,
        240
      ],
      "parameters": {
        "unit": "days",
        "amount": 14
      },
      "typeVersion": 1.1
    },
    {
      "id": "b48ab0b4-1747-48a3-8562-807169094255",
      "name": "Format AI response",
      "type": "n8n-nodes-base.code",
      "position": [
        2336,
        240
      ],
      "parameters": {
        "jsCode": "return $input.all().map(item => {\n  const original = item.json;\n\n  const text = original.output || \"\";\n\n  // Split by \"* \" and clean items\n  const items = text\n    .split(\"* \")\n    .map(i => i.trim())\n    .filter(i => i.length > 0);\n\n  // Build proper HTML list\n  const html =\n    \"<ul style='padding-left:20px; margin:0;'>\" +\n    items.map(i => `<li>${i}</li>`).join(\"\") +\n    \"</ul>\";\n\n  // IMPORTANT: merge old data + new field\n  return {\n    json: {\n      ...original,\n      formattedOutput: html\n    }\n  };\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "ba84ac7c-da4e-452c-9241-190ff673bb78",
      "name": "Order Placed Ack.",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1040,
        320
      ],
      "parameters": {
        "sendTo": "={{ $json.email }}",
        "message": "=<!DOCTYPE html>\n<html>\n<body style=\"margin:0; padding:0; background-color:#f9f9f9; font-family:Arial, sans-serif;\">\n\n  <div style=\"max-width:600px; margin:30px auto; background:#ffffff; padding:24px; border-radius:8px;\">\n    \n    <h2 style=\"color:#333;\">Your Order Is Confirmed</h2>\n\n    <p>Hi <strong>{{ $json.client_name }}</strong>,</p>\n\n    <p>\n      Thank you for your purchase!  \n      We\u2019re happy to let you know that your order for\n      <strong>{{ $json.product_name }}</strong> has been successfully placed.\n    </p>\n\n    <p style=\"margin:16px 0;\">\n      <strong>Estimated Delivery Date:</strong> {{ $json.estimated_delivery_date.split('T')[0] }}\n\n    </p>\n\n    <p>\n      We\u2019ll notify you once your order is on the way.\n    </p>\n\n    <p>\n      If you have any questions, just reply to this email \u2014 we\u2019re here to help!\n    </p>\n\n    <p style=\"margin-top:32px;\">\n      Best regards,<br>\n      <strong>Customer Support Team</strong>\n    </p>\n\n  </div>\n\n</body>\n</html>\n",
        "options": {},
        "subject": "Your Order Is Confirmed "
      },
      "typeVersion": 2.2
    },
    {
      "id": "923fc1a9-f06c-41c8-acca-d1f6976b6380",
      "name": "Get Product Usage Tips",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2032,
        240
      ],
      "parameters": {},
      "typeVersion": 3.1
    },
    {
      "id": "00374acb-792e-44cc-80dd-5928fe83be20",
      "name": "Send Tips to User",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2512,
        240
      ],
      "parameters": {
        "sendTo": "={{ $('Execute a SQL query').item.json.email }}",
        "message": "=<!DOCTYPE html>\n<html>\n  <body style=\"margin:0; padding:0; background-color:#f6f6f6; font-family:Arial, sans-serif;\">\n\n    <div style=\"max-width:600px; margin:24px auto; background:#ffffff; padding:24px; border-radius:8px;\">\n\n      <h2 style=\"margin-top:0; color:#222;\">\n        Get the Best Out of Your {{ $('Execute a SQL query').item.json.product_name }}\n      </h2>\n\n      <p style=\"color:#333;\">\n        Hi <strong>Devansh</strong>,\n      </p>\n      <p style=\"color:#333;\">\n        Here are a few practical tips to help you get the most out of your\n        <strong>{{ $('Execute a SQL query').item.json.product_name }}</strong>:\n      </p>\n\n      <!-- IMPORTANT: NO <p> HERE -->\n      <div style=\"margin:16px 0; color:#333;\">\n        {{$json.formattedOutput}}\n      </div>\n\n      <p style=\"color:#333;\">\n        If you have any questions, just reply to this email \u2014 we\u2019re happy to help.\n      </p>\n\n      <p style=\"margin-top:24px; color:#333;\">\n        Best regards,<br>\n        <strong>Customer Success Team</strong>\n      </p>\n\n    </div>\n\n  </body>\n</html>\n",
        "options": {},
        "subject": "=Getting Started with Your {{ $('Execute a SQL query').item.json.product_name }}"
      },
      "typeVersion": 2.2
    },
    {
      "id": "5a26b805-5130-4de6-b852-91e5e6c48229",
      "name": "Get Complementary Products",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2864,
        240
      ],
      "parameters": {},
      "typeVersion": 3.1
    },
    {
      "id": "abc44bb7-175c-4a3b-a216-609fb0b0db5e",
      "name": "Code in JavaScript",
      "type": "n8n-nodes-base.code",
      "position": [
        3168,
        240
      ],
      "parameters": {
        "jsCode": "return $input.all().map(item => {\n  const original = item.json;\n\n  const rawText = original.output || \"\";\n\n  // Split on \"* \" bullet\n  const items = rawText\n    .split('* ')\n    .map(i => i.trim())\n    .filter(i => i.length > 0);\n\n  // Convert to HTML list\n  const listItems = items.map(i => `<li>${i}</li>`).join('');\n\n  return {\n    json: {\n      ...original, // \ud83d\udc48 preserve email, product_name, etc.\n      formattedOutput: `<ul style=\"padding-left:20px; margin:0;\">${listItems}</ul>`\n    }\n  };\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "5cd6110c-df5b-40f4-a463-7f7f0496c139",
      "name": "Send Tips to User1",
      "type": "n8n-nodes-base.gmail",
      "position": [
        3360,
        240
      ],
      "parameters": {
        "sendTo": "={{ $('Execute a SQL query').item.json.email }}",
        "message": "=<!DOCTYPE html>\n<html>\n<body style=\"margin:0; padding:0; background-color:#f9f9f9; font-family:Arial, sans-serif;\">\n\n  <div style=\"max-width:600px; margin:30px auto; background:#ffffff; padding:24px; border-radius:8px;\">\n\n    <h2 style=\"color:#333;\">A Perfect Match for Your {{ $('Execute a SQL query').item.json.product_name }}</h2>\n\n    <p>Hi <strong>{{ $('Execute a SQL query').item.json.client_name }}</strong>,</p>\n\n    <p>\n      We hope you\u2019ve been loving your <strong>{{ $('Execute a SQL query').item.json.product_name }}</strong>! \n    </p>\n\n    <p>\n      Based on your purchase, we thought you might find this helpful:\n    </p>\n\n    <div style=\"background:#f2f2f2; padding:16px; border-radius:6px; margin:16px 0;\">\n      {{$json.formattedOutput}}\n    </div>\n\n    <p>\n      Many customers use this together with your current product to get even better results.\n    </p>\n\n    <p>\n      If you\u2019d like to know more, just reply to this email \u2014 we\u2019d be happy to help!\n    </p>\n\n    <p style=\"margin-top:32px;\">\n      Warm regards,<br>\n      <strong>Product Recommendations Team</strong>\n    </p>\n\n  </div>\n\n</body>\n</html>\n",
        "options": {},
        "subject": "=You Might Love This with Your {{ $('Execute a SQL query').item.json.product_name }}\n"
      },
      "typeVersion": 2.2
    },
    {
      "id": "c8a66438-5763-4eda-9084-76c55f921256",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        656,
        320
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 2
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "40da5167-6e26-4d1a-9de2-7c866738bc7a",
      "name": "Execute a SQL query",
      "type": "n8n-nodes-base.postgres",
      "position": [
        848,
        320
      ],
      "parameters": {
        "query": "SELECT *\nFROM orders\nWHERE created_at >= NOW() - INTERVAL '2 minute'\nORDER BY created_at ASC;\n",
        "options": {},
        "operation": "executeQuery"
      },
      "typeVersion": 2.6
    },
    {
      "id": "b85971cf-b0de-482f-aec4-21c1fb5725ea",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        1616,
        320
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "0c24df6c-7ff0-4612-8076-7877b8eee733",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $json.delivered }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "5432f11d-e880-489f-928c-987215922205",
      "name": "Select rows from a table",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1440,
        320
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "orders",
          "cachedResultName": "orders"
        },
        "where": {
          "values": [
            {
              "value": "={{ $('Execute a SQL query').item.json.order_id }}",
              "column": "order_id"
            }
          ]
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "select"
      },
      "typeVersion": 2.6
    },
    {
      "id": "63a47401-9647-4fdb-a8ed-457c23fd9e6a",
      "name": "Wait for a day",
      "type": "n8n-nodes-base.wait",
      "position": [
        1808,
        416
      ],
      "parameters": {
        "unit": "days",
        "amount": 1
      },
      "typeVersion": 1.1
    },
    {
      "id": "074ed9a7-b999-44c3-9175-ec15843f8134",
      "name": "Groq Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        2080,
        464
      ],
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "31682f41-9d9f-44c6-8305-e80e0c4e8d6c",
      "name": "Groq Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        2912,
        448
      ],
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "be190506-bfaa-4c93-b220-581161631558",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1200,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 768,
        "height": 752,
        "content": "## Step 2 - Delivery Status Check\nWaits for delivery and rechecks the order status until it is marked as delivered.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "99169450-5335-4d36-a0fb-656825eab5df",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2000,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 624,
        "height": 752,
        "content": "## Step 3 - Product Usage Tips (AI)\nGenerates product usage tips using AI and sends them to the customer."
      },
      "typeVersion": 1
    },
    {
      "id": "c74ab52c-e07a-4ef0-a1e9-7381155d55ae",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2656,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 896,
        "height": 752,
        "content": "## Step 4 - Product Recommendations (AI)\nWaits 2 weeks and sends AI-generated complementary product recommendations based on last purchase.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "8ec1bda5-146f-4301-99b7-9bce9e58fa31",
      "name": "Step -1 Trigger & Order Detection",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        608,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 560,
        "height": 752,
        "content": "## Step 1 - Order Detection & Confirmation Email\nDetects new orders placed  and sends an order confirmation email.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "40edc986-ad23-4ab5-a71e-463bab9b64f2",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 576,
        "height": 752,
        "content": "## Order Placed \u2192 Delivery-Based Upsell Automation\n\nThis workflow automates post-purchase engagement by confirming new orders, tracking delivery status, sending helpful product usage tips, and following up with personalized upsell recommendations to increase repeat purchases.\n\n### How it works\nStep 1: A scheduled trigger checks for new orders and sends an order confirmation email immediately.\n\nStep 2: The workflow waits until the expected delivery time, then checks if the order is delivered. If not, it waits 1 day and checks again. This repeats until delivery is confirmed.\n\nStep 3: Once delivered, an AI agent generates short product usage tips. These are formatted and emailed to the customer.\n\nStep 4: After 2 weeks, another AI agent generates complementary product suggestions, which are sent as an upsell email.\n\n### Setup steps\n1. Connect your Postgres database  \n2. Configure Gmail credentials  \n3. Ensure delivery status field is correct  \n4. Add Groq/OpenAI credentials  \n5. Review AI prompts if needed  \n6. Activate and test with sample data"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "If": {
      "main": [
        [],
        [
          {
            "node": "Wait for a day",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for a day": {
      "main": [
        [
          {
            "node": "Select rows from a table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Order Placed Ack.": {
      "main": [
        [
          {
            "node": "Wait until product get deliver",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Tips to User": {
      "main": [
        [
          {
            "node": "Wait for 2 weeks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Send Tips to User1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format AI response": {
      "main": [
        [
          {
            "node": "Send Tips to User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "Order Placed Ack.",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select rows from a table": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait until product get deliver": {
      "main": [
        [
          {
            "node": "Select rows from a table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}