# Approval Workflow Flow Diagram

## 🔄 **Complete Approval Process Flow**

### **1. System Setup Phase**

```
┌─────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   CLIENT        │───▶│  APPROVAL_WORKFLOWS  │───▶│ APPROVAL_WORKFLOW   │
│   CREATION      │    │  (Master Template)   │    │ STEPS               │
└─────────────────┘    └──────────────────────┘    └─────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │ CLIENT_APPROVAL_     │
                       │ SETTINGS             │
                       │ (Configuration)      │
                       └──────────────────────┘
```

### **2. Article Submission Phase**

```
┌─────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   USER LEVEL 3  │───▶│   CREATE ARTICLE     │───▶│  CHECK USER LEVEL   │
│   (POLRES)      │    │                      │    │  REQUIRES APPROVAL? │
└─────────────────┘    └──────────────────────┘    └─────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │   YES - CREATE       │
                       │   APPROVAL FLOW      │
                       └──────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │ ARTICLE_APPROVAL_    │
                       │ FLOWS                │
                       │ (current_step: 1)    │
                       └──────────────────────┘
```

### **3. Step-by-Step Approval Process**

```
STEP 1: Level 2 Approval
┌─────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   USER LEVEL 2  │───▶│   REVIEW ARTICLE     │───▶│   APPROVE/REJECT    │
│   (POLDAS)      │    │   (Step 1)           │    │                     │
└─────────────────┘    └──────────────────────┘    └─────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │ CREATE STEP LOG      │
                       │ (ARTICLE_APPROVAL_   │
                       │  STEP_LOGS)          │
                       └──────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │ MOVE TO STEP 2       │
                       │ (current_step: 2)    │
                       └──────────────────────┘

STEP 2: Level 1 Approval
┌─────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   USER LEVEL 1  │───▶│   REVIEW ARTICLE     │───▶│   APPROVE/REJECT    │
│   (POLDAS)      │    │   (Step 2)           │    │                     │
└─────────────────┘    └──────────────────────┘    └─────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │ CREATE STEP LOG      │
                       │ (ARTICLE_APPROVAL_   │
                       │  STEP_LOGS)          │
                       └──────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │ COMPLETE APPROVAL    │
                       │ (status: approved)   │
                       └──────────────────────┘
```

## 📊 **Database State Changes**

### **Initial State (Article Created)**
```
ARTICLES:
├── id: 101
├── title: "Sample Article"
├── workflow_id: 1
├── current_approval_step: 1
├── status_id: 1 (pending)
└── created_by_id: 3

ARTICLE_APPROVAL_FLOWS:
├── id: 1
├── article_id: 101
├── workflow_id: 1
├── current_step: 1
├── status_id: 1 (pending)
├── submitted_by_id: 3
└── submitted_at: 2025-01-15 10:00:00

ARTICLE_APPROVALS (Legacy):
├── id: 1
├── article_id: 101
├── approval_at_level: 2
├── status_id: 1 (pending)
└── message: "Need Approval"
```

### **After Step 1 Approval (Level 2)**
```
ARTICLES:
├── current_approval_step: 2  ← UPDATED
└── status_id: 1 (pending)

ARTICLE_APPROVAL_FLOWS:
├── current_step: 2  ← UPDATED
└── status_id: 1 (pending)

ARTICLE_APPROVAL_STEP_LOGS:  ← NEW RECORD
├── id: 1
├── approval_flow_id: 1
├── step_order: 1
├── step_name: "Level 2 Review"
├── approved_by_id: 2
├── action: "approve"
├── message: "Approved by Level 2"
└── processed_at: 2025-01-15 11:00:00
```

### **After Step 2 Approval (Level 1) - Final**
```
ARTICLES:
├── current_approval_step: null  ← UPDATED
├── status_id: 2 (approved)  ← UPDATED
├── is_publish: true  ← UPDATED
└── published_at: 2025-01-15 12:00:00  ← UPDATED

ARTICLE_APPROVAL_FLOWS:
├── current_step: 2
├── status_id: 2 (approved)  ← UPDATED
└── completed_at: 2025-01-15 12:00:00  ← UPDATED

ARTICLE_APPROVAL_STEP_LOGS:  ← NEW RECORD
├── id: 2
├── approval_flow_id: 1
├── step_order: 2
├── step_name: "Level 1 Final Approval"
├── approved_by_id: 1
├── action: "approve"
├── message: "Final approval by Level 1"
└── processed_at: 2025-01-15 12:00:00
```      

