Files
food-ai/backend/internal/domain/home/handler.go
dbastrikin 5096df2102 fix: fix menu generation errors and show planned meals on home screen
Backend fixes:
- migration 003: add 'menu' value to recipe_source enum (was causing SQLSTATE 22P02)
- migration 004: rename recipe_products→recipe_ingredients, product_id→ingredient_id (was causing SQLSTATE 42P01)
- dish/repository.go: fix INSERT INTO tags using $1/$1 for two columns → $1/$2 (was causing SQLSTATE 42P08)
- home/handler.go: replace non-existent saved_recipes table with correct joins (recipes→dishes→dish_translations, user_saved_recipes) so today's plan and recommendations load correctly
- reqlog: new slog.Handler wrapper that adds request_id and stack trace to ERROR-level logs
- all handlers: slog.Error→slog.ErrorContext so error logs include request context; writeError includes request_id in response body

Client:
- home_screen.dart: extend home screen to future dates, show planned meals as ghost entries
- l10n: add new localisation keys for home screen date navigation and planned meal UI

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-22 00:35:11 +02:00

262 lines
7.0 KiB
Go

package home
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"time"
"github.com/food-ai/backend/internal/infra/middleware"
"github.com/food-ai/backend/internal/infra/locale"
"github.com/jackc/pgx/v5/pgxpool"
)
// Handler handles GET /home/summary.
type Handler struct {
pool *pgxpool.Pool
}
// NewHandler creates a new Handler.
func NewHandler(pool *pgxpool.Pool) *Handler {
return &Handler{pool: pool}
}
// GetSummary handles GET /home/summary.
func (h *Handler) GetSummary(w http.ResponseWriter, r *http.Request) {
userID := middleware.UserIDFromCtx(r.Context())
if userID == "" {
writeError(w, r, http.StatusUnauthorized, "unauthorized")
return
}
ctx := r.Context()
now := time.Now().UTC()
todayStr := now.Format("2006-01-02")
// ISO day of week: Monday=1 … Sunday=7.
wd := int(now.Weekday())
if wd == 0 {
wd = 7
}
// Monday of the current ISO week.
year, week := now.ISOWeek()
weekStart := mondayOfISOWeek(year, week).Format("2006-01-02")
// Daily calorie goal from user profile.
dailyGoal := h.getDailyGoal(ctx, userID)
summary := Summary{
Today: TodaySummary{
Date: todayStr,
DailyGoal: dailyGoal,
LoggedCalories: h.getLoggedCalories(ctx, userID, todayStr),
Plan: h.getTodayPlan(ctx, userID, weekStart, wd),
},
ExpiringSoon: h.getExpiringSoon(ctx, userID),
Recommendations: h.getRecommendations(ctx, userID),
}
writeJSON(w, http.StatusOK, summary)
}
// getDailyGoal returns the user's daily_calories setting (default 2000).
func (h *Handler) getDailyGoal(ctx context.Context, userID string) int {
var goal int
err := h.pool.QueryRow(ctx,
`SELECT COALESCE(daily_calories, 2000) FROM users WHERE id = $1`,
userID,
).Scan(&goal)
if err != nil {
slog.WarnContext(ctx, "home: get daily goal", "user_id", userID, "err", err)
return 2000
}
return goal
}
// getLoggedCalories returns total calories logged in meal_diary for today.
// Supports both recipe-based entries (via recipes JOIN) and catalog product entries (via products JOIN).
func (h *Handler) getLoggedCalories(ctx context.Context, userID, date string) float64 {
var total float64
_ = h.pool.QueryRow(ctx,
`SELECT COALESCE(SUM(
COALESCE(
r.calories_per_serving * md.portions,
p.calories_per_100g * md.portion_g / 100,
0
)
), 0)
FROM meal_diary md
LEFT JOIN recipes r ON r.id = md.recipe_id
LEFT JOIN products p ON p.id = md.product_id
WHERE md.user_id = $1 AND md.date::text = $2`,
userID, date,
).Scan(&total)
return total
}
// getTodayPlan returns the three meal slots planned for today.
// If no menu exists, all three slots are returned with nil recipe fields.
func (h *Handler) getTodayPlan(ctx context.Context, userID, weekStart string, dow int) []MealPlan {
lang := locale.FromContext(ctx)
const q = `
SELECT mi.meal_type,
COALESCE(dt.name, d.name) AS title,
d.image_url,
rec.calories_per_serving
FROM menu_plans mp
JOIN menu_items mi ON mi.menu_plan_id = mp.id
LEFT JOIN recipes rec ON rec.id = mi.recipe_id
LEFT JOIN dishes d ON d.id = rec.dish_id
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $4
WHERE mp.user_id = $1
AND mp.week_start::text = $2
AND mi.day_of_week = $3`
rows, err := h.pool.Query(ctx, q, userID, weekStart, dow, lang)
if err != nil {
slog.WarnContext(ctx, "home: get today plan", "err", err)
return defaultPlan()
}
defer rows.Close()
found := map[string]MealPlan{}
for rows.Next() {
var mealType string
var title, imageURL *string
var calories *float64
if err := rows.Scan(&mealType, &title, &imageURL, &calories); err != nil {
continue
}
found[mealType] = MealPlan{
MealType: mealType,
RecipeTitle: title,
RecipeImageURL: imageURL,
Calories: calories,
}
}
// Always return all three meal types in order.
mealTypes := []string{"breakfast", "lunch", "dinner"}
plan := make([]MealPlan, 0, 3)
for _, mt := range mealTypes {
if mp, ok := found[mt]; ok {
plan = append(plan, mp)
} else {
plan = append(plan, MealPlan{MealType: mt})
}
}
return plan
}
// defaultPlan returns three empty meal slots.
func defaultPlan() []MealPlan {
return []MealPlan{
{MealType: "breakfast"},
{MealType: "lunch"},
{MealType: "dinner"},
}
}
// getExpiringSoon returns products expiring within the next 3 days.
func (h *Handler) getExpiringSoon(ctx context.Context, userID string) []ExpiringSoon {
rows, err := h.pool.Query(ctx, `
WITH p AS (
SELECT name, quantity, unit,
(added_at + storage_days * INTERVAL '1 day') AS expires_at
FROM user_products
WHERE user_id = $1
)
SELECT name, quantity, unit,
GREATEST(0, EXTRACT(EPOCH FROM (expires_at - now())) / 86400)::int
FROM p
WHERE expires_at > now()
AND expires_at <= now() + INTERVAL '3 days'
ORDER BY expires_at
LIMIT 5`,
userID,
)
if err != nil {
slog.WarnContext(ctx, "home: get expiring soon", "err", err)
return nil
}
defer rows.Close()
var result []ExpiringSoon
for rows.Next() {
var name, unit string
var quantity float64
var daysLeft int
if err := rows.Scan(&name, &quantity, &unit, &daysLeft); err != nil {
continue
}
result = append(result, ExpiringSoon{
Name: name,
ExpiresInDays: daysLeft,
Quantity: fmt.Sprintf("%.0f %s", quantity, unit),
})
}
return result
}
// getRecommendations returns the 3 most recently saved recommendation recipes.
func (h *Handler) getRecommendations(ctx context.Context, userID string) []Recommendation {
lang := locale.FromContext(ctx)
rows, err := h.pool.Query(ctx, `
SELECT rec.id,
COALESCE(dt.name, d.name),
COALESCE(d.image_url, ''),
rec.calories_per_serving
FROM user_saved_recipes usr
JOIN recipes rec ON rec.id = usr.recipe_id
JOIN dishes d ON d.id = rec.dish_id
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $2
WHERE usr.user_id = $1 AND rec.source = 'recommendation'
ORDER BY usr.saved_at DESC
LIMIT 3`,
userID, lang,
)
if err != nil {
slog.WarnContext(ctx, "home: get recommendations", "err", err)
return nil
}
defer rows.Close()
var result []Recommendation
for rows.Next() {
var rec Recommendation
var cal *float64
if err := rows.Scan(&rec.ID, &rec.Title, &rec.ImageURL, &cal); err != nil {
continue
}
rec.Calories = cal
result = append(result, rec)
}
return result
}
// mondayOfISOWeek returns the Monday of the given ISO year/week.
func mondayOfISOWeek(year, week int) time.Time {
jan4 := time.Date(year, time.January, 4, 0, 0, 0, 0, time.UTC)
wd := int(jan4.Weekday())
if wd == 0 {
wd = 7
}
monday1 := jan4.AddDate(0, 0, 1-wd)
return monday1.AddDate(0, 0, (week-1)*7)
}
func writeError(w http.ResponseWriter, r *http.Request, status int, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]any{"error": msg, "request_id": middleware.RequestIDFromCtx(r.Context())})
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}