File size: 6,357 Bytes
dce1329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7a8704
 
 
 
 
 
dce1329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7a8704
 
 
 
 
 
dce1329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Organizations API Guide

Foundation layer for managing Clients and Contractors.

## Overview

- **Clients**: Companies that hire contractors (e.g., Airtel Kenya, Safaricom)
- **Contractors**: Companies that execute field work (e.g., TechInstall Ltd)

## Authentication

All endpoints require authentication. Include the JWT token in the header:
```
Authorization: Bearer <your_token>
```

## Clients API

### 1. Create Client

**POST** `/api/v1/clients`

**Requires:** `platform_admin`, `client_admin`, or `contractor_admin` role

**Business Logic:** Self-service organization onboarding
- Platform admins can create any client
- Client admins can create clients (their own organization or partners)
- Contractor admins can create clients they work with

```json
{
  "name": "Airtel Kenya",
  "description": "Leading telecommunications provider",
  "industry": "Telecommunications",
  "main_email": "contact@airtel.co.ke",
  "main_phone": "+254700000000",
  "website": "https://airtel.co.ke",
  "default_sla_days": 3
}
```

**Response:**
```json
{
  "id": "uuid",
  "name": "Airtel Kenya",
  "description": "Leading telecommunications provider",
  "industry": "Telecommunications",
  "main_email": "contact@airtel.co.ke",
  "main_phone": "+254700000000",
  "website": "https://airtel.co.ke",
  "is_active": true,
  "default_sla_days": 3,
  "additional_metadata": {},
  "created_at": "2025-11-15T22:00:00Z",
  "updated_at": "2025-11-15T22:00:00Z"
}
```

### 2. List Clients

**GET** `/api/v1/clients?skip=0&limit=100&is_active=true`

**Query Parameters:**
- `skip`: Number of records to skip (default: 0)
- `limit`: Max records to return (default: 100, max: 100)
- `is_active`: Filter by active status (optional)

**Response:** Array of client objects

### 3. Get Client

**GET** `/api/v1/clients/{client_id}`

**Response:** Single client object

### 4. Update Client

**PUT** `/api/v1/clients/{client_id}`

**Requires:** `platform_admin` or `client_admin` (own organization only)

```json
{
  "description": "Updated description",
  "is_active": true,
  "default_sla_days": 5
}
```

### 5. Delete Client

**DELETE** `/api/v1/clients/{client_id}`

**Requires:** `platform_admin` role

Performs soft delete (sets `deleted_at` timestamp).

---

## Contractors API

### 1. Create Contractor

**POST** `/api/v1/contractors`

**Requires:** `platform_admin`, `client_admin`, or `contractor_admin` role

**Business Logic:** Self-service organization onboarding
- Platform admins can create any contractor
- Client admins can create contractors they want to hire
- Contractor admins can create contractors (their own organization or partners)

```json
{
  "name": "TechInstall Ltd",
  "description": "Professional field service provider",
  "website": "https://techinstall.co.ke",
  "main_email": "info@techinstall.co.ke",
  "main_phone": "+254711111111",
  "competencies": ["FTTH", "Fixed Wireless", "Fiber Splicing"]
}
```

**Response:**
```json
{
  "id": "uuid",
  "name": "TechInstall Ltd",
  "description": "Professional field service provider",
  "website": "https://techinstall.co.ke",
  "main_email": "info@techinstall.co.ke",
  "main_phone": "+254711111111",
  "is_active": true,
  "competencies": ["FTTH", "Fixed Wireless", "Fiber Splicing"],
  "onboarding_status": "started",
  "onboarding_completed_at": null,
  "additional_metadata": {},
  "created_at": "2025-11-15T22:00:00Z",
  "updated_at": "2025-11-15T22:00:00Z"
}
```

### 2. List Contractors

**GET** `/api/v1/contractors?skip=0&limit=100&is_active=true`

**Query Parameters:**
- `skip`: Number of records to skip (default: 0)
- `limit`: Max records to return (default: 100, max: 100)
- `is_active`: Filter by active status (optional)

**Response:** Array of contractor objects

### 3. Get Contractor

**GET** `/api/v1/contractors/{contractor_id}`

**Response:** Single contractor object

### 4. Update Contractor

**PUT** `/api/v1/contractors/{contractor_id}`

**Requires:** `platform_admin` or `contractor_admin` (own organization only)

```json
{
  "description": "Updated description",
  "is_active": true,
  "competencies": ["FTTH", "Fixed Wireless", "Fiber Splicing", "FTTB"],
  "onboarding_status": "completed"
}
```

**Note:** Setting `onboarding_status` to `"completed"` automatically sets `onboarding_completed_at` timestamp.

### 5. Delete Contractor

**DELETE** `/api/v1/contractors/{contractor_id}`

**Requires:** `platform_admin` role

Performs soft delete (sets `deleted_at` timestamp).

---

## Testing with cURL

### Create Client
```bash
curl -X POST https://your-space.hf.space/api/v1/clients \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Airtel Kenya",
    "industry": "Telecommunications",
    "main_email": "contact@airtel.co.ke",
    "default_sla_days": 3
  }'
```

### List Clients
```bash
curl -X GET "https://your-space.hf.space/api/v1/clients?limit=10" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

### Create Contractor
```bash
curl -X POST https://your-space.hf.space/api/v1/contractors \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "TechInstall Ltd",
    "main_email": "info@techinstall.co.ke",
    "competencies": ["FTTH", "Fixed Wireless"]
  }'
```

---

## Error Responses

### 400 Bad Request
```json
{
  "detail": "Client with name 'Airtel Kenya' already exists"
}
```

### 403 Forbidden
```json
{
  "detail": "Only platform administrators can create clients"
}
```

### 404 Not Found
```json
{
  "detail": "Client not found"
}
```

---

## Authorization Matrix

| Endpoint | platform_admin | client_admin | contractor_admin | Others |
|----------|---------------|--------------|------------------|--------|
| Create Client | βœ… | ❌ | ❌ | ❌ |
| List Clients | βœ… | βœ… | βœ… | βœ… |
| Get Client | βœ… | βœ… | βœ… | βœ… |
| Update Client | βœ… | βœ… (own) | ❌ | ❌ |
| Delete Client | βœ… | ❌ | ❌ | ❌ |
| Create Contractor | βœ… | ❌ | ❌ | ❌ |
| List Contractors | βœ… | βœ… | βœ… | βœ… |
| Get Contractor | βœ… | βœ… | βœ… | βœ… |
| Update Contractor | βœ… | ❌ | βœ… (own) | ❌ |
| Delete Contractor | βœ… | ❌ | ❌ | ❌ |

---

## Next Steps

1. Deploy updated code
2. Test client creation
3. Test contractor creation
4. Assign users to organizations
5. Build projects layer (clients + contractors β†’ projects)