Skip to main content

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

PropertyValue
Function Namecancel-historical-sync
HTTP MethodPOST
AuthenticationSupabase JWT (Bearer token in Authorization header)
Required RoleAny authenticated agency user (ownership validated by matching agency_id)

Request

Headers

HeaderRequiredDescription
AuthorizationYesBearer <supabase_jwt>
Content-TypeYesapplication/json

Body

FieldTypeRequiredDescription
job_idstring (UUID)YesThe 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

StatusCondition
400job_id is missing from the request body
400Job is not in a cancellable state (status is not pending or in_progress). Response includes current_status field.
401Missing or invalid Authorization header / JWT token
403Authenticated user is not associated with an agency
403Job belongs to a different agency than the authenticated user
404No job found with the provided job_id
405HTTP method is not POST
500Unexpected server error

Behavior

  • Handles CORS preflight (OPTIONS) requests and rejects non-POST methods with a 405.
  • Validates the JWT token via supabase.auth.getUser().
  • Looks up the user's agency_id from the users table.
  • Fetches the sync job from historical_sync_jobs and verifies:
    • The job exists (404 if not).
    • The job's agency_id matches the user's agency_id (403 if not).
    • The job's status is pending or in_progress (400 if not cancellable).
  • Calls the cancel_sync_job RPC to update the job status to cancelled.

Database Tables / RPCs

Table / RPCOperationDescription
usersReadFetches agency_id for the authenticated user
historical_sync_jobsReadFetches job record to verify existence, ownership, and status
cancel_sync_job (RPC)WriteSets the job status to cancelled

External APIs

None.