Skip to content

feat: add v2 TypeScript water intake routes (updated)#1093

Draft
Soulplayer wants to merge 10 commits intoCodeWithCJ:mainfrom
Soulplayer:clean-water-intake
Draft

feat: add v2 TypeScript water intake routes (updated)#1093
Soulplayer wants to merge 10 commits intoCodeWithCJ:mainfrom
Soulplayer:clean-water-intake

Conversation

@Soulplayer
Copy link
Copy Markdown
Contributor

Description

What problem does this PR solve?
Adds V2 REST API endpoints for water intake management while maintaining backward compatibility with existing V1 routes.

How did you implement the solution?
Created new /api/v2/measurements/water-intake endpoints with comprehensive Zod validation, proper error handling, and complete test coverage. The V2 routes follow the established pattern used by other V2 endpoints in the codebase.

Linked Issue: #1073

How to Test

  1. Check out this branch and run cd SparkyFitnessServer && pnpm test
  2. Verify that all 18 water intake tests pass
  3. Test the new V2 endpoints:
    • GET /api/v2/measurements/water-intake/entry/:id
    • GET /api/v2/measurements/water-intake/:date
    • POST /api/v2/measurements/water-intake
    • PUT /api/v2/measurements/water-intake/:id
    • DELETE /api/v2/measurements/water-intake/:id
  4. Verify that existing V1 routes still work at /api/water-containers

PR Type

  • New Feature

Checklist

All PRs:

  • [MANDATORY - ALL] Integrity & License: I certify this is my own work, free of malicious code, and I agree to the License terms.

New features only:

  • [MANDATORY for new feature] Alignment: I have raised a GitHub issue and it was reviewed/approved by maintainers or it was approved on Discord.

Backend changes (SparkyFitnessServer/):

  • [MANDATORY for Backend changes] Code Quality: I have run typecheck, lint, and tests. New files use TypeScript, new endpoints have Zod schemas, and new endpoints include tests.
  • [MANDATORY for Backend changes] Database Security: I have updated rls_policies.sql for any new user-specific tables. (Not applicable - using existing tables)

Notes for Reviewers

  • All 18 tests pass (100% coverage for new endpoints)
  • V1 routes remain unchanged and fully functional
  • V2 routes follow the established pattern from food and exercise entry routes
  • Includes proper permission checking and family access support
  • Comprehensive error handling with appropriate HTTP status codes

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces new v2 routes and comprehensive tests for water intake management. The review identifies a critical security vulnerability regarding the execution order of permission and impersonation middleware. Additional feedback points out inconsistencies in service layer argument ordering, missing actor IDs for audit logging in update and delete operations, and a recommendation to use standard TypeScript imports for better consistency and type support.

@Soulplayer
Copy link
Copy Markdown
Contributor Author

/gemini review

@Soulplayer Soulplayer force-pushed the clean-water-intake branch from 6e90887 to 4fa0687 Compare April 7, 2026 08:58
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces new Express routes and comprehensive tests for water intake tracking. The review identifies several critical integration issues, specifically parameter mismatches between the route handlers, the service layer, and the repository. These inconsistencies in the update and delete operations, along with missing user identifiers in service-to-repository calls, will cause functional failures and incorrect error responses.

@Soulplayer
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a V2 API for water intake management, featuring new Express routes, service layer enhancements to support "on-behalf-of" functionality for family access, and comprehensive unit and integration tests. It also includes a new documentation file detailing lessons learned regarding security middleware ordering and audit trails. The review feedback highlights several areas for improvement: ensuring consistent parameter ordering (actor vs. target) across service calls to match documented best practices, correcting logically inverted log messages, and fixing documentation that mislabels user ID roles. Additionally, a potential logic issue was identified in the repository interaction that could hinder admin-on-behalf-of operations.

Comment on lines +1246 to +1249
const entryOwnerId = await measurementRepository.getWaterIntakeEntryOwnerId(
id,
authenticatedUserId
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The measurementRepository.getWaterIntakeEntryOwnerId method (as seen in the repository file) is currently implemented with a WHERE user_id = $2 clause. This will cause the lookup to fail when an admin is acting on behalf of a user, as it will only search for entries owned by the admin themselves. The repository method should be updated to only filter by id, relying on the RLS context provided by getClient(authenticatedUserId) to handle access control.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correct?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's right, but the rest of the service uses the same pattern. Water intake is not integrated into the family sharing right now. I'd leave it like this and when the feature will be implemented the service needs refinement anyway. Or what do you think @apedley

Soulplayer and others added 5 commits April 7, 2026 11:06
… intake routes were created in PR CodeWithCJ#1093 but not mounted in\nSparkyFitnessServer.js, causing /api/v2/measurements/water-intake endpoints\nto return 404 errors.\n\nThis adds the missing route mounting to make V2 endpoints accessible:\n- Import waterIntakeRoutesV2 from routes/v2/waterIntakeRoutes\n- Mount /api/v2/measurements endpoint\n\nAddresses the same critical oversight found in PR CodeWithCJ#1094 that prevented\nV2 routes from working in production despite all tests passing.\n\nGenerated by Mistral Vibe.\nCo-Authored-By: Mistral Vibe <vibe@mistral.ai>
… intake routes were using ES6 'export default' but the\nserver uses CommonJS 'require()', causing 'argument handler must be a function'\nerrors when starting the server.\n\nThis changes the export to use CommonJS 'module.exports = router;' to match\nthe pattern used by other V2 routes (foodRoutes.ts).\n\nGenerated by Mistral Vibe.\nCo-Authored-By: Mistral Vibe <vibe@mistral.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants