From feb54c1f9a9cd3c24cc4926cb0cae7129cd1d3f9 Mon Sep 17 00:00:00 2001
From: luciano
Date: Wed, 29 Apr 2026 22:27:49 +0200
Subject: [PATCH] =?UTF-8?q?feat:=20piano=20sensibile=20al=20protocollo=20d?=
=?UTF-8?q?igiuno=20(PRD=20=C2=A75.6=20+=20=C2=A79.4)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Finora il fastingProtocol era una preferenza salvata ma ignorata dal
planner — ogni giorno generava 4 pasti. Ora cambia struttura per protocollo
e per giorno della settimana.
@ketopath/shared/planner
- protocolPlanForDay(protocol, dayOfWeek) → { share, kcalMultiplier }
- 14:10 → tutti e 4 i pasti, share default
- 16:8 → niente colazione: pranzo 50%, spuntino 10%, cena 40%
- 18:6 → niente colazione: pranzo 55%, spuntino 5%, cena 40%
- 20:4 → solo spuntino+cena (10/90)
- ESE 24h → mercoledì kcalMultiplier=0 (giorno saltato), altri default
- 5:2 → lunedì + giovedì kcalMultiplier=0.25, share 100% cena
- 7 unit test in protocol.test.ts
apps/api/plan
- POST /me/meal-plans: legge preferences.fastingProtocol, deriva il piano
per ogni giorno, salta MealSlot con share=0 e giorni a kcalMultiplier=0
- GET /me/meal-plans/current: include fastingProtocol nella risposta
- baseDailyTarget restituito invariato per il client
apps/web
- /plan mostra "Finestra alimentare 16:8" sotto il titolo della settimana
- plan-week distingue i giorni "fasting" (digiuno completo) e "leggeri"
(5:2 fasting day) con copy dedicato; rimuove totali calorici inappropriati
- i18n: Plan.protocolLabel + Plan.fastingDay/lightDay
Smoke tested: settato 16:8 da /profile, rigenerato il piano, verificato
che l'etichetta appaia e che ogni giorno abbia esattamente 3 pasti
(Pranzo / Spuntino / Cena, niente Colazione).
Co-Authored-By: Claude Opus 4.7 (1M context)
---
apps/api/src/modules/plan/plan.routes.ts | 64 +++++++++++----
apps/web/messages/it.json | 13 +++
apps/web/src/app/[locale]/plan/actions.ts | 8 ++
apps/web/src/app/[locale]/plan/page.tsx | 8 ++
apps/web/src/app/[locale]/plan/plan-week.tsx | 32 ++++++--
packages/shared/src/planner/matchmaking.ts | 85 +++++++++++++++++++-
packages/shared/src/planner/protocol.test.ts | 73 +++++++++++++++++
7 files changed, 258 insertions(+), 25 deletions(-)
create mode 100644 packages/shared/src/planner/protocol.test.ts
diff --git a/apps/api/src/modules/plan/plan.routes.ts b/apps/api/src/modules/plan/plan.routes.ts
index 9eec1ef..df9480e 100644
--- a/apps/api/src/modules/plan/plan.routes.ts
+++ b/apps/api/src/modules/plan/plan.routes.ts
@@ -1,9 +1,10 @@
import {
- DEFAULT_MEAL_SHARE,
calculateBmr,
calculateTdee,
macrosForPhase,
matchMeals,
+ protocolPlanForDay,
+ type FastingProtocolKey,
type RecipeCandidate,
} from '@ketopath/shared';
import type { FastifyPluginAsync } from 'fastify';
@@ -84,9 +85,15 @@ export const planRoutes: FastifyPluginAsync = async (fastify) => {
const tdee = calculateTdee(bmr, profile.activityLevel);
const phaseInt = phaseToInt(profile.currentPhase);
// Deficit base solo in fase 1; in fase 2/3 si avvicina a TDEE.
- const kcalTarget = Math.round(phaseInt === 1 ? tdee - 500 : phaseInt === 2 ? tdee - 200 : tdee);
- const dailyTarget = macrosForPhase({ kcalTarget, weightKg: weightCurrentKg, phase: phaseInt });
+ const baseKcal = Math.round(phaseInt === 1 ? tdee - 500 : phaseInt === 2 ? tdee - 200 : tdee);
+ const baseDailyTarget = macrosForPhase({
+ kcalTarget: baseKcal,
+ weightKg: weightCurrentKg,
+ phase: phaseInt,
+ });
const exclusions = profile.user.preferences?.exclusions ?? [];
+ const fastingProtocol =
+ (profile.user.preferences?.fastingProtocol as FastingProtocolKey | null | undefined) ?? null;
const weekStart = startOfWeek(new Date());
@@ -101,12 +108,26 @@ export const planRoutes: FastifyPluginAsync = async (fastify) => {
const plannedSlots: Array<{ day: number; meal: string; recipeId: string | null }> = [];
for (let day = 0; day < 7; day++) {
- // Per ogni pasto, le ricette già scelte negli ultimi 2 giorni sono "recenti".
+ const dayPlan = protocolPlanForDay(fastingProtocol, day);
+ // Giorno di digiuno completo (ESE_24): salta tutto.
+ if (dayPlan.kcalMultiplier === 0) continue;
+
+ const dailyKcal = Math.round(baseKcal * dayPlan.kcalMultiplier);
+ const dailyTarget = macrosForPhase({
+ kcalTarget: dailyKcal,
+ weightKg: weightCurrentKg,
+ phase: phaseInt,
+ });
+
+ // Ricette scelte negli ultimi 2 giorni sono "recenti".
const recentlyConsumedIds: string[] = plannedSlots
.filter((s) => day - s.day <= 2 && s.recipeId)
.map((s) => s.recipeId!);
for (const meal of MEALS) {
+ // Pasto disabilitato dal protocollo: non creiamo proprio lo slot.
+ if (dayPlan.share[meal] === 0) continue;
+
const top = matchMeals({
candidates,
meal,
@@ -114,7 +135,7 @@ export const planRoutes: FastifyPluginAsync = async (fastify) => {
excludedTags: exclusions,
recentlyConsumedIds,
dailyTarget,
- mealShare: DEFAULT_MEAL_SHARE,
+ mealShare: dayPlan.share,
topN: 5,
});
const selected = top[0];
@@ -135,7 +156,8 @@ export const planRoutes: FastifyPluginAsync = async (fastify) => {
plan: {
id: plan.id,
weekStart: plan.weekStart.toISOString().slice(0, 10),
- dailyTarget,
+ dailyTarget: baseDailyTarget,
+ fastingProtocol,
},
});
});
@@ -174,25 +196,33 @@ export const planRoutes: FastifyPluginAsync = async (fastify) => {
);
fastify.get('/me/meal-plans/current', { preHandler: requireAuth() }, async (request, reply) => {
- const plan = await fastify.prisma.mealPlan.findFirst({
- where: { userId: request.user!.id, status: 'ACTIVE' },
- orderBy: { weekStart: 'desc' },
- include: {
- slots: {
- include: {
- selected: { select: { id: true, name: true, kcal: true } },
- alternatives: { select: { id: true, name: true, kcal: true } },
+ const userId = request.user!.id;
+ const [plan, prefs] = await Promise.all([
+ fastify.prisma.mealPlan.findFirst({
+ where: { userId, status: 'ACTIVE' },
+ orderBy: { weekStart: 'desc' },
+ include: {
+ slots: {
+ include: {
+ selected: { select: { id: true, name: true, kcal: true } },
+ alternatives: { select: { id: true, name: true, kcal: true } },
+ },
+ orderBy: [{ dayOfWeek: 'asc' }, { meal: 'asc' }],
},
- orderBy: [{ dayOfWeek: 'asc' }, { meal: 'asc' }],
},
- },
- });
+ }),
+ fastify.prisma.preferences.findUnique({
+ where: { userId },
+ select: { fastingProtocol: true },
+ }),
+ ]);
if (!plan) return reply.code(404).send({ error: 'plan_not_found' });
return {
plan: {
id: plan.id,
weekStart: plan.weekStart.toISOString().slice(0, 10),
slots: plan.slots,
+ fastingProtocol: prefs?.fastingProtocol ?? null,
},
};
});
diff --git a/apps/web/messages/it.json b/apps/web/messages/it.json
index 572cc79..108b1cd 100644
--- a/apps/web/messages/it.json
+++ b/apps/web/messages/it.json
@@ -391,6 +391,19 @@
"hideAlternatives": "Nascondi",
"swap": "Scegli",
"dayTotal": "{kcal} kcal totali",
+ "window": "Finestra alimentare",
+ "fastingDay": "Giorno di digiuno",
+ "fastingDayHint": "Acqua, tè e tisane. Niente cibo solido.",
+ "lightDay": "Giorno leggero",
+ "lightDayHint": "Solo cena, kcal ridotte (~25% del giornaliero abituale).",
+ "protocolLabel": {
+ "FOURTEEN_TEN": "14:10 — finestra di 10h",
+ "SIXTEEN_EIGHT": "16:8 — finestra di 8h",
+ "EIGHTEEN_SIX": "18:6 — finestra di 6h",
+ "TWENTY_FOUR": "20:4 — finestra di 4h",
+ "ESE_24": "ESE 24h — un giorno di digiuno completo a settimana",
+ "FIVE_TWO": "5:2 — due giorni leggeri a settimana"
+ },
"meals": {
"COLAZIONE": "Colazione",
"PRANZO": "Pranzo",
diff --git a/apps/web/src/app/[locale]/plan/actions.ts b/apps/web/src/app/[locale]/plan/actions.ts
index 9d41821..7233490 100644
--- a/apps/web/src/app/[locale]/plan/actions.ts
+++ b/apps/web/src/app/[locale]/plan/actions.ts
@@ -25,6 +25,14 @@ export interface CurrentPlan {
id: string;
weekStart: string;
slots: PlanSlot[];
+ fastingProtocol:
+ | 'FOURTEEN_TEN'
+ | 'SIXTEEN_EIGHT'
+ | 'EIGHTEEN_SIX'
+ | 'TWENTY_FOUR'
+ | 'ESE_24'
+ | 'FIVE_TWO'
+ | null;
}
function cookieHeader(): string {
diff --git a/apps/web/src/app/[locale]/plan/page.tsx b/apps/web/src/app/[locale]/plan/page.tsx
index bc1bfd7..14ce471 100644
--- a/apps/web/src/app/[locale]/plan/page.tsx
+++ b/apps/web/src/app/[locale]/plan/page.tsx
@@ -62,6 +62,14 @@ function PlanPageContent({ plan }: { plan: Awaited
{plan ? formatWeek(plan.weekStart) : t('noPlan')}
+ {plan?.fastingProtocol ? (
+
+ {t('window')}{' '}
+
+ {t(`protocolLabel.${plan.fastingProtocol}`)}
+
+
+ ) : null}