# Troubleshooting Auto Approval Flow

## 🚨 **Masalah: Artikel Tidak Masuk ke Approval Queue**

### **Gejala:**
- Artikel dibuat dengan `workflow_id = NULL`
- Tidak ada record di `article_approval_flows` table
- User approver tidak melihat artikel di queue mereka

### **Penyebab Umum:**

#### **1. User Level Tidak Memerlukan Approval**
```sql
-- Cek user level
SELECT 
    id,
    level_name,
    level_number,
    is_approval_active
FROM user_levels 
WHERE id = 5;
```

**Solusi:**
```sql
-- Set user level memerlukan approval
UPDATE user_levels 
SET is_approval_active = true 
WHERE id = 5;
```

#### **2. Tidak Ada Default Workflow**
```sql
-- Cek default workflow
SELECT 
    id,
    name,
    is_active,
    is_default,
    client_id
FROM approval_workflows 
WHERE is_default = true 
AND is_active = true;
```

**Solusi:**
```sql
-- Set workflow sebagai default
UPDATE approval_workflows 
SET is_default = true, is_active = true 
WHERE name = 'Multi-Branch Article Approval';
```

#### **3. Workflow Tidak Aktif**
```sql
-- Cek status workflow
SELECT 
    id,
    name,
    is_active,
    is_default
FROM approval_workflows 
WHERE name = 'Multi-Branch Article Approval';
```

**Solusi:**
```sql
-- Aktifkan workflow
UPDATE approval_workflows 
SET is_active = true 
WHERE name = 'Multi-Branch Article Approval';
```

#### **4. Client ID Mismatch**
```sql
-- Cek client ID workflow vs user
SELECT 
    aw.id as workflow_id,
    aw.name as workflow_name,
    aw.client_id as workflow_client_id,
    u.id as user_id,
    u.client_id as user_client_id
FROM approval_workflows aw
CROSS JOIN users u
WHERE u.id = 5
AND aw.is_default = true;
```

**Solusi:**
```sql
-- Update workflow client ID jika perlu
UPDATE approval_workflows 
SET client_id = '338571d5-3836-47c0-a84f-e88f6fbcbb09'
WHERE name = 'Multi-Branch Article Approval';
```

### **Debug Steps:**

#### **Step 1: Cek Log Aplikasi**
```bash
# Cek log aplikasi untuk error messages
tail -f logs/app.log | grep -i "approval\|workflow\|branch"
```

**Expected Log Messages:**
```
User level requires approval - setting to pending
User level requires approval - proceeding with workflow assignment
Found default workflow
Article updated with workflow successfully
Approval flow created successfully
Multi-branch flow initialized successfully
```

#### **Step 2: Cek Database State**
```sql
-- 1. Cek user level
SELECT 
    u.id,
    u.name,
    u.user_level_id,
    ul.level_name,
    ul.level_number,
    ul.is_approval_active
FROM users u
JOIN user_levels ul ON u.user_level_id = ul.id
WHERE u.id = 5;

-- 2. Cek default workflow
SELECT 
    id,
    name,
    is_active,
    is_default,
    client_id
FROM approval_workflows 
WHERE is_default = true 
AND is_active = true;

-- 3. Cek artikel terbaru
SELECT 
    id,
    title,
    created_by_id,
    workflow_id,
    current_approval_step,
    status_id,
    bypass_approval,
    approval_exempt,
    created_at
FROM articles 
ORDER BY created_at DESC 
LIMIT 3;

-- 4. Cek approval flows
SELECT 
    aaf.id,
    aaf.article_id,
    aaf.workflow_id,
    aaf.current_step,
    aaf.current_branch,
    aaf.status_id,
    aaf.submitted_by_id,
    aaf.submitted_at,
    a.title as article_title
FROM article_approval_flows aaf
JOIN articles a ON aaf.article_id = a.id
ORDER BY aaf.created_at DESC 
LIMIT 3;
```

#### **Step 3: Test dengan API**
```bash
# Test create artikel baru
curl -X POST "http://localhost:8080/api/articles" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_5_TOKEN" \
  -d '{
    "title": "Test Debug Article",
    "content": "Debug content",
    "categoryId": 1,
    "isDraft": false
  }'

# Cek approval status
curl -X GET "http://localhost:8080/api/articles/{article_id}/approval-status" \
  -H "Authorization: Bearer USER_TOKEN"
```

### **Quick Fix Script:**

```sql
-- Fix script untuk masalah umum
-- 1. Pastikan user level 5 memerlukan approval
UPDATE user_levels 
SET is_approval_active = true 
WHERE id = 5;

-- 2. Pastikan workflow adalah default
UPDATE approval_workflows 
SET is_default = true, is_active = true 
WHERE name = 'Multi-Branch Article Approval';

-- 3. Pastikan client ID sama
UPDATE approval_workflows 
SET client_id = '338571d5-3836-47c0-a84f-e88f6fbcbb09'
WHERE name = 'Multi-Branch Article Approval';

-- 4. Verifikasi fix
SELECT 
    ul.id as level_id,
    ul.level_name,
    ul.is_approval_active,
    aw.id as workflow_id,
    aw.name as workflow_name,
    aw.is_active,
    aw.is_default,
    aw.client_id
FROM user_levels ul
CROSS JOIN approval_workflows aw
WHERE ul.id = 5 
AND aw.is_default = true;
```

### **Expected Results After Fix:**

#### **Database State:**
```sql
-- Artikel harus memiliki workflow_id
SELECT 
    id,
    title,
    workflow_id,
    current_approval_step,
    status_id
FROM articles 
WHERE title = 'Test Tni Artikel 1';

-- Approval flow harus terbuat
SELECT 
    aaf.id,
    aaf.article_id,
    aaf.workflow_id,
    aaf.current_step,
    aaf.current_branch,
    aaf.status_id
FROM article_approval_flows aaf
JOIN articles a ON aaf.article_id = a.id
WHERE a.title = 'Test Tni Artikel 1';
```

#### **API Response:**
```json
{
  "success": true,
  "data": {
    "article_id": 123,
    "workflow_id": 1,
    "current_step": 1,
    "current_branch": "Branch_A",
    "status": "pending_approval",
    "next_approver_level": 3
  }
}
```

### **Monitoring Commands:**

```bash
# Monitor log real-time
tail -f logs/app.log | grep -E "(approval|workflow|branch|User level)"

# Cek database changes
watch -n 5 "psql -d your_db -c \"SELECT COUNT(*) FROM article_approval_flows WHERE created_at > NOW() - INTERVAL '1 hour';\""

# Test API endpoint
watch -n 10 "curl -s http://localhost:8080/api/article-approval-flows/my-queue -H 'Authorization: Bearer APPROVER_TOKEN' | jq '.data | length'"
```

---

**Jika masalah masih terjadi setelah mengikuti troubleshooting ini, cek log aplikasi untuk error messages yang lebih spesifik!** 🔍
