feat(projects): collaborator access guard and the two project reads (app#2048)

Merged
SW
3mo ago
+769-0

Implements the first api row of recoupable/app#2048. Contract: recoupable/docs#326.

Blocked on recoupable/database#68. The tables do not exist yet, so Tables<"project_collaborators"> is not in types/database.types.ts and tsc fails with Type '"project_collaborators"' does not satisfy the constraint. Merge that PR, run pnpm update-types, and I will push the regenerated types onto this branch to turn CI green. Nothing else about this PR is waiting.

What this adds

EndpointReturns
GET /api/projects/{projectId}The project, its tasks oldest first each carrying comment_count, and its collaborators
GET /api/projects/{projectId}/tasks/{taskId}One task with its comment feed and the project's collaborators

Plus requireProjectAccess, the gate the other two api PRs in this issue also use.

The decisions worth reviewing

404, never 403. A caller with no project_collaborators row gets 404, and so does an id that does not exist. A 403 tells the caller the project id is real, and these ids travel in links we send clients. getProjectHandler also answers 404 rather than 500 for a collaborator row whose project is missing — a broken invariant the caller can do nothing about either way.

hasProjectAccess denies on a lookup failure rather than propagating. Treating a database error as access would turn one bad minute into a disclosure. There is a test for it.

No account_id override. Access here is per project, so a caller acting for another account still needs that account's collaborator row — which is what the row already expresses. validateAuthContext is called with no override input.

Comment counts are one grouped read, not a count per task, so the project page does not scale calls with the length of the timeline.

Both ids are matched on the task read. selectProjectTask filters on project_id and id, so a task id belonging to another project cannot be read through a project the caller does collaborate on.

Verification

  • vitest lib/projects: 3 tests, passing. Written test-first, RED confirmed on "module not found" before hasProjectAccess existed.
  • eslint: clean across every file in this diff.
  • tsc: fails only on the missing table types described above. No other errors in these files.
  • Not preview-verified yet. Live checks against the deployment — 200 for a collaborator, 404 for a signed-in non-collaborator, 401 with no token, a deliberate non-UUID — run once the tables exist, and the results go in a comment before this merges.

🤖 Generated with Claude Code

https://claude.ai/code/session_01BTaK5sDuryYsQG5W56mR5n


Summary by cubic

Adds the two project reads and the collaborator access guard they share, implementing the first api row of app#2048. A malformed non-UUID path segment now answers 400 instead of 500.

  • GET /api/projects/{projectId} returns the project, its tasks oldest first each with comment_count, and its collaborators.
  • GET /api/projects/{projectId}/tasks/{taskId} returns one task with its comment feed and the project's collaborators.
  • A caller without a project_collaborators row gets 404, never 403.
  • A collaborator lookup failure denies access instead of throwing.
  • One grouped comment select feeds the per-task counts and a task's comment feed, so counts don't scale with the timeline.
  • The task read matches both project_id and id, so a task id from another project can't be read through a project the caller does collaborate on.
  • Regenerated Supabase types for the four project tables are included, so tsc passes.

Summary by CodeRabbit

  • New Features
    • Added project and project-task API endpoints for retrieving project details, tasks, collaborators, and comments.
    • Project responses now include task comment counts and collaborator and comment author names.
    • Added secure access checks for project data using supported API credentials.
    • Added consistent handling for missing projects or tasks, unauthorized access, and server errors.