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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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Task Service API Quick Reference

## Base URL
```
http://localhost:8082
```

## Authentication
All endpoints (except health check) require JWT authentication in the header:
```
Authorization: Bearer <your_jwt_token>
```

---

## Endpoints Summary

| Method | Endpoint | Description | Auth | Admin Only |
|--------|----------|-------------|------|------------|
| GET | `/tasks` | Health check | No | No |
| POST | `/api/tasks` | Create task | Yes | Yes |
| GET | `/api/tasks` | Get all tasks | Yes | No |
| GET | `/api/tasks/{id}` | Get task by ID | Yes | No |
| GET | `/api/tasks/user` | Get user's assigned tasks | Yes | No |
| PUT | `/api/tasks/{id}` | Update task | Yes | No |
| PUT | `/api/tasks/{id}/user/{userId}/assigned` | Assign task to user | Yes | No |
| PUT | `/api/tasks/{id}/complete` | Mark task as complete | Yes | No |
| DELETE | `/api/tasks/{id}` | Delete task | Yes | No |

---

## Detailed API Documentation

### 1. Health Check
Check if service is running.

**Request:**
```http
GET /tasks
```

**Response:**
```json
"Welcome to Task Service"
```

---

### 2. Create Task (Admin Only)
Create a new task. Only users with ROLE_ADMIN can create tasks.

**Request:**
```http
POST /api/tasks
Authorization: Bearer <jwt_token>
Content-Type: application/json

{
  "title": "Implement new feature",
  "description": "Implement the new authentication feature",
  "imageUrl": "https://example.com/image.png",
  "deadline": "2024-12-31T23:59:59",
  "tags": ["backend", "security", "priority-high"]
}
```

**Response (201 CREATED):**
```json
{
  "id": "65f9a1234567890abcdef123",
  "title": "Implement new feature",
  "description": "Implement the new authentication feature",
  "imageUrl": "https://example.com/image.png",
  "assignedUserId": null,
  "status": "PENDING",
  "deadline": "2024-12-31T23:59:59",
  "createAt": "2024-11-10T10:30:00",
  "tags": ["backend", "security", "priority-high"]
}
```

---

### 3. Get All Tasks
Retrieve all tasks with optional filtering and sorting.

**Request:**
```http
GET /api/tasks?status=PENDING&sortByDeadline=asc
Authorization: Bearer <jwt_token>
```

**Query Parameters:**
- `status` (optional): Filter by status - `PENDING`, `ASSIGNED`, or `DONE`
- `sortByDeadline` (optional): Sort by deadline - `asc` or `desc`
- `sortByCreatedAt` (optional): Sort by creation date - `asc` or `desc`

**Response (200 OK):**
```json
[
  {
    "id": "65f9a1234567890abcdef123",
    "title": "Implement new feature",
    "description": "Implement the new authentication feature",
    "imageUrl": "https://example.com/image.png",
    "assignedUserId": null,
    "status": "PENDING",
    "deadline": "2024-12-31T23:59:59",
    "createAt": "2024-11-10T10:30:00",
    "tags": ["backend", "security"]
  },
  {
    "id": "65f9a1234567890abcdef456",
    "title": "Fix bug in payment module",
    "description": "Resolve the payment gateway timeout issue",
    "imageUrl": null,
    "assignedUserId": null,
    "status": "PENDING",
    "deadline": "2024-11-15T18:00:00",
    "createAt": "2024-11-10T11:00:00",
    "tags": ["bug", "payment"]
  }
]
```

---

### 4. Get Task by ID
Retrieve a specific task by its ID.

**Request:**
```http
GET /api/tasks/65f9a1234567890abcdef123
Authorization: Bearer <jwt_token>
```

**Response (200 OK):**
```json
{
  "id": "65f9a1234567890abcdef123",
  "title": "Implement new feature",
  "description": "Implement the new authentication feature",
  "imageUrl": "https://example.com/image.png",
  "assignedUserId": "user123",
  "status": "ASSIGNED",
  "deadline": "2024-12-31T23:59:59",
  "createAt": "2024-11-10T10:30:00",
  "tags": ["backend", "security"]
}
```

**Error Response (404 NOT FOUND):**
```json
{
  "timestamp": "2024-11-10T10:30:00",
  "message": "Task not found with id: 65f9a1234567890abcdef123",
  "status": 404,
  "error": "Task Not Found"
}
```

---

### 5. Get User's Assigned Tasks
Retrieve all tasks assigned to the authenticated user.

**Request:**
```http
GET /api/tasks/user?status=ASSIGNED&sortByDeadline=asc
Authorization: Bearer <jwt_token>
```

**Query Parameters:**
- `status` (optional): Filter by status - `PENDING`, `ASSIGNED`, or `DONE`
- `sortByDeadline` (optional): Sort by deadline - `asc` or `desc`
- `sortByCreatedAt` (optional): Sort by creation date - `asc` or `desc`

**Response (200 OK):**
```json
[
  {
    "id": "65f9a1234567890abcdef123",
    "title": "Implement new feature",
    "description": "Implement the new authentication feature",
    "imageUrl": "https://example.com/image.png",
    "assignedUserId": "user123",
    "status": "ASSIGNED",
    "deadline": "2024-12-31T23:59:59",
    "createAt": "2024-11-10T10:30:00",
    "tags": ["backend", "security"]
  }
]
```

