10 KiB
Multi-Branch Approval System Implementation
🎯 Overview
Sistem Multi-Branch Approval telah berhasil diimplementasikan untuk mendukung workflow approval yang dapat bercabang berdasarkan User Level dari submitter artikel. Implementasi ini memungkinkan:
- Conditional Routing: Approval flow mengikuti jalur berbeda berdasarkan level user yang submit artikel
- Hierarchical Branching: Support untuk struktur cabang yang kompleks dengan multiple level
- Dynamic Configuration: Workflow dapat dikonfigurasi secara dinamis tanpa mengubah kode
- Backward Compatibility: Sistem lama tetap berfungsi tanpa perubahan
🚀 Quick Start
1. Database Migration
Jalankan script migration untuk menambahkan support multi-branch:
# Jalankan migration script
psql -d your_database -f docs/migrations/add_multi_branch_support.sql
2. Create Multi-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": "Multi-Branch Article Approval",
"description": "Workflow dengan cabang berdasarkan user level",
"isActive": true,
"steps": [
{
"stepOrder": 1,
"stepName": "Level 2 Branch",
"requiredUserLevelId": 2,
"conditionType": "user_level_hierarchy",
"conditionValue": "{\"applies_to_levels\": [4,5,6,7,8,9]}",
"branchName": "Branch_A"
},
{
"stepOrder": 1,
"stepName": "Level 3 Branch",
"requiredUserLevelId": 3,
"conditionType": "user_level_hierarchy",
"conditionValue": "{\"applies_to_levels\": [10,11,12,13,14,15]}",
"branchName": "Branch_B"
},
{
"stepOrder": 2,
"stepName": "Level 1 Final Approval",
"requiredUserLevelId": 1,
"conditionType": "always",
"branchName": "Final_Approval"
}
]
}'
3. Submit Article untuk Approval
curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"workflowId": 1
}'
4. Process Multi-Branch Approval
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"
}'
📁 File Changes
Database Entities
-
app/database/entity/approval_workflow_steps.entity.go- Added multi-branch support fields
- Added parent-child relationships
- Added condition types and values
-
app/database/entity/article_approval_flows.entity.go- Added branch tracking fields
- Added parallel flow support
- Added parent-child flow relationships
Service Layer
-
app/module/approval_workflow_steps/repository/approval_workflow_steps.repository.go- Added multi-branch query methods
- Added condition-based filtering
- Added branch-specific queries
-
app/module/article_approval_flows/service/article_approval_flows.service.go- Added multi-branch approval processing
- Added condition evaluation logic
- Added branch determination methods
API Layer
-
app/module/approval_workflows/request/approval_workflows.request.go- Added multi-branch fields to request structures
- Updated entity conversion methods
-
app/module/article_approval_flows/controller/article_approval_flows.controller.go- Added multi-branch approval endpoints
- Added next steps preview endpoint
🔧 Configuration
Condition Types
-
user_level: Steps berlaku untuk user level tertentu"conditionValue": "[4, 5, 6]" -
user_level_hierarchy: Steps berlaku berdasarkan hierarki"conditionValue": "{\"applies_to_levels\": [4,5,6], \"min_level\": 4, \"max_level\": 9}" -
always: Steps selalu berlaku"conditionValue": "{}" -
custom: Steps dengan kondisi custom (untuk ekstensi masa depan)
Branch Configuration
branchName: Nama cabang untuk identifikasibranchOrder: Urutan dalam cabang yang samaparentStepId: ID step parent untuk hierarchical branchingisParallel: Apakah step ini berjalan parallel
📊 Flow Examples
Example 1: User Level 5 Submit Article
User Level 5 → Branch A (Level 2) → Final Approval (Level 1)
Example 2: User Level 8 Submit Article
User Level 8 → Branch A (Level 2) → Final Approval (Level 1)
Example 3: User Level 12 Submit Article
User Level 12 → Branch B (Level 3) → Final Approval (Level 1)
🧪 Testing
Test Scenarios
-
Basic Branching Test:
# Submit dari user level 5 curl -X POST "http://localhost:8080/api/articles/123/submit-approval" \ -H "Authorization: Bearer USER_LEVEL_5_TOKEN" \ -d '{"workflowId": 1}' # Check next steps curl -X GET "http://localhost:8080/api/article-approval-flows/456/next-steps-preview" \ -H "Authorization: Bearer USER_LEVEL_5_TOKEN" -
Multiple Branch Test:
# Test dengan user level berbeda curl -X POST "http://localhost:8080/api/articles/124/submit-approval" \ -H "Authorization: Bearer USER_LEVEL_12_TOKEN" \ -d '{"workflowId": 1}'
Performance Testing
# Load testing dengan multiple submissions
for i in {1..100}; do
curl -X POST "http://localhost:8080/api/articles/$i/submit-approval" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"workflowId": 1}' &
done
wait
🔍 Monitoring
Check Workflow Status
# Get workflow dengan steps
curl -X GET "http://localhost:8080/api/approval-workflows/1/with-steps" \
-H "Authorization: Bearer YOUR_TOKEN"
# Get next steps preview
curl -X GET "http://localhost:8080/api/article-approval-flows/456/next-steps-preview" \
-H "Authorization: Bearer YOUR_TOKEN"
Dashboard Statistics
# Get approval statistics
curl -X GET "http://localhost:8080/api/article-approval-flows/dashboard-stats" \
-H "Authorization: Bearer YOUR_TOKEN"
🚨 Error Handling
Common Errors
-
No Applicable Steps:
{ "success": false, "messages": ["No applicable next steps found for this user level"], "error": "NO_APPLICABLE_STEPS" } -
Invalid Condition Value:
{ "success": false, "messages": ["Invalid condition value format"], "error": "CONDITION_VALUE_INVALID" } -
Workflow Not Found:
{ "success": false, "messages": ["Workflow not found"], "error": "WORKFLOW_NOT_FOUND" }
🔄 Migration Guide
From Linear to Multi-Branch
-
Backup existing data:
CREATE TABLE approval_workflow_steps_backup AS SELECT * FROM approval_workflow_steps; CREATE TABLE article_approval_flows_backup AS SELECT * FROM article_approval_flows; -
Run migration script:
psql -d your_database -f docs/migrations/add_multi_branch_support.sql -
Test migration:
- Verify existing workflows still work
- Test new multi-branch functionality
- Rollback if issues found
Rollback (if needed)
-- Uncomment rollback section in migration script
-- Run rollback commands to revert changes
📈 Performance Considerations
Database Indexes
-- Indexes untuk performance
CREATE INDEX idx_approval_workflow_steps_condition ON approval_workflow_steps(condition_type, branch_name);
CREATE INDEX idx_article_approval_flows_branch ON article_approval_flows(current_branch);
Caching Strategy
- Cache workflow steps by condition type
- Cache user level mappings
- Cache branch determination results
🔒 Security
Access Control
- Validate user permissions for each approval step
- Ensure users can only approve steps they're authorized for
- Log all approval actions for audit
Data Validation
- Validate condition values before processing
- Sanitize JSON input in condition_value
- Check for SQL injection in dynamic queries
🐛 Troubleshooting
Common Issues
-
Steps Not Found:
- Check condition_type and condition_value
- Verify user level mapping
- Check workflow activation status
-
Infinite Loops:
- Validate step dependencies
- Check for circular references in parent_step_id
- Ensure proper step ordering
-
Performance Issues:
- Check database indexes
- Optimize condition evaluation queries
- Consider caching strategies
Debug Commands
# Check workflow steps
curl -X GET "http://localhost:8080/api/approval-workflows/1/with-steps" \
-H "Authorization: Bearer YOUR_TOKEN"
# Check user level
curl -X GET "http://localhost:8080/api/users/456" \
-H "Authorization: Bearer YOUR_TOKEN"
# Check approval flow status
curl -X GET "http://localhost:8080/api/article-approval-flows/123" \
-H "Authorization: Bearer YOUR_TOKEN"
🚀 Future Enhancements
- Visual Workflow Designer: UI untuk membuat workflow secara visual
- Condition Builder: UI untuk membuat kondisi tanpa JSON manual
- Workflow Templates: Template workflow untuk berbagai use case
- Advanced Branching: Support untuk conditional branching yang lebih kompleks
- Parallel Processing: True parallel approval processing
- Workflow Analytics: Analytics untuk workflow performance dan bottlenecks
📚 Documentation
🤝 Support
Untuk pertanyaan atau masalah dengan implementasi multi-branch approval system, silakan:
- Check dokumentasi lengkap di
docs/folder - Review curl examples untuk testing
- Check troubleshooting section
- Contact development team untuk support lebih lanjut
Status: ✅ Implementasi selesai dan siap untuk production Version: 1.0.0 Last Updated: $(date)