{
  "name": "Obatin \u2014 Push Scheduler",
  "nodes": [
    {
      "id": "f4403c78-c3f1-4018-9083-22aac2f3c08a",
      "name": "Cron 30s",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 2,
      "position": [
        250,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "seconds",
              "secondsInterval": 30
            }
          ]
        }
      }
    },
    {
      "id": "4991c61c-1bdd-4f70-99e3-18cf8caa55f0",
      "name": "Cek Jadwal",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        550,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT j.id AS jadwal_id, j.waktu, o.id AS obat_id, o.nama AS obat_nama, o.dosis, a.nama AS anak_nama, a.id AS anak_id FROM jadwal j JOIN obat o ON o.id=j.obat_id JOIN anak a ON a.id=o.anak_id WHERE j.active=true AND o.active=true AND a.active=true AND j.waktu <= CURRENT_TIME AND (j.durasi_mulai IS NULL OR j.durasi_mulai <= CURRENT_DATE) AND (j.durasi_selesai IS NULL OR j.durasi_selesai >= CURRENT_DATE) AND NOT EXISTS (SELECT 1 FROM log_minum l WHERE l.jadwal_id=j.id AND l.tanggal=CURRENT_DATE AND l.status IN ('tepat','terlewat')) LIMIT 20",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "e55d0011-74c7-4f04-9145-12ff6289666a",
      "name": "Insert Log Pending",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        850,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "={{ 'INSERT INTO log_minum (jadwal_id, obat_id, tanggal, waktu_jadwal, status) VALUES (''' + $json.jadwal_id + ''', ''' + $json.obat_id + ''', CURRENT_DATE, ''' + $json.waktu + ''', ''pending'') ON CONFLICT (jadwal_id, tanggal) DO NOTHING' }}",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "42317eca-1be2-46f3-a93f-a120f84f3083",
      "name": "Mark Terlewat",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        1150,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE log_minum SET status='terlewat' WHERE status='pending' AND created_at < NOW() - INTERVAL '2 hours'",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "4126e7d2-ca33-4f20-b780-595f8ba430bd",
      "name": "Cek Notif Pasangan",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        1450,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT l.id, l.jadwal_id, l.obat_id, l.diberi_oleh, l.tanggal, o.nama AS obat_nama, a.nama AS anak_nama FROM log_minum l JOIN obat o ON o.id=l.obat_id JOIN anak a ON a.id=o.anak_id WHERE l.status='tepat' AND l.notif_pasangan=false AND l.waktu_minum > NOW() - INTERVAL '5 minutes' LIMIT 10",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "44c6fd0e-2e94-4e48-94fd-60394804c6d9",
      "name": "Get Subscriptions",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        1750,
        100
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT endpoint, p256dh, auth, identitas FROM push_subscription WHERE active=true",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "01acb642-7991-451e-864c-63327940c196",
      "name": "Update Notif Flag",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        2050,
        250
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "={{ 'UPDATE log_minum SET notif_pasangan=true WHERE id=''' + $json.id + '''' }}",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron 30s": {
      "main": [
        [
          {
            "node": "Cek Jadwal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cek Jadwal": {
      "main": [
        [
          {
            "node": "Insert Log Pending",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Log Pending": {
      "main": [
        [
          {
            "node": "Mark Terlewat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Terlewat": {
      "main": [
        [
          {
            "node": "Cek Notif Pasangan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cek Notif Pasangan": {
      "main": [
        [
          {
            "node": "Get Subscriptions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Subscriptions": {
      "main": [
        [
          {
            "node": "Update Notif Flag",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": []
}