Bug: Fuel logs display 0.000 for efficiency and gallons due to DECIMAL type mismatch #49

Closed
opened 2026-01-18 04:26:10 +00:00 by egullickson · 0 comments
Owner

Summary

The fuel logs screen displays incorrect values:

  • Total Fuel shows 0.00 gallons instead of actual total
  • Each entry shows 0.000 @ $0.000 instead of {gallons} @ ${pricePerGallon}
  • Recent Fuel Logs don't update dynamically when adding new entries

Root Cause Analysis

PostgreSQL DECIMAL columns are returned as strings by the pg driver. The backend toEnhancedResponse() method in fuel-logs.service.ts converts total_cost to a number but not fuel_units or cost_per_unit:

// fuel-logs.service.ts:273-295
private toEnhancedResponse(row: any, efficiency: number | undefined, unitSystem): EnhancedFuelLogResponse {
  return {
    fuelUnits: row.fuel_units,           // NOT converted - arrives as string
    costPerUnit: row.cost_per_unit,      // NOT converted - arrives as string
    totalCost: Number(row.total_cost),   // Converted - works correctly
    // ...
  };
}

Frontend components have defensive checks that fail for strings:

// FuelStatsCard.tsx and FuelLogsList.tsx
const fuelUnits = typeof log.fuelUnits === 'number' ? log.fuelUnits.toFixed(3) : '0.000';

Affected Files

File Issue
backend/src/features/fuel-logs/domain/fuel-logs.service.ts:273-295 Missing Number() conversion on fuel_units and cost_per_unit
frontend/src/features/fuel-logs/hooks/useFuelLogs.tsx:46-49 Query invalidation may need review for dynamic updates

Steps to Reproduce

  1. Navigate to Fuel Logs page
  2. Observe Summary shows 0.00 gallons for Total Fuel
  3. Observe each log entry shows 0.000 @ $0.000 instead of actual values
  4. Add a new fuel log entry
  5. Observe the Recent Fuel Logs list doesn't update

Expected Behavior

  • Summary shows correct total gallons (e.g., 14.5 gallons)
  • Each entry shows {gallons} @ ${pricePerGallon} (e.g., 5.500 @ $3.459)
  • Recent Fuel Logs updates immediately when adding new entries

Acceptance Criteria

  • toEnhancedResponse() converts fuel_units and cost_per_unit to numbers using Number()
  • Summary card displays correct total fuel units
  • Log entries display correct gallons and price per gallon
  • Adding a new fuel log immediately updates the Recent Fuel Logs list
  • All existing tests pass
  • No TypeScript errors

Technical Notes

  • The getVehicleStats() method already handles this correctly with Number(r.fuel_units)
  • The fix should follow the same pattern as totalCost: Number(row.total_cost)
  • Review useFuelLogs.tsx mutation onSuccess to ensure proper query invalidation
## Summary The fuel logs screen displays incorrect values: - **Total Fuel** shows `0.00 gallons` instead of actual total - **Each entry** shows `0.000 @ $0.000` instead of `{gallons} @ ${pricePerGallon}` - **Recent Fuel Logs** don't update dynamically when adding new entries ## Root Cause Analysis PostgreSQL DECIMAL columns are returned as strings by the `pg` driver. The backend `toEnhancedResponse()` method in `fuel-logs.service.ts` converts `total_cost` to a number but not `fuel_units` or `cost_per_unit`: ```typescript // fuel-logs.service.ts:273-295 private toEnhancedResponse(row: any, efficiency: number | undefined, unitSystem): EnhancedFuelLogResponse { return { fuelUnits: row.fuel_units, // NOT converted - arrives as string costPerUnit: row.cost_per_unit, // NOT converted - arrives as string totalCost: Number(row.total_cost), // Converted - works correctly // ... }; } ``` Frontend components have defensive checks that fail for strings: ```typescript // FuelStatsCard.tsx and FuelLogsList.tsx const fuelUnits = typeof log.fuelUnits === 'number' ? log.fuelUnits.toFixed(3) : '0.000'; ``` ## Affected Files | File | Issue | |------|-------| | `backend/src/features/fuel-logs/domain/fuel-logs.service.ts:273-295` | Missing `Number()` conversion on `fuel_units` and `cost_per_unit` | | `frontend/src/features/fuel-logs/hooks/useFuelLogs.tsx:46-49` | Query invalidation may need review for dynamic updates | ## Steps to Reproduce 1. Navigate to Fuel Logs page 2. Observe Summary shows `0.00 gallons` for Total Fuel 3. Observe each log entry shows `0.000 @ $0.000` instead of actual values 4. Add a new fuel log entry 5. Observe the Recent Fuel Logs list doesn't update ## Expected Behavior - Summary shows correct total gallons (e.g., `14.5 gallons`) - Each entry shows `{gallons} @ ${pricePerGallon}` (e.g., `5.500 @ $3.459`) - Recent Fuel Logs updates immediately when adding new entries ## Acceptance Criteria - [ ] `toEnhancedResponse()` converts `fuel_units` and `cost_per_unit` to numbers using `Number()` - [ ] Summary card displays correct total fuel units - [ ] Log entries display correct gallons and price per gallon - [ ] Adding a new fuel log immediately updates the Recent Fuel Logs list - [ ] All existing tests pass - [ ] No TypeScript errors ## Technical Notes - The `getVehicleStats()` method already handles this correctly with `Number(r.fuel_units)` - The fix should follow the same pattern as `totalCost: Number(row.total_cost)` - Review `useFuelLogs.tsx` mutation `onSuccess` to ensure proper query invalidation
egullickson added the
status
backlog
type
bug
labels 2026-01-18 04:26:15 +00:00
egullickson added
status
in-progress
and removed
status
backlog
labels 2026-01-18 04:36:21 +00:00
egullickson added
status
review
and removed
status
in-progress
labels 2026-01-18 04:38:27 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: egullickson/motovaultpro#49