Cancel Historical Sync
Cancels an active historical sync job. Only jobs in pending or in_progress status can be cancelled. The caller must belong to the same agency that owns the job.
Endpoint
| Property | Value |
|---|---|
| Function Name | cancel-historical-sync |
| HTTP Method | POST |
| Authentication | Supabase JWT (Bearer token in Authorization header) |
| Required Role | Any authenticated agency user (ownership validated by matching agency_id) |
Request
Headers
| Header | Required | Description |
|---|---|---|
Authorization | Yes | Bearer <supabase_jwt> |
Content-Type | Yes | application/json |
Body
| Field | Type | Required | Description |
|---|---|---|---|
job_id | string (UUID) | Yes | The ID of the sync job to cancel |
{
"job_id": "550e8400-e29b-41d4-a716-446655440000"
}
Response
Success (200)
{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "cancelled"
}
Error Responses
| Status | Condition |
|---|---|
| 400 | job_id is missing from the request body |
| 400 | Job is not in a cancellable state (status is not pending or in_progress). Response includes current_status field. |
| 401 | Missing or invalid Authorization header / JWT token |
| 403 | Authenticated user is not associated with an agency |
| 403 | Job belongs to a different agency than the authenticated user |
| 404 | No job found with the provided job_id |
| 405 | HTTP method is not POST |
| 500 | Unexpected server error |
Behavior
- Handles CORS preflight (
OPTIONS) requests and rejects non-POSTmethods with a405. - Validates the JWT token via
supabase.auth.getUser(). - Looks up the user's
agency_idfrom theuserstable. - Fetches the sync job from
historical_sync_jobsand verifies:- The job exists (404 if not).
- The job's
agency_idmatches the user'sagency_id(403 if not). - The job's status is
pendingorin_progress(400 if not cancellable).
- Calls the
cancel_sync_jobRPC to update the job status tocancelled.
Database Tables / RPCs
| Table / RPC | Operation | Description |
|---|---|---|
users | Read | Fetches agency_id for the authenticated user |
historical_sync_jobs | Read | Fetches job record to verify existence, ownership, and status |
cancel_sync_job (RPC) | Write | Sets the job status to cancelled |
External APIs
None.
Related Functions
start-historical-sync-- Creates new sync jobsprocess-historical-sync-- Processes pending sync jobsget-sync-status-- Retrieves progress information for active and recent sync jobs