diff --git a/src/lib/server/shopping/repository.ts b/src/lib/server/shopping/repository.ts index 833e174..39432bd 100644 --- a/src/lib/server/shopping/repository.ts +++ b/src/lib/server/shopping/repository.ts @@ -1,5 +1,9 @@ import type Database from 'better-sqlite3'; +// Fallback when a recipe has no servings_default set — matches the default +// used by RecipeEditor's "new recipe" template. +const DEFAULT_SERVINGS = 4; + export type ShoppingCartRecipe = { recipe_id: number; title: string; @@ -33,7 +37,9 @@ export function addRecipeToCart( const row = db .prepare('SELECT servings_default FROM recipe WHERE id = ?') .get(recipeId) as { servings_default: number | null } | undefined; - const resolved = servings ?? row?.servings_default ?? 4; + const resolved = servings ?? row?.servings_default ?? DEFAULT_SERVINGS; + // ON CONFLICT updates only servings — added_by_profile_id stays with the + // first profile that added the recipe (household cart, audit trail). db.prepare( `INSERT INTO shopping_cart_recipe (recipe_id, servings, added_by_profile_id) VALUES (?, ?, ?) @@ -63,6 +69,8 @@ export function listShoppingList(db: Database.Database): ShoppingListSnapshot { ORDER BY cr.added_at ASC` ) .all() as ShoppingCartRecipe[]; + // TODO(Task 6): rows + uncheckedCount are populated by the aggregation query. + // Until then, callers must not rely on these fields. return { recipes, rows: [], uncheckedCount: 0 }; }