---

### 6. Update Task
Update an existing task's information.

**Request:**
```http
PUT /api/tasks/65f9a1234567890abcdef123
Authorization: Bearer <jwt_token>
Content-Type: application/json

{
  "title": "Implement new feature (Updated)",
  "description": "Updated description",
  "status": "ASSIGNED",
  "deadline": "2024-12-25T23:59:59",
  "tags": ["backend", "security", "updated"]
}
```

**Response (200 OK):**
```json
{
  "id": "65f9a1234567890abcdef123",
  "title": "Implement new feature (Updated)",
  "description": "Updated description",
  "imageUrl": "https://example.com/image.png",
  "assignedUserId": "user123",
  "status": "ASSIGNED",
  "deadline": "2024-12-25T23:59:59",
  "createAt": "2024-11-10T10:30:00",
  "tags": ["backend", "security", "updated"]
}
```

---

### 7. Assign Task to User
Assign a task to a specific user.

**Request:**
```http
PUT /api/tasks/65f9a1234567890abcdef123/user/user456/assigned
Authorization: Bearer <jwt_token>
```

**Response (200 OK):**
```json
{
  "id": "65f9a1234567890abcdef123",
  "title": "Implement new feature",
  "description": "Implement the new authentication feature",
  "imageUrl": "https://example.com/image.png",
  "assignedUserId": "user456",
  "status": "ASSIGNED",
  "deadline": "2024-12-31T23:59:59",
  "createAt": "2024-11-10T10:30:00",
  "tags": ["backend", "security"]
}
```

---

### 8. Complete Task
Mark a task as completed.

**Request:**
```http
PUT /api/tasks/65f9a1234567890abcdef123/complete
Authorization: Bearer <jwt_token>
```

**Response (200 OK):**
```json
{
  "id": "65f9a1234567890abcdef123",
  "title": "Implement new feature",
  "description": "Implement the new authentication feature",
  "imageUrl": "https://example.com/image.png",
  "assignedUserId": "user123",
  "status": "DONE",
  "deadline": "2024-12-31T23:59:59",
  "createAt": "2024-11-10T10:30:00",
  "tags": ["backend", "security"]
}
```

---

### 9. Delete Task
Delete a task by ID.

**Request:**
```http
DELETE /api/tasks/65f9a1234567890abcdef123
Authorization: Bearer <jwt_token>
```

**Response (204 NO CONTENT):**
```
(No body returned)
```

---

## Task Status Flow

```
PENDING → ASSIGNED → DONE
```

1. **PENDING**: Task created by admin, not assigned to anyone
2. **ASSIGNED**: Task assigned to a user
3. **DONE**: Task marked as completed

---

## Common Error Responses

### 400 Bad Request
```json
{
  "timestamp": "2024-11-10T10:30:00",
  "message": "Invalid request parameters",
  "status": 400,
  "error": "Bad Request"
}
```

### 401 Unauthorized
```json
{
  "timestamp": "2024-11-10T10:30:00",
  "message": "JWT required...",
  "status": 401,
  "error": "Unauthorized"
}
```

### 403 Forbidden
```json
{
  "timestamp": "2024-11-10T10:30:00",
  "message": "Only admin can create tasks",
  "status": 403,
  "error": "Forbidden"
}
```

### 404 Not Found
```json
{
  "timestamp": "2024-11-10T10:30:00",
  "message": "Task not found with id: 65f9a1234567890abcdef123",
  "status": 404,
  "error": "Task Not Found"
}
```

### 500 Internal Server Error
```json
{
  "timestamp": "2024-11-10T10:30:00",
  "message": "Internal server error",
  "status": 500,
  "error": "Internal Server Error"
}
```

---

## Testing with cURL

### Create Task (Admin)
```bash
curl -X POST http://localhost:8082/api/tasks \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Test Task",
    "description": "This is a test task",
    "deadline": "2024-12-31T23:59:59",
    "tags": ["test"]
  }'
```

### Get All Tasks
```bash
curl -X GET "http://localhost:8082/api/tasks?status=PENDING" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"
```

### Get Task by ID
```bash
curl -X GET http://localhost:8082/api/tasks/TASK_ID \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"
```

### Update Task
```bash
curl -X PUT http://localhost:8082/api/tasks/TASK_ID \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Updated Task Title",
    "status": "ASSIGNED"
  }'
```

### Complete Task
```bash
curl -X PUT http://localhost:8082/api/tasks/TASK_ID/complete \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"
```

### Delete Task
```bash
curl -X DELETE http://localhost:8082/api/tasks/TASK_ID \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"
```

---

## Notes

- All timestamps are in ISO 8601 format
- Task IDs are MongoDB ObjectIds (24-character hexadecimal strings)
- JWT tokens must be obtained from the User Service authentication endpoint
- Only admin users can create tasks
- Tasks can be in one of three states: PENDING, ASSIGNED, or DONE
- Sorting can be applied to either deadline or creation date, but not both simultaneously