# Multi-Branch Approval System - Curl Examples

## Setup dan Konfigurasi

### 1. Buat Multi-Branch Workflow

```bash
# Example 1: Simple Two-Branch Workflow
curl -X POST "http://localhost:8080/api/approval-workflows/with-steps" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Simple Two-Branch Approval",
    "description": "Workflow dengan 2 cabang berdasarkan user level",
    "isActive": true,
    "isDefault": false,
    "requiresApproval": true,
    "autoPublish": false,
    "steps": [
      {
        "stepOrder": 1,
        "stepName": "Branch A - Level 2",
        "requiredUserLevelId": 2,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [4,5,6]}",
        "branchName": "Branch_A",
        "branchOrder": 1
      },
      {
        "stepOrder": 1,
        "stepName": "Branch B - Level 3",
        "requiredUserLevelId": 3,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [7,8,9]}",
        "branchName": "Branch_B",
        "branchOrder": 1
      },
      {
        "stepOrder": 2,
        "stepName": "Final Approval",
        "requiredUserLevelId": 1,
        "canSkip": false,
        "isActive": true,
        "conditionType": "always",
        "branchName": "Final",
        "branchOrder": 1
      }
    ]
  }'
```

### 2. Complex Multi-Level Branching

```bash
# Example 2: Complex Multi-Level Branching
curl -X POST "http://localhost:8080/api/approval-workflows/with-steps" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Complex Multi-Level Branching",
    "description": "Workflow dengan multiple level branching",
    "isActive": true,
    "isDefault": false,
    "requiresApproval": true,
    "autoPublish": false,
    "steps": [
      {
        "stepOrder": 1,
        "stepName": "Level 2 Branch",
        "requiredUserLevelId": 2,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [4,5,6,7,8,9]}",
        "branchName": "Branch_A",
        "branchOrder": 1
      },
      {
        "stepOrder": 1,
        "stepName": "Level 3 Branch",
        "requiredUserLevelId": 3,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [10,11,12,13,14,15]}",
        "branchName": "Branch_B",
        "branchOrder": 1
      },
      {
        "stepOrder": 2,
        "stepName": "Sub-branch A1",
        "requiredUserLevelId": 4,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [4,5,6]}",
        "branchName": "SubBranch_A1",
        "branchOrder": 1
      },
      {
        "stepOrder": 2,
        "stepName": "Sub-branch A2",
        "requiredUserLevelId": 5,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [7,8,9]}",
        "branchName": "SubBranch_A2",
        "branchOrder": 1
      },
      {
        "stepOrder": 3,
        "stepName": "Level 1 Final Approval",
        "requiredUserLevelId": 1,
        "canSkip": false,
        "isActive": true,
        "conditionType": "always",
        "branchName": "Final_Approval",
        "branchOrder": 1
      }
    ]
  }'
```

### 3. Workflow dengan Specific User Levels

```bash
# Example 3: Workflow untuk specific user levels
curl -X POST "http://localhost:8080/api/approval-workflows/with-steps" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Specific User Level Workflow",
    "description": "Workflow untuk user level tertentu",
    "isActive": true,
    "isDefault": false,
    "requiresApproval": true,
    "autoPublish": false,
    "steps": [
      {
        "stepOrder": 1,
        "stepName": "Level 2 Approval",
        "requiredUserLevelId": 2,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level",
        "conditionValue": "[4,5,6]",
        "branchName": "Level2_Branch",
        "branchOrder": 1
      },
      {
        "stepOrder": 1,
        "stepName": "Level 3 Approval",
        "requiredUserLevelId": 3,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level",
        "conditionValue": "[7,8,9]",
        "branchName": "Level3_Branch",
        "branchOrder": 1
      },
      {
        "stepOrder": 2,
        "stepName": "Final Approval",
        "requiredUserLevelId": 1,
        "canSkip": false,
        "isActive": true,
        "conditionType": "always",
        "branchName": "Final",
        "branchOrder": 1
      }
    ]
  }'
```

## Workflow Management

### 1. Get All Workflows

```bash
curl -X GET "http://localhost:8080/api/approval-workflows" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 2. Get Workflow dengan Steps

```bash
curl -X GET "http://localhost:8080/api/approval-workflows/1/with-steps" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 3. Update Workflow

```bash
curl -X PUT "http://localhost:8080/api/approval-workflows/1/with-steps" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Updated Multi-Branch Workflow",
    "description": "Updated description",
    "isActive": true,
    "isDefault": false,
    "requiresApproval": true,
    "autoPublish": false,
    "steps": [
      {
        "stepOrder": 1,
        "stepName": "Updated Step",
        "requiredUserLevelId": 2,
        "canSkip": false,
        "isActive": true,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "{\"applies_to_levels\": [4,5,6]}",
        "branchName": "Updated_Branch",
        "branchOrder": 1
      }
    ]
  }'
```

### 4. Activate/Deactivate Workflow

