File size: 5,232 Bytes
32eb084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Task Status Update API - Quick Reference

## Endpoint
```
PATCH /tracker/tasks/{task_id}/status
```

## Request Format
```json
{
  "status": "in_progress",
  "timestamp": 1708156800000,
  "latitude": 19.0760,
  "longitude": 72.8777
}
```

## Status Values
- `not_started` - Initial state
- `in_progress` - Task is being worked on
- `completed` - Task is finished

## Valid Status Transitions

### From: not_started
βœ… `not_started` β†’ `in_progress` (captures started_at)
βœ… `not_started` β†’ `completed` (direct completion)

### From: in_progress
βœ… `in_progress` β†’ `completed` (captures completed_at)
βœ… `in_progress` β†’ `not_started` (revert)

### From: completed
βœ… `completed` β†’ `in_progress` (reopen)
❌ `completed` β†’ `not_started` (must go through in_progress)

## Validation Rules

### Required Fields
- `status` - Must be one of: not_started, in_progress, completed
- `timestamp` - Unix timestamp in milliseconds (positive integer)
- `latitude` - Must be between -90 and 90
- `longitude` - Must be between -180 and 180

### Authorization
- Must be authenticated (JWT token required)
- Task must be assigned to the authenticated employee
- Task must belong to employee's merchant

## Response Codes

### 200 OK - Success
```json
{
  "success": true,
  "message": "Task status updated successfully"
}
```

### 400 Bad Request - Invalid Transition
```json
{
  "success": false,
  "error": "Cannot transition from 'completed' to 'not_started'",
  "detail": "Cannot transition from 'completed' to 'not_started'"
}
```

### 404 Not Found - Task Not Found or Unauthorized
```json
{
  "success": false,
  "error": "Task with ID {task_id} not found",
  "detail": "Task with ID {task_id} not found"
}
```

### 422 Unprocessable Entity - Validation Error
```json
{
  "success": false,
  "error": "Validation Error",
  "errors": [
    {
      "field": "body -> latitude",
      "message": "ensure this value is greater than or equal to -90",
      "type": "value_error.number.not_ge"
    }
  ]
}
```

## Timestamp Tracking

### started_at
- Set when status changes to `in_progress`
- Stored as Unix timestamp in milliseconds (BIGINT)
- Captured from request payload

### completed_at
- Set when status changes to `completed`
- Stored as Unix timestamp in milliseconds (BIGINT)
- Captured from request payload

### updated_at
- Set on every status update
- Stored as PostgreSQL TIMESTAMP
- Automatically set by database

## Location Tracking

Every status update captures the employee's location:
- `latitude` - Updated on every status change
- `longitude` - Updated on every status change

This allows tracking where the employee was when they:
- Started the task
- Completed the task
- Made any status changes

## Example Usage

### Start a task
```bash
curl -X PATCH "http://localhost:8003/tracker/tasks/550e8400-e29b-41d4-a716-446655440000/status" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "status": "in_progress",
    "timestamp": 1708156800000,
    "latitude": 19.0760,
    "longitude": 72.8777
  }'
```

### Complete a task
```bash
curl -X PATCH "http://localhost:8003/tracker/tasks/550e8400-e29b-41d4-a716-446655440000/status" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "status": "completed",
    "timestamp": 1708160400000,
    "latitude": 19.0761,
    "longitude": 72.8778
  }'
```

### Reopen a completed task
```bash
curl -X PATCH "http://localhost:8003/tracker/tasks/550e8400-e29b-41d4-a716-446655440000/status" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "status": "in_progress",
    "timestamp": 1708164000000,
    "latitude": 19.0762,
    "longitude": 72.8779
  }'
```

## Testing

Run the comprehensive test suite:
```bash
python test_tasks_api.py
```

This will:
1. Create a test task
2. Test GET /tasks/today
3. Test all valid status transitions
4. Test invalid transitions (should fail)
5. Test with invalid task ID (should fail)

## Database Schema

The status update modifies these columns in `trans.scm_tasks`:
- `status` - Updated to new status
- `started_at` - Set when transitioning to in_progress
- `completed_at` - Set when transitioning to completed
- `latitude` - Updated on every change
- `longitude` - Updated on every change
- `updated_at` - Automatically updated by database

## Security Features

1. **Authentication** - JWT token required
2. **Authorization** - Only assigned employee can update
3. **Merchant Validation** - Task must belong to employee's merchant
4. **Privacy** - Returns 404 for unauthorized access (doesn't reveal task existence)
5. **Validation** - All inputs validated before processing
6. **Idempotency** - Same status updates are allowed (no error)

## Best Practices

1. **Always capture current location** - Don't reuse old coordinates
2. **Use server time** - Generate timestamp on client but validate on server
3. **Handle errors gracefully** - Check response status and show appropriate messages
4. **Validate before sending** - Check coordinates and timestamp format
5. **Retry on network errors** - But not on 400/404 errors
6. **Log status changes** - For audit trail and debugging