{
  "name": "B - Application Tracker & Reminders",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 24,
              "triggerAtHour": 9
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -400,
        0
      ],
      "id": "schedule-1",
      "name": "Daily 9am Check"
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "REPLACE_WITH_YOUR_SHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Opportunities",
          "mode": "name"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        -160,
        0
      ],
      "id": "sheets-read-1",
      "name": "Read Opportunities Sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst now = new Date();\nconst followUps = [];\nconst paymentOverdue = [];\n\nfor (const item of items) {\n  const row = item.json;\n  const status = (row.Status || '').toLowerCase();\n  const appliedDate = row.AppliedDate ? new Date(row.AppliedDate) : null;\n  const daysSinceApplied = appliedDate ? Math.floor((now - appliedDate) / 86400000) : null;\n\n  // Applied but no brand response after 5 days -> follow up\n  if (status === 'applied' && daysSinceApplied !== null && daysSinceApplied >= 5) {\n    followUps.push({ brand: row.Brand, niche: row.Niche, daysSinceApplied, rowRef: row.Brand + ' / ' + row.Date });\n  }\n\n  // Marked accepted/completed but payment not received after 7 days -> flag\n  const paymentReceived = (row.PaymentReceived || '').toLowerCase() === 'yes' || (row.PaymentReceived || '').toLowerCase() === 'true';\n  if ((status === 'accepted' || status === 'completed') && !paymentReceived && daysSinceApplied !== null && daysSinceApplied >= 7) {\n    paymentOverdue.push({ brand: row.Brand, niche: row.Niche, daysSinceApplied, expectedPayout: row.ExpectedPayout });\n  }\n}\n\nreturn [{ json: { followUps, paymentOverdue, followUpCount: followUps.length, paymentOverdueCount: paymentOverdue.length } }];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        80,
        0
      ],
      "id": "code-analyze-1",
      "name": "Find Follow-ups & Overdue"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.followUpCount }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        320,
        -80
      ],
      "id": "if-followup-1",
      "name": "Any Follow-ups Needed?"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{ $json.paymentOverdueCount }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        320,
        120
      ],
      "id": "if-overdue-1",
      "name": "Any Payments Overdue?"
    },
    {
      "parameters": {
        "chatId": "REPLACE_WITH_YOUR_TELEGRAM_CHAT_ID",
        "text": "=\ud83d\udccb Follow-up reminder: {{ $json.followUpCount }} brand(s) haven't responded in 5+ days:\n{{ $json.followUps.map(f => `- ${f.brand} (${f.niche}) - ${f.daysSinceApplied} days`).join('\\n') }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        560,
        -80
      ],
      "id": "telegram-followup-1",
      "name": "Send Follow-up Reminder",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "REPLACE_WITH_YOUR_TELEGRAM_CHAT_ID",
        "text": "=\ud83d\udcb0 Payment check: {{ $json.paymentOverdueCount }} payout(s) overdue (7+ days since completion):\n{{ $json.paymentOverdue.map(p => `- ${p.brand} (${p.niche}) - expected ${p.expectedPayout}, ${p.daysSinceApplied} days`).join('\\n') }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        560,
        120
      ],
      "id": "telegram-overdue-1",
      "name": "Send Payment Overdue Alert",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Daily 9am Check": {
      "main": [
        [
          {
            "node": "Read Opportunities Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Opportunities Sheet": {
      "main": [
        [
          {
            "node": "Find Follow-ups & Overdue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Follow-ups & Overdue": {
      "main": [
        [
          {
            "node": "Any Follow-ups Needed?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Any Payments Overdue?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Any Follow-ups Needed?": {
      "main": [
        [
          {
            "node": "Send Follow-up Reminder",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Any Payments Overdue?": {
      "main": [
        [
          {
            "node": "Send Payment Overdue Alert",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {}
}