fix: weekly meal-plan nutrition compared a week's total against a daily goal

userNutritionGoals are daily targets (the nutrition diary already compares
a single day's totals against them directly) — but the weekly meal-plan
route summed an entire week's entries and compared that raw total straight
against the same daily number, so coverage read roughly 7x too high.

Now computes a daily average (across days that actually have planned
meals) and compares that against the goal instead, adds a per-day
breakdown (byDay) for a future day-view, and tracks unknownCount for
entries whose recipe has no nutrition data yet, matching the diary route.

Also fixed a real bug this surfaced: meal-plan/page.tsx and print/meal-plan
parsed the ?week= date via new Date(dateStr) (UTC midnight) then read
.getDay() (local time) — in positive-UTC-offset timezones this resolves to
the wrong Monday, and the reverse (Date -> string via toISOString()) has
the same mismatch in the other direction. Both now do local y/m/d math
throughout.

Verified locally: correct daily-average math end to end (recipe entry +
batch-dish entry, which already attributed nutrition correctly via its
required parent recipeId — no separate fix needed there), and confirmed
the meal-plan page now resolves the right week and renders real coverage
bars against actual planned entries.
This commit is contained in:
Arnaud
2026-07-12 18:35:58 +02:00
parent a9dc1b63c1
commit 9566e19cd0
4 changed files with 118 additions and 53 deletions
@@ -25,6 +25,8 @@ type NutritionCoverage = {
type NutritionResponse = {
totals: NutritionTotals;
dailyAverage: NutritionTotals;
unknownCount: number;
goals: NutritionGoals;
coverage: NutritionCoverage;
};
@@ -54,12 +56,12 @@ export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
if (!data || !data.goals) return null;
const { totals, goals, coverage } = data;
const { dailyAverage, goals, coverage, unknownCount } = data;
const bars: BarItem[] = [
{
label: "Calories",
value: totals.calories,
value: dailyAverage.calories,
goal: goals.caloriesKcal,
unit: "kcal",
percentage: coverage.calories,
@@ -67,7 +69,7 @@ export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
},
{
label: "Protein",
value: totals.protein,
value: dailyAverage.protein,
goal: goals.proteinG,
unit: "g",
percentage: coverage.protein,
@@ -75,7 +77,7 @@ export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
},
{
label: "Carbs",
value: totals.carbs,
value: dailyAverage.carbs,
goal: goals.carbsG,
unit: "g",
percentage: coverage.carbs,
@@ -83,7 +85,7 @@ export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
},
{
label: "Fat",
value: totals.fat,
value: dailyAverage.fat,
goal: goals.fatG,
unit: "g",
percentage: coverage.fat,
@@ -93,6 +95,7 @@ export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
return (
<div className="space-y-3">
<p className="text-xs text-muted-foreground">Daily average vs. your goals</p>
{bars.map((bar) => {
if (bar.goal == null) return null;
const width = Math.min(bar.percentage, 100);
@@ -113,6 +116,11 @@ export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
</div>
);
})}
{unknownCount > 0 && (
<p className="text-xs text-muted-foreground">
{unknownCount} planned {unknownCount === 1 ? "meal" : "meals"} missing nutrition data estimate it from the recipe page for a more accurate picture.
</p>
)}
</div>
);
}