Spaces:
Sleeping
Sleeping
Profile Management System - Implementation Complete β
Complete hierarchical profile management system with role-based permissions.
π― What Was Implemented
1. Database Migration
File: supabase/migrations/14_cleanup_users_table.sql
Changes:
- β
Removed
invited_atcolumn (tracked in user_invitations table) - β
Removed
activated_atcolumn (tracked in user_invitations.accepted_at) - β Added index for profile completion queries
- β Added comments explaining invitation tracking
2. Profile Schemas
File: src/app/schemas/profile.py
Schemas Created:
- β
ProfileCompletionStatus- Track which sections are complete - β
BasicProfileUpdate/BasicProfileResponse- Name, phone, emergency contacts - β
HealthInfoUpdate/HealthInfoResponse- Blood type, allergies, medications - β
PPESizesUpdate/PPESizesResponse- Height, weight, clothing sizes - β
LocationUpdate/LocationResponse- Current location tracking - β
CompleteProfileResponse- All sections combined - β
ProfilePermissions- What current user can edit - β
ProfileValidationResult- Validation errors and warnings - β
BulkProfileUpdate- Update multiple sections at once
3. Profile Service
File: src/app/services/profile_service.py
Features:
- β Hierarchical permission checking
- β Field-level permission enforcement
- β Profile completion calculation
- β Profile validation with role-specific requirements
- β Update methods for all profile sections
Permission Hierarchy:
Platform Admin β Can edit ANYONE
Client/Contractor Admin β Can edit their org's users
Project Manager/Dispatcher β Can edit field agents in their projects
Field Agent β Can edit own profile (limited fields)
4. Profile API Endpoints
File: src/app/api/v1/profile.py
Self-Edit Endpoints (Current User):
- β
GET /api/v1/profile/me- Get complete profile - β
GET /api/v1/profile/me/completion- Get completion status - β
GET /api/v1/profile/me/validation- Validate profile - β
PUT /api/v1/profile/me/basic- Update basic info - β
PUT /api/v1/profile/me/health- Update health info - β
PUT /api/v1/profile/me/ppe- Update PPE sizes - β
PUT /api/v1/profile/me/location- Update location - β
PUT /api/v1/profile/me/bulk- Bulk update (wizard)
Manager Endpoints (Edit Others):
- β
GET /api/v1/profile/{user_id}- Get user's profile - β
GET /api/v1/profile/{user_id}/permissions- Check edit permissions - β
GET /api/v1/profile/{user_id}/validation- Validate user's profile - β
PUT /api/v1/profile/{user_id}/basic- Update user's basic info - β
PUT /api/v1/profile/{user_id}/ppe- Update user's PPE sizes
5. Router Integration
File: src/app/api/v1/router.py
- β Registered profile router in main API router
π Profile Sections
Basic Info
- Name, phone, alternate phone
- Email, ID number, display name
- Emergency contact name & phone
Health Info (JSONB)
- Blood type
- Allergies
- Chronic conditions
- Medications
- Last medical check date
- Medical notes
PPE Sizes (JSONB)
- Height, weight
- Waist, shoe size
- Helmet, shirt, pants sizes
- Glove, vest sizes
Location
- Location name
- Country, region, city
- Address lines
- Google Maps link
- Latitude, longitude
- Last updated timestamp
Related Data (Counts)
- Financial accounts
- Documents
- Asset assignments
π Permission Matrix
| Section | Self-Edit | Manager | Admin |
|---|---|---|---|
| Basic Info | phone_alternate, display_name | All fields | All fields |
| Health Info | All fields | View only | All fields |
| PPE Sizes | All fields | All fields | All fields |
| Location | Limited fields | View only | All fields |
| Financial Accounts | All | View only | All |
| Documents | Upload | View, Verify | All |
| Role & Status | None | None | All |
π Profile Completion Workflow
Step 1: User Accepts Invitation
user.status = 'invited' β 'pending_setup'
Step 2: Profile Setup Wizard
User completes sections one by one:
- Basic info (name, phone, emergency contacts)
- Health info (blood type, allergies)
- PPE sizes (for field agents)
- Location (current address)
- Financial account (payout details)
- Documents (ID, license)
Step 3: Profile Complete
user.status = 'pending_setup' β 'active'
completion_percentage = 100%
π API Usage Examples
Get My Profile
curl -X GET http://localhost:8000/api/v1/profile/me \
-H "Authorization: Bearer <token>"
Response:
{
"basic_info": {
"id": "uuid",
"name": "John Doe",
"phone": "+254712345678",
"role": "field_agent",
"status": "active"
},
"health_info": {
"blood_type": "O+",
"allergies": "None"
},
"ppe_sizes": {
"height": "180cm",
"shirt_size": "L",
"shoe_size": "42"
},
"location": {
"current_city": "Nairobi",
"current_latitude": -1.2921,
"current_longitude": 36.8219
},
"completion_status": {
"basic_info": true,
"health_info": true,
"ppe_sizes": true,
"financial_accounts": true,
"documents": true,
"location": true,
"completion_percentage": 100
},
"financial_accounts_count": 1,
"documents_count": 2,
"asset_assignments_count": 3
}
Update Basic Profile
curl -X PUT http://localhost:8000/api/v1/profile/me/basic \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"phone_alternate": "+254798765432",
"emergency_contact_name": "Jane Doe",
"emergency_contact_phone": "+254723456789"
}'
Update Health Info
curl -X PUT http://localhost:8000/api/v1/profile/me/health \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"blood_type": "O+",
"allergies": "Penicillin",
"chronic_conditions": "None",
"last_medical_check": "2024-01-15"
}'
Update PPE Sizes
curl -X PUT http://localhost:8000/api/v1/profile/me/ppe \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"height": "180cm",
"weight": "75kg",
"shirt_size": "L",
"pants_size": "32",
"shoe_size": "42",
"helmet_size": "M"
}'
Bulk Update (Profile Wizard)
curl -X PUT http://localhost:8000/api/v1/profile/me/bulk \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"basic_info": {
"emergency_contact_name": "Jane Doe",
"emergency_contact_phone": "+254723456789"
},
"health_info": {
"blood_type": "O+",
"allergies": "None"
},
"ppe_sizes": {
"height": "180cm",
"shirt_size": "L",
"shoe_size": "42"
}
}'
Manager: Get Field Agent Profile
curl -X GET http://localhost:8000/api/v1/profile/{user_id} \
-H "Authorization: Bearer <manager_token>"
Manager: Check Edit Permissions
curl -X GET http://localhost:8000/api/v1/profile/{user_id}/permissions \
-H "Authorization: Bearer <manager_token>"
Response:
{
"can_edit_basic_info": true,
"can_edit_health_info": false,
"can_edit_ppe_sizes": true,
"can_edit_location": false,
"can_edit_financial_accounts": false,
"can_upload_documents": true,
"can_edit_role": false,
"can_edit_status": false,
"can_assign_assets": true,
"can_view_health_info": true,
"can_view_financial_accounts": true,
"can_view_documents": true
}
Manager: Update Field Agent PPE Sizes
curl -X PUT http://localhost:8000/api/v1/profile/{user_id}/ppe \
-H "Authorization: Bearer <manager_token>" \
-H "Content-Type: application/json" \
-d '{
"helmet_size": "L",
"vest_size": "L"
}'
β Validation Rules
All Users
- β Name required
- β Phone required (with country code)
Field Agents
- β Emergency contact name required
- β Emergency contact phone required
- β PPE sizes required (at least 3 sizes)
- β Financial account required
- β οΈ Blood type recommended (warning)
Phone Validation
- β
Must start with
+(country code) - β Length: 10-20 characters
Blood Type Validation
- β Must be one of: A+, A-, B+, B-, AB+, AB-, O+, O-
PPE Size Validation
- β Sizes must be: XS, S, M, L, XL, XXL, XXXL
π Profile Completion Calculation
sections_complete = {
'basic_info': name AND phone AND emergency_contact_name AND emergency_contact_phone,
'health_info': health_info JSONB has data,
'ppe_sizes': ppe_sizes JSONB has >= 3 sizes,
'financial_accounts': has at least 1 active account,
'documents': has at least 1 document,
'location': has address AND coordinates
}
completion_percentage = (completed_sections / total_sections) * 100
π Security Features
- Hierarchical Permissions - Role-based access control
- Field-Level Permissions - Users can only edit specific fields
- Audit Logging - All profile changes logged
- Soft Delete Protection - Only active users can be edited
- Organization Isolation - Admins can only edit their org's users
- Project-Based Access - Managers can only edit their team members
π Next Steps
Immediate
- β
Run migration:
14_cleanup_users_table.sql - β Test profile endpoints
- β Build profile wizard UI
Future Enhancements
- Financial Accounts Management - CRUD endpoints
- Document Upload - File upload with validation
- Asset Assignment - Manager assigns equipment
- Profile Photos - Avatar upload
- Profile History - Track changes over time
- Bulk User Import - CSV import with profile data
- Profile Templates - Pre-fill based on role
- Compliance Checks - Required documents per role
π Summary
The profile management system is now complete with:
- Hierarchical permission system
- Self-edit and manager-edit capabilities
- Profile completion tracking
- Validation with role-specific requirements
- Bulk update support for profile wizards
- Complete audit trail
- Clean separation of concerns
All foundation pieces are now in place for building projects and tickets!