feat(projects): task comments, read and post (app#2048)

Merged
SW
3mo ago
+325-0

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

Stacked on #881 and includes its commits until that merges, because both handlers go through its requireProjectAccess guard and reuse its selectProjectTask and toProjectComment. Independent of #882 — these two can merge in either order once #881 lands.

Also blocked on recoupable/database#68: the tables are not in types/database.types.ts yet, so tsc fails. Merge that, run pnpm update-types, and I will push the regenerated types.

What this adds

EndpointPurpose
GET /api/projects/{projectId}/tasks/{taskId}/commentsThe task's comments, oldest first
POST /api/projects/{projectId}/tasks/{taskId}/commentsPost a comment, attributed to the caller

This is the thread that replaces the email chain: the client comments on the task itself, and we answer in the same place.

The decisions worth reviewing

The author is the authenticated account, never a body field. There is no way to post as someone else, which is the whole reason comment identity moved to Privy rather than a ?as= token.

Both handlers check the task belongs to the project first. A task id from another project cannot be read or written through a project the caller does happen to collaborate on.

The validator mirrors the column's length(body) <= 4000 CHECK rather than trusting it. A 400 naming the field beats a 500 from a constraint violation, and there are tests at the cap and one over it.

Trimmed, and whitespace-only is rejected — matching the btrim(body) <> '' CHECK, so the same input cannot pass validation and then fail at the database.

Append-only. No edit, no delete, no markdown, no mentions, no attachments. Each is a real feature with real edge cases and none is needed to replace an email thread.

Verification

  • vitest lib/projects: 8 tests over this branch's units, passing. validateCreateProjectCommentBody was written test-first.
  • eslint: clean.
  • tsc: fails only on the missing table types described above.
  • Not preview-verified yet. Live checks — posting as a collaborator and seeing the author resolve, a 400 for an empty and an over-length body, a 404 for a task on another project, a 401 with no token — go in a comment before this merges.

🤖 Generated with Claude Code

https://claude.ai/code/session_01BTaK5sDuryYsQG5W56mR5n


Summary by cubic

Adds GET and POST /api/projects/{projectId}/tasks/{taskId}/comments so collaborators can read a task's comments and post new ones, replacing the email chain with a per-task thread.

Behavior notes

  • Both endpoints require a project_collaborators row; callers without one get a 404, never a 403, so a response never confirms a project id exists.
  • Access checks run before body validation, so unauthorized callers never learn whether their comment body would have been accepted.
  • Malformed projectId or taskId values return 400 instead of reaching Postgres as a 500.
  • Comment authors are always the authenticated account; the body cannot name anyone else.
  • Both handlers confirm the task belongs to the project before reading or writing its comments.
  • Comment bodies are trimmed, capped at 4000 characters, and whitespace-only input is rejected, mirroring the column's database checks.
  • Comments are append-only plain text: no edit, delete, markdown, mentions, or attachments.
  • Requires recoupable/database#68 to land first; run pnpm update-types afterward to generate the table types before merging.

Summary by CodeRabbit

  • New Features
    • Added comments support for project tasks.
    • Users can view task comments in chronological order.
    • Users can add comments with validated content up to 4,000 characters.
    • Added access checks and clear responses for invalid requests, missing tasks, and unexpected errors.