```bash
# Activate workflow
curl -X PUT "http://localhost:8080/api/approval-workflows/1/activate" \
  -H "Authorization: Bearer YOUR_TOKEN"

# Deactivate workflow
curl -X PUT "http://localhost:8080/api/approval-workflows/1/deactivate" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

## Article Approval Flow

### 1. Submit Article untuk Approval

```bash
# Submit artikel dengan workflow ID tertentu
curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "workflowId": 1
  }'

# Submit artikel dengan default workflow
curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{}'
```

### 2. Process Multi-Branch Approval

```bash
# Approve dengan multi-branch logic
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": "Article looks good, approved for next level"
  }'

# Approve dengan message yang berbeda
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": "Content is excellent, proceeding to final approval"
  }'
```

### 3. Get Next Steps Preview

```bash
# Get preview next steps
curl -X GET "http://localhost:8080/api/article-approval-flows/456/next-steps-preview" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 4. Traditional Approval (untuk backward compatibility)

```bash
# Approve dengan method traditional
curl -X POST "http://localhost:8080/api/article-approval-flows/456/approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "message": "Approved using traditional method"
  }'
```

### 5. Reject Article

```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/456/reject" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "reason": "Content needs improvement"
  }'
```

### 6. Request Revision

```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/456/request-revision" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "revisionMessage": "Please improve the introduction section"
  }'
```

### 7. Resubmit After Revision

```bash
curl -X POST "http://localhost:8080/api/article-approval-flows/456/resubmit" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

## Monitoring dan Analytics

### 1. Get Approval History

```bash
curl -X GET "http://localhost:8080/api/articles/123/approval-history" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 2. Get My Approval Queue

```bash
curl -X GET "http://localhost:8080/api/article-approval-flows/my-queue" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 3. Get Pending Approvals

```bash
curl -X GET "http://localhost:8080/api/article-approval-flows/pending-approvals" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 4. Get Dashboard Statistics

```bash
curl -X GET "http://localhost:8080/api/article-approval-flows/dashboard-stats" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### 5. Get Workload Statistics

```bash
curl -X GET "http://localhost:8080/api/article-approval-flows/workload-stats" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

## Testing Scenarios

### Scenario 1: User Level 5 Submit Article

```bash
# 1. Submit artikel dari user level 5
curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_5_TOKEN" \
  -d '{
    "workflowId": 1
  }'

# 2. Check next steps preview
curl -X GET "http://localhost:8080/api/article-approval-flows/456/next-steps-preview" \
  -H "Authorization: Bearer USER_LEVEL_5_TOKEN"

# 3. Approve dengan multi-branch logic
curl -X POST "http://localhost:8080/api/article-approval-flows/456/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer LEVEL_2_APPROVER_TOKEN" \
  -d '{
    "message": "Approved by Level 2 approver"
  }'

# 4. Final approval
curl -X POST "http://localhost:8080/api/article-approval-flows/456/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer LEVEL_1_APPROVER_TOKEN" \
  -d '{
    "message": "Final approval completed"
  }'
```

### Scenario 2: User Level 8 Submit Article

```bash
# 1. Submit artikel dari user level 8
curl -X POST "http://localhost:8080/api/articles/124/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_8_TOKEN" \
  -d '{
    "workflowId": 1
  }'

# 2. Check next steps preview (should show different branch)
curl -X GET "http://localhost:8080/api/article-approval-flows/457/next-steps-preview" \
  -H "Authorization: Bearer USER_LEVEL_8_TOKEN"

# 3. Approve dengan multi-branch logic
curl -X POST "http://localhost:8080/api/article-approval-flows/457/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer LEVEL_3_APPROVER_TOKEN" \
  -d '{
    "message": "Approved by Level 3 approver"
  }'

# 4. Final approval
curl -X POST "http://localhost:8080/api/article-approval-flows/457/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer LEVEL_1_APPROVER_TOKEN" \
  -d '{
    "message": "Final approval completed"
  }'
```

### Scenario 3: User Level 12 Submit Article

```bash
# 1. Submit artikel dari user level 12
curl -X POST "http://localhost:8080/api/articles/125/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_12_TOKEN" \
  -d '{
    "workflowId": 1
  }'

# 2. Check next steps preview (should show Branch B)
curl -X GET "http://localhost:8080/api/article-approval-flows/458/next-steps-preview" \
  -H "Authorization: Bearer USER_LEVEL_12_TOKEN"

# 3. Approve dengan multi-branch logic
curl -X POST "http://localhost:8080/api/article-approval-flows/458/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer LEVEL_3_APPROVER_TOKEN" \
  -d '{
    "message": "Approved by Level 3 approver for Branch B"
  }'

# 4. Final approval
curl -X POST "http://localhost:8080/api/article-approval-flows/458/multi-branch-approve" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer LEVEL_1_APPROVER_TOKEN" \
  -d '{
    "message": "Final approval completed"
  }'
```

