# WhatsApp Gateway Operations

Dokumen ini dipakai untuk menjalankan notifikasi WhatsApp internal secara otomatis.

## 1. Arsitektur proses

- `printfreight-wa-gateway` dijaga `pm2` untuk session WhatsApp.
- `printfreight-notification-worker` dijaga `pm2` untuk polling queue `notification_jobs`.
- Worker tidak lagi dijalankan lewat `cron` di mesin ini karena konteks `cron` gagal membuka koneksi DB (`SQLSTATE[HY000] [2002] Operation not permitted`).

## 2. Jalankan gateway dengan PM2

Masuk ke folder gateway:

```bash
cd /Users/dianmaulana/Desktop/project/printfreight/tools/whatsapp_gateway
```

Start:

```bash
pm2 start ecosystem.config.cjs
```

Lihat status:

```bash
pm2 status
pm2 logs printfreight-wa-gateway
pm2 logs printfreight-notification-worker
```

Simpan startup process:

```bash
pm2 save
pm2 startup
```

## 3. Jalankan worker manual

```bash
cd /Users/dianmaulana/Desktop/project/printfreight
php scripts/run_notification_worker.php --mode=gateway --limit=20
```

Atau gunakan shell runner:

```bash
cd /Users/dianmaulana/Desktop/project/printfreight
./scripts/run_notification_worker_gateway.sh
```

Untuk loop worker yang dijaga PM2:

```bash
cd /Users/dianmaulana/Desktop/project/printfreight/tools/whatsapp_gateway
pm2 start ecosystem.config.cjs
```

## 4. Health checks

Gateway:

```bash
curl -s http://127.0.0.1:3010/health
curl -s http://127.0.0.1:3010/session
```

Halaman admin:

```text
/spk/notifications
```

## 5. Lokasi log

- Gateway PM2: `pm2 logs printfreight-wa-gateway`
- Worker PM2: `pm2 logs printfreight-notification-worker`
- Worker PHP: `storage/logs/notification_worker.log`
- Dispatch detail: `storage/logs/notification_dispatch.log`
- Enqueue / producer detail: `storage/logs/notification_jobs.log`

## 6. Jika session logout

1. Buka gateway session status
2. Jika status `qr_required`, render QR lagi
3. Scan ulang dari WhatsApp `Linked Devices`
4. Pastikan status kembali `connected`

## 7. Catatan mode internal

- Fitur ini tetap internal-only
- Tenant harus `Force Enable` pada feature override `notification.whatsapp_internal`
- Jangan dipakai sebagai arsitektur final public SaaS
