fix(fitness): only show projected kcal on future days in nutrition

Past and today return projectedExercise=null, so the estimate no longer
appears on days where the user could have just skipped a workout. Also
skips the WorkoutSchedule lookup when not needed.
This commit is contained in:
2026-04-21 07:45:12 +02:00
parent 9093e0fe51
commit 53f803c04c
2 changed files with 6 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "homepage", "name": "homepage",
"version": "1.37.3", "version": "1.37.4",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
@@ -14,6 +14,8 @@ export const load: PageServerLoad = async ({ fetch, params, locals }) => {
// Run all independent work in parallel: 3 API calls + workout kcal DB query // Run all independent work in parallel: 3 API calls + workout kcal DB query
const dayStart = new Date(dateParam + 'T00:00:00.000Z'); const dayStart = new Date(dateParam + 'T00:00:00.000Z');
const dayEnd = new Date(dateParam + 'T23:59:59.999Z'); const dayEnd = new Date(dateParam + 'T23:59:59.999Z');
const todayStr = new Date().toISOString().slice(0, 10);
const isFuture = dateParam > todayStr;
const exercisePromise = (async () => { const exercisePromise = (async () => {
try { try {
@@ -24,16 +26,16 @@ export const load: PageServerLoad = async ({ fetch, params, locals }) => {
createdBy: user.nickname, createdBy: user.nickname,
startTime: { $gte: dayStart, $lte: dayEnd } startTime: { $gte: dayStart, $lte: dayEnd }
}).select('kcalEstimate').lean(), }).select('kcalEstimate').lean(),
WorkoutSchedule.findOne({ userId: user.nickname }).lean() isFuture ? WorkoutSchedule.findOne({ userId: user.nickname }).lean() : Promise.resolve(null)
]); ]);
let kcal = 0; let kcal = 0;
for (const s of sessions) { for (const s of sessions) {
if (s.kcalEstimate?.kcal) kcal += s.kcalEstimate.kcal; if (s.kcalEstimate?.kcal) kcal += s.kcalEstimate.kcal;
} }
// If no exercise done today, project kcal from the next scheduled template // For future days without exercise, project kcal from the next scheduled template
let projected = null; let projected = null;
if (kcal === 0) { if (kcal === 0 && isFuture) {
if (schedule?.templateOrder?.length) { if (schedule?.templateOrder?.length) {
const lastScheduled = await WorkoutSession.findOne({ const lastScheduled = await WorkoutSession.findOne({
createdBy: user.nickname, createdBy: user.nickname,
@@ -71,7 +73,6 @@ export const load: PageServerLoad = async ({ fetch, params, locals }) => {
const recentFrom = new Date(); const recentFrom = new Date();
recentFrom.setDate(recentFrom.getDate() - 3); recentFrom.setDate(recentFrom.getDate() - 3);
const recentFromStr = recentFrom.toISOString().slice(0, 10); const recentFromStr = recentFrom.toISOString().slice(0, 10);
const todayStr = new Date().toISOString().slice(0, 10);
const [foodRes, goalRes, weightRes, exerciseData, favRes, recentRes] = await Promise.all([ const [foodRes, goalRes, weightRes, exerciseData, favRes, recentRes] = await Promise.all([
fetch(`/api/fitness/food-log?date=${dateParam}`), fetch(`/api/fitness/food-log?date=${dateParam}`),