## Error Handling Examples

### 1. Invalid Workflow ID

```bash
curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "workflowId": 999
  }'
```

**Expected Response**:
```json
{
  "success": false,
  "messages": ["Workflow not found"],
  "error": "WORKFLOW_NOT_FOUND"
}
```

### 2. Invalid Condition Value

```bash
curl -X POST "http://localhost:8080/api/approval-workflows/with-steps" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Invalid Workflow",
    "description": "Workflow dengan invalid condition",
    "steps": [
      {
        "stepOrder": 1,
        "stepName": "Invalid Step",
        "requiredUserLevelId": 2,
        "conditionType": "user_level_hierarchy",
        "conditionValue": "invalid json",
        "branchName": "Invalid_Branch"
      }
    ]
  }'
```

**Expected Response**:
```json
{
  "success": false,
  "messages": ["Invalid condition value format"],
  "error": "CONDITION_VALUE_INVALID"
}
```

### 3. No Applicable Steps

```bash
# Submit artikel dari user level yang tidak ada di condition
curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer USER_LEVEL_99_TOKEN" \
  -d '{
    "workflowId": 1
  }'
```

**Expected Response**:
```json
{
  "success": false,
  "messages": ["No applicable steps found for this user level"],
  "error": "NO_APPLICABLE_STEPS"
}
```

## Performance Testing

### 1. Load Testing dengan Multiple Submissions

```bash
# Script untuk test multiple submissions
for i in {1..100}; do
  curl -X POST "http://localhost:8080/api/articles/$i/submit-approval" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -d '{
      "workflowId": 1
    }' &
done
wait
```

### 2. Concurrent Approval Testing

```bash
# Script untuk test concurrent approvals
for i in {1..50}; do
  curl -X POST "http://localhost:8080/api/article-approval-flows/$i/multi-branch-approve" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer APPROVER_TOKEN" \
    -d '{
      "message": "Concurrent approval test"
    }' &
done
wait
```

## Debugging Commands

### 1. Check Workflow Steps

```bash
curl -X GET "http://localhost:8080/api/approval-workflows/1/with-steps" \
  -H "Authorization: Bearer YOUR_TOKEN" | jq '.'
```

### 2. Check User Level

```bash
curl -X GET "http://localhost:8080/api/users/123" \
  -H "Authorization: Bearer YOUR_TOKEN" | jq '.data.user_levels'
```

### 3. Check Approval Flow Status

```bash
curl -X GET "http://localhost:8080/api/article-approval-flows/456" \
  -H "Authorization: Bearer YOUR_TOKEN" | jq '.'
```

### 4. Check Next Steps Preview

```bash
curl -X GET "http://localhost:8080/api/article-approval-flows/456/next-steps-preview" \
  -H "Authorization: Bearer YOUR_TOKEN" | jq '.'
```

## Environment Variables

```bash
# Set environment variables untuk testing
export API_BASE_URL="http://localhost:8080"
export ADMIN_TOKEN="your_admin_token_here"
export USER_LEVEL_5_TOKEN="your_user_level_5_token_here"
export USER_LEVEL_8_TOKEN="your_user_level_8_token_here"
export USER_LEVEL_12_TOKEN="your_user_level_12_token_here"
export LEVEL_1_APPROVER_TOKEN="your_level_1_approver_token_here"
export LEVEL_2_APPROVER_TOKEN="your_level_2_approver_token_here"
export LEVEL_3_APPROVER_TOKEN="your_level_3_approver_token_here"
```

## Batch Operations

### 1. Create Multiple Workflows

```bash
# Script untuk create multiple workflows
for i in {1..5}; do
  curl -X POST "$API_BASE_URL/api/approval-workflows/with-steps" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $ADMIN_TOKEN" \
    -d "{
      \"name\": \"Workflow $i\",
      \"description\": \"Test workflow $i\",
      \"isActive\": true,
      \"steps\": [
        {
          \"stepOrder\": 1,
          \"stepName\": \"Step 1\",
          \"requiredUserLevelId\": 2,
          \"conditionType\": \"always\",
          \"branchName\": \"Branch_$i\"
        }
      ]
    }"
done
```

### 2. Submit Multiple Articles

```bash
# Script untuk submit multiple articles
for i in {1..10}; do
  curl -X POST "$API_BASE_URL/api/articles/$i/submit-approval" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $USER_LEVEL_5_TOKEN" \
    -d '{
      "workflowId": 1
    }'
done
```

## Monitoring Commands

### 1. Check System Health

```bash
curl -X GET "$API_BASE_URL/health" \
  -H "Authorization: Bearer $ADMIN_TOKEN"
```

### 2. Get System Statistics

```bash
curl -X GET "$API_BASE_URL/api/article-approval-flows/dashboard-stats" \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.'
```

### 3. Check Workflow Performance

```bash
curl -X GET "$API_BASE_URL/api/approval-workflows" \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.data | length'
```
