Spaces:
Sleeping
Sleeping
Task Status Update API - Quick Reference
Endpoint
PATCH /tracker/tasks/{task_id}/status
Request Format
{
"status": "in_progress",
"timestamp": 1708156800000,
"latitude": 19.0760,
"longitude": 72.8777
}
Status Values
not_started- Initial statein_progress- Task is being worked oncompleted- 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, completedtimestamp- Unix timestamp in milliseconds (positive integer)latitude- Must be between -90 and 90longitude- 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
{
"success": true,
"message": "Task status updated successfully"
}
400 Bad Request - Invalid Transition
{
"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
{
"success": false,
"error": "Task with ID {task_id} not found",
"detail": "Task with ID {task_id} not found"
}
422 Unprocessable Entity - Validation Error
{
"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 changelongitude- 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
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
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
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:
python test_tasks_api.py
This will:
- Create a test task
- Test GET /tasks/today
- Test all valid status transitions
- Test invalid transitions (should fail)
- Test with invalid task ID (should fail)
Database Schema
The status update modifies these columns in trans.scm_tasks:
status- Updated to new statusstarted_at- Set when transitioning to in_progresscompleted_at- Set when transitioning to completedlatitude- Updated on every changelongitude- Updated on every changeupdated_at- Automatically updated by database
Security Features
- Authentication - JWT token required
- Authorization - Only assigned employee can update
- Merchant Validation - Task must belong to employee's merchant
- Privacy - Returns 404 for unauthorized access (doesn't reveal task existence)
- Validation - All inputs validated before processing
- Idempotency - Same status updates are allowed (no error)
Best Practices
- Always capture current location - Don't reuse old coordinates
- Use server time - Generate timestamp on client but validate on server
- Handle errors gracefully - Check response status and show appropriate messages
- Validate before sending - Check coordinates and timestamp format
- Retry on network errors - But not on 400/404 errors
- Log status changes - For audit trail and debugging