# Simplified Article Approval API - Usage Guide

## 🎯 **Overview**

API baru ini menyederhanakan proses approval artikel. Sebelumnya user perlu mengetahui flow ID dan step yang sedang berjalan, sekarang cukup dengan article ID saja.

## 🔄 **Before vs After**

### **❌ Before (Complex):**
```bash
# User perlu tahu flow ID (456) dan step yang sedang berjalan
curl -X POST "http://localhost:8080/api/article-approval-flows/456/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{"message": "Approved"}'
```

### **✅ After (Simple):**
```bash
# User hanya perlu tahu article ID (123)
curl -X POST "http://localhost:8080/api/article-approval-flows/articles/123/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{"message": "Approved"}'
```

## 🚀 **New Endpoint**

```
POST /api/article-approval-flows/articles/{articleId}/approve
```

## 📋 **Request Format**

```json
{
  "message": "Your approval message here"
}
```

## 🧪 **Practical Examples**

### **1. Approve Article dari User Level 3 (Branch A):**
```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/articles/10/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_3_TOKEN" \
  -d '{
    "message": "Content reviewed and approved. Ready for final approval."
  }'
```

**Expected Response:**
```json
{
  "success": true,
  "messages": ["Article successfully approved through active approval flow"],
  "data": {
    "article_id": 10,
    "flow_id": 1,
    "current_step": 2,
    "current_branch": "Final_Approval",
    "workflow_id": 1
  }
}
```

### **2. Approve Article dari User Level 2 (Final Approval):**
```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/articles/10/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_2_TOKEN" \
  -d '{
    "message": "Final approval granted. Article ready for publication."
  }'
```

**Expected Response:**
```json
{
  "success": true,
  "messages": ["Article successfully approved through active approval flow"],
  "data": {
    "article_id": 10,
    "flow_id": 1,
    "current_step": 3,
    "current_branch": "Final_Approval",
    "workflow_id": 1
  }
}
```

### **3. Approve Article tanpa Message:**
```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/articles/10/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{}'
```

## 🔍 **How It Works Internally**

1. **Input**: Article ID (123)
2. **Find Active Flow**: 
   ```sql
   SELECT * FROM article_approval_flows 
   WHERE article_id = 123 AND status_id = 1
   ```
3. **Get Flow Info**: 
   - Flow ID: 456
   - Current Step: 2
   - Current Branch: "Branch_A"
   - Workflow ID: 1
4. **Process Approval**: Menggunakan multi-branch logic
5. **Update Flow**: Step dan branch diupdate sesuai workflow
6. **Response**: Return informasi yang relevan

## 🎯 **Benefits**

### **1. Simplified User Experience**
- ✅ Tidak perlu tahu flow ID
- ✅ Tidak perlu tahu step yang sedang berjalan
- ✅ Tidak perlu tahu branch yang aktif
- ✅ Cukup masukkan article ID

### **2. Reduced Complexity**
- ✅ API call lebih sederhana
- ✅ Error handling lebih mudah
- ✅ Integration lebih straightforward
- ✅ Maintenance lebih mudah

### **3. Better Error Messages**
- ✅ "No active approval flow found" - jelas dan informatif
- ✅ "Invalid article ID format" - mudah dipahami
- ✅ Automatic flow detection - tidak perlu manual check

## 🔄 **Integration Examples**

### **Frontend Integration:**
```javascript
// Simple approval function
async function approveArticle(articleId, message = '') {
  const response = await fetch(`/api/article-approval-flows/articles/${articleId}/approve`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({ message })
  });
  
  const result = await response.json();
  return result;
}

// Usage
approveArticle(123, 'Content looks good!');
```

### **Mobile App Integration:**
```javascript
// React Native example
const approveArticle = async (articleId) => {
  try {
    const response = await fetch(`${API_BASE}/article-approval-flows/articles/${articleId}/approve`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${userToken}`
      },
      body: JSON.stringify({
        message: 'Approved from mobile app'
      })
    });
    
    const result = await response.json();
    if (result.success) {
      Alert.alert('Success', 'Article approved successfully!');
    } else {
      Alert.alert('Error', result.messages[0]);
    }
  } catch (error) {
    Alert.alert('Error', 'Failed to approve article');
  }
};
```

## 🚨 **Error Scenarios**

### **1. No Active Flow:**
```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/articles/999/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{"message": "Approved"}'
```

**Response:**
```json
{
  "success": false,
  "messages": ["No active approval flow found for this article"]
}
```

### **2. Invalid Article ID:**
```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/articles/invalid/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{"message": "Approved"}'
```

**Response:**
```json
{
  "success": false,
  "messages": ["Invalid article ID format"]
}
```

## 📊 **Testing Scenarios**

### **Test Case 1: Normal Approval Flow**
1. Create article dengan user level 5
2. Article otomatis masuk ke Branch A (Level 2 A Branch)
3. User level 3 approve → masuk ke Final Approval
4. User level 2 approve → artikel published

### **Test Case 2: Branch B Flow**
1. Create article dengan user level 6
2. Article otomatis masuk ke Branch B (Level 2 B Branch)
3. User level 4 approve → masuk ke Final Approval
4. User level 2 approve → artikel published

### **Test Case 3: Error Handling**
1. Try approve artikel yang tidak ada approval flow
2. Try approve dengan user level yang tidak sesuai
3. Try approve artikel yang sudah selesai

## 🎉 **Summary**

API baru ini memberikan:

- ✅ **Simplified Interface**: Cukup article ID
- ✅ **Automatic Flow Detection**: Sistem otomatis cari flow aktif
- ✅ **Multi-Branch Support**: Tetap mendukung multi-branch logic
- ✅ **Better UX**: User experience yang lebih baik
- ✅ **Easy Integration**: Mudah diintegrasikan dengan frontend/mobile

**Sekarang user tidak perlu lagi repot dengan flow ID atau step yang sedang berjalan - cukup masukkan article ID dan sistem akan menangani sisanya!** 🚀
