{
  "name": "Reminder Notifier v2 (WA + Push)",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 * * * *"
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Schedule (Setiap Jam)",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        320
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT r.id, r.user_id, r.title, r.amount, r.category, r.notes, r.due_date, r.remind_days_before, r.recurrence, u.wa_number, u.nama, EXISTS(SELECT 1 FROM push_subscriptions ps WHERE ps.user_id = u.id) AS has_push\nFROM reminders r\nJOIN users u ON u.id = r.user_id\nWHERE r.status = 'pending'\n  AND r.notify_via_wa = true\n  AND (r.due_date - (r.remind_days_before * INTERVAL '1 day')) <= NOW()\n  AND r.due_date >= (NOW() - INTERVAL '7 days')\n  AND (r.reminded_at IS NULL OR r.reminded_at < NOW() - INTERVAL '23 hours')\nORDER BY r.due_date ASC\nLIMIT 50",
        "options": {}
      },
      "id": "fetch-due-reminders",
      "name": "Ambil Reminders Jatuh Tempo",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        460,
        320
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Format pesan WA + payload Push per reminder\nconst items = $input.all();\nconst results = [];\n\nfor (const item of items) {\n  const r = item.json;\n  if (!r || !r.wa_number) continue;\n\n  const today = new Date();\n  today.setHours(0, 0, 0, 0);\n  const due = new Date(r.due_date);\n  due.setHours(0, 0, 0, 0);\n  const diffDays = Math.ceil((due - today) / (1000 * 60 * 60 * 24));\n\n  let dueText, pushBodyShort;\n  if (diffDays < 0) {\n    dueText = `\u26a0\ufe0f Sudah lewat ${Math.abs(diffDays)} hari`;\n    pushBodyShort = `Lewat ${Math.abs(diffDays)} hari`;\n  } else if (diffDays === 0) {\n    dueText = '\ud83d\udd25 Hari ini!';\n    pushBodyShort = 'Jatuh tempo hari ini';\n  } else if (diffDays === 1) {\n    dueText = '\ud83d\udccc Besok';\n    pushBodyShort = 'Jatuh tempo besok';\n  } else {\n    dueText = `\ud83d\udcc5 ${diffDays} hari lagi`;\n    pushBodyShort = `${diffDays} hari lagi`;\n  }\n\n  const dueDateFormatted = due.toLocaleDateString('id-ID', {\n    day: 'numeric', month: 'long', year: 'numeric'\n  });\n\n  const namaSapaan = r.nama ? r.nama.split(' ')[0] : 'Kak';\n\n  let amountText = '', amountShort = '';\n  if (r.amount && r.amount > 0) {\n    const formatted = `Rp ${Number(r.amount).toLocaleString('id-ID')}`;\n    amountText = `\\n\ud83d\udcb0 *Estimasi:* ${formatted}`;\n    amountShort = ` \u00b7 ${formatted}`;\n  }\n\n  let categoryText = '';\n  if (r.category) categoryText = `\\n\ud83d\udcc2 *Kategori:* ${r.category}`;\n\n  let notesText = '';\n  if (r.notes) notesText = `\\n\ud83d\udcdd *Catatan:* ${r.notes}`;\n\n  const waMessage = `Halo ${namaSapaan}! \ud83d\udc4b\\n\\n` +\n    `Ada reminder buat kamu:\\n\\n` +\n    `*${r.title}*\\n` +\n    `${dueText} (${dueDateFormatted})` +\n    amountText +\n    categoryText +\n    notesText +\n    `\\n\\nBuka dashboard untuk tandai sudah dibayar:\\nnayyiraai.online/reminders`;\n\n  // Format chatId WA\n  let waFormatted = String(r.wa_number).replace(/\\D/g, '');\n  if (waFormatted.startsWith('0')) waFormatted = '62' + waFormatted.slice(1);\n  if (waFormatted.startsWith('8')) waFormatted = '62' + waFormatted;\n  const chatId = `${waFormatted}@s.whatsapp.net`;\n\n  // Push payload \u2014 short and actionable\n  const pushTitle = `\ud83d\udd14 ${r.title}`;\n  const pushBody = `${pushBodyShort}${amountShort}`;\n\n  results.push({\n    json: {\n      reminderId: r.id,\n      userId: r.user_id,\n      hasPush: !!r.has_push,\n      // WA fields\n      chatId: chatId,\n      waMessage: waMessage,\n      // Push fields\n      pushTitle: pushTitle,\n      pushBody: pushBody,\n      // Misc\n      title: r.title,\n      waNumber: waFormatted,\n    }\n  });\n}\n\nreturn results;"
      },
      "id": "format-message",
      "name": "Format Pesan",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        320
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.WAHA_URL }}/api/sendText",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Api-Key",
              "value": "={{ $env.WAHA_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"session\": \"default\",\n  \"chatId\": \"{{ $json.chatId }}\",\n  \"text\": {{ JSON.stringify($json.waMessage) }}\n}",
        "options": {
          "timeout": 30000
        }
      },
      "id": "send-wa",
      "name": "Kirim WhatsApp (WAHA)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        220
      ],
      "notes": "Session 'default' karena WAHA Core. Kalau pakai WAHA PLUS dengan multi-session, ganti ke session yang sesuai."
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "has-push",
              "leftValue": "={{ $json.hasPush }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-has-push",
      "name": "Punya Push Subscription?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        420
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.DASHBOARD_URL }}/api/push/send",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $env.INTERNAL_API_TOKEN }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"user_id\": \"{{ $json.userId }}\",\n  \"title\": {{ JSON.stringify($json.pushTitle) }},\n  \"body\": {{ JSON.stringify($json.pushBody) }},\n  \"url\": \"/reminders\",\n  \"tag\": \"reminder-{{ $json.reminderId }}\"\n}",
        "options": {
          "timeout": 15000
        }
      },
      "id": "send-push",
      "name": "Kirim Push Notification",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        420
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE reminders SET reminded_at = NOW() WHERE id = '{{ $('Format Pesan').item.json.reminderId }}'",
        "options": {}
      },
      "id": "update-reminded-at",
      "name": "Update reminded_at",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        1340,
        320
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Schedule (Setiap Jam)": {
      "main": [
        [
          {
            "node": "Ambil Reminders Jatuh Tempo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ambil Reminders Jatuh Tempo": {
      "main": [
        [
          {
            "node": "Format Pesan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Pesan": {
      "main": [
        [
          {
            "node": "Kirim WhatsApp (WAHA)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Punya Push Subscription?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Kirim WhatsApp (WAHA)": {
      "main": [
        [
          {
            "node": "Update reminded_at",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Punya Push Subscription?": {
      "main": [
        [
          {
            "node": "Kirim Push Notification",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [
    {
      "name": "nayyira"
    },
    {
      "name": "reminder"
    }
  ]
}