Task Routes

Webhook handlers for Notion integrations, organized into focused modules.

File Structure

/backend/routes/tasks/
├── _tasks.routes.ts          # Main router + catch-all
├── webhook.routes.ts         # Production webhook endpoints
├── debug.routes.ts          # Debug/test endpoints
└── [new-feature].routes.ts  # Future endpoints (auto-mounted safely)

Adding New Endpoints

To add new /tasks endpoints:

  1. Create a new route file (e.g., feature.routes.ts)
  2. Import and mount it in _tasks.routes.ts BEFORE the catch-all
  3. Routes are automatically protected from the catch-all footgun

Webhook Endpoints (Production)

POST /tasks/url

Updates a Notion page's URL property with a glimpse URL.

Authentication: Requires X-API-KEY header with NOTION_WEBHOOK_SECRET value.

Request Body:

{ "data": { "id": "page-id-here" } }

Alternative payload structures supported:

{ "id": "page-id-here" }
{ "page_id": "page-id-here" }

Success Response (200):

{ "success": true, "message": "Page URL updated successfully", "pageId": "page-id-here", "url": "https://your-domain.com/glimpse/page-id-here", "timestamp": "2024-01-01T00:00:00.000Z" }

Error Responses:

400 - Missing page ID:

{ "success": false, "error": "Page ID is required in webhook payload" }

400 - Missing host header:

{ "success": false, "error": "Host header is required" }

500 - Notion API error:

{ "success": false, "error": "Failed to update Notion page with URL", "details": "Notion API error message" }

Testing:

curl -X POST https://your-val.web.val.run/tasks/url \ -H "X-API-KEY: your-notion-webhook-secret" \ -H "Content-Type: application/json" \ -d '{"data": {"id": "your-page-id"}}'

POST /tasks/assign

Assigns agents to demos based on Notion page assignments. Processes the "Assigned" property and creates agent blob assignments.

Authentication: Requires X-API-KEY header with NOTION_WEBHOOK_SECRET value.

Request Body:

{ "data": { "id": "page-id-here" } }

Alternative payload structures supported:

{ "id": "page-id-here" }
{ "page_id": "page-id-here" }

Process Flow:

  1. Extracts page ID from webhook payload
  2. Fetches Notion page data
  3. Extracts assigned person ID from "Assigned" property
  4. Queries agents database for matching agents
  5. Clears current demo's agent blob
  6. Removes assigned agents from other demo blobs (prevents double-assignment)
  7. Updates current demo's agent blob with new assignment

Success Response (200):

{ "success": true, "message": "Task assignment completed successfully (multi-blob clearing approach)", "pageId": "page-id-here", "personId": "person-id-here", "blobCleared": true, "agentsAssigned": 1, "agentBlobUpdated": true, "note": "Agents removed from other demo blobs and Notion database cleanup will be handled by cron jobs", "timestamp": "2024-01-01T00:00:00.000Z" }

Success Response - No Assignment Needed:

{ "success": true, "message": "No person ID found in Assigned property - no assignment needed", "pageId": "page-id-here", "timestamp": "2024-01-01T00:00:00.000Z" }

Error Responses:

400 - Missing page ID:

{ "success": false, "error": "Page ID is required in webhook payload" }

500 - Failed to fetch page data:

{ "success": false, "error": "Failed to fetch page data", "details": "Notion API error message" }

500 - Missing agents database configuration:

{ "success": false, "error": "GLANCE_AGENTS_DB_ID environment variable not configured" }

Testing:

curl -X POST https://your-val.web.val.run/tasks/assign \ -H "X-API-KEY: your-notion-webhook-secret" \ -H "Content-Type: application/json" \ -d '{"data": {"id": "your-page-id"}}'

Debug/Test Endpoints

POST /tasks/notion-webhook

Test webhook receiver - logs payload and returns success. Not for production use.

Authentication: Requires X-API-KEY header with NOTION_WEBHOOK_SECRET value.

Response:

{ "success": true }

POST /tasks/test

Test endpoint for webhook authentication.

Authentication: Requires X-API-KEY header with NOTION_WEBHOOK_SECRET value.

Success Response (200):

{ "success": true, "message": "Webhook authentication successful", "timestamp": "2024-01-01T00:00:00.000Z" }

GET /tasks/debug-webhook

Debug endpoint to check webhook configuration.

No authentication required.

Response:

{ "hasSecret": true, "secretLength": 32 }