File size: 10,198 Bytes
dacae32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# 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_at` column (tracked in user_invitations table)
- βœ… Removed `activated_at` column (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:
1. Basic info (name, phone, emergency contacts)
2. Health info (blood type, allergies)
3. PPE sizes (for field agents)
4. Location (current address)
5. Financial account (payout details)
6. Documents (ID, license)

### Step 3: Profile Complete
```
user.status = 'pending_setup' β†’ 'active'
completion_percentage = 100%
```

## πŸ“ API Usage Examples

### Get My Profile
```bash
curl -X GET http://localhost:8000/api/v1/profile/me \
  -H "Authorization: Bearer <token>"
```

Response:
```json
{
  "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
```bash
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
```bash
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
```bash
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)
```bash
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
```bash
curl -X GET http://localhost:8000/api/v1/profile/{user_id} \
  -H "Authorization: Bearer <manager_token>"
```

### Manager: Check Edit Permissions
```bash
curl -X GET http://localhost:8000/api/v1/profile/{user_id}/permissions \
  -H "Authorization: Bearer <manager_token>"
```

Response:
```json
{
  "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
```bash
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

```python
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

1. **Hierarchical Permissions** - Role-based access control
2. **Field-Level Permissions** - Users can only edit specific fields
3. **Audit Logging** - All profile changes logged
4. **Soft Delete Protection** - Only active users can be edited
5. **Organization Isolation** - Admins can only edit their org's users
6. **Project-Based Access** - Managers can only edit their team members

## πŸ“‹ Next Steps

### Immediate
1. βœ… Run migration: `14_cleanup_users_table.sql`
2. βœ… Test profile endpoints
3. βœ… Build profile wizard UI

### Future Enhancements
1. **Financial Accounts Management** - CRUD endpoints
2. **Document Upload** - File upload with validation
3. **Asset Assignment** - Manager assigns equipment
4. **Profile Photos** - Avatar upload
5. **Profile History** - Track changes over time
6. **Bulk User Import** - CSV import with profile data
7. **Profile Templates** - Pre-fill based on role
8. **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!