## 🔧 **Module Interaction Matrix**

| Module | Purpose | Reads From | Writes To | Triggers |
|--------|---------|------------|-----------|----------|
| `articles` | Article Management | - | `articles` | Creates approval flow |
| `approval_workflows` | Workflow Templates | `approval_workflows` | - | Defines steps |
| `approval_workflow_steps` | Step Definitions | `approval_workflow_steps` | - | Defines requirements |
| `article_approval_flows` | Active Instances | `approval_workflows` | `article_approval_flows` | Manages progression |
| `article_approval_step_logs` | Audit Trail | `article_approval_flows` | `article_approval_step_logs` | Logs actions |
| `article_approvals` | Legacy Support | - | `article_approvals` | Backward compatibility |
| `client_approval_settings` | Configuration | `clients` | `client_approval_settings` | Sets defaults |

## 🚨 **Error Handling Flow**

```
┌─────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   ERROR         │───▶│   LOG ERROR          │───▶│   ROLLBACK          │
│   OCCURS        │    │   (Zerolog)          │    │   TRANSACTION       │
└─────────────────┘    └──────────────────────┘    └─────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │   NOTIFY USER        │
                       │   (Error Response)   │
                       └──────────────────────┘
```

## 🔄 **Auto-Skip Logic Flow**

```
┌─────────────────┐    ┌──────────────────────┐    ┌─────────────────────┐
│   USER LEVEL 1  │───▶│   CHECK CAN SKIP     │───▶│   SKIP ALL STEPS    │
│   (HIGHEST)     │    │   ALL STEPS?         │    │                     │
└─────────────────┘    └──────────────────────┘    └─────────────────────┘
                                │
                                ▼
                       ┌──────────────────────┐
                       │   AUTO APPROVE       │
                       │   (status: approved) │
                       └──────────────────────┘
```

## 📈 **Performance Considerations**

### **Database Indexes Needed**
```sql
-- For fast approval flow lookups
CREATE INDEX idx_article_approval_flows_status ON article_approval_flows(status_id);
CREATE INDEX idx_article_approval_flows_current_step ON article_approval_flows(current_step);

-- For step log queries
CREATE INDEX idx_article_approval_step_logs_flow_id ON article_approval_step_logs(approval_flow_id);
CREATE INDEX idx_article_approval_step_logs_processed_at ON article_approval_step_logs(processed_at);

-- For user level queries
CREATE INDEX idx_users_user_level_id ON users(user_level_id);
CREATE INDEX idx_user_levels_level_number ON user_levels(level_number);
```

### **Caching Strategy**
```
1. Workflow Templates → Cache in Redis
2. User Level Mappings → Cache in Memory
3. Active Approval Flows → Cache with TTL
4. Step Requirements → Cache per Workflow
```

## 🎯 **Key Success Metrics**

1. **Approval Time**: Average time from submission to approval
2. **Step Completion Rate**: Percentage of steps completed successfully
3. **Error Rate**: Percentage of failed approval processes
4. **User Satisfaction**: Feedback on approval process
5. **System Performance**: Response times for approval actions

## 🔍 **Debugging Checklist**

- [ ] Check if workflow exists and is active
- [ ] Verify user level permissions
- [ ] Confirm step order is correct
- [ ] Validate foreign key relationships
- [ ] Check transaction rollback scenarios
- [ ] Monitor step log creation
- [ ] Verify notification delivery
- [ ] Test edge cases (auto-skip, rejection, etc.)

## 📝 **Summary**

Sistem approval workflow MEDOLS menggunakan pendekatan modular yang memungkinkan:

1. **Fleksibilitas**: Workflow dapat dikonfigurasi per client
2. **Auditability**: Setiap action dicatat dalam step logs
3. **Scalability**: Dapat menangani multiple workflow types
4. **Backward Compatibility**: Legacy system tetap berfungsi
5. **User Experience**: Auto-skip untuk user level tinggi

Dengan arsitektur ini, sistem dapat menangani berbagai skenario approval dengan efisien dan dapat diandalkan.
