@@ -803,6 +896,40 @@ function buildModalContent({ mode, date, mealType, meal }) {
@@ -888,6 +1015,10 @@ async function saveModal(overlay) {
const notes = overlay.querySelector('#modal-notes').value.trim() || null;
const recipe_url = overlay.querySelector('#modal-recipe-url').value.trim() || null;
const recipe_id = overlay.querySelector('#modal-recipe-id')?.value || null;
+ const meal_category = overlay.querySelector('#modal-meal-category')?.value || 'other';
+ const protein = overlay.querySelector('#modal-protein')?.value || 'mixed';
+ const style = overlay.querySelector('#modal-style')?.value || 'family';
+ const leftover_from_meal_id = overlay.querySelector('#modal-leftover-from-meal-id')?.value || null;
const cookSelect = overlay.querySelector('#modal-cook-user-id');
const cook_user_id = cookSelect?.value ? Number(cookSelect.value) : null;
@@ -909,7 +1040,7 @@ async function saveModal(overlay) {
try {
const { mode, meal } = state.modal;
- const mealPayload = { date, meal_type, title, notes, recipe_url, recipe_id, cook_user_id };
+ const mealPayload = { date, meal_type, title, notes, recipe_url, recipe_id, meal_category, protein, style, leftover_from_meal_id, cook_user_id };
if (mode === 'create') {
const res = await api.post('/meals', { ...mealPayload, ingredients });
diff --git a/public/pages/recipes.js b/public/pages/recipes.js
index 9f6bcb0..8848311 100644
--- a/public/pages/recipes.js
+++ b/public/pages/recipes.js
@@ -8,14 +8,28 @@ import { t } from '/i18n.js';
import { openModal as openSharedModal, closeModal as closeSharedModal } from '/components/modal.js';
import { DEFAULT_CATEGORY_NAME, categoryLabel } from '/utils/shopping-categories.js';
import { renderKitchenTabsBar } from '/utils/kitchen-tabs.js';
+import { esc } from '/utils/html.js';
let _container = null;
const state = {
recipes: [],
categories: [],
+ familyMembers: [],
+ recipeSignals: [],
};
+const MEAL_CATEGORY_OPTIONS = [
+ ['meat', 'Kød'], ['fish', 'Fisk'], ['pasta', 'Pasta'], ['rice', 'Ris'],
+ ['vegetarian', 'Grønt'], ['soup', 'Suppe'], ['leftovers', 'Rester'], ['cozy', 'Hygge'], ['other', 'Andet'],
+];
+const PROTEIN_OPTIONS = [['mixed', 'Blandet'], ['chicken', 'Kylling'], ['beef', 'Okse'], ['pork', 'Svin'], ['fish', 'Fisk'], ['vegetarian', 'Vegetar'], ['none', 'Ingen'], ['other', 'Andet']];
+const STYLE_OPTIONS = [['family', 'Familie'], ['quick', 'Hurtig'], ['cozy', 'Hygge'], ['grill', 'Grill'], ['vegetarian', 'Vegetar'], ['kids', 'Børnevenlig'], ['leftovers', 'Rester'], ['other', 'Andet']];
+
+function optionHtml(options, selected) {
+ return options.map(([value, label]) => ``).join('');
+}
+
function mealCategories() {
return state.categories.filter((c) => c.name !== 'Haushalt' && c.name !== 'Drogerie');
}
@@ -34,6 +48,45 @@ async function loadCategories() {
}
}
+async function loadFamilyMembers() {
+ try {
+ const res = await api.get('/family/members');
+ state.familyMembers = res.data;
+ } catch { state.familyMembers = []; }
+}
+
+async function loadRecipeSignals() {
+ try {
+ const res = await api.get('/meal-planning/recipe-signals');
+ state.recipeSignals = res.data;
+ } catch { state.recipeSignals = []; }
+}
+
+function signalsForRecipe(recipeId) {
+ return state.recipeSignals.filter((signal) => Number(signal.recipe_id) === Number(recipeId));
+}
+
+function signalFor(recipeId, userId) {
+ return state.recipeSignals.find((signal) => Number(signal.recipe_id) === Number(recipeId) && Number(signal.user_id) === Number(userId));
+}
+
+async function saveRecipeSignal(recipeId, userId, patch) {
+ const current = signalFor(recipeId, userId) || {};
+ const payload = {
+ user_id: userId,
+ preference: current.preference || 'neutral',
+ can_cook: !!current.can_cook,
+ can_help_cook: !!current.can_help_cook,
+ will_eat_modified: !!current.will_eat_modified,
+ adult_only: !!current.adult_only,
+ ...patch,
+ };
+ const res = await api.put(`/meal-planning/recipe-signals/${recipeId}`, payload);
+ state.recipeSignals = state.recipeSignals.filter((signal) => !(Number(signal.recipe_id) === Number(recipeId) && Number(signal.user_id) === Number(userId)));
+ state.recipeSignals.push(res.data);
+ return res.data;
+}
+
export async function render(container) {
_container = container;
@@ -75,7 +128,7 @@ export async function render(container) {
if (window.lucide) window.lucide.createIcons();
- await Promise.all([loadRecipes(), loadCategories()]);
+ await Promise.all([loadRecipes(), loadCategories(), loadFamilyMembers(), loadRecipeSignals()]);
renderRecipeList();
addBtn.addEventListener('click', () => openRecipeModal('create'));
@@ -107,6 +160,13 @@ export async function render(container) {
if (actionBtn.dataset.action === 'add-to-meals') {
window.oikos?.navigate(`/meals?recipe=${recipe.id}`);
}
+
+ if (actionBtn.dataset.action === 'quick-favorite') {
+ const memberId = Number(actionBtn.dataset.memberId);
+ await saveRecipeSignal(recipe.id, memberId, { preference: 'favorite' });
+ renderRecipeList();
+ window.oikos?.showToast('Favorit gemt på profilen', 'success');
+ }
});
}
@@ -155,6 +215,27 @@ function renderRecipeList() {
card.appendChild(h);
+ const taxonomy = [
+ recipe.meal_category ? MEAL_CATEGORY_OPTIONS.find(([v]) => v === recipe.meal_category)?.[1] || recipe.meal_category : '',
+ recipe.protein ? PROTEIN_OPTIONS.find(([v]) => v === recipe.protein)?.[1] || recipe.protein : '',
+ recipe.style ? STYLE_OPTIONS.find(([v]) => v === recipe.style)?.[1] || recipe.style : '',
+ ].filter(Boolean);
+ if (taxonomy.length) {
+ const meta = document.createElement('p');
+ meta.className = 'recipe-card__notes';
+ meta.textContent = `Kategori: ${taxonomy.join(' · ')}`;
+ card.appendChild(meta);
+ }
+
+ const recipeSignals = signalsForRecipe(recipe.id);
+ const favoriteSignals = recipeSignals.filter((signal) => signal.preference === 'favorite');
+ if (favoriteSignals.length) {
+ const fav = document.createElement('p');
+ fav.className = 'recipe-card__notes';
+ fav.textContent = `⭐ Favorit hos ${favoriteSignals.map((signal) => signal.user_name).filter(Boolean).join(', ')}`;
+ card.appendChild(fav);
+ }
+
if (recipe.notes) {
const notes = document.createElement('p');
notes.className = 'recipe-card__notes';
@@ -220,6 +301,26 @@ function renderRecipeList() {
actions.append(addToMeals, edit, duplicate, del);
card.appendChild(actions);
+ if (state.familyMembers.length) {
+ const pref = document.createElement('div');
+ pref.className = 'recipe-card__actions';
+ const label = document.createElement('span');
+ label.className = 'recipe-card__notes';
+ label.textContent = 'Gem som favorit for:';
+ pref.appendChild(label);
+ for (const member of state.familyMembers.slice(0, 6)) {
+ const btn = document.createElement('button');
+ btn.className = 'btn btn--ghost';
+ btn.type = 'button';
+ btn.dataset.action = 'quick-favorite';
+ btn.dataset.id = String(recipe.id);
+ btn.dataset.memberId = String(member.id);
+ btn.textContent = `⭐ ${member.display_name}`;
+ pref.appendChild(btn);
+ }
+ card.appendChild(pref);
+ }
+
list.appendChild(card);
}
}
@@ -298,6 +399,24 @@ function openRecipeModal(mode, recipe = null) {
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -348,6 +467,10 @@ async function saveRecipe(panel, mode, recipe) {
const title = panel.querySelector('#recipe-title')?.value.trim() || '';
const notes = panel.querySelector('#recipe-notes')?.value.trim() || null;
const recipe_url = panel.querySelector('#recipe-url')?.value.trim() || null;
+ const meal_category = panel.querySelector('#recipe-meal-category')?.value || 'other';
+ const protein = panel.querySelector('#recipe-protein')?.value || 'mixed';
+ const style = panel.querySelector('#recipe-style')?.value || 'family';
+ const tags = (panel.querySelector('#recipe-tags')?.value || '').split(',').map((tag) => tag.trim()).filter(Boolean);
if (!title) {
window.oikos?.showToast(t('recipes.titleRequired'), 'error');
@@ -366,10 +489,10 @@ async function saveRecipe(panel, mode, recipe) {
try {
if (mode === 'create') {
- const res = await api.post('/recipes', { title, notes, recipe_url, ingredients });
+ const res = await api.post('/recipes', { title, notes, recipe_url, meal_category, protein, style, tags, ingredients });
state.recipes.push(res.data);
} else {
- const res = await api.put(`/recipes/${recipe.id}`, { title, notes, recipe_url, ingredients });
+ const res = await api.put(`/recipes/${recipe.id}`, { title, notes, recipe_url, meal_category, protein, style, tags, ingredients });
const idx = state.recipes.findIndex((r) => r.id === recipe.id);
if (idx >= 0) state.recipes[idx] = res.data;
}
diff --git a/public/pages/settings.js b/public/pages/settings.js
index 1499f0b..d673d17 100644
--- a/public/pages/settings.js
+++ b/public/pages/settings.js
@@ -2334,6 +2334,8 @@ function memberHtml(u) {
u.phone ? t('settings.memberPhoneMeta', { value: u.phone }) : '',
u.email || '',
u.birth_date ? t('settings.memberBirthdayMeta', { date: formatDate(u.birth_date) }) : '',
+ Number(u.favorite_meal_count || 0) ? `⭐ ${Number(u.favorite_meal_count)} favorite meals` : '',
+ Number(u.can_cook_meal_count || 0) ? `👩🍳 ${Number(u.can_cook_meal_count)} can cook` : '',
].filter(Boolean).map(esc).join(' · ');
return `
diff --git a/server/auth.js b/server/auth.js
index 0c4ea36..6071286 100644
--- a/server/auth.js
+++ b/server/auth.js
@@ -31,7 +31,9 @@ const USER_PUBLIC_COLUMNS = `
created_at,
(SELECT phone FROM contacts WHERE contacts.family_user_id = users.id LIMIT 1) AS phone,
(SELECT email FROM contacts WHERE contacts.family_user_id = users.id LIMIT 1) AS email,
- (SELECT birth_date FROM birthdays WHERE birthdays.family_user_id = users.id LIMIT 1) AS birth_date
+ (SELECT birth_date FROM birthdays WHERE birthdays.family_user_id = users.id LIMIT 1) AS birth_date,
+ (SELECT COUNT(*) FROM recipe_family_preferences WHERE recipe_family_preferences.user_id = users.id AND preference = 'favorite') AS favorite_meal_count,
+ (SELECT COUNT(*) FROM recipe_family_preferences WHERE recipe_family_preferences.user_id = users.id AND can_cook = 1) AS can_cook_meal_count
`;
// --------------------------------------------------------
diff --git a/server/db.js b/server/db.js
index 0444d51..8783e92 100644
--- a/server/db.js
+++ b/server/db.js
@@ -1482,6 +1482,34 @@ const MIGRATIONS = [
`);
},
},
+ {
+ version: 41,
+ description: 'Structured meal taxonomy and leftover links',
+ up(database) {
+ const columns = (table) => new Set(database.prepare(`PRAGMA table_info(${table})`).all().map((row) => row.name));
+ const addColumn = (table, name, definition) => {
+ if (!columns(table).has(name)) database.exec(`ALTER TABLE ${table} ADD COLUMN ${name} ${definition}`);
+ };
+
+ addColumn('recipes', 'meal_category', "TEXT");
+ addColumn('recipes', 'protein', "TEXT");
+ addColumn('recipes', 'style', "TEXT");
+ addColumn('recipes', 'tags_json', "TEXT");
+
+ addColumn('meals', 'meal_category', "TEXT");
+ addColumn('meals', 'protein', "TEXT");
+ addColumn('meals', 'style', "TEXT");
+ addColumn('meals', 'leftover_from_meal_id', "INTEGER REFERENCES meals(id) ON DELETE SET NULL");
+ addColumn('meals', 'source_plan_id', "TEXT");
+
+ database.exec(`
+ CREATE INDEX IF NOT EXISTS idx_recipes_meal_taxonomy ON recipes(meal_category, protein, style);
+ CREATE INDEX IF NOT EXISTS idx_meals_meal_taxonomy ON meals(meal_category, protein, style);
+ CREATE INDEX IF NOT EXISTS idx_meals_leftover_from ON meals(leftover_from_meal_id);
+ CREATE INDEX IF NOT EXISTS idx_meals_source_plan ON meals(source_plan_id);
+ `);
+ },
+ },
];
/**
diff --git a/server/routes/family.js b/server/routes/family.js
index b783afd..9c656cf 100644
--- a/server/routes/family.js
+++ b/server/routes/family.js
@@ -22,6 +22,8 @@ router.get('/members', (req, res) => {
c.phone,
c.email,
b.birth_date,
+ (SELECT COUNT(*) FROM recipe_family_preferences p WHERE p.user_id = u.id AND p.preference = 'favorite') AS favorite_meal_count,
+ (SELECT COUNT(*) FROM recipe_family_preferences p WHERE p.user_id = u.id AND p.can_cook = 1) AS can_cook_meal_count,
u.created_at
FROM users u
LEFT JOIN contacts c ON c.family_user_id = u.id
diff --git a/server/routes/meals.js b/server/routes/meals.js
index bee015e..cb5e64f 100644
--- a/server/routes/meals.js
+++ b/server/routes/meals.js
@@ -15,6 +15,9 @@ const log = createLogger('Meals');
const router = express.Router();
const VALID_MEAL_TYPES = ['breakfast', 'lunch', 'dinner', 'snack'];
+const VALID_MEAL_CATEGORIES = ['meat', 'fish', 'pasta', 'rice', 'vegetarian', 'soup', 'leftovers', 'cozy', 'breakfast', 'snack', 'other'];
+const VALID_PROTEINS = ['mixed', 'chicken', 'beef', 'pork', 'fish', 'vegetarian', 'none', 'other'];
+const VALID_STYLES = ['family', 'quick', 'cozy', 'grill', 'vegetarian', 'kids', 'leftovers', 'other'];
// --------------------------------------------------------
// Hilfsfunktionen
@@ -75,6 +78,21 @@ function currentUserId(req) {
return req.authUserId ?? req.session?.userId ?? null;
}
+function normalizeEnum(value, allowed, fallback = null) {
+ const normalized = String(value || '').trim().toLowerCase();
+ return allowed.includes(normalized) ? normalized : fallback;
+}
+
+function validateLeftoverMealId(raw) {
+ if (raw === undefined) return { present: false, value: null, error: null };
+ if (raw === null || raw === '') return { present: true, value: null, error: null };
+ const id = Number(raw);
+ if (!Number.isInteger(id) || id <= 0) return { present: true, value: null, error: 'Leftover source meal ID is invalid.' };
+ const exists = db.get().prepare('SELECT id FROM meals WHERE id = ?').get(id);
+ if (!exists) return { present: true, value: null, error: 'Leftover source meal not found.' };
+ return { present: true, value: id, error: null };
+}
+
function tableColumns(table) {
return new Set(db.get().prepare(`PRAGMA table_info(${table})`).all().map((row) => row.name));
}
@@ -224,7 +242,7 @@ router.get('/', (req, res) => {
/**
* POST /api/v1/meals
* Neue Mahlzeit anlegen.
- * Body: { date, meal_type, title, notes?, cook_user_id?, source_plan_id?, ingredients?: [{ name, quantity? }] }
+ * Body: { date, meal_type, title, notes?, meal_category?, protein?, style?, leftover_from_meal_id?, cook_user_id?, source_plan_id?, ingredients?: [{ name, quantity? }] }
* Response: { data: Meal }
*/
router.post('/', (req, res) => {
@@ -238,12 +256,19 @@ router.post('/', (req, res) => {
const vRecipeId = num(req.body.recipe_id, 'Rezept-ID', { required: false });
const cookUserRaw = Object.hasOwn(req.body, 'cook_user_id') ? req.body.cook_user_id : req.body.cookUserId;
const vCookUserId = validateCookUserId(cookUserRaw);
+ const leftoverRaw = Object.hasOwn(req.body, 'leftover_from_meal_id') ? req.body.leftover_from_meal_id : req.body.leftoverFromMealId;
+ const vLeftoverFromMealId = validateLeftoverMealId(leftoverRaw);
const vSourcePlanId = str(req.body.source_plan_id ?? req.body.sourcePlanId, 'Plan-ID', { max: MAX_SHORT, required: false });
const errors = collectErrors([vDate, vType, vTitle, vNotes, vRecipeUrl, vRecipeId, vSourcePlanId]);
if (vCookUserId.error) errors.push(vCookUserId.error);
+ if (vLeftoverFromMealId.error) errors.push(vLeftoverFromMealId.error);
if (!req.body.meal_type) errors.push('Mahlzeit-Typ ist erforderlich.');
if (errors.length) return res.status(400).json({ error: errors.join(' '), code: 400 });
+ const mealCategory = normalizeEnum(req.body.meal_category ?? req.body.mealCategory, VALID_MEAL_CATEGORIES, vLeftoverFromMealId.value ? 'leftovers' : 'other');
+ const protein = normalizeEnum(req.body.protein, VALID_PROTEINS, vLeftoverFromMealId.value ? 'none' : 'mixed');
+ const style = normalizeEnum(req.body.style, VALID_STYLES, vLeftoverFromMealId.value ? 'leftovers' : 'family');
+
if (vRecipeId.value !== null) {
const recipeExists = db.get().prepare('SELECT id FROM recipes WHERE id = ?').get(vRecipeId.value);
if (!recipeExists) return res.status(400).json({ error: 'Rezept nicht gefunden.', code: 400 });
@@ -251,9 +276,9 @@ router.post('/', (req, res) => {
const meal = db.transaction(() => {
const result = db.get().prepare(`
- INSERT INTO meals (date, meal_type, title, notes, recipe_url, recipe_id, created_by)
- VALUES (?, ?, ?, ?, ?, ?, ?)
- `).run(vDate.value, vType.value, vTitle.value, vNotes.value, vRecipeUrl.value, vRecipeId.value, currentUserId(req));
+ INSERT INTO meals (date, meal_type, title, notes, recipe_url, recipe_id, meal_category, protein, style, leftover_from_meal_id, source_plan_id, created_by)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ `).run(vDate.value, vType.value, vTitle.value, vNotes.value, vRecipeUrl.value, vRecipeId.value, mealCategory, protein, style, vLeftoverFromMealId.value, vSourcePlanId.value, currentUserId(req));
const mealId = result.lastInsertRowid;
@@ -316,9 +341,12 @@ router.put('/:id', (req, res) => {
if (req.body.recipe_id !== undefined) checks.push(num(req.body.recipe_id, 'Rezept-ID', { required: false }));
const cookUserRaw = Object.hasOwn(req.body, 'cook_user_id') ? req.body.cook_user_id : req.body.cookUserId;
const vCookUserId = validateCookUserId(cookUserRaw);
+ const leftoverRaw = Object.hasOwn(req.body, 'leftover_from_meal_id') ? req.body.leftover_from_meal_id : req.body.leftoverFromMealId;
+ const vLeftoverFromMealId = validateLeftoverMealId(leftoverRaw);
const vSourcePlanId = str(req.body.source_plan_id ?? req.body.sourcePlanId, 'Plan-ID', { max: MAX_SHORT, required: false });
const errors = collectErrors([...checks, vSourcePlanId]);
if (vCookUserId.error) errors.push(vCookUserId.error);
+ if (vLeftoverFromMealId.error) errors.push(vLeftoverFromMealId.error);
if (errors.length) return res.status(400).json({ error: errors.join(' '), code: 400 });
if (req.body.recipe_id !== undefined && req.body.recipe_id !== null && req.body.recipe_id !== '') {
@@ -326,6 +354,12 @@ router.put('/:id', (req, res) => {
if (!recipeExists) return res.status(400).json({ error: 'Rezept nicht gefunden.', code: 400 });
}
+ const mealCategory = req.body.meal_category !== undefined || req.body.mealCategory !== undefined
+ ? normalizeEnum(req.body.meal_category ?? req.body.mealCategory, VALID_MEAL_CATEGORIES, meal.meal_category || 'other')
+ : meal.meal_category;
+ const protein = req.body.protein !== undefined ? normalizeEnum(req.body.protein, VALID_PROTEINS, meal.protein || 'mixed') : meal.protein;
+ const style = req.body.style !== undefined ? normalizeEnum(req.body.style, VALID_STYLES, meal.style || 'family') : meal.style;
+
db.get().prepare(`
UPDATE meals
SET date = COALESCE(?, date),
@@ -333,7 +367,12 @@ router.put('/:id', (req, res) => {
title = COALESCE(?, title),
notes = ?,
recipe_url = ?,
- recipe_id = ?
+ recipe_id = ?,
+ meal_category = ?,
+ protein = ?,
+ style = ?,
+ leftover_from_meal_id = ?,
+ source_plan_id = ?
WHERE id = ?
`).run(
req.body.date ?? null,
@@ -342,6 +381,11 @@ router.put('/:id', (req, res) => {
req.body.notes !== undefined ? (req.body.notes || null) : meal.notes,
req.body.recipe_url !== undefined ? (req.body.recipe_url || null) : meal.recipe_url,
req.body.recipe_id !== undefined ? (req.body.recipe_id || null) : meal.recipe_id,
+ mealCategory,
+ protein,
+ style,
+ vLeftoverFromMealId.present ? vLeftoverFromMealId.value : meal.leftover_from_meal_id,
+ req.body.source_plan_id !== undefined || req.body.sourcePlanId !== undefined ? vSourcePlanId.value : meal.source_plan_id,
id
);
diff --git a/server/routes/recipes.js b/server/routes/recipes.js
index 17448d4..34cef8a 100644
--- a/server/routes/recipes.js
+++ b/server/routes/recipes.js
@@ -12,6 +12,22 @@ import { str, num, collectErrors, MAX_TITLE, MAX_TEXT, MAX_SHORT } from '../midd
const log = createLogger('Recipes');
const router = express.Router();
+const VALID_MEAL_CATEGORIES = ['meat', 'fish', 'pasta', 'rice', 'vegetarian', 'soup', 'leftovers', 'cozy', 'breakfast', 'snack', 'other'];
+const VALID_PROTEINS = ['mixed', 'chicken', 'beef', 'pork', 'fish', 'vegetarian', 'none', 'other'];
+const VALID_STYLES = ['family', 'quick', 'cozy', 'grill', 'vegetarian', 'kids', 'leftovers', 'other'];
+
+function normalizeEnum(value, allowed, fallback = null) {
+ const normalized = String(value || '').trim().toLowerCase();
+ return allowed.includes(normalized) ? normalized : fallback;
+}
+
+function normalizeTags(value) {
+ const tags = Array.isArray(value)
+ ? value
+ : String(value || '').split(',');
+ return tags.map((tag) => String(tag || '').trim().toLowerCase()).filter(Boolean).slice(0, 12);
+}
+
function loadRecipeWithIngredients(id) {
const recipe = db.get().prepare(`
SELECT r.*, u.display_name AS creator_name, u.avatar_color AS creator_color
@@ -75,11 +91,16 @@ router.post('/', (req, res) => {
const errors = collectErrors([vTitle, vNotes, vRecipeUrl]);
if (errors.length) return res.status(400).json({ error: errors.join(' '), code: 400 });
+ const mealCategory = normalizeEnum(req.body.meal_category ?? req.body.mealCategory, VALID_MEAL_CATEGORIES, 'other');
+ const protein = normalizeEnum(req.body.protein, VALID_PROTEINS, 'mixed');
+ const style = normalizeEnum(req.body.style, VALID_STYLES, 'family');
+ const tagsJson = JSON.stringify(normalizeTags(req.body.tags ?? req.body.tags_json));
+
const recipeId = db.transaction(() => {
const result = db.get().prepare(`
- INSERT INTO recipes (title, notes, recipe_url, created_by)
- VALUES (?, ?, ?, ?)
- `).run(vTitle.value, vNotes.value, vRecipeUrl.value, req.session.userId);
+ INSERT INTO recipes (title, notes, recipe_url, meal_category, protein, style, tags_json, created_by)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+ `).run(vTitle.value, vNotes.value, vRecipeUrl.value, mealCategory, protein, style, tagsJson, req.session.userId);
const rid = Number(result.lastInsertRowid);
const insertIng = db.get().prepare(`
@@ -122,12 +143,17 @@ router.put('/:id', (req, res) => {
const errors = collectErrors([vTitle, vNotes, vRecipeUrl]);
if (errors.length) return res.status(400).json({ error: errors.join(' '), code: 400 });
+ const mealCategory = normalizeEnum(req.body.meal_category ?? req.body.mealCategory, VALID_MEAL_CATEGORIES, existing.meal_category || 'other');
+ const protein = normalizeEnum(req.body.protein, VALID_PROTEINS, existing.protein || 'mixed');
+ const style = normalizeEnum(req.body.style, VALID_STYLES, existing.style || 'family');
+ const tagsJson = JSON.stringify(normalizeTags(req.body.tags ?? req.body.tags_json));
+
db.transaction(() => {
db.get().prepare(`
UPDATE recipes
- SET title = ?, notes = ?, recipe_url = ?
+ SET title = ?, notes = ?, recipe_url = ?, meal_category = ?, protein = ?, style = ?, tags_json = ?
WHERE id = ?
- `).run(vTitle.value, vNotes.value, vRecipeUrl.value, id);
+ `).run(vTitle.value, vNotes.value, vRecipeUrl.value, mealCategory, protein, style, tagsJson, id);
db.get().prepare('DELETE FROM recipe_ingredients WHERE recipe_id = ?').run(id);
diff --git a/tests/e2e/oikos-kitchen-assist-flow.spec.mjs b/tests/e2e/oikos-kitchen-assist-flow.spec.mjs
index 62cc7aa..1fa71b5 100644
--- a/tests/e2e/oikos-kitchen-assist-flow.spec.mjs
+++ b/tests/e2e/oikos-kitchen-assist-flow.spec.mjs
@@ -61,6 +61,24 @@ async function openMealPlanStudioFromAssist(page, diagnostics, prompt = 'Lav en
return studioHost;
}
+async function readStudioPlan(page) {
+ return page.evaluate(() => JSON.parse(localStorage.getItem('oikos-meal-plan-studio-v1') || 'null'));
+}
+
+function assertStructuredStudioPlan(plan) {
+ expect(plan?.slots, 'Studio plan should exist in localStorage for structural assertions').toHaveLength(7);
+ const categories = plan.slots.map((slot) => slot.meal_category || slot.variation?.category || 'other');
+ expect(categories.every((category) => category && category !== 'other'), 'all Studio slots should carry structured categories').toBeTruthy();
+ for (let i = 1; i < categories.length; i += 1) {
+ expect(categories[i], `planner should not repeat category on consecutive days (${i - 1}/${i})`).not.toBe(categories[i - 1]);
+ }
+ const counts = categories.reduce((acc, category) => ({ ...acc, [category]: (acc[category] || 0) + 1 }), {});
+ expect(Math.max(...Object.values(counts)), `planner should not overuse one category: ${JSON.stringify(counts)}`).toBeLessThanOrEqual(2);
+ const leftovers = plan.slots.filter((slot) => (slot.meal_category || slot.variation?.category) === 'leftovers' || slot.leftover_from_meal_id);
+ expect(leftovers.length, 'leftovers should be represented as a dedicated structured option when meal history exists').toBeGreaterThanOrEqual(1);
+ expect(leftovers.every((slot) => slot.leftover_from_meal_id || slot.context?.leftoverSource?.id), 'leftover slots should link to a source dish id').toBeTruthy();
+}
+
async function deleteMeals(page, mealIds) {
if (!mealIds.length) return [];
return page.evaluate(async (ids) => {
@@ -147,6 +165,8 @@ test.describe('Oikos Kitchen + Assist meal-planning flow', () => {
await openMealPlanStudioFromAssist(page, diagnostics);
+ assertStructuredStudioPlan(await readStudioPlan(page));
+
expect(diagnostics.pageErrors, 'No browser page errors during Kitchen/Assist flow').toEqual([]);
await attachDiagnostics(testInfo, diagnostics);
});
@@ -184,6 +204,8 @@ test.describe('Oikos Kitchen + Assist meal-planning flow', () => {
const editPayload = await editResponse.json();
expect(editPayload.source, 'edit feedback should flow through the native meal-planning API').toBe('oikos-native-api');
+ assertStructuredStudioPlan(await readStudioPlan(page));
+
const actionResponsePromise = page.waitForResponse((res) => res.url().includes('/ai/api/action') && res.request().method() === 'POST');
await studioHost.locator('[data-assist-studio-confirm]').click();
const actionResponse = await actionResponsePromise;
@@ -199,6 +221,48 @@ test.describe('Oikos Kitchen + Assist meal-planning flow', () => {
const mealsInOikos = await listMealsById(page, createdMealIds, createdMeals.map((meal) => meal?.date));
expect(mealsInOikos, 'created meals should be readable from the native Oikos meals API').toHaveLength(7);
expect(mealsInOikos.some((meal) => String(meal.title || '').includes('[E2E cleanup]'))).toBeTruthy();
+ expect(mealsInOikos.every((meal) => meal.meal_category), 'confirmed meals should preserve structured meal categories').toBeTruthy();
+ const leftoverMeal = mealsInOikos.find((meal) => meal.meal_category === 'leftovers');
+ expect(leftoverMeal?.leftover_from_meal_id, 'confirmed leftover meal should link to a source dish').toBeTruthy();
+
+ const favoriteCandidate = mealsInOikos.find((meal) => meal.recipe_id);
+ if (favoriteCandidate) {
+ await page.goto(`/meals?week=${favoriteCandidate.date}`, { waitUntil: 'domcontentloaded' });
+ await dismissOnboardingIfPresent(page);
+ const candidateCard = page.locator('.meal-card').filter({ hasText: favoriteCandidate.title }).first();
+ if (await candidateCard.count()) {
+ await candidateCard.click();
+ } else {
+ await page.locator('.meal-card').first().click();
+ }
+ await expect(page.locator('#modal-recipe-pref-member')).toBeVisible();
+ let modalRecipeId = Number(await page.locator('#modal-recipe-id').inputValue());
+ if (!modalRecipeId) {
+ await page.locator('#modal-recipe-id').selectOption(String(favoriteCandidate.recipe_id));
+ modalRecipeId = Number(await page.locator('#modal-recipe-id').inputValue());
+ }
+ expect(modalRecipeId, 'favorite flow should originate from a meal modal with a saved recipe').toBeTruthy();
+ const memberValue = await page.locator('#modal-recipe-pref-member option').nth(1).getAttribute('value');
+ if (memberValue) {
+ await page.locator('#modal-recipe-pref-member').selectOption(memberValue);
+ const prefResponsePromise = page.waitForResponse((res) => res.url().includes(`/api/v1/meal-planning/recipe-signals/${modalRecipeId}`) && res.request().method() === 'PUT');
+ await page.locator('[data-meal-recipe-pref="favorite"]').click();
+ const prefResponse = await prefResponsePromise;
+ expect(prefResponse.ok(), 'meal modal favorite action should write native recipe signals').toBeTruthy();
+ const members = await page.evaluate(async () => (await (await fetch('/api/v1/family/members', { credentials: 'same-origin' })).json()).data || []);
+ const member = members.find((item) => String(item.id) === String(memberValue));
+ expect(Number(member?.favorite_meal_count || 0), 'family profile card data should reflect favorite meal count').toBeGreaterThanOrEqual(1);
+ diagnostics.favoriteSignalCleanup = await page.evaluate(async ({ recipeId, userId }) => {
+ const csrf = document.cookie.split(';').map((c) => c.trim()).find((c) => c.startsWith('csrf-token='))?.slice('csrf-token='.length) || '';
+ const res = await fetch(`/api/v1/meal-planning/recipe-signals/${recipeId}`, {
+ method: 'PUT', credentials: 'same-origin',
+ headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': decodeURIComponent(csrf) },
+ body: JSON.stringify({ user_id: userId, preference: 'neutral', can_cook: false, can_help_cook: false, will_eat_modified: false, adult_only: false }),
+ });
+ return { ok: res.ok, status: res.status };
+ }, { recipeId: modalRecipeId, userId: memberValue });
+ }
+ }
} finally {
const cleanup = await deleteMeals(page, createdMealIds).catch((err) => [{ error: err.message }]);
diagnostics.cleanup = cleanup;