# Cara Menjalankan QR Addon di Local

## 🔧 Masalah yang Diperbaiki

1. ✅ **Middleware plan check** di-bypass untuk local development
2. ✅ **QR code polling endpoint** ditambahkan (`/get_qr`)
3. ✅ **QR code tidak langsung muncul** - sekarang ada delay 5-10 detik
4. ✅ **Auto-reconnect** jika koneksi terputus

---

## 🚀 Langkah-langkah

### 1. Restart Server
```bash
# Stop server jika sedang berjalan (Ctrl+C)
# Kemudian jalankan ulang
npm start
```

### 2. Login ke Aplikasi
Buka browser: `http://localhost:8001`
Login dengan akun user kamu

### 3. Generate QR Code

#### Option A: Via API (Postman/Thunder Client)

**Endpoint:** `POST http://localhost:8001/api/qr/gen_qr`

**Headers:**
```
Content-Type: application/json
Authorization: Bearer YOUR_JWT_TOKEN
```

**Body:**
```json
{
  "title": "WhatsApp Saya",
  "uniqueId": "device_001"
}
```

**Response:**
```json
{
  "success": true,
  "msg": "QR code is generating. Please wait 5-10 seconds.",
  "uniqueId": "device_001"
}
```

#### Option B: Via Frontend (jika ada UI)
Klik tombol "Add Device" atau "Generate QR" di halaman WhatsApp/Instances

---

### 4. Polling QR Code (PENTING!)

QR code **TIDAK langsung muncul**! Harus di-polling dulu.

**Endpoint:** `GET http://localhost:8001/api/qr/get_qr?uniqueId=device_001`

**Response jika QR siap:**
```json
{
  "success": true,
  "status": "QR_READY",
  "qr": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
  "number": null,
  "title": "WhatsApp Saya"
}
```

**Response jika belum siap:**
```json
{
  "success": true,
  "status": "GENERATING",
  "qr": null,
  "number": null,
  "title": "WhatsApp Saya"
}
```

**Response jika sudah terhubung:**
```json
{
  "success": true,
  "status": "ACTIVE",
  "qr": "",
  "number": "6281234567890",
  "title": "WhatsApp Saya"
}
```

---

### 5. Scan QR Code

1. Buka WhatsApp di HP
2. Settings > Linked Devices > Link a Device
3. Scan QR code yang muncul
4. Status akan berubah jadi `ACTIVE`

---

## 🧪 Test dengan cURL

### Step 1: Generate QR
```bash
curl -X POST http://localhost:8001/api/qr/gen_qr \
  -H "Content-Type: application/json" \
  -d '{"title":"Test Device","uniqueId":"test_001"}'
```

### Step 2: Poll QR (tunggu 5 detik)
```bash
curl http://localhost:8001/api/qr/get_qr?uniqueId=test_001
```

### Step 3: Ulangi Step 2 sampai status="QR_READY"

---

## 🐛 Troubleshooting

### QR Code Tidak Muncul

**Cek server logs:**
```
QR code generated for session: device_001
```

**Cek database:**
```sql
SELECT * FROM instance WHERE uniqueId = 'device_001';
```

**Kolom `qr` harus berisi base64 image**

### Status Tetap "GENERATING"

**Kemungkinan penyebab:**
1. Baileys belum terhubung ke WhatsApp server
2. Network/firewall blocking
3. Versi Baileys tidak kompatibel

**Solusi:**
```sql
-- Hapus instance yang stuck
DELETE FROM instance WHERE uniqueId = 'device_001';
```

Kemudian coba generate ulang.

### Error: "session already exists"

Instance sudah ada di database. Hapus dulu:
```sql
DELETE FROM instance WHERE uniqueId = 'YOUR_UNIQUE_ID';
```

Kemudian restart server dan coba lagi.

---

## 📊 Status Flow

```
1. GENERATING   → Session dibuat, menunggu QR dari Baileys
2. QR_READY     → QR code siap di-scan (ada di kolom `qr`)
3. ACTIVE       → WhatsApp terhubung (ada di kolom `number`)
4. DISCONNECTED → Koneksi terputus (auto-reconnect)
```

---

## 🔍 Debug Mode

Tambahkan di `.env`:
```env
NODE_ENV=development
```

Server akan log lebih detail tentang proses QR generation.

---

## 📝 Catatan Penting

1. **QR code expired setelah ~30 detik** - harus cepat scan
2. **Satu instance = satu session** - tidak bisa reuse
3. **Auto-reconnect** jika disconnect (kecuali logout)
4. **Sessions disimpan di folder `sessions/`** untuk persist auth

---

**Last Updated:** April 9, 2026
