feat: flexible meal planning wizard — plan 1 meal, 1 day, several days, or a week

Backend:
- migration 005: expand menu_items.meal_type CHECK to all 6 types (second_breakfast, afternoon_snack, snack)
- ai/types.go: add Days and MealTypes to MenuRequest for partial generation
- openai/menu.go: parametrize GenerateMenu — use requested meal types and day count; add caloric fractions for all 6 meal types
- menu/repository.go: add UpsertItemsInTx for partial upsert (preserves existing slots); fix meal_type sort order in GetByWeek
- menu/handler.go: add dates+meal_types path to POST /ai/generate-menu; extract fetchImages/saveRecipes helpers; returns {"plans":[...]} for dates mode; backward-compatible with week mode

Client:
- PlanMenuSheet: bottom sheet with 4 planning horizon options
- PlanDatePickerSheet: adaptive sheet with date strip (single day/meal) or custom CalendarRangePicker (multi-day/week); sliding 7-day window for week mode
- menu_service.dart: add generateForDates
- menu_provider.dart: add PlanMenuService (generates + invalidates week providers), lastPlannedDateProvider
- home_screen.dart: add _PlanMenuButton card below quick actions; opens planning wizard
- l10n: 16 new keys for planning UI across all 12 languages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-22 12:10:52 +02:00
parent 5096df2102
commit 9580bff54e
35 changed files with 2025 additions and 136 deletions

View File

@@ -47,7 +47,15 @@ func (r *Repository) GetByWeek(ctx context.Context, userID, weekStart string) (*
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $3
WHERE mp.user_id = $1 AND mp.week_start::text = $2
ORDER BY mi.day_of_week,
CASE mi.meal_type WHEN 'breakfast' THEN 1 WHEN 'lunch' THEN 2 ELSE 3 END`
CASE mi.meal_type
WHEN 'breakfast' THEN 1
WHEN 'second_breakfast' THEN 2
WHEN 'lunch' THEN 3
WHEN 'afternoon_snack' THEN 4
WHEN 'dinner' THEN 5
WHEN 'snack' THEN 6
ELSE 7
END`
rows, err := r.pool.Query(ctx, q, userID, weekStart, lang)
if err != nil {
@@ -163,6 +171,43 @@ func (r *Repository) SaveMenuInTx(ctx context.Context, userID, weekStart string,
return planID, nil
}
// UpsertItemsInTx creates (or retrieves) the menu_plan for the given week and
// upserts only the specified meal slots, leaving all other existing slots untouched.
func (r *Repository) UpsertItemsInTx(ctx context.Context, userID, weekStart string, items []PlanItem) (string, error) {
transaction, beginError := r.pool.BeginTx(ctx, pgx.TxOptions{})
if beginError != nil {
return "", fmt.Errorf("begin tx: %w", beginError)
}
defer transaction.Rollback(ctx) //nolint:errcheck
var planID string
upsertError := transaction.QueryRow(ctx, `
INSERT INTO menu_plans (user_id, week_start)
VALUES ($1, $2::date)
ON CONFLICT (user_id, week_start) DO UPDATE SET created_at = now()
RETURNING id`, userID, weekStart).Scan(&planID)
if upsertError != nil {
return "", fmt.Errorf("upsert menu_plan: %w", upsertError)
}
for _, item := range items {
if _, insertError := transaction.Exec(ctx, `
INSERT INTO menu_items (menu_plan_id, day_of_week, meal_type, recipe_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (menu_plan_id, day_of_week, meal_type)
DO UPDATE SET recipe_id = EXCLUDED.recipe_id`,
planID, item.DayOfWeek, item.MealType, item.RecipeID,
); insertError != nil {
return "", fmt.Errorf("upsert menu item day=%d meal=%s: %w", item.DayOfWeek, item.MealType, insertError)
}
}
if commitError := transaction.Commit(ctx); commitError != nil {
return "", fmt.Errorf("commit tx: %w", commitError)
}
return planID, nil
}
// UpdateItem replaces the recipe in a menu slot.
func (r *Repository) UpdateItem(ctx context.Context, itemID, userID, recipeID string) error {
tag, err := r.pool.Exec(ctx, `