fix: Fuel Logs API returns 500 error due to repository/service snake_case mismatch #47
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Bug Description
The Fuel Logs API (
GET /api/fuel-logs) returns a 500 Internal Server Error. Users cannot view or add fuel log entries.Root Cause
There is a snake_case/camelCase mismatch between the repository and service layers:
Repository
mapRow()method (fuel-logs.repository.ts:232-248) converts database snake_case columns to TypeScript camelCase properties:row.user_id->userIdrow.created_at->createdAtrow.updated_at->updatedAtEnhanced repository methods (
findByUserIdEnhanced,findByVehicleIdEnhanced,findByIdEnhanced) incorrectly callthis.mapRow()on their results (lines 299-318).Service
toEnhancedResponse()(fuel-logs.service.ts:273-295) expects raw database rows with snake_case properties:When
toEnhancedResponse()triesnew Date(row.created_at).toISOString()withrow.created_atbeingundefined(because mapRow converted it tocreatedAt), it throws:Affected Code
backend/src/features/fuel-logs/data/fuel-logs.repository.ts- lines 299-318findByVehicleIdEnhanced,findByUserIdEnhanced,findByIdEnhancedFix
The enhanced repository methods should return raw database rows (not call
mapRow()), since the service'stoEnhancedResponse()handles the snake_case to camelCase conversion.Change:
To:
Apply same fix to:
findByVehicleIdEnhanced(line 304)findByUserIdEnhanced(line 312)findByIdEnhanced(line 317)getPreviousLogByOdometer(line 325)getLatestLogForVehicle(line 333)createEnhanced(line 296)updateEnhanced(line 419)Reproduction Steps
GET /api/fuel-logsreturns 500Scope
fuel-logs.repository.ts)