Compare commits
7 Commits
b85f869c09
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91fbf27269 | ||
|
|
b556eb39b3 | ||
|
|
c177c1dc5f | ||
|
|
b2337a5c2a | ||
|
|
f2656bd9e3 | ||
|
|
fd55a44bfb | ||
|
|
14cf1b1d35 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "kochwas",
|
||||
"version": "1.4.1",
|
||||
"version": "1.4.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "kochwas",
|
||||
"version": "1.4.1",
|
||||
"version": "1.4.2",
|
||||
"dependencies": {
|
||||
"@google/generative-ai": "^0.24.1",
|
||||
"@types/archiver": "^7.0.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kochwas",
|
||||
"version": "1.4.1",
|
||||
"version": "1.4.2",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export function formatQuantity(q: number | null): string {
|
||||
if (q === null || q === undefined) return '';
|
||||
const rounded = Math.round(q);
|
||||
if (Math.abs(q - rounded) < 0.01) return String(rounded);
|
||||
// auf max. 2 Nachkommastellen, trailing Nullen raus
|
||||
return q.toFixed(2).replace(/\.?0+$/, '');
|
||||
return q.toLocaleString('de-DE', {
|
||||
maximumFractionDigits: 2,
|
||||
useGrouping: false
|
||||
});
|
||||
}
|
||||
|
||||
14
src/lib/server/db/migrations/015_shopping_check_family.sql
Normal file
14
src/lib/server/db/migrations/015_shopping_check_family.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
-- Konsolidierung: unit_key in shopping_cart_check wird zum Family-Key, damit
|
||||
-- Abhaks stabil bleiben wenn Display-Unit zwischen g und kg wechselt.
|
||||
-- g/kg → 'weight', ml/l → 'volume', Rest bleibt unveraendert.
|
||||
UPDATE shopping_cart_check SET unit_key = 'weight' WHERE LOWER(TRIM(unit_key)) IN ('g', 'kg');
|
||||
UPDATE shopping_cart_check SET unit_key = 'volume' WHERE LOWER(TRIM(unit_key)) IN ('ml', 'l');
|
||||
|
||||
-- Nach Relabeling koennen Duplikate entstehen (zwei Zeilen mit 'weight' pro
|
||||
-- name_key). Juengsten Eintrag behalten.
|
||||
DELETE FROM shopping_cart_check
|
||||
WHERE rowid NOT IN (
|
||||
SELECT MAX(rowid)
|
||||
FROM shopping_cart_check
|
||||
GROUP BY name_key, unit_key
|
||||
);
|
||||
@@ -1,4 +1,5 @@
|
||||
import type Database from 'better-sqlite3';
|
||||
import { consolidate, unitFamily } from '../unit-consolidation';
|
||||
|
||||
// Fallback when a recipe has no servings_default set — matches the default
|
||||
// used by RecipeEditor's "new recipe" template.
|
||||
@@ -80,7 +81,18 @@ export function listShoppingList(
|
||||
)
|
||||
.all() as ShoppingCartRecipe[];
|
||||
|
||||
const rows = db
|
||||
// SQL aggregates per (name, raw-unit). Family-grouping + consolidation is
|
||||
// done in TypeScript so SQL stays readable and the logic is unit-testable.
|
||||
type RawRow = {
|
||||
name_key: string;
|
||||
unit_key: string;
|
||||
display_name: string;
|
||||
display_unit: string | null;
|
||||
total_quantity: number | null;
|
||||
from_recipes: string;
|
||||
};
|
||||
|
||||
const raw = db
|
||||
.prepare(
|
||||
`SELECT
|
||||
LOWER(TRIM(i.name)) AS name_key,
|
||||
@@ -88,19 +100,61 @@ export function listShoppingList(
|
||||
MIN(i.name) AS display_name,
|
||||
MIN(i.unit) AS display_unit,
|
||||
SUM(i.quantity * cr.servings * 1.0 / NULLIF(COALESCE(r.servings_default, cr.servings), 0)) AS total_quantity,
|
||||
GROUP_CONCAT(DISTINCT r.title) AS from_recipes,
|
||||
EXISTS(
|
||||
SELECT 1 FROM shopping_cart_check c
|
||||
WHERE c.name_key = LOWER(TRIM(i.name))
|
||||
AND c.unit_key = LOWER(TRIM(COALESCE(i.unit, '')))
|
||||
) AS checked
|
||||
GROUP_CONCAT(DISTINCT r.title) AS from_recipes
|
||||
FROM shopping_cart_recipe cr
|
||||
JOIN recipe r ON r.id = cr.recipe_id
|
||||
JOIN ingredient i ON i.recipe_id = r.id
|
||||
GROUP BY name_key, unit_key
|
||||
ORDER BY checked ASC, display_name COLLATE NOCASE`
|
||||
GROUP BY name_key, unit_key`
|
||||
)
|
||||
.all() as ShoppingListRow[];
|
||||
.all() as RawRow[];
|
||||
|
||||
// Load all checked keys up front
|
||||
const checkedSet = new Set(
|
||||
(
|
||||
db
|
||||
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
|
||||
.all() as { name_key: string; unit_key: string }[]
|
||||
).map((c) => `${c.name_key}|${c.unit_key}`)
|
||||
);
|
||||
|
||||
// Group by (name_key, unitFamily(unit_key))
|
||||
const grouped = new Map<string, RawRow[]>();
|
||||
for (const r of raw) {
|
||||
const familyKey = unitFamily(r.unit_key);
|
||||
const key = `${r.name_key}|${familyKey}`;
|
||||
const arr = grouped.get(key) ?? [];
|
||||
arr.push(r);
|
||||
grouped.set(key, arr);
|
||||
}
|
||||
|
||||
const rows: ShoppingListRow[] = [];
|
||||
for (const members of grouped.values()) {
|
||||
const nameKey = members[0].name_key;
|
||||
const familyKey = unitFamily(members[0].unit_key);
|
||||
const consolidated = consolidate(
|
||||
members.map((m) => ({ quantity: m.total_quantity, unit: m.display_unit }))
|
||||
);
|
||||
const displayName = members[0].display_name;
|
||||
const allRecipes = new Set<string>();
|
||||
for (const m of members) {
|
||||
for (const t of m.from_recipes.split(',')) allRecipes.add(t);
|
||||
}
|
||||
rows.push({
|
||||
name_key: nameKey,
|
||||
unit_key: familyKey,
|
||||
display_name: displayName,
|
||||
display_unit: consolidated.unit,
|
||||
total_quantity: consolidated.quantity,
|
||||
from_recipes: [...allRecipes].join(','),
|
||||
checked: checkedSet.has(`${nameKey}|${familyKey}`) ? 1 : 0
|
||||
});
|
||||
}
|
||||
|
||||
// Sort: unchecked first, then alphabetically by display_name
|
||||
rows.sort((a, b) => {
|
||||
if (a.checked !== b.checked) return a.checked - b.checked;
|
||||
return a.display_name.localeCompare(b.display_name, 'de', { sensitivity: 'base' });
|
||||
});
|
||||
|
||||
const uncheckedCount = rows.reduce((n, r) => n + (r.checked ? 0 : 1), 0);
|
||||
return { recipes, rows, uncheckedCount };
|
||||
@@ -127,23 +181,33 @@ export function toggleCheck(
|
||||
|
||||
export function clearCheckedItems(db: Database.Database): void {
|
||||
const tx = db.transaction(() => {
|
||||
// Alle aggregierten Zeilen mit checked-Status holen, pro recipe_id gruppieren
|
||||
// und Rezepte finden, deren Zeilen ALLE abgehakt sind.
|
||||
const allRows = db
|
||||
// Rohe (name, unit)-Zeilen holen, checked-Status per Family-Key-Lookup
|
||||
// in JS entscheiden. Rezepte mit ALLEN Zeilen abgehakt werden raus.
|
||||
const allRowsRaw = db
|
||||
.prepare(
|
||||
`SELECT
|
||||
cr.recipe_id,
|
||||
LOWER(TRIM(i.name)) AS name_key,
|
||||
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key,
|
||||
EXISTS(
|
||||
SELECT 1 FROM shopping_cart_check c
|
||||
WHERE c.name_key = LOWER(TRIM(i.name))
|
||||
AND c.unit_key = LOWER(TRIM(COALESCE(i.unit, '')))
|
||||
) AS checked
|
||||
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key
|
||||
FROM shopping_cart_recipe cr
|
||||
JOIN ingredient i ON i.recipe_id = cr.recipe_id`
|
||||
)
|
||||
.all() as { recipe_id: number; name_key: string; unit_key: string; checked: 0 | 1 }[];
|
||||
.all() as { recipe_id: number; name_key: string; unit_key: string }[];
|
||||
|
||||
const checkSet = new Set(
|
||||
(
|
||||
db
|
||||
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
|
||||
.all() as { name_key: string; unit_key: string }[]
|
||||
).map((c) => `${c.name_key}|${c.unit_key}`)
|
||||
);
|
||||
|
||||
const allRows = allRowsRaw.map((r) => ({
|
||||
recipe_id: r.recipe_id,
|
||||
name_key: r.name_key,
|
||||
unit_key: r.unit_key,
|
||||
checked: checkSet.has(`${r.name_key}|${unitFamily(r.unit_key)}`) ? (1 as const) : (0 as const)
|
||||
}));
|
||||
|
||||
const perRecipe = new Map<number, { total: number; checked: number }>();
|
||||
for (const r of allRows) {
|
||||
@@ -160,9 +224,9 @@ export function clearCheckedItems(db: Database.Database): void {
|
||||
db.prepare('DELETE FROM shopping_cart_recipe WHERE recipe_id = ?').run(id);
|
||||
}
|
||||
|
||||
// Orphan-Checks raeumen: alle Check-Keys, die jetzt in KEINEM Cart-Rezept
|
||||
// mehr vorkommen.
|
||||
const activeKeys = db
|
||||
// Orphan-Checks raeumen: Active-Keys nach (name_key, unitFamily(raw-unit))
|
||||
// bauen, damit Checks mit family-key korrekt gematcht werden.
|
||||
const activeRaw = db
|
||||
.prepare(
|
||||
`SELECT DISTINCT
|
||||
LOWER(TRIM(i.name)) AS name_key,
|
||||
@@ -171,7 +235,9 @@ export function clearCheckedItems(db: Database.Database): void {
|
||||
JOIN ingredient i ON i.recipe_id = cr.recipe_id`
|
||||
)
|
||||
.all() as { name_key: string; unit_key: string }[];
|
||||
const activeSet = new Set(activeKeys.map((k) => `${k.name_key} ${k.unit_key}`));
|
||||
const activeSet = new Set(
|
||||
activeRaw.map((k) => `${k.name_key}|${unitFamily(k.unit_key)}`)
|
||||
);
|
||||
const allChecks = db
|
||||
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
|
||||
.all() as { name_key: string; unit_key: string }[];
|
||||
@@ -179,7 +245,7 @@ export function clearCheckedItems(db: Database.Database): void {
|
||||
'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?'
|
||||
);
|
||||
for (const c of allChecks) {
|
||||
if (!activeSet.has(`${c.name_key} ${c.unit_key}`)) {
|
||||
if (!activeSet.has(`${c.name_key}|${c.unit_key}`)) {
|
||||
del.run(c.name_key, c.unit_key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ describe('listShoppingList aggregation', () => {
|
||||
const rows = listShoppingList(db).rows;
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].name_key).toBe('mehl');
|
||||
expect(rows[0].unit_key).toBe('g');
|
||||
expect(rows[0].unit_key).toBe('weight');
|
||||
expect(rows[0].total_quantity).toBe(400);
|
||||
expect(rows[0].from_recipes).toContain('Carbonara');
|
||||
expect(rows[0].from_recipes).toContain('Lasagne');
|
||||
@@ -201,15 +201,15 @@ describe('toggleCheck', () => {
|
||||
|
||||
it('marks a row as checked', () => {
|
||||
const { db } = setupOneRowCart();
|
||||
toggleCheck(db, 'mehl', 'g', true);
|
||||
toggleCheck(db, 'mehl', 'weight', true);
|
||||
const rows = listShoppingList(db).rows;
|
||||
expect(rows[0].checked).toBe(1);
|
||||
});
|
||||
|
||||
it('unchecks a row when passed false', () => {
|
||||
const { db } = setupOneRowCart();
|
||||
toggleCheck(db, 'mehl', 'g', true);
|
||||
toggleCheck(db, 'mehl', 'g', false);
|
||||
toggleCheck(db, 'mehl', 'weight', true);
|
||||
toggleCheck(db, 'mehl', 'weight', false);
|
||||
expect(listShoppingList(db).rows[0].checked).toBe(0);
|
||||
});
|
||||
|
||||
@@ -225,7 +225,7 @@ describe('toggleCheck', () => {
|
||||
}));
|
||||
addRecipeToCart(db, a, null);
|
||||
addRecipeToCart(db, b, null);
|
||||
toggleCheck(db, 'mehl', 'g', true);
|
||||
toggleCheck(db, 'mehl', 'weight', true);
|
||||
// Rezept A weg, Mehl kommt noch aus B — check bleibt, mit neuer Menge
|
||||
removeRecipeFromCart(db, a);
|
||||
const rows = listShoppingList(db).rows;
|
||||
@@ -304,3 +304,178 @@ describe('clearCart', () => {
|
||||
expect(anyCheck).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('toggleCheck — stabil ueber Unit-Family', () => {
|
||||
it('haekchen bleibt erhalten wenn Gesamtmenge von kg auf g faellt', () => {
|
||||
const db = openInMemoryForTest();
|
||||
const a = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R1',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Kartoffeln', quantity: 500, unit: 'g', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
const b = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R2',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Kartoffeln', quantity: 1, unit: 'kg', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
addRecipeToCart(db, a, null);
|
||||
addRecipeToCart(db, b, null);
|
||||
|
||||
// Abhaken der konsolidierten 1,5-kg-Zeile via family-key
|
||||
const before = listShoppingList(db).rows[0];
|
||||
toggleCheck(db, before.name_key, before.unit_key, true);
|
||||
expect(listShoppingList(db).rows[0].checked).toBe(1);
|
||||
|
||||
// Ein Rezept rausnehmen → nur noch 500 g, display wechselt auf g
|
||||
removeRecipeFromCart(db, b);
|
||||
const after = listShoppingList(db).rows[0];
|
||||
expect(after.display_unit).toBe('g');
|
||||
expect(after.total_quantity).toBe(500);
|
||||
// Haekchen bleibt: unit_key ist weiterhin 'weight'
|
||||
expect(after.checked).toBe(1);
|
||||
});
|
||||
|
||||
it('clearCheckedItems respektiert family-key beim Orphan-Cleanup', () => {
|
||||
const db = openInMemoryForTest();
|
||||
const a = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R1',
|
||||
servings_default: 4,
|
||||
ingredients: [
|
||||
{ position: 1, name: 'Kartoffeln', quantity: 500, unit: 'g', note: null, raw_text: '', section_heading: null },
|
||||
{ position: 2, name: 'Salz', quantity: 1, unit: 'Prise', note: null, raw_text: '', section_heading: null }
|
||||
]
|
||||
})
|
||||
);
|
||||
addRecipeToCart(db, a, null);
|
||||
const rows = listShoppingList(db).rows;
|
||||
// Alle abhaken
|
||||
for (const r of rows) toggleCheck(db, r.name_key, r.unit_key, true);
|
||||
clearCheckedItems(db);
|
||||
// Das Rezept sollte raus sein
|
||||
expect(listShoppingList(db).recipes).toHaveLength(0);
|
||||
// Check-Tabelle sollte leer sein (keine Orphans)
|
||||
const remaining = (db.prepare('SELECT COUNT(*) AS c FROM shopping_cart_check').get() as { c: number }).c;
|
||||
expect(remaining).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('listShoppingList — Konsolidierung ueber Einheiten', () => {
|
||||
it('fasst 500 g + 1 kg Kartoffeln zu 1,5 kg zusammen', () => {
|
||||
const db = openInMemoryForTest();
|
||||
const a = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'Kartoffelsuppe',
|
||||
servings_default: 4,
|
||||
ingredients: [
|
||||
{ position: 1, name: 'Kartoffeln', quantity: 500, unit: 'g', note: null, raw_text: '', section_heading: null }
|
||||
]
|
||||
})
|
||||
);
|
||||
const b = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'Kartoffelpuffer',
|
||||
servings_default: 4,
|
||||
ingredients: [
|
||||
{ position: 1, name: 'Kartoffeln', quantity: 1, unit: 'kg', note: null, raw_text: '', section_heading: null }
|
||||
]
|
||||
})
|
||||
);
|
||||
addRecipeToCart(db, a, null);
|
||||
addRecipeToCart(db, b, null);
|
||||
|
||||
const snap = listShoppingList(db);
|
||||
const kartoffeln = snap.rows.filter((r) => r.display_name.toLowerCase() === 'kartoffeln');
|
||||
expect(kartoffeln).toHaveLength(1);
|
||||
expect(kartoffeln[0].total_quantity).toBe(1.5);
|
||||
expect(kartoffeln[0].display_unit).toBe('kg');
|
||||
});
|
||||
|
||||
it('kombiniert ml + l korrekt (400 ml + 0,5 l → 900 ml)', () => {
|
||||
const db = openInMemoryForTest();
|
||||
const a = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R1',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Milch', quantity: 400, unit: 'ml', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
const b = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R2',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Milch', quantity: 0.5, unit: 'l', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
addRecipeToCart(db, a, null);
|
||||
addRecipeToCart(db, b, null);
|
||||
|
||||
const milch = listShoppingList(db).rows.filter((r) => r.display_name.toLowerCase() === 'milch');
|
||||
expect(milch).toHaveLength(1);
|
||||
expect(milch[0].total_quantity).toBe(900);
|
||||
expect(milch[0].display_unit).toBe('ml');
|
||||
});
|
||||
|
||||
it('laesst inkompatible Families getrennt (5 Stueck Eier + 500 g Eier = 2 Zeilen)', () => {
|
||||
const db = openInMemoryForTest();
|
||||
const a = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R1',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Eier', quantity: 5, unit: 'Stück', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
const b = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R2',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Eier', quantity: 500, unit: 'g', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
addRecipeToCart(db, a, null);
|
||||
addRecipeToCart(db, b, null);
|
||||
|
||||
const eier = listShoppingList(db).rows.filter((r) => r.display_name.toLowerCase() === 'eier');
|
||||
expect(eier).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('summiert gleiche Unit-Family ohne Konversion (2 Bund + 1 Bund → 3 Bund)', () => {
|
||||
const db = openInMemoryForTest();
|
||||
const a = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R1',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Petersilie', quantity: 2, unit: 'Bund', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
const b = insertRecipe(
|
||||
db,
|
||||
recipe({
|
||||
title: 'R2',
|
||||
servings_default: 4,
|
||||
ingredients: [{ position: 1, name: 'Petersilie', quantity: 1, unit: 'Bund', note: null, raw_text: '', section_heading: null }]
|
||||
})
|
||||
);
|
||||
addRecipeToCart(db, a, null);
|
||||
addRecipeToCart(db, b, null);
|
||||
|
||||
const petersilie = listShoppingList(db).rows.filter((r) => r.display_name.toLowerCase() === 'petersilie');
|
||||
expect(petersilie).toHaveLength(1);
|
||||
expect(petersilie[0].total_quantity).toBe(3);
|
||||
expect(petersilie[0].display_unit?.toLowerCase()).toBe('bund');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,10 +16,10 @@ describe('formatQuantity', () => {
|
||||
});
|
||||
|
||||
it('renders fractional with up to 2 decimals, trailing zeros trimmed', () => {
|
||||
expect(formatQuantity(0.5)).toBe('0.5');
|
||||
expect(formatQuantity(0.333333)).toBe('0.33');
|
||||
expect(formatQuantity(1.1)).toBe('1.1');
|
||||
expect(formatQuantity(1.1)).toBe('1.1');
|
||||
expect(formatQuantity(0.5)).toBe('0,5');
|
||||
expect(formatQuantity(0.333333)).toBe('0,33');
|
||||
expect(formatQuantity(1.1)).toBe('1,1');
|
||||
expect(formatQuantity(1.1)).toBe('1,1');
|
||||
});
|
||||
|
||||
it('handles zero', () => {
|
||||
|
||||
Reference in New Issue
Block a user