Compare commits
36 Commits
005c3ea7b5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91fbf27269 | ||
|
|
b556eb39b3 | ||
|
|
c177c1dc5f | ||
|
|
b2337a5c2a | ||
|
|
f2656bd9e3 | ||
|
|
fd55a44bfb | ||
|
|
14cf1b1d35 | ||
|
|
b85f869c09 | ||
|
|
c6a549699a | ||
|
|
29f0245ce0 | ||
|
|
59b232c5fc | ||
|
|
b9b06e161c | ||
|
|
2f0a45f487 | ||
|
|
a68b99c807 | ||
|
|
2573f80940 | ||
|
|
0a97ea2fea | ||
|
|
12f499cb98 | ||
|
|
829850aa88 | ||
|
|
2b0bd4dc44 | ||
|
|
e7318164cb | ||
|
|
2216c89a04 | ||
|
|
01d29bff0e | ||
|
|
a5321d620a | ||
|
|
b31223add5 | ||
|
|
f495c024c6 | ||
|
|
1214b9e01d | ||
|
|
82d4348873 | ||
|
|
6f54b004ca | ||
|
|
226ca5e5ed | ||
|
|
5357c9787b | ||
|
|
6c8de6fa3a | ||
|
|
866a222265 | ||
|
|
543008b0f2 | ||
|
|
2cd9b47450 | ||
|
|
98894bb895 | ||
|
|
363ea6fbe7 |
@@ -0,0 +1,887 @@
|
|||||||
|
# Einkaufsliste Mengen-Konsolidierung Implementation Plan
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Verschiedene Unit-Varianten derselben Zutat (500 g + 1 kg Kartoffeln) in der Einkaufsliste zu einer Zeile konsolidieren (→ 1,5 kg). Scope: g↔kg, ml↔l.
|
||||||
|
|
||||||
|
**Architecture:** Zwei reine TS-Utilities (`unitFamily`, `consolidate`) kapseln die Logik. `listShoppingList()` lässt SQL weiterhin pro (name, unit) aggregieren, bündelt die Zeilen dann in TS pro `(name, unitFamily)` und konsolidiert. Migration 015 macht `shopping_cart_check.unit_key` zum Family-Key, damit Abhaks nicht verloren gehen wenn Display-Unit zwischen g und kg wechselt. `formatQuantity` wechselt app-weit auf `toLocaleString('de-DE')` (Komma als Dezimaltrennzeichen).
|
||||||
|
|
||||||
|
**Tech Stack:** SvelteKit, better-sqlite3, Vitest. Keine neuen Deps.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## File Structure
|
||||||
|
|
||||||
|
**Create:**
|
||||||
|
- `src/lib/server/shopping/unit-consolidation.ts` — `unitFamily()` + `consolidate()`
|
||||||
|
- `src/lib/server/db/migrations/015_shopping_check_family.sql` — Family-Key-Migration
|
||||||
|
- `tests/unit/unit-consolidation.test.ts` — Unit-Tests
|
||||||
|
|
||||||
|
**Modify:**
|
||||||
|
- `src/lib/quantity-format.ts` — `toLocaleString('de-DE', …)` statt Punkt
|
||||||
|
- `tests/unit/quantity-format.test.ts` — Erwartungen auf Komma anpassen
|
||||||
|
- `src/lib/server/shopping/repository.ts` — `listShoppingList`, `toggleCheck`, `clearCheckedItems` auf Family-Key umstellen
|
||||||
|
- `tests/integration/shopping-repository.test.ts` — neue Describe-Blöcke für Konsolidierung
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 1: Unit-Family-Utility
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/server/unit-consolidation.ts`
|
||||||
|
- Test: `tests/unit/unit-consolidation.test.ts`
|
||||||
|
|
||||||
|
Hinweis: Datei bewusst in `src/lib/server/` (nicht in `shopping/`), weil `unitFamily` auch vom Migration-Code referenziert wird — eine Ebene höher ist intuitiver.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write failing tests for `unitFamily`**
|
||||||
|
|
||||||
|
Create `tests/unit/unit-consolidation.test.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { unitFamily } from '../../src/lib/server/unit-consolidation';
|
||||||
|
|
||||||
|
describe('unitFamily', () => {
|
||||||
|
it('maps g and kg to weight', () => {
|
||||||
|
expect(unitFamily('g')).toBe('weight');
|
||||||
|
expect(unitFamily('kg')).toBe('weight');
|
||||||
|
});
|
||||||
|
it('maps ml and l to volume', () => {
|
||||||
|
expect(unitFamily('ml')).toBe('volume');
|
||||||
|
expect(unitFamily('l')).toBe('volume');
|
||||||
|
});
|
||||||
|
it('lowercases and trims unknown units', () => {
|
||||||
|
expect(unitFamily(' Bund ')).toBe('bund');
|
||||||
|
expect(unitFamily('TL')).toBe('tl');
|
||||||
|
expect(unitFamily('Stück')).toBe('stück');
|
||||||
|
});
|
||||||
|
it('is case-insensitive for weight/volume', () => {
|
||||||
|
expect(unitFamily('Kg')).toBe('weight');
|
||||||
|
expect(unitFamily('ML')).toBe('volume');
|
||||||
|
});
|
||||||
|
it('returns empty string for null/undefined/empty', () => {
|
||||||
|
expect(unitFamily(null)).toBe('');
|
||||||
|
expect(unitFamily(undefined)).toBe('');
|
||||||
|
expect(unitFamily('')).toBe('');
|
||||||
|
expect(unitFamily(' ')).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run tests to verify they fail**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/unit/unit-consolidation.test.ts`
|
||||||
|
Expected: All fail with "Cannot find module …/unit-consolidation".
|
||||||
|
|
||||||
|
- [ ] **Step 3: Implement `unitFamily`**
|
||||||
|
|
||||||
|
Create `src/lib/server/unit-consolidation.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export type UnitFamily = 'weight' | 'volume' | string;
|
||||||
|
|
||||||
|
const WEIGHT_UNITS = new Set(['g', 'kg']);
|
||||||
|
const VOLUME_UNITS = new Set(['ml', 'l']);
|
||||||
|
|
||||||
|
export function unitFamily(unit: string | null | undefined): UnitFamily {
|
||||||
|
const u = (unit ?? '').trim().toLowerCase();
|
||||||
|
if (WEIGHT_UNITS.has(u)) return 'weight';
|
||||||
|
if (VOLUME_UNITS.has(u)) return 'volume';
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run tests to verify they pass**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/unit/unit-consolidation.test.ts`
|
||||||
|
Expected: 5 tests pass.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/server/unit-consolidation.ts tests/unit/unit-consolidation.test.ts
|
||||||
|
git commit -m "feat(shopping): unitFamily-Utility fuer Konsolidierung"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 2: Consolidate-Funktion
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/server/unit-consolidation.ts`
|
||||||
|
- Modify: `tests/unit/unit-consolidation.test.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Append failing tests for `consolidate` to the existing test file**
|
||||||
|
|
||||||
|
Append to `tests/unit/unit-consolidation.test.ts` (after the `unitFamily` describe):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { consolidate } from '../../src/lib/server/unit-consolidation';
|
||||||
|
|
||||||
|
describe('consolidate', () => {
|
||||||
|
it('kombiniert 500 g + 1 kg zu 1,5 kg', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 500, unit: 'g' },
|
||||||
|
{ quantity: 1, unit: 'kg' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1.5, unit: 'kg' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('bleibt bei g wenn Summe < 1 kg', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 200, unit: 'g' },
|
||||||
|
{ quantity: 300, unit: 'g' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 500, unit: 'g' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('kombiniert ml + l analog (400 ml + 0,5 l → 900 ml)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 400, unit: 'ml' },
|
||||||
|
{ quantity: 0.5, unit: 'l' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 900, unit: 'ml' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('promoted zu l ab 1000 ml (0,5 l + 0,8 l → 1,3 l)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 0.5, unit: 'l' },
|
||||||
|
{ quantity: 0.8, unit: 'l' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1.3, unit: 'l' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('summiert gleiche nicht-family-units (2 Bund + 1 Bund → 3 Bund)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 2, unit: 'Bund' },
|
||||||
|
{ quantity: 1, unit: 'Bund' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 3, unit: 'Bund' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('behandelt quantity=null als 0', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: null, unit: 'TL' },
|
||||||
|
{ quantity: 1, unit: 'TL' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1, unit: 'TL' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gibt null zurueck wenn alle quantities null sind', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: null, unit: 'Prise' },
|
||||||
|
{ quantity: null, unit: 'Prise' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: null, unit: 'Prise' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rundet Float-Artefakte auf 2 Dezimalen (0,1 + 0,2 kg → 0,3 kg)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 0.1, unit: 'kg' },
|
||||||
|
{ quantity: 0.2, unit: 'kg' }
|
||||||
|
]);
|
||||||
|
// 0.1 + 0.2 in kg = 0.3 kg, in g = 300 → promoted? 300 < 1000 → 300 g
|
||||||
|
expect(out).toEqual({ quantity: 300, unit: 'g' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('nimmt unit vom ersten Eintrag bei unbekannter family', () => {
|
||||||
|
const out = consolidate([{ quantity: 5, unit: 'Stück' }]);
|
||||||
|
expect(out).toEqual({ quantity: 5, unit: 'Stück' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run tests to verify they fail**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/unit/unit-consolidation.test.ts`
|
||||||
|
Expected: Fail with "consolidate is not a function" or similar (9 new tests fail).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Implement `consolidate`**
|
||||||
|
|
||||||
|
Append to `src/lib/server/unit-consolidation.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export interface QuantityInUnit {
|
||||||
|
quantity: number | null;
|
||||||
|
unit: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function round2(n: number): number {
|
||||||
|
return Math.round(n * 100) / 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konsolidiert mehrere {quantity, unit}-Eintraege derselben Unit-Family
|
||||||
|
* zu einer gemeinsamen Menge + Display-Unit.
|
||||||
|
*
|
||||||
|
* - Gewicht (g, kg): summiert in g, promoted bei >=1000 g auf kg.
|
||||||
|
* - Volumen (ml, l): summiert in ml, promoted bei >=1000 ml auf l.
|
||||||
|
* - Andere: summiert quantity ohne Umrechnung, Display-Unit vom ersten
|
||||||
|
* Eintrag.
|
||||||
|
*
|
||||||
|
* quantity=null wird als 0 behandelt. Wenn ALLE quantities null sind,
|
||||||
|
* ist die Gesamtmenge ebenfalls null.
|
||||||
|
*/
|
||||||
|
export function consolidate(rows: QuantityInUnit[]): QuantityInUnit {
|
||||||
|
if (rows.length === 0) return { quantity: null, unit: null };
|
||||||
|
|
||||||
|
const family = unitFamily(rows[0].unit);
|
||||||
|
const firstUnit = rows[0].unit;
|
||||||
|
|
||||||
|
const allNull = rows.every((r) => r.quantity === null);
|
||||||
|
|
||||||
|
if (family === 'weight') {
|
||||||
|
if (allNull) return { quantity: null, unit: firstUnit };
|
||||||
|
const grams = rows.reduce((sum, r) => {
|
||||||
|
const q = r.quantity ?? 0;
|
||||||
|
return sum + (unitFamily(r.unit) === 'weight' && r.unit?.toLowerCase().trim() === 'kg' ? q * 1000 : q);
|
||||||
|
}, 0);
|
||||||
|
if (grams >= 1000) return { quantity: round2(grams / 1000), unit: 'kg' };
|
||||||
|
return { quantity: round2(grams), unit: 'g' };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (family === 'volume') {
|
||||||
|
if (allNull) return { quantity: null, unit: firstUnit };
|
||||||
|
const ml = rows.reduce((sum, r) => {
|
||||||
|
const q = r.quantity ?? 0;
|
||||||
|
return sum + (r.unit?.toLowerCase().trim() === 'l' ? q * 1000 : q);
|
||||||
|
}, 0);
|
||||||
|
if (ml >= 1000) return { quantity: round2(ml / 1000), unit: 'l' };
|
||||||
|
return { quantity: round2(ml), unit: 'ml' };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-family: summiere quantity direkt
|
||||||
|
if (allNull) return { quantity: null, unit: firstUnit };
|
||||||
|
const sum = rows.reduce((acc, r) => acc + (r.quantity ?? 0), 0);
|
||||||
|
return { quantity: round2(sum), unit: firstUnit };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run tests to verify they pass**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/unit/unit-consolidation.test.ts`
|
||||||
|
Expected: 14 tests pass (5 from Task 1 + 9 new).
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/server/unit-consolidation.ts tests/unit/unit-consolidation.test.ts
|
||||||
|
git commit -m "feat(shopping): consolidate() fuer g/kg + ml/l Summierung"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 3: formatQuantity auf deutsches Locale
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/quantity-format.ts`
|
||||||
|
- Modify: `tests/unit/quantity-format.test.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Update tests to expect comma decimal**
|
||||||
|
|
||||||
|
Open `tests/unit/quantity-format.test.ts`. Jede Erwartung mit Dezimalpunkt auf Komma ändern, z. B.:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// vorher: expect(formatQuantity(0.333)).toBe('0.33');
|
||||||
|
// nachher:
|
||||||
|
expect(formatQuantity(0.333)).toBe('0,33');
|
||||||
|
```
|
||||||
|
|
||||||
|
Betroffene Assertions (aus dem bestehenden Test-File):
|
||||||
|
- `formatQuantity(0.333)` → `'0,33'`
|
||||||
|
- `formatQuantity(0.5)` → `'0,5'`
|
||||||
|
- `formatQuantity(1.25)` → `'1,25'`
|
||||||
|
|
||||||
|
Ganze Zahlen (`formatQuantity(3)` → `'3'`) und null (`''`) bleiben gleich.
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run tests to verify they fail with current implementation**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/unit/quantity-format.test.ts`
|
||||||
|
Expected: Tests with decimal values fail (`'0.33'` received, `'0,33'` expected).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Rewrite `formatQuantity` mit toLocaleString**
|
||||||
|
|
||||||
|
Replace contents of `src/lib/quantity-format.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
export function formatQuantity(q: number | null): string {
|
||||||
|
if (q === null || q === undefined) return '';
|
||||||
|
return q.toLocaleString('de-DE', {
|
||||||
|
maximumFractionDigits: 2,
|
||||||
|
useGrouping: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run tests to verify they pass**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/unit/quantity-format.test.ts`
|
||||||
|
Expected: Alle 5 Tests grün.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Run full suite to catch app-wide regressions**
|
||||||
|
|
||||||
|
Run: `npm test`
|
||||||
|
Expected: Alle Tests grün. Falls andere Tests (z. B. Rezept-Detail-Rendering) Erwartungen auf `'.'` haben und fehlschlagen, Assertions dort auf Komma anpassen und in denselben Commit nehmen.
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/quantity-format.ts tests/unit/quantity-format.test.ts
|
||||||
|
git commit -m "feat(format): formatQuantity app-weit auf de-DE Komma-Dezimal"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 4: Migration 015 — Check-Keys auf Family
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/server/db/migrations/015_shopping_check_family.sql`
|
||||||
|
|
||||||
|
Hinweis: Migrations werden via `import.meta.glob('./migrations/*.sql', {eager, query:'?raw'})` gebundelt (siehe CLAUDE.md) — kein Dockerfile-Copy nötig.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the migration**
|
||||||
|
|
||||||
|
Create `src/lib/server/db/migrations/015_shopping_check_family.sql`:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- 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
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Verify migration runs (smoke test via any integration test)**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/integration/shopping-repository.test.ts`
|
||||||
|
Expected: Alle bestehenden Tests grün (Migration läuft beim `openInMemoryForTest()`, bricht nichts weil Tabelle beim ersten Lauf leer ist).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/server/db/migrations/015_shopping_check_family.sql
|
||||||
|
git commit -m "feat(shopping): Migration 015 — Check-Keys auf Unit-Family"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 5: listShoppingList mit Family-Konsolidierung
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/server/shopping/repository.ts:70-107`
|
||||||
|
- Modify: `tests/integration/shopping-repository.test.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write failing integration test**
|
||||||
|
|
||||||
|
Append to `tests/integration/shopping-repository.test.ts` (z. B. nach dem vorhandenen `addRecipeToCart`-Block, ein eigener Describe-Block):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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: null }
|
||||||
|
]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const b = insertRecipe(
|
||||||
|
db,
|
||||||
|
recipe({
|
||||||
|
title: 'Kartoffelpuffer',
|
||||||
|
servings_default: 4,
|
||||||
|
ingredients: [
|
||||||
|
{ position: 1, name: 'Kartoffeln', quantity: 1, unit: 'kg', note: null, raw_text: 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: 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: 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: null }]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const b = insertRecipe(
|
||||||
|
db,
|
||||||
|
recipe({
|
||||||
|
title: 'R2',
|
||||||
|
servings_default: 4,
|
||||||
|
ingredients: [{ position: 1, name: 'Eier', quantity: 500, unit: 'g', note: null, raw_text: 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: null }]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const b = insertRecipe(
|
||||||
|
db,
|
||||||
|
recipe({
|
||||||
|
title: 'R2',
|
||||||
|
servings_default: 4,
|
||||||
|
ingredients: [{ position: 1, name: 'Petersilie', quantity: 1, unit: 'Bund', note: null, raw_text: 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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run tests to verify they fail**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/integration/shopping-repository.test.ts`
|
||||||
|
Expected: 4 neue Tests fail (Konsolidierung existiert noch nicht).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Rewrite `listShoppingList` to use TS-side consolidation**
|
||||||
|
|
||||||
|
Replace the `listShoppingList` body in `src/lib/server/shopping/repository.ts` (Zeilen 70-107):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { consolidate, unitFamily } from '../unit-consolidation';
|
||||||
|
|
||||||
|
// (oben im File unter den bestehenden Imports einfuegen)
|
||||||
|
|
||||||
|
export function listShoppingList(
|
||||||
|
db: Database.Database
|
||||||
|
): ShoppingListSnapshot {
|
||||||
|
const recipes = db
|
||||||
|
.prepare(
|
||||||
|
`SELECT cr.recipe_id, r.title, r.image_path, cr.servings,
|
||||||
|
COALESCE(r.servings_default, cr.servings) AS servings_default
|
||||||
|
FROM shopping_cart_recipe cr
|
||||||
|
JOIN recipe r ON r.id = cr.recipe_id
|
||||||
|
ORDER BY cr.added_at ASC`
|
||||||
|
)
|
||||||
|
.all() as ShoppingCartRecipe[];
|
||||||
|
|
||||||
|
// SQL aggregiert weiterhin pro (name, raw-unit). Die family-Gruppierung
|
||||||
|
// + Konsolidierung macht TypeScript, damit SQL lesbar bleibt und die
|
||||||
|
// Logik Unit-testbar ist.
|
||||||
|
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,
|
||||||
|
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_key,
|
||||||
|
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
|
||||||
|
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`
|
||||||
|
)
|
||||||
|
.all() as RawRow[];
|
||||||
|
|
||||||
|
// Check-Keys einmalig vorladen
|
||||||
|
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}`)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Gruppieren nach (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 [key, members] of grouped) {
|
||||||
|
const [nameKey, familyKey] = key.split('|');
|
||||||
|
const consolidated = consolidate(
|
||||||
|
members.map((m) => ({ quantity: m.total_quantity, unit: m.display_unit }))
|
||||||
|
);
|
||||||
|
// display_name: ersten nehmen (alle Member haben dasselbe name_key)
|
||||||
|
const displayName = members[0].display_name;
|
||||||
|
// from_recipes: alle unique Titel aus den Members kombinieren
|
||||||
|
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, // wichtig: family-key, matched mit checked-Lookup
|
||||||
|
display_name: displayName,
|
||||||
|
display_unit: consolidated.unit,
|
||||||
|
total_quantity: consolidated.quantity,
|
||||||
|
from_recipes: [...allRecipes].join(','),
|
||||||
|
checked: checkedSet.has(`${nameKey}|${familyKey}`) ? 1 : 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort wie bisher: erst unchecked, dann alphabetisch 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 };
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run tests to verify they pass**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/integration/shopping-repository.test.ts`
|
||||||
|
Expected: Alle Tests grün (bestehende + 4 neue).
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/server/shopping/repository.ts tests/integration/shopping-repository.test.ts
|
||||||
|
git commit -m "feat(shopping): listShoppingList konsolidiert g/kg + ml/l"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 6: toggleCheck + clearCheckedItems auf Family-Key
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/server/shopping/repository.ts:109-188`
|
||||||
|
- Modify: `tests/integration/shopping-repository.test.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write failing integration tests**
|
||||||
|
|
||||||
|
Append to `tests/integration/shopping-repository.test.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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: null }]
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const b = insertRecipe(
|
||||||
|
db,
|
||||||
|
recipe({
|
||||||
|
title: 'R2',
|
||||||
|
servings_default: 4,
|
||||||
|
ingredients: [{ position: 1, name: 'Kartoffeln', quantity: 1, unit: 'kg', note: null, raw_text: 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run tests to verify they fail**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/integration/shopping-repository.test.ts`
|
||||||
|
Expected: Der neue Test failt, weil `toggleCheck` noch mit `unit_key` als raw unit arbeitet — der Check wird mit `'weight'` geschrieben, ABER das Schreiben selbst könnte fälschlich doch durchgehen (toggleCheck macht ja nur INSERT mit dem gegebenen Key). Der Failure entsteht beim zweiten `listShoppingList()`: der Lookup-Key matched noch nicht mit dem gespeicherten Check.
|
||||||
|
|
||||||
|
Tatsächlich: Mit Task 5 schreibt `toggleCheck(db, name, 'weight', true)` einen Eintrag `(kartoffeln, 'weight')` in `shopping_cart_check`. `listShoppingList` liest den Check mit dem Family-Key — also passt. Der Test müsste grün sein _wenn_ toggleCheck unverändert funktioniert.
|
||||||
|
|
||||||
|
Hmm — let me re-check. `toggleCheck(db, nameKey, unitKey, checked)` nimmt einfach den String, den der Caller übergibt, und speichert. Das ist agnostisch. Also wenn die UI `row.unit_key` durchreicht (was ja jetzt 'weight' ist), funktioniert das. Kein Code-Change nötig in toggleCheck.
|
||||||
|
|
||||||
|
`clearCheckedItems` hingegen vergleicht Check-Keys mit der Ingredient-Tabelle via `LOWER(TRIM(COALESCE(i.unit, '')))` — das ist aber der RAW unit, nicht der Family-Key. Hier ist der Fix nötig.
|
||||||
|
|
||||||
|
→ Step 2 wird daher beide Facetten prüfen: (1) toggleCheck/round-trip funktioniert bereits (Test grün), (2) clearCheckedItems dedupliziert korrekt.
|
||||||
|
|
||||||
|
Ich füge daher einen expliziten clearCheckedItems-Test hinzu:
|
||||||
|
|
||||||
|
Append weitere Test-Case in denselben Block:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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: null },
|
||||||
|
{ position: 2, name: 'Salz', quantity: 1, unit: 'Prise', note: null, raw_text: 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);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Run: `npm test -- tests/integration/shopping-repository.test.ts`
|
||||||
|
Expected: Der clearCheckedItems-Test könnte failen weil der Orphan-Cleanup mit raw-unit vergleicht — der Check hat 'weight', das Ingredient hat 'g', Key-Match schlägt fehl, Check bleibt als Orphan.
|
||||||
|
|
||||||
|
- [ ] **Step 3: Fix `clearCheckedItems` to use family-key for orphan comparison**
|
||||||
|
|
||||||
|
In `src/lib/server/shopping/repository.ts`, in `clearCheckedItems` den Orphan-Cleanup-Block:
|
||||||
|
|
||||||
|
Ersetzen (aktuell Zeilen 163-185):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Orphan-Checks raeumen: alle Check-Keys, die jetzt in KEINEM Cart-Rezept
|
||||||
|
// mehr vorkommen.
|
||||||
|
const activeKeys = db
|
||||||
|
.prepare(
|
||||||
|
`SELECT DISTINCT
|
||||||
|
LOWER(TRIM(i.name)) AS name_key,
|
||||||
|
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 { name_key: string; unit_key: string }[];
|
||||||
|
const activeSet = new Set(activeKeys.map((k) => `${k.name_key} ${k.unit_key}`));
|
||||||
|
const allChecks = db
|
||||||
|
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
|
||||||
|
.all() as { name_key: string; unit_key: string }[];
|
||||||
|
const del = db.prepare(
|
||||||
|
'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}`)) {
|
||||||
|
del.run(c.name_key, c.unit_key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
durch:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 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,
|
||||||
|
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 { name_key: string; unit_key: string }[];
|
||||||
|
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 }[];
|
||||||
|
const del = db.prepare(
|
||||||
|
'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}`)) {
|
||||||
|
del.run(c.name_key, c.unit_key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Analog den oberen Block in `clearCheckedItems` (perRecipe-Gruppierung, Zeilen 132-146), der `unit_key` mit `LOWER(TRIM(i.unit))` matched — da wird pro recipe_id gezählt, ob alle Zeilen abgehakt sind. Der Count-Vergleich mit `shopping_cart_check` erfolgt auch hier via unit_key. Anpassen:
|
||||||
|
|
||||||
|
Ersetzen (aktuell Zeilen 132-147):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const allRows = 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
|
||||||
|
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 }[];
|
||||||
|
```
|
||||||
|
|
||||||
|
durch:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Rohe (name, unit)-Zeilen holen, checked-Status per Family-Key-Lookup
|
||||||
|
// in JS entscheiden (SQL-CASE-Duplikation vermeiden).
|
||||||
|
const allRowsRaw = db
|
||||||
|
.prepare(
|
||||||
|
`SELECT
|
||||||
|
cr.recipe_id,
|
||||||
|
LOWER(TRIM(i.name)) AS name_key,
|
||||||
|
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 }[];
|
||||||
|
|
||||||
|
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)
|
||||||
|
}));
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run tests to verify they pass**
|
||||||
|
|
||||||
|
Run: `npm test -- tests/integration/shopping-repository.test.ts`
|
||||||
|
Expected: Alle Tests grün.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Run full suite + typecheck**
|
||||||
|
|
||||||
|
Run: `npm test && npm run check`
|
||||||
|
Expected:
|
||||||
|
- Tests: alle grün
|
||||||
|
- svelte-check: `0 ERRORS 0 WARNINGS`
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/server/shopping/repository.ts tests/integration/shopping-repository.test.ts
|
||||||
|
git commit -m "feat(shopping): clearCheckedItems auf Family-Key umgestellt"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 7: End-to-End-Smoketest im Dev-Deployment
|
||||||
|
|
||||||
|
**Files:** keine
|
||||||
|
|
||||||
|
- [ ] **Step 1: Push und warten auf CI-Deploy**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push
|
||||||
|
```
|
||||||
|
|
||||||
|
CI baut arm64-Image, deployt nach dev. ~5 Min.
|
||||||
|
|
||||||
|
- [ ] **Step 2: Manuell auf `https://kochwas-dev.siegeln.net/shopping-list` prüfen**
|
||||||
|
|
||||||
|
Check-Liste:
|
||||||
|
- Zwei Rezepte mit 500 g + 1 kg gleicher Zutat in den Warenkorb → eine Zeile mit "1,5 kg".
|
||||||
|
- 400 ml + 0,5 l → "900 ml".
|
||||||
|
- Komma-Darstellung in Rezept-Detail überall ok (keine Regressionen).
|
||||||
|
- Abhaken + Rezept rausnehmen → Haken bleibt.
|
||||||
|
|
||||||
|
Wenn alle grün: Feature ist done. Kein separater Commit nötig.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Self-Review Checklist
|
||||||
|
|
||||||
|
- [x] Spec-Coverage: Alle Sektionen abgedeckt (Unit-Konsolidierung → Task 1+2, Migration → Task 4, Formatter → Task 3, listShoppingList-Integration → Task 5, Check-Stabilität → Task 6).
|
||||||
|
- [x] Keine Placeholder: alle Tests und Implementierungen vollständig ausgeschrieben.
|
||||||
|
- [x] Type-Konsistenz: `QuantityInUnit`, `ShoppingListRow` einheitlich referenziert. `unit_key` bleibt derselbe Feldname, semantisch jetzt Family-Key.
|
||||||
|
- [x] Scope: eine einzelne Phase, atomic commits, TDD.
|
||||||
1241
docs/superpowers/plans/2026-04-22-views-and-collapsibles.md
Normal file
1241
docs/superpowers/plans/2026-04-22-views-and-collapsibles.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,208 @@
|
|||||||
|
# Einkaufsliste: Mengen-Konsolidierung über Einheiten
|
||||||
|
|
||||||
|
## Kontext
|
||||||
|
|
||||||
|
Die Einkaufsliste (`/src/lib/server/shopping/repository.ts`, `listShoppingList()`) aggregiert
|
||||||
|
Zutaten aus allen Warenkorb-Rezepten dynamisch per `GROUP BY LOWER(TRIM(name)), LOWER(TRIM(unit))`
|
||||||
|
und summiert die skalierten Mengen. Verschiedene Einheiten für dieselbe Zutat bleiben separate
|
||||||
|
Zeilen — typisches Beispiel: `500 g Kartoffeln` (Rezept A) und `1 kg Kartoffeln` (Rezept B)
|
||||||
|
erscheinen als zwei Zeilen. Gewünscht: beides konsolidiert zu `1,5 kg Kartoffeln`.
|
||||||
|
|
||||||
|
## Design-Entscheidungen (durch Brainstorming bestätigt)
|
||||||
|
|
||||||
|
- **Scope**: nur Gewicht (g ↔ kg) und Volumen (ml ↔ l). TL/EL/Tasse/Stück bleiben unverändert.
|
||||||
|
- **Anzeige-Einheit**: Auto-Promote ab ≥ 1000 in Basis-Einheit (500 g + 1 kg → "1,5 kg",
|
||||||
|
200 g + 300 g → "500 g", 400 ml + 0,5 l → "900 ml", 0,5 l + 0,8 l → "1,3 l").
|
||||||
|
- **Formatter**: `formatQuantity` wechselt app-weit auf `toLocaleString('de-DE', …)` →
|
||||||
|
deutsches Komma als Dezimaltrennzeichen überall, kein Tausender-Grouping.
|
||||||
|
- **Check-Stabilität**: der „abgehakt"-State hängt künftig an der Unit-Family (weight / volume /
|
||||||
|
raw-unit), nicht an einer Display-Einheit, damit Hin-und-her-Wechsel zwischen g und kg den
|
||||||
|
Haken nicht verlieren.
|
||||||
|
|
||||||
|
## Sektion 1 — Unit-Konsolidierung
|
||||||
|
|
||||||
|
### Neue Utility: `src/lib/server/shopping/unit-consolidation.ts`
|
||||||
|
|
||||||
|
Zwei reine Funktionen, vollständig getestet per Unit-Tests:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
export type UnitFamily = 'weight' | 'volume' | string;
|
||||||
|
|
||||||
|
const WEIGHT_UNITS = new Set(['g', 'kg']);
|
||||||
|
const VOLUME_UNITS = new Set(['ml', 'l']);
|
||||||
|
|
||||||
|
export function unitFamily(unit: string | null | undefined): UnitFamily {
|
||||||
|
const u = (unit ?? '').trim().toLowerCase();
|
||||||
|
if (WEIGHT_UNITS.has(u)) return 'weight';
|
||||||
|
if (VOLUME_UNITS.has(u)) return 'volume';
|
||||||
|
return u; // leer bleibt leer → eigene Gruppe
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface QuantityInUnit {
|
||||||
|
quantity: number | null;
|
||||||
|
unit: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function consolidate(rows: QuantityInUnit[]): QuantityInUnit {
|
||||||
|
// Gewicht: in g summieren, ≥1000 → kg, sonst g
|
||||||
|
// Volumen: in ml summieren, ≥1000 → l, sonst ml
|
||||||
|
// Andere: quantity einfach summieren, unit vom ersten Eintrag
|
||||||
|
// (alle rows einer Gruppe haben dieselbe Family = denselben unit-string)
|
||||||
|
// quantity=null wird als 0 behandelt (z. B. "etwas Salz" + "1 TL Salz" → "1 TL")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Rundung Promote-Schwelle**: Vergleich passiert auf summierter Basis-Einheit
|
||||||
|
(z. B. 1500 g ≥ 1000 → kg). Ergebnis-Rundung: `Math.round(x * 100) / 100` (max.
|
||||||
|
zwei Nachkommastellen), die finale Display-Formatierung macht `formatQuantity`.
|
||||||
|
|
||||||
|
**Edge-Cases, die expliziter Test-Fall sind**:
|
||||||
|
- `500 g + 1 kg` → `{quantity: 1.5, unit: 'kg'}`
|
||||||
|
- `200 g + 300 g` → `{quantity: 500, unit: 'g'}`
|
||||||
|
- `400 ml + 0.5 l` → `{quantity: 900, unit: 'ml'}`
|
||||||
|
- `0.5 l + 0.8 l` → `{quantity: 1.3, unit: 'l'}`
|
||||||
|
- `2 Bund + 1 Bund` → `{quantity: 3, unit: 'Bund'}` (unchanged family)
|
||||||
|
- `5 Stück + 3 Stück` → `{quantity: 8, unit: 'Stück'}`
|
||||||
|
- `null + 1 TL Salz` (eine Menge unbekannt) → `{quantity: 1, unit: 'TL'}`
|
||||||
|
- `null + null` → `{quantity: null, unit: '<leer oder erster unit>'}`
|
||||||
|
|
||||||
|
### Integration in `listShoppingList()`
|
||||||
|
|
||||||
|
Die existierende SQL-Query liefert schon skalierte Mengen pro Zutat-Zeile
|
||||||
|
(quantity * servings / servings_default). Änderung:
|
||||||
|
|
||||||
|
1. **GROUP BY** der SQL-Query wechselt von `LOWER(TRIM(unit))` auf einen
|
||||||
|
Family-Key (inline per `CASE`):
|
||||||
|
|
||||||
|
```sql
|
||||||
|
GROUP BY LOWER(TRIM(name)),
|
||||||
|
CASE LOWER(TRIM(unit))
|
||||||
|
WHEN 'g' THEN 'weight'
|
||||||
|
WHEN 'kg' THEN 'weight'
|
||||||
|
WHEN 'ml' THEN 'volume'
|
||||||
|
WHEN 'l' THEN 'volume'
|
||||||
|
ELSE LOWER(TRIM(unit))
|
||||||
|
END
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **SUM()** wird nicht mehr blind über quantity gerechnet (500 + 1 ≠ 1500
|
||||||
|
in Basis). Stattdessen liefert SQL pro Gruppe eine Liste der einzelnen
|
||||||
|
`(quantity, unit)`-Paare — z. B. via `json_group_array(json_object('quantity', q, 'unit', u))`.
|
||||||
|
TypeScript ruft dann `consolidate()` pro Zeile auf.
|
||||||
|
|
||||||
|
Alternative: SQL liefert für Familien 'weight' und 'volume' schon die
|
||||||
|
summierten Basis-Werte (via `SUM(q * CASE WHEN unit='kg' THEN 1000 ELSE 1 END)`),
|
||||||
|
für andere Families die unveränderte `SUM(q)`. Spart den json_group_array-Trick,
|
||||||
|
ist aber in SQL hässlich. **Empfehlung**: json_group_array + consolidate in TS —
|
||||||
|
SQL bleibt lesbar, Logik testbar.
|
||||||
|
|
||||||
|
3. Der Rückgabewert `ShoppingListItem` bekommt zwei zusätzliche Felder (wenn
|
||||||
|
nicht schon vorhanden):
|
||||||
|
- `quantity: number | null` (finaler Display-Wert)
|
||||||
|
- `unit: string | null` (finale Display-Einheit)
|
||||||
|
- `unitFamilyKey: string` (für den Check-Lookup clientseitig)
|
||||||
|
|
||||||
|
## Sektion 2 — Check-Key-Stabilität
|
||||||
|
|
||||||
|
Aktuelle Tabelle (aus `013_shopping_list.sql`):
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE shopping_cart_check (
|
||||||
|
name_key TEXT NOT NULL,
|
||||||
|
unit_key TEXT NOT NULL,
|
||||||
|
checked_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||||
|
PRIMARY KEY (name_key, unit_key)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Neue Migration `015_shopping_check_family.sql`
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- Unit-Key wird zum Family-Key: g/kg → 'weight', ml/l → 'volume', sonst lowercased unit.
|
||||||
|
-- Wir migrieren bestehende Einträge damit alte Abhaks gültig bleiben.
|
||||||
|
UPDATE shopping_cart_check SET unit_key = 'weight' WHERE unit_key IN ('g', 'kg');
|
||||||
|
UPDATE shopping_cart_check SET unit_key = 'volume' WHERE unit_key IN ('ml', 'l');
|
||||||
|
|
||||||
|
-- Nach Umetikettierung können Duplikate entstehen (z. B. zwei Einträge mit
|
||||||
|
-- 'weight' für dieselbe Zutat). Deduplizieren: jüngsten behalten.
|
||||||
|
DELETE FROM shopping_cart_check
|
||||||
|
WHERE rowid NOT IN (
|
||||||
|
SELECT MAX(rowid)
|
||||||
|
FROM shopping_cart_check
|
||||||
|
GROUP BY name_key, unit_key
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Code-Änderungen
|
||||||
|
|
||||||
|
- `listShoppingList()`: beim Joinen von `shopping_cart_check` mit den aggregierten
|
||||||
|
Zeilen matched jetzt `(name_key, unit_family_key)` statt `(name_key, unit_key)`.
|
||||||
|
- `toggleCheck(name, unit, checked)`: speichert/löscht Check mit
|
||||||
|
`unitFamily(unit)` statt raw unit.
|
||||||
|
|
||||||
|
## Sektion 3 — Display-Formatter
|
||||||
|
|
||||||
|
### `src/lib/quantity-format.ts`
|
||||||
|
|
||||||
|
```ts
|
||||||
|
export function formatQuantity(q: number | null): string {
|
||||||
|
if (q === null || q === undefined) return '';
|
||||||
|
return q.toLocaleString('de-DE', {
|
||||||
|
maximumFractionDigits: 2,
|
||||||
|
useGrouping: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Kleinere Datei, dieselbe Semantik (max. 2 Dezimalen, ganze Zahlen ohne Dezimal),
|
||||||
|
plus deutsches Dezimalkomma app-weit.
|
||||||
|
|
||||||
|
### Test-Anpassung `tests/unit/quantity-format.test.ts`
|
||||||
|
|
||||||
|
Erwartungswerte von `"0.33"` auf `"0,33"` etc. ziehen. Bestehende 5 Tests müssen mit.
|
||||||
|
|
||||||
|
## Sektion 4 — Tests
|
||||||
|
|
||||||
|
### Neu: `tests/unit/unit-consolidation.test.ts`
|
||||||
|
|
||||||
|
Alle Edge-Cases aus Sektion 1 als expect-Assertions. Plus: `unitFamily`-Table-Tests.
|
||||||
|
|
||||||
|
### Ergänzung: `tests/integration/shopping-repository.test.ts`
|
||||||
|
|
||||||
|
Ein neuer `describe`-Block „konsolidiert über Einheiten":
|
||||||
|
- Rezept A mit `500 g Kartoffeln`, Rezept B mit `1 kg Kartoffeln` → eine Zeile
|
||||||
|
`{name: 'kartoffeln', quantity: 1.5, unit: 'kg'}`.
|
||||||
|
- Analog Volumen mit ml + l.
|
||||||
|
- Gemischte Units wie `2 Bund Petersilie + 1 Bund Petersilie` → eine Zeile `3 Bund`.
|
||||||
|
- `5 Stück Eier + 500 g Eier` → **zwei** Zeilen (verschiedene Families).
|
||||||
|
- Abhaken einer konsolidierten kg-Zeile → nach Entfernung eines Rezepts (jetzt nur
|
||||||
|
noch 800 g) bleibt die Zeile abgehakt (Family = 'weight' stabil).
|
||||||
|
|
||||||
|
### Ergänzung: Migration-Test
|
||||||
|
|
||||||
|
Ein kleiner Test ähnlich dem Stil anderer Migration-Tests im Repo, der verifiziert:
|
||||||
|
- Alt-Einträge `(milch, 'ml')` und `(milch, 'l')` kollabieren zu einem `(milch, 'volume')`.
|
||||||
|
- Unveränderte Einträge wie `(petersilie, 'bund')` bleiben.
|
||||||
|
|
||||||
|
## Was explizit NICHT dabei ist (YAGNI)
|
||||||
|
|
||||||
|
- **Fuzzy-Name-Matching** (Kartoffel vs Kartoffeln, „Zwiebeln, rot" vs „rote Zwiebeln") —
|
||||||
|
ausgeschlossen, hohe Fehlerrate.
|
||||||
|
- **Stück-zu-Gramm-Mappings** (1 Zwiebel ≈ 80 g) — semantisch fraglich, nicht deterministisch.
|
||||||
|
- **TL/EL/Tasse-Konvertierung** — Einkauft man nicht in.
|
||||||
|
- **User-editierbare Custom-Units** — Overkill für eine Familien-PWA.
|
||||||
|
- **UI-Anzeige der zugrundeliegenden Einzelmengen** („1,5 kg — aus 500 g + 1 kg") — wäre
|
||||||
|
nett, aber nicht notwendig für die Hauptfunktion.
|
||||||
|
|
||||||
|
## Phase-Gliederung (für die spätere writing-plans-Phase)
|
||||||
|
|
||||||
|
Eine Phase reicht aus:
|
||||||
|
1. `unit-consolidation.ts` + Unit-Tests
|
||||||
|
2. `quantity-format.ts` auf `toLocaleString` umbauen + Tests updaten
|
||||||
|
3. Migration `015_shopping_check_family.sql`
|
||||||
|
4. `listShoppingList()` integriert Konsolidierung + Check-Join
|
||||||
|
5. `toggleCheck()` auf Family-Key umstellen
|
||||||
|
6. Integration-Tests
|
||||||
|
|
||||||
|
Alles in einer Phase, weil Änderungen eng verzahnt sind (Migration + Repository + Formatter
|
||||||
|
müssen zusammen deployt werden, sonst gibt es UI-Inkonsistenzen).
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
# Hauptseite: "Zuletzt angesehen" Sort + Collapsible Sections
|
||||||
|
|
||||||
|
## Kontext
|
||||||
|
|
||||||
|
Die Hauptseite (`src/routes/+page.svelte`) hat heute drei Sektionen — "Deine
|
||||||
|
Favoriten", "Zuletzt hinzugefügt", "Alle Rezepte" — und vier Sort-Optionen
|
||||||
|
für "Alle Rezepte" (Name, Bewertung, Zuletzt gekocht, Hinzugefügt). Der
|
||||||
|
User möchte:
|
||||||
|
|
||||||
|
1. Eine fünfte Sort-Option "Zuletzt angesehen" für "Alle Rezepte"
|
||||||
|
2. "Deine Favoriten" und "Zuletzt hinzugefügt" auf-/zuklappbar machen
|
||||||
|
|
||||||
|
Beides reduziert visuelle Last und gibt Zugriff auf "kürzlich
|
||||||
|
beschäftigte mich" Rezepte ohne Suche.
|
||||||
|
|
||||||
|
## Design-Entscheidungen (durch Brainstorming bestätigt)
|
||||||
|
|
||||||
|
- **View-Tracking**: zählt sofort beim Laden der Detailseite — kein Threshold
|
||||||
|
- **Storage**: SQLite, pro Profil (konsistent mit Ratings, Cooked, Wishlist)
|
||||||
|
- **Collapsibles**: standardmäßig offen, User-Wahl persistiert pro Device
|
||||||
|
|
||||||
|
## Sektion 1 — Schema & View-Tracking
|
||||||
|
|
||||||
|
### Migration
|
||||||
|
|
||||||
|
Neue Datei `src/lib/server/db/migrations/014_recipe_view.sql`
|
||||||
|
(Numbering: aktuell ist die letzte Migration `013_shopping_list.sql`):
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE recipe_view (
|
||||||
|
profile_id INTEGER NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
|
||||||
|
recipe_id INTEGER NOT NULL REFERENCES recipes(id) ON DELETE CASCADE,
|
||||||
|
last_viewed_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||||
|
PRIMARY KEY (profile_id, recipe_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX idx_recipe_view_recent
|
||||||
|
ON recipe_view(profile_id, last_viewed_at DESC);
|
||||||
|
```
|
||||||
|
|
||||||
|
Idempotent über `INSERT OR REPLACE` — mehrfache Visits ein- und desselben
|
||||||
|
Profils auf dasselbe Rezept führen nur zur Aktualisierung des Timestamps,
|
||||||
|
kein Multi-Insert.
|
||||||
|
|
||||||
|
Cascade auf beide FKs: löscht ein User ein Rezept oder ein Profil, gehen
|
||||||
|
zugehörige Views automatisch mit.
|
||||||
|
|
||||||
|
### API
|
||||||
|
|
||||||
|
Neuer Endpoint `POST /api/recipes/[id]/view`:
|
||||||
|
|
||||||
|
```
|
||||||
|
Request body: { "profile_id": number }
|
||||||
|
Response: 204 No Content
|
||||||
|
Errors:
|
||||||
|
- 400 wenn profile_id fehlt oder kein Number
|
||||||
|
- 404 wenn Recipe nicht existiert (FK-Violation)
|
||||||
|
- 404 wenn Profil nicht existiert (FK-Violation)
|
||||||
|
```
|
||||||
|
|
||||||
|
Implementation: einfache `INSERT OR REPLACE` mit den IDs. `last_viewed_at`
|
||||||
|
nutzt den Default (`datetime('now')`).
|
||||||
|
|
||||||
|
### Client-Hook
|
||||||
|
|
||||||
|
In `src/routes/recipes/[id]/+page.svelte`, in `onMount`:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
if (profileStore.active) {
|
||||||
|
void fetch(`/api/recipes/${recipe.id}/view`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ profile_id: profileStore.active.id })
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Fire-and-forget, kein UI-Block, kein Error-Handling — wenn der Beacon
|
||||||
|
fehlschlägt, ist es kein User-Visible-Bug, das nächste View korrigiert
|
||||||
|
es.
|
||||||
|
|
||||||
|
## Sektion 2 — Sort "Zuletzt angesehen"
|
||||||
|
|
||||||
|
### Page
|
||||||
|
|
||||||
|
In `src/routes/+page.svelte`:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type AllSort = 'name' | 'rating' | 'cooked' | 'created' | 'viewed';
|
||||||
|
const ALL_SORTS = [
|
||||||
|
...,
|
||||||
|
{ value: 'viewed', label: 'Zuletzt angesehen' }
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
### API
|
||||||
|
|
||||||
|
`GET /api/recipes/all` bekommt einen optionalen `profile_id`-Query-Param.
|
||||||
|
Der Endpoint reicht ihn an `listAllRecipesPaginated` durch.
|
||||||
|
|
||||||
|
### DB-Layer
|
||||||
|
|
||||||
|
`listAllRecipesPaginated` in `src/lib/server/recipes/search-local.ts`
|
||||||
|
bekommt einen optionalen `profileId: number | null`-Parameter. Wenn
|
||||||
|
`sort === 'viewed'` UND `profileId !== null`:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
SELECT r.*, ...
|
||||||
|
FROM recipes r
|
||||||
|
LEFT JOIN recipe_view v
|
||||||
|
ON v.recipe_id = r.id AND v.profile_id = :profileId
|
||||||
|
ORDER BY v.last_viewed_at DESC NULLS LAST,
|
||||||
|
r.title COLLATE NOCASE ASC
|
||||||
|
LIMIT :limit OFFSET :offset
|
||||||
|
```
|
||||||
|
|
||||||
|
Bei `sort === 'viewed'` ohne `profileId`: fällt auf alphabetische
|
||||||
|
Sortierung zurück (kein Crash, sinnvolles Default-Verhalten).
|
||||||
|
|
||||||
|
### Reactive Refetch bei Profile-Switch
|
||||||
|
|
||||||
|
Auf Home-Page-Ebene: ein `$effect` der auf `profileStore.activeId` lauscht
|
||||||
|
und — wenn `allSort === 'viewed'` — `setAllSort('viewed')` retriggert
|
||||||
|
(forciert Refetch mit neuem profile_id). Sonst (anderer Sort) keine
|
||||||
|
Aktion, weil andere Sorts nicht profilabhängig sind.
|
||||||
|
|
||||||
|
### Snapshot-Kompatibilität
|
||||||
|
|
||||||
|
Der existierende `rehydrateAll(sort, count, exhausted)` in `+page.svelte`
|
||||||
|
muss `profile_id` mitschicken, sonst zeigt der Back-Nav für sort='viewed'
|
||||||
|
einen anderen Inhalt als vor dem Forward-Klick. Das gleiche gilt für
|
||||||
|
`loadAllMore` und `setAllSort`.
|
||||||
|
|
||||||
|
## Sektion 3 — Auf-/Zuklappbare Sektionen
|
||||||
|
|
||||||
|
### State
|
||||||
|
|
||||||
|
In `src/routes/+page.svelte`:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type CollapseKey = 'favorites' | 'recent';
|
||||||
|
let collapsed = $state<Record<CollapseKey, boolean>>({
|
||||||
|
favorites: false,
|
||||||
|
recent: false
|
||||||
|
});
|
||||||
|
|
||||||
|
const STORAGE_KEY = 'kochwas.collapsed.sections';
|
||||||
|
|
||||||
|
function toggle(key: CollapseKey) {
|
||||||
|
collapsed[key] = !collapsed[key];
|
||||||
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(collapsed));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In `onMount`: aus localStorage parsen, fehlerhafte JSON ignorieren
|
||||||
|
(default-state behalten).
|
||||||
|
|
||||||
|
### Markup
|
||||||
|
|
||||||
|
Pro Sektion:
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<section class="listing">
|
||||||
|
<button
|
||||||
|
class="section-head"
|
||||||
|
onclick={() => toggle('favorites')}
|
||||||
|
aria-expanded={!collapsed.favorites}
|
||||||
|
>
|
||||||
|
<ChevronDown size={18} class:rotated={collapsed.favorites} />
|
||||||
|
<h2>Deine Favoriten</h2>
|
||||||
|
<span class="count">{favorites.length}</span>
|
||||||
|
</button>
|
||||||
|
{#if !collapsed.favorites}
|
||||||
|
<div transition:slide={{ duration: 180 }}>
|
||||||
|
<ul class="cards">…</ul>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</section>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Visual / CSS
|
||||||
|
|
||||||
|
- Header `<button>`: transparenter Border, full-width, `display: flex`,
|
||||||
|
`align-items: center`, `gap: 0.5rem`, `min-height: 44px` (Tap-Target)
|
||||||
|
- Chevron-Icon (lucide-svelte `ChevronDown`): rotiert auf
|
||||||
|
`transform: rotate(-90deg)` wenn `.rotated`
|
||||||
|
- Count-Pill rechts: kleiner grauer Text, hilft zu sehen wie viel hinter
|
||||||
|
einer zugeklappten Sektion steckt
|
||||||
|
- Hover: leichter Hintergrund (`#f4f8f5`, wie andere interaktive Elemente)
|
||||||
|
- Animation: `svelte/transition`'s `slide`, ~180 ms
|
||||||
|
|
||||||
|
### Persistenz-Format
|
||||||
|
|
||||||
|
```json
|
||||||
|
{ "favorites": false, "recent": true }
|
||||||
|
```
|
||||||
|
|
||||||
|
Truthy = collapsed. Default-Zustand wenn key fehlt: beide false.
|
||||||
|
|
||||||
|
### "Alle Rezepte" bleibt nicht-collapsible
|
||||||
|
|
||||||
|
Hauptliste, immer sichtbar — User würde das Scrollen verlieren.
|
||||||
|
|
||||||
|
## Test-Strategie
|
||||||
|
|
||||||
|
### Schema/Migration
|
||||||
|
|
||||||
|
- Migrations-Test (existierendes Pattern in `tests/integration`): nach
|
||||||
|
`applyMigrations` muss `recipe_view` existieren mit erwarteten
|
||||||
|
Spalten
|
||||||
|
|
||||||
|
### View-Endpoint
|
||||||
|
|
||||||
|
- `POST /api/recipes/[id]/view` Integration-Test:
|
||||||
|
- Erstes POST → Row mit `last_viewed_at` ungefähr `now`
|
||||||
|
- Zweites POST → gleiche Row, `last_viewed_at` aktualisiert
|
||||||
|
- POST mit ungültiger profile_id → 404
|
||||||
|
- POST mit ungültiger recipe_id → 404
|
||||||
|
- POST ohne profile_id im Body → 400
|
||||||
|
|
||||||
|
### Sort-Logik
|
||||||
|
|
||||||
|
- Unit-Test für `listAllRecipesPaginated(db, 'viewed', limit, offset, profileId)`:
|
||||||
|
- Mit Views-Daten: angesehene Rezepte zuerst (DESC nach `last_viewed_at`),
|
||||||
|
Rest alphabetisch
|
||||||
|
- Ohne profileId: fallback auf alphabetisch
|
||||||
|
- Mit profileId aber ohne Views: alle als NULL → alphabetisch
|
||||||
|
|
||||||
|
### Collapsibles (manuell oder unit)
|
||||||
|
|
||||||
|
- localStorage-Persistenz: Toggle, Reload, gleicher State
|
||||||
|
- Default-State wenn localStorage leer/corrupt: beide offen
|
||||||
|
- Ein Unit-Test für eine reine Helper-Funktion (parse/serialize), Markup
|
||||||
|
ist Snapshot-mässig nicht so wertvoll testbar
|
||||||
|
|
||||||
|
## Reihenfolge der Umsetzung
|
||||||
|
|
||||||
|
1. Migration + DB-Layer + Sort-Query (`search-local.ts`-Erweiterung)
|
||||||
|
2. View-Endpoint (`POST /api/recipes/[id]/view`) + Client-Beacon in
|
||||||
|
`recipes/[id]/+page.svelte`
|
||||||
|
3. Sort-Option in `+page.svelte` UI + API-Param weiterreichen +
|
||||||
|
profile_id in `loadAllMore`/`rehydrateAll`/`setAllSort` durchreichen
|
||||||
|
4. Collapsible-Pattern in `+page.svelte` für Favoriten und Recent
|
||||||
|
|
||||||
|
Jede Phase atomar committen + pushen.
|
||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "kochwas",
|
"name": "kochwas",
|
||||||
"version": "1.3.0",
|
"version": "1.4.2",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "kochwas",
|
"name": "kochwas",
|
||||||
"version": "1.3.0",
|
"version": "1.4.2",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@google/generative-ai": "^0.24.1",
|
"@google/generative-ai": "^0.24.1",
|
||||||
"@types/archiver": "^7.0.0",
|
"@types/archiver": "^7.0.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kochwas",
|
"name": "kochwas",
|
||||||
"version": "1.3.0",
|
"version": "1.4.2",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
export function formatQuantity(q: number | null): string {
|
export function formatQuantity(q: number | null): string {
|
||||||
if (q === null || q === undefined) return '';
|
if (q === null || q === undefined) return '';
|
||||||
const rounded = Math.round(q);
|
return q.toLocaleString('de-DE', {
|
||||||
if (Math.abs(q - rounded) < 0.01) return String(rounded);
|
maximumFractionDigits: 2,
|
||||||
// auf max. 2 Nachkommastellen, trailing Nullen raus
|
useGrouping: false
|
||||||
return q.toFixed(2).replace(/\.?0+$/, '');
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/lib/server/db/migrations/014_recipe_view.sql
Normal file
10
src/lib/server/db/migrations/014_recipe_view.sql
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
-- Merkt je Profil, wann ein Rezept zuletzt angesehen wurde.
|
||||||
|
-- Dient als Basis fuer "Zuletzt gesehen"-Sortierung auf der Startseite.
|
||||||
|
CREATE TABLE recipe_view (
|
||||||
|
profile_id INTEGER NOT NULL REFERENCES profile(id) ON DELETE CASCADE,
|
||||||
|
recipe_id INTEGER NOT NULL REFERENCES recipe(id) ON DELETE CASCADE,
|
||||||
|
last_viewed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (profile_id, recipe_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX idx_recipe_view_recent
|
||||||
|
ON recipe_view (profile_id, last_viewed_at DESC);
|
||||||
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
|
||||||
|
);
|
||||||
@@ -88,18 +88,44 @@ export function listAllRecipes(db: Database.Database): SearchHit[] {
|
|||||||
.all() as SearchHit[];
|
.all() as SearchHit[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AllRecipesSort = 'name' | 'rating' | 'cooked' | 'created';
|
export type AllRecipesSort = 'name' | 'rating' | 'cooked' | 'created' | 'viewed';
|
||||||
|
|
||||||
export function listAllRecipesPaginated(
|
export function listAllRecipesPaginated(
|
||||||
db: Database.Database,
|
db: Database.Database,
|
||||||
sort: AllRecipesSort,
|
sort: AllRecipesSort,
|
||||||
limit: number,
|
limit: number,
|
||||||
offset: number
|
offset: number,
|
||||||
|
profileId: number | null = null
|
||||||
): SearchHit[] {
|
): SearchHit[] {
|
||||||
|
// 'viewed' branch needs a JOIN against recipe_view — diverges from the
|
||||||
|
// simpler ORDER-BY-only path. We keep it in a separate prepare for
|
||||||
|
// clarity. Without profileId, fall back to alphabetical so the
|
||||||
|
// sort-chip still produces a sensible list.
|
||||||
|
if (sort === 'viewed' && profileId !== null) {
|
||||||
|
return db
|
||||||
|
.prepare(
|
||||||
|
`SELECT r.id,
|
||||||
|
r.title,
|
||||||
|
r.description,
|
||||||
|
r.image_path,
|
||||||
|
r.source_domain,
|
||||||
|
(SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) AS avg_stars,
|
||||||
|
(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) AS last_cooked_at
|
||||||
|
FROM recipe r
|
||||||
|
LEFT JOIN recipe_view v
|
||||||
|
ON v.recipe_id = r.id AND v.profile_id = ?
|
||||||
|
ORDER BY CASE WHEN v.last_viewed_at IS NULL THEN 1 ELSE 0 END,
|
||||||
|
v.last_viewed_at DESC,
|
||||||
|
r.title COLLATE NOCASE ASC
|
||||||
|
LIMIT ? OFFSET ?`
|
||||||
|
)
|
||||||
|
.all(profileId, limit, offset) as SearchHit[];
|
||||||
|
}
|
||||||
|
|
||||||
// NULLS-last-Emulation per CASE-Expression — SQLite unterstützt NULLS LAST
|
// NULLS-last-Emulation per CASE-Expression — SQLite unterstützt NULLS LAST
|
||||||
// zwar seit 3.30, aber der Pi könnte auf einer älteren Version laufen und
|
// zwar seit 3.30, aber der Pi könnte auf einer älteren Version laufen und
|
||||||
// CASE ist überall zuverlässig.
|
// CASE ist überall zuverlässig.
|
||||||
const orderBy: Record<AllRecipesSort, string> = {
|
const orderBy: Record<Exclude<AllRecipesSort, 'viewed'>, string> = {
|
||||||
name: 'r.title COLLATE NOCASE ASC',
|
name: 'r.title COLLATE NOCASE ASC',
|
||||||
rating:
|
rating:
|
||||||
'CASE WHEN (SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) IS NULL THEN 1 ELSE 0 END, ' +
|
'CASE WHEN (SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) IS NULL THEN 1 ELSE 0 END, ' +
|
||||||
@@ -109,6 +135,8 @@ export function listAllRecipesPaginated(
|
|||||||
'(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) DESC, r.title COLLATE NOCASE ASC',
|
'(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) DESC, r.title COLLATE NOCASE ASC',
|
||||||
created: 'r.created_at DESC, r.id DESC'
|
created: 'r.created_at DESC, r.id DESC'
|
||||||
};
|
};
|
||||||
|
// Without profile, 'viewed' degrades to alphabetical.
|
||||||
|
const effectiveSort = sort === 'viewed' ? 'name' : sort;
|
||||||
return db
|
return db
|
||||||
.prepare(
|
.prepare(
|
||||||
`SELECT r.id,
|
`SELECT r.id,
|
||||||
@@ -119,7 +147,7 @@ export function listAllRecipesPaginated(
|
|||||||
(SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) AS avg_stars,
|
(SELECT AVG(stars) FROM rating WHERE recipe_id = r.id) AS avg_stars,
|
||||||
(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) AS last_cooked_at
|
(SELECT MAX(cooked_at) FROM cooking_log WHERE recipe_id = r.id) AS last_cooked_at
|
||||||
FROM recipe r
|
FROM recipe r
|
||||||
ORDER BY ${orderBy[sort]}
|
ORDER BY ${orderBy[effectiveSort]}
|
||||||
LIMIT ? OFFSET ?`
|
LIMIT ? OFFSET ?`
|
||||||
)
|
)
|
||||||
.all(limit, offset) as SearchHit[];
|
.all(limit, offset) as SearchHit[];
|
||||||
|
|||||||
37
src/lib/server/recipes/views.ts
Normal file
37
src/lib/server/recipes/views.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import type Database from 'better-sqlite3';
|
||||||
|
|
||||||
|
export function recordView(
|
||||||
|
db: Database.Database,
|
||||||
|
profileId: number,
|
||||||
|
recipeId: number
|
||||||
|
): void {
|
||||||
|
// ON CONFLICT DO UPDATE bumps only the timestamp field — avoids the
|
||||||
|
// DELETE+INSERT that INSERT OR REPLACE performs under the hood, which would
|
||||||
|
// silently cascade-delete any future FK-referencing children.
|
||||||
|
db.prepare(
|
||||||
|
`INSERT INTO recipe_view (profile_id, recipe_id)
|
||||||
|
VALUES (?, ?)
|
||||||
|
ON CONFLICT(profile_id, recipe_id) DO UPDATE
|
||||||
|
SET last_viewed_at = CURRENT_TIMESTAMP`
|
||||||
|
).run(profileId, recipeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ViewRow = {
|
||||||
|
profile_id: number;
|
||||||
|
recipe_id: number;
|
||||||
|
last_viewed_at: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function listViews(
|
||||||
|
db: Database.Database,
|
||||||
|
profileId: number
|
||||||
|
): ViewRow[] {
|
||||||
|
return db
|
||||||
|
.prepare(
|
||||||
|
`SELECT profile_id, recipe_id, last_viewed_at
|
||||||
|
FROM recipe_view
|
||||||
|
WHERE profile_id = ?
|
||||||
|
ORDER BY last_viewed_at DESC`
|
||||||
|
)
|
||||||
|
.all(profileId) as ViewRow[];
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import type Database from 'better-sqlite3';
|
import type Database from 'better-sqlite3';
|
||||||
|
import { consolidate, unitFamily } from '../unit-consolidation';
|
||||||
|
|
||||||
// Fallback when a recipe has no servings_default set — matches the default
|
// Fallback when a recipe has no servings_default set — matches the default
|
||||||
// used by RecipeEditor's "new recipe" template.
|
// used by RecipeEditor's "new recipe" template.
|
||||||
@@ -80,7 +81,18 @@ export function listShoppingList(
|
|||||||
)
|
)
|
||||||
.all() as ShoppingCartRecipe[];
|
.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(
|
.prepare(
|
||||||
`SELECT
|
`SELECT
|
||||||
LOWER(TRIM(i.name)) AS name_key,
|
LOWER(TRIM(i.name)) AS name_key,
|
||||||
@@ -88,19 +100,61 @@ export function listShoppingList(
|
|||||||
MIN(i.name) AS display_name,
|
MIN(i.name) AS display_name,
|
||||||
MIN(i.unit) AS display_unit,
|
MIN(i.unit) AS display_unit,
|
||||||
SUM(i.quantity * cr.servings * 1.0 / NULLIF(COALESCE(r.servings_default, cr.servings), 0)) AS total_quantity,
|
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,
|
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
|
|
||||||
FROM shopping_cart_recipe cr
|
FROM shopping_cart_recipe cr
|
||||||
JOIN recipe r ON r.id = cr.recipe_id
|
JOIN recipe r ON r.id = cr.recipe_id
|
||||||
JOIN ingredient i ON i.recipe_id = r.id
|
JOIN ingredient i ON i.recipe_id = r.id
|
||||||
GROUP BY name_key, unit_key
|
GROUP BY name_key, unit_key`
|
||||||
ORDER BY checked ASC, display_name COLLATE NOCASE`
|
|
||||||
)
|
)
|
||||||
.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);
|
const uncheckedCount = rows.reduce((n, r) => n + (r.checked ? 0 : 1), 0);
|
||||||
return { recipes, rows, uncheckedCount };
|
return { recipes, rows, uncheckedCount };
|
||||||
@@ -127,23 +181,33 @@ export function toggleCheck(
|
|||||||
|
|
||||||
export function clearCheckedItems(db: Database.Database): void {
|
export function clearCheckedItems(db: Database.Database): void {
|
||||||
const tx = db.transaction(() => {
|
const tx = db.transaction(() => {
|
||||||
// Alle aggregierten Zeilen mit checked-Status holen, pro recipe_id gruppieren
|
// Rohe (name, unit)-Zeilen holen, checked-Status per Family-Key-Lookup
|
||||||
// und Rezepte finden, deren Zeilen ALLE abgehakt sind.
|
// in JS entscheiden. Rezepte mit ALLEN Zeilen abgehakt werden raus.
|
||||||
const allRows = db
|
const allRowsRaw = db
|
||||||
.prepare(
|
.prepare(
|
||||||
`SELECT
|
`SELECT
|
||||||
cr.recipe_id,
|
cr.recipe_id,
|
||||||
LOWER(TRIM(i.name)) AS name_key,
|
LOWER(TRIM(i.name)) AS name_key,
|
||||||
LOWER(TRIM(COALESCE(i.unit, ''))) AS unit_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
|
|
||||||
FROM shopping_cart_recipe cr
|
FROM shopping_cart_recipe cr
|
||||||
JOIN ingredient i ON i.recipe_id = cr.recipe_id`
|
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 }>();
|
const perRecipe = new Map<number, { total: number; checked: number }>();
|
||||||
for (const r of allRows) {
|
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);
|
db.prepare('DELETE FROM shopping_cart_recipe WHERE recipe_id = ?').run(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Orphan-Checks raeumen: alle Check-Keys, die jetzt in KEINEM Cart-Rezept
|
// Orphan-Checks raeumen: Active-Keys nach (name_key, unitFamily(raw-unit))
|
||||||
// mehr vorkommen.
|
// bauen, damit Checks mit family-key korrekt gematcht werden.
|
||||||
const activeKeys = db
|
const activeRaw = db
|
||||||
.prepare(
|
.prepare(
|
||||||
`SELECT DISTINCT
|
`SELECT DISTINCT
|
||||||
LOWER(TRIM(i.name)) AS name_key,
|
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`
|
JOIN ingredient i ON i.recipe_id = cr.recipe_id`
|
||||||
)
|
)
|
||||||
.all() as { name_key: string; unit_key: string }[];
|
.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
|
const allChecks = db
|
||||||
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
|
.prepare('SELECT name_key, unit_key FROM shopping_cart_check')
|
||||||
.all() as { name_key: string; unit_key: string }[];
|
.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 = ?'
|
'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?'
|
||||||
);
|
);
|
||||||
for (const c of allChecks) {
|
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);
|
del.run(c.name_key, c.unit_key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
66
src/lib/server/unit-consolidation.ts
Normal file
66
src/lib/server/unit-consolidation.ts
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
export type UnitFamily = 'weight' | 'volume' | string;
|
||||||
|
|
||||||
|
const WEIGHT_UNITS = new Set(['g', 'kg']);
|
||||||
|
const VOLUME_UNITS = new Set(['ml', 'l']);
|
||||||
|
|
||||||
|
export function unitFamily(unit: string | null | undefined): UnitFamily {
|
||||||
|
const u = (unit ?? '').trim().toLowerCase();
|
||||||
|
if (WEIGHT_UNITS.has(u)) return 'weight';
|
||||||
|
if (VOLUME_UNITS.has(u)) return 'volume';
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface QuantityInUnit {
|
||||||
|
quantity: number | null;
|
||||||
|
unit: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function round2(n: number): number {
|
||||||
|
return Math.round(n * 100) / 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konsolidiert mehrere {quantity, unit}-Eintraege derselben Unit-Family
|
||||||
|
* zu einer gemeinsamen Menge + Display-Unit.
|
||||||
|
*
|
||||||
|
* - Gewicht (g, kg): summiert in g, promoted bei >=1000 g auf kg.
|
||||||
|
* - Volumen (ml, l): summiert in ml, promoted bei >=1000 ml auf l.
|
||||||
|
* - Andere: summiert quantity ohne Umrechnung, Display-Unit vom ersten
|
||||||
|
* Eintrag.
|
||||||
|
*
|
||||||
|
* quantity=null wird als 0 behandelt. Wenn ALLE quantities null sind,
|
||||||
|
* ist die Gesamtmenge ebenfalls null.
|
||||||
|
*/
|
||||||
|
export function consolidate(rows: QuantityInUnit[]): QuantityInUnit {
|
||||||
|
if (rows.length === 0) return { quantity: null, unit: null };
|
||||||
|
|
||||||
|
const family = unitFamily(rows[0].unit);
|
||||||
|
const firstUnit = rows[0].unit;
|
||||||
|
|
||||||
|
const allNull = rows.every((r) => r.quantity === null);
|
||||||
|
|
||||||
|
if (family === 'weight') {
|
||||||
|
if (allNull) return { quantity: null, unit: firstUnit };
|
||||||
|
const grams = rows.reduce((sum, r) => {
|
||||||
|
const q = r.quantity ?? 0;
|
||||||
|
return sum + (r.unit?.toLowerCase().trim() === 'kg' ? q * 1000 : q);
|
||||||
|
}, 0);
|
||||||
|
if (grams >= 1000) return { quantity: round2(grams / 1000), unit: 'kg' };
|
||||||
|
return { quantity: round2(grams), unit: 'g' };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (family === 'volume') {
|
||||||
|
if (allNull) return { quantity: null, unit: firstUnit };
|
||||||
|
const ml = rows.reduce((sum, r) => {
|
||||||
|
const q = r.quantity ?? 0;
|
||||||
|
return sum + (r.unit?.toLowerCase().trim() === 'l' ? q * 1000 : q);
|
||||||
|
}, 0);
|
||||||
|
if (ml >= 1000) return { quantity: round2(ml / 1000), unit: 'l' };
|
||||||
|
return { quantity: round2(ml), unit: 'ml' };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-family: summiere quantity direkt
|
||||||
|
if (allNull) return { quantity: null, unit: firstUnit };
|
||||||
|
const sum = rows.reduce((acc, r) => acc + (r.quantity ?? 0), 0);
|
||||||
|
return { quantity: round2(sum), unit: firstUnit };
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount, tick } from 'svelte';
|
import { onMount, tick, untrack } from 'svelte';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { CookingPot, X } from 'lucide-svelte';
|
import { CookingPot, X, ChevronDown } from 'lucide-svelte';
|
||||||
|
import { slide } from 'svelte/transition';
|
||||||
import type { Snapshot } from './$types';
|
import type { Snapshot } from './$types';
|
||||||
import type { SearchHit } from '$lib/server/recipes/search-local';
|
import type { SearchHit } from '$lib/server/recipes/search-local';
|
||||||
import { randomQuote } from '$lib/quotes';
|
import { randomQuote } from '$lib/quotes';
|
||||||
@@ -27,13 +28,20 @@
|
|||||||
let favorites = $state<SearchHit[]>([]);
|
let favorites = $state<SearchHit[]>([]);
|
||||||
|
|
||||||
const ALL_PAGE = 10;
|
const ALL_PAGE = 10;
|
||||||
type AllSort = 'name' | 'rating' | 'cooked' | 'created';
|
type AllSort = 'name' | 'rating' | 'cooked' | 'created' | 'viewed';
|
||||||
const ALL_SORTS: { value: AllSort; label: string }[] = [
|
const ALL_SORTS: { value: AllSort; label: string }[] = [
|
||||||
{ value: 'name', label: 'Name' },
|
{ value: 'name', label: 'Name' },
|
||||||
{ value: 'rating', label: 'Bewertung' },
|
{ value: 'rating', label: 'Bewertung' },
|
||||||
{ value: 'cooked', label: 'Zuletzt gekocht' },
|
{ value: 'cooked', label: 'Zuletzt gekocht' },
|
||||||
{ value: 'created', label: 'Hinzugefügt' }
|
{ value: 'created', label: 'Hinzugefügt' },
|
||||||
|
{ value: 'viewed', label: 'Zuletzt angesehen' }
|
||||||
];
|
];
|
||||||
|
function buildAllUrl(sort: AllSort, limit: number, offset: number): string {
|
||||||
|
const profileId = profileStore.active?.id;
|
||||||
|
const profilePart = profileId ? `&profile_id=${profileId}` : '';
|
||||||
|
return `/api/recipes/all?sort=${sort}&limit=${limit}&offset=${offset}${profilePart}`;
|
||||||
|
}
|
||||||
|
|
||||||
let allRecipes = $state<SearchHit[]>([]);
|
let allRecipes = $state<SearchHit[]>([]);
|
||||||
let allSort = $state<AllSort>('name');
|
let allSort = $state<AllSort>('name');
|
||||||
let allExhausted = $state(false);
|
let allExhausted = $state(false);
|
||||||
@@ -42,6 +50,20 @@
|
|||||||
let allChips: HTMLElement | undefined = $state();
|
let allChips: HTMLElement | undefined = $state();
|
||||||
let allObserver: IntersectionObserver | null = null;
|
let allObserver: IntersectionObserver | null = null;
|
||||||
|
|
||||||
|
type CollapseKey = 'favorites' | 'recent';
|
||||||
|
const COLLAPSE_STORAGE_KEY = 'kochwas.collapsed.sections';
|
||||||
|
let collapsed = $state<Record<CollapseKey, boolean>>({
|
||||||
|
favorites: false,
|
||||||
|
recent: false
|
||||||
|
});
|
||||||
|
|
||||||
|
function toggleCollapsed(key: CollapseKey) {
|
||||||
|
collapsed[key] = !collapsed[key];
|
||||||
|
if (typeof localStorage !== 'undefined') {
|
||||||
|
localStorage.setItem(COLLAPSE_STORAGE_KEY, JSON.stringify(collapsed));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Snapshot persists across history navigation. We capture not only the
|
// Snapshot persists across history navigation. We capture not only the
|
||||||
// search store, but also the pagination depth ("user had loaded 60
|
// search store, but also the pagination depth ("user had loaded 60
|
||||||
// recipes via infinite scroll") so on back-nav we can re-hydrate the
|
// recipes via infinite scroll") so on back-nav we can re-hydrate the
|
||||||
@@ -79,7 +101,7 @@
|
|||||||
async function rehydrateAll(sort: AllSort, count: number, exhausted: boolean) {
|
async function rehydrateAll(sort: AllSort, count: number, exhausted: boolean) {
|
||||||
allLoading = true;
|
allLoading = true;
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/recipes/all?sort=${sort}&limit=${count}&offset=0`);
|
const res = await fetch(buildAllUrl(sort, count, 0));
|
||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
const body = await res.json();
|
const body = await res.json();
|
||||||
const hits = body.hits as SearchHit[];
|
const hits = body.hits as SearchHit[];
|
||||||
@@ -100,9 +122,7 @@
|
|||||||
if (allLoading || allExhausted) return;
|
if (allLoading || allExhausted) return;
|
||||||
allLoading = true;
|
allLoading = true;
|
||||||
try {
|
try {
|
||||||
const res = await fetch(
|
const res = await fetch(buildAllUrl(allSort, ALL_PAGE, allRecipes.length));
|
||||||
`/api/recipes/all?sort=${allSort}&limit=${ALL_PAGE}&offset=${allRecipes.length}`
|
|
||||||
);
|
|
||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
const body = await res.json();
|
const body = await res.json();
|
||||||
const more = body.hits as SearchHit[];
|
const more = body.hits as SearchHit[];
|
||||||
@@ -126,9 +146,7 @@
|
|||||||
const chipsBefore = allChips?.getBoundingClientRect().top ?? 0;
|
const chipsBefore = allChips?.getBoundingClientRect().top ?? 0;
|
||||||
allLoading = true;
|
allLoading = true;
|
||||||
try {
|
try {
|
||||||
const res = await fetch(
|
const res = await fetch(buildAllUrl(next, ALL_PAGE, 0));
|
||||||
`/api/recipes/all?sort=${next}&limit=${ALL_PAGE}&offset=0`
|
|
||||||
);
|
|
||||||
if (!res.ok) return;
|
if (!res.ok) return;
|
||||||
const body = await res.json();
|
const body = await res.json();
|
||||||
const hits = body.hits as SearchHit[];
|
const hits = body.hits as SearchHit[];
|
||||||
@@ -164,7 +182,7 @@
|
|||||||
void loadRecent();
|
void loadRecent();
|
||||||
void searchFilterStore.load();
|
void searchFilterStore.load();
|
||||||
const saved = localStorage.getItem('kochwas.allSort');
|
const saved = localStorage.getItem('kochwas.allSort');
|
||||||
if (saved && ['name', 'rating', 'cooked', 'created'].includes(saved)) {
|
if (saved && ['name', 'rating', 'cooked', 'created', 'viewed'].includes(saved)) {
|
||||||
allSort = saved as AllSort;
|
allSort = saved as AllSort;
|
||||||
}
|
}
|
||||||
// Fresh-mount: kick off the initial 10. On back-nav, snapshot.restore
|
// Fresh-mount: kick off the initial 10. On back-nav, snapshot.restore
|
||||||
@@ -172,6 +190,16 @@
|
|||||||
// this; if loadAllMore lands first, rehydrateAll's larger result
|
// this; if loadAllMore lands first, rehydrateAll's larger result
|
||||||
// simply overwrites allRecipes once it resolves.
|
// simply overwrites allRecipes once it resolves.
|
||||||
void loadAllMore();
|
void loadAllMore();
|
||||||
|
const rawCollapsed = localStorage.getItem(COLLAPSE_STORAGE_KEY);
|
||||||
|
if (rawCollapsed) {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(rawCollapsed) as Partial<Record<CollapseKey, boolean>>;
|
||||||
|
if (typeof parsed.favorites === 'boolean') collapsed.favorites = parsed.favorites;
|
||||||
|
if (typeof parsed.recent === 'boolean') collapsed.recent = parsed.recent;
|
||||||
|
} catch {
|
||||||
|
// Corrupt JSON — keep defaults (both open).
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// IntersectionObserver an den Sentinel hängen — wenn sichtbar, nachladen.
|
// IntersectionObserver an den Sentinel hängen — wenn sichtbar, nachladen.
|
||||||
@@ -203,6 +231,37 @@
|
|||||||
store.reSearch();
|
store.reSearch();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 'viewed' sort depends on the active profile. When the user switches
|
||||||
|
// profiles, refetch with the new profile_id so the list reflects what
|
||||||
|
// the *current* profile has viewed. Other sorts are profile-agnostic
|
||||||
|
// and don't need this.
|
||||||
|
//
|
||||||
|
// Only `profileStore.active` must be a tracked dep. `allSort` /
|
||||||
|
// `allLoading` are read inside untrack: otherwise the `allLoading = false`
|
||||||
|
// write in the fetch-finally would re-trigger the effect and start the
|
||||||
|
// next fetch → endless loop.
|
||||||
|
$effect(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
|
profileStore.active;
|
||||||
|
untrack(() => {
|
||||||
|
if (allSort !== 'viewed') return;
|
||||||
|
if (allLoading) return;
|
||||||
|
void (async () => {
|
||||||
|
allLoading = true;
|
||||||
|
try {
|
||||||
|
const res = await fetch(buildAllUrl('viewed', ALL_PAGE, 0));
|
||||||
|
if (!res.ok) return;
|
||||||
|
const body = await res.json();
|
||||||
|
const hits = body.hits as SearchHit[];
|
||||||
|
allRecipes = hits;
|
||||||
|
allExhausted = hits.length < ALL_PAGE;
|
||||||
|
} finally {
|
||||||
|
allLoading = false;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Sync current query back into the URL as ?q=... via replaceState,
|
// Sync current query back into the URL as ?q=... via replaceState,
|
||||||
// without spamming the history stack. Pushing a new entry happens only
|
// without spamming the history stack. Pushing a new entry happens only
|
||||||
// when the user clicks a result or otherwise navigates away.
|
// when the user clicks a result or otherwise navigates away.
|
||||||
@@ -346,57 +405,91 @@
|
|||||||
{:else}
|
{:else}
|
||||||
{#if profileStore.active && favorites.length > 0}
|
{#if profileStore.active && favorites.length > 0}
|
||||||
<section class="listing">
|
<section class="listing">
|
||||||
<h2>Deine Favoriten</h2>
|
<button
|
||||||
<ul class="cards">
|
type="button"
|
||||||
{#each favorites as r (r.id)}
|
class="section-head"
|
||||||
<li class="card-wrap">
|
onclick={() => toggleCollapsed('favorites')}
|
||||||
<a href={`/recipes/${r.id}`} class="card">
|
aria-expanded={!collapsed.favorites}
|
||||||
{#if r.image_path}
|
>
|
||||||
<img src={`/images/${r.image_path}`} alt="" loading="lazy" />
|
<ChevronDown
|
||||||
{:else}
|
size={18}
|
||||||
<div class="placeholder"><CookingPot size={36} /></div>
|
strokeWidth={2.2}
|
||||||
{/if}
|
class={collapsed.favorites ? 'chev rotated' : 'chev'}
|
||||||
<div class="card-body">
|
/>
|
||||||
<div class="title">{r.title}</div>
|
<h2>Deine Favoriten</h2>
|
||||||
{#if r.source_domain}
|
<span class="count">{favorites.length}</span>
|
||||||
<div class="domain">{r.source_domain}</div>
|
</button>
|
||||||
{/if}
|
{#if !collapsed.favorites}
|
||||||
</div>
|
<div transition:slide={{ duration: 180 }}>
|
||||||
</a>
|
<ul class="cards">
|
||||||
</li>
|
{#each favorites as r (r.id)}
|
||||||
{/each}
|
<li class="card-wrap">
|
||||||
</ul>
|
<a href={`/recipes/${r.id}`} class="card">
|
||||||
|
{#if r.image_path}
|
||||||
|
<img src={`/images/${r.image_path}`} alt="" loading="lazy" />
|
||||||
|
{:else}
|
||||||
|
<div class="placeholder"><CookingPot size={36} /></div>
|
||||||
|
{/if}
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="title">{r.title}</div>
|
||||||
|
{#if r.source_domain}
|
||||||
|
<div class="domain">{r.source_domain}</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
{/if}
|
{/if}
|
||||||
{#if recent.length > 0}
|
{#if recent.length > 0}
|
||||||
<section class="listing">
|
<section class="listing">
|
||||||
<h2>Zuletzt hinzugefügt</h2>
|
<button
|
||||||
<ul class="cards">
|
type="button"
|
||||||
{#each recent as r (r.id)}
|
class="section-head"
|
||||||
<li class="card-wrap">
|
onclick={() => toggleCollapsed('recent')}
|
||||||
<a href={`/recipes/${r.id}`} class="card">
|
aria-expanded={!collapsed.recent}
|
||||||
{#if r.image_path}
|
>
|
||||||
<img src={`/images/${r.image_path}`} alt="" loading="lazy" />
|
<ChevronDown
|
||||||
{:else}
|
size={18}
|
||||||
<div class="placeholder"><CookingPot size={36} /></div>
|
strokeWidth={2.2}
|
||||||
{/if}
|
class={collapsed.recent ? 'chev rotated' : 'chev'}
|
||||||
<div class="card-body">
|
/>
|
||||||
<div class="title">{r.title}</div>
|
<h2>Zuletzt hinzugefügt</h2>
|
||||||
{#if r.source_domain}
|
<span class="count">{recent.length}</span>
|
||||||
<div class="domain">{r.source_domain}</div>
|
</button>
|
||||||
{/if}
|
{#if !collapsed.recent}
|
||||||
</div>
|
<div transition:slide={{ duration: 180 }}>
|
||||||
</a>
|
<ul class="cards">
|
||||||
<button
|
{#each recent as r (r.id)}
|
||||||
class="dismiss"
|
<li class="card-wrap">
|
||||||
aria-label="Aus Zuletzt-hinzugefügt entfernen"
|
<a href={`/recipes/${r.id}`} class="card">
|
||||||
onclick={(e) => dismissFromRecent(r.id, e)}
|
{#if r.image_path}
|
||||||
>
|
<img src={`/images/${r.image_path}`} alt="" loading="lazy" />
|
||||||
<X size={16} strokeWidth={2.5} />
|
{:else}
|
||||||
</button>
|
<div class="placeholder"><CookingPot size={36} /></div>
|
||||||
</li>
|
{/if}
|
||||||
{/each}
|
<div class="card-body">
|
||||||
</ul>
|
<div class="title">{r.title}</div>
|
||||||
|
{#if r.source_domain}
|
||||||
|
<div class="domain">{r.source_domain}</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</a>
|
||||||
|
<button
|
||||||
|
class="dismiss"
|
||||||
|
aria-label="Aus Zuletzt-hinzugefügt entfernen"
|
||||||
|
onclick={(e) => dismissFromRecent(r.id, e)}
|
||||||
|
>
|
||||||
|
<X size={16} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
{/if}
|
{/if}
|
||||||
<section class="listing">
|
<section class="listing">
|
||||||
@@ -516,6 +609,49 @@
|
|||||||
color: #444;
|
color: #444;
|
||||||
margin: 0 0 0.75rem;
|
margin: 0 0 0.75rem;
|
||||||
}
|
}
|
||||||
|
.section-head {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.4rem 0.25rem;
|
||||||
|
background: transparent;
|
||||||
|
border: 0;
|
||||||
|
border-radius: 8px;
|
||||||
|
text-align: left;
|
||||||
|
cursor: pointer;
|
||||||
|
font-family: inherit;
|
||||||
|
color: inherit;
|
||||||
|
min-height: 44px;
|
||||||
|
margin-bottom: 0.4rem;
|
||||||
|
}
|
||||||
|
.section-head:hover {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.section-head:focus-visible {
|
||||||
|
outline: 2px solid #2b6a3d;
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
.section-head h2 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 1.05rem;
|
||||||
|
color: #444;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
.section-head .count {
|
||||||
|
margin-left: auto;
|
||||||
|
color: #888;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
|
.section-head :global(.chev) {
|
||||||
|
color: #2b6a3d;
|
||||||
|
flex-shrink: 0;
|
||||||
|
transition: transform 180ms;
|
||||||
|
}
|
||||||
|
.section-head :global(.chev.rotated) {
|
||||||
|
transform: rotate(-90deg);
|
||||||
|
}
|
||||||
.listing-head {
|
.listing-head {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
27
src/routes/api/recipes/[id]/view/+server.ts
Normal file
27
src/routes/api/recipes/[id]/view/+server.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import type { RequestHandler } from './$types';
|
||||||
|
import { z } from 'zod';
|
||||||
|
import { error } from '@sveltejs/kit';
|
||||||
|
import { getDb } from '$lib/server/db';
|
||||||
|
import { validateBody, parsePositiveIntParam } from '$lib/server/api-helpers';
|
||||||
|
import { recordView } from '$lib/server/recipes/views';
|
||||||
|
|
||||||
|
const Schema = z.object({
|
||||||
|
profile_id: z.number().int().positive()
|
||||||
|
});
|
||||||
|
|
||||||
|
export const POST: RequestHandler = async ({ params, request }) => {
|
||||||
|
const recipeId = parsePositiveIntParam(params.id, 'id');
|
||||||
|
const body = validateBody(await request.json().catch(() => null), Schema);
|
||||||
|
|
||||||
|
try {
|
||||||
|
recordView(getDb(), body.profile_id, recipeId);
|
||||||
|
} catch (e) {
|
||||||
|
// FK violation (unknown profile or recipe) → 404
|
||||||
|
if (e instanceof Error && /FOREIGN KEY constraint failed/i.test(e.message)) {
|
||||||
|
error(404, { message: 'Recipe or profile not found' });
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(null, { status: 204 });
|
||||||
|
};
|
||||||
@@ -6,7 +6,19 @@ import {
|
|||||||
type AllRecipesSort
|
type AllRecipesSort
|
||||||
} from '$lib/server/recipes/search-local';
|
} from '$lib/server/recipes/search-local';
|
||||||
|
|
||||||
const VALID_SORTS = new Set<AllRecipesSort>(['name', 'rating', 'cooked', 'created']);
|
const VALID_SORTS = new Set<AllRecipesSort>([
|
||||||
|
'name',
|
||||||
|
'rating',
|
||||||
|
'cooked',
|
||||||
|
'created',
|
||||||
|
'viewed'
|
||||||
|
]);
|
||||||
|
|
||||||
|
function parseProfileId(raw: string | null): number | null {
|
||||||
|
if (!raw) return null;
|
||||||
|
const n = Number(raw);
|
||||||
|
return Number.isInteger(n) && n > 0 ? n : null;
|
||||||
|
}
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ url }) => {
|
export const GET: RequestHandler = async ({ url }) => {
|
||||||
const sortRaw = (url.searchParams.get('sort') ?? 'name') as AllRecipesSort;
|
const sortRaw = (url.searchParams.get('sort') ?? 'name') as AllRecipesSort;
|
||||||
@@ -17,6 +29,7 @@ export const GET: RequestHandler = async ({ url }) => {
|
|||||||
// one round-trip so document height matches and scroll-restore lands.
|
// one round-trip so document height matches and scroll-restore lands.
|
||||||
const limit = Math.min(200, Math.max(1, Number(url.searchParams.get('limit') ?? 10)));
|
const limit = Math.min(200, Math.max(1, Number(url.searchParams.get('limit') ?? 10)));
|
||||||
const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0));
|
const offset = Math.max(0, Number(url.searchParams.get('offset') ?? 0));
|
||||||
const hits = listAllRecipesPaginated(getDb(), sortRaw, limit, offset);
|
const profileId = parseProfileId(url.searchParams.get('profile_id'));
|
||||||
|
const hits = listAllRecipesPaginated(getDb(), sortRaw, limit, offset, profileId);
|
||||||
return json({ sort: sortRaw, limit, offset, hits });
|
return json({ sort: sortRaw, limit, offset, hits });
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -355,9 +355,28 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
document.addEventListener('visibilitychange', onVisibility);
|
document.addEventListener('visibilitychange', onVisibility);
|
||||||
|
|
||||||
return () => document.removeEventListener('visibilitychange', onVisibility);
|
return () => document.removeEventListener('visibilitychange', onVisibility);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Track view per active profile (fire-and-forget). Lives in $effect, not
|
||||||
|
// onMount, because profileStore.load() runs from layout's onMount and the
|
||||||
|
// child onMount fires first — at mount time profileStore.active is still
|
||||||
|
// null on cold loads. The effect re-runs once active populates, the
|
||||||
|
// viewBeaconSent flag prevents duplicate POSTs on subsequent profile
|
||||||
|
// switches within the same page instance.
|
||||||
|
let viewBeaconSent = $state(false);
|
||||||
|
$effect(() => {
|
||||||
|
if (viewBeaconSent) return;
|
||||||
|
if (!profileStore.active) return;
|
||||||
|
viewBeaconSent = true;
|
||||||
|
void fetch(`/api/recipes/${data.recipe.id}/view`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ profile_id: profileStore.active.id })
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
void releaseWakeLock();
|
void releaseWakeLock();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -284,6 +284,8 @@
|
|||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
line-height: 1.3;
|
line-height: 1.3;
|
||||||
|
overflow-wrap: break-word;
|
||||||
|
word-break: break-word;
|
||||||
}
|
}
|
||||||
.meta {
|
.meta {
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -340,4 +342,51 @@
|
|||||||
font-size: 0.85rem;
|
font-size: 0.85rem;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Handy: 2-Spalten-Grid — Bild links ueber alle Rows, rechts stapeln
|
||||||
|
sich Titel, Meta, Actions. `display: contents` auf .body/.text zieht
|
||||||
|
die DOM-Kinder direkt in die Card-Grid, ohne Markup-Umbau. Vermeidet
|
||||||
|
die tote Weissflaeche unter dem Bild bei schmalen Viewports. */
|
||||||
|
@media (max-width: 600px) {
|
||||||
|
.card {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 96px 1fr;
|
||||||
|
grid-template-areas:
|
||||||
|
'img title'
|
||||||
|
'img meta'
|
||||||
|
'img actions';
|
||||||
|
column-gap: 0;
|
||||||
|
}
|
||||||
|
.body {
|
||||||
|
display: contents;
|
||||||
|
}
|
||||||
|
.body img,
|
||||||
|
.placeholder {
|
||||||
|
grid-area: img;
|
||||||
|
width: 96px;
|
||||||
|
height: 100%;
|
||||||
|
min-height: 100%;
|
||||||
|
}
|
||||||
|
.text {
|
||||||
|
display: contents;
|
||||||
|
}
|
||||||
|
.title {
|
||||||
|
grid-area: title;
|
||||||
|
padding: 0.7rem 0.75rem 0.15rem;
|
||||||
|
}
|
||||||
|
.meta {
|
||||||
|
grid-area: meta;
|
||||||
|
padding: 0 0.75rem;
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
.actions-top {
|
||||||
|
grid-area: actions;
|
||||||
|
position: static;
|
||||||
|
display: flex;
|
||||||
|
gap: 0.4rem;
|
||||||
|
padding: 0.5rem 0.75rem 0.7rem;
|
||||||
|
justify-content: flex-end;
|
||||||
|
align-self: end;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
270
tests/integration/recipe-views.test.ts
Normal file
270
tests/integration/recipe-views.test.ts
Normal file
@@ -0,0 +1,270 @@
|
|||||||
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||||
|
import { openInMemoryForTest } from '../../src/lib/server/db';
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Module-level mock so the POST handler uses the in-memory test DB.
|
||||||
|
// Must be declared before any import of the handler itself.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
const { testDb } = vi.hoisted(() => ({
|
||||||
|
testDb: { current: null as ReturnType<typeof openInMemoryForTest> | null }
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('$lib/server/db', async () => {
|
||||||
|
const actual =
|
||||||
|
await vi.importActual<typeof import('../../src/lib/server/db')>(
|
||||||
|
'../../src/lib/server/db'
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
getDb: () => {
|
||||||
|
if (!testDb.current) throw new Error('test DB not initialised');
|
||||||
|
return testDb.current;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
import { recordView, listViews } from '../../src/lib/server/recipes/views';
|
||||||
|
import { createProfile } from '../../src/lib/server/profiles/repository';
|
||||||
|
import { listAllRecipesPaginated } from '../../src/lib/server/recipes/search-local';
|
||||||
|
import { POST } from '../../src/routes/api/recipes/[id]/view/+server';
|
||||||
|
|
||||||
|
function seedRecipe(db: ReturnType<typeof openInMemoryForTest>, title: string): number {
|
||||||
|
const r = db
|
||||||
|
.prepare("INSERT INTO recipe (title, created_at) VALUES (?, datetime('now')) RETURNING id")
|
||||||
|
.get(title) as { id: number };
|
||||||
|
return r.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mkReq(body: unknown) {
|
||||||
|
return new Request('http://test/api/recipes/1/view', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('014_recipe_views migration', () => {
|
||||||
|
it('creates recipe_view table with expected columns', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const cols = db.prepare("PRAGMA table_info(recipe_view)").all() as Array<{
|
||||||
|
name: string;
|
||||||
|
type: string;
|
||||||
|
notnull: number;
|
||||||
|
pk: number;
|
||||||
|
}>;
|
||||||
|
const byName = Object.fromEntries(cols.map((c) => [c.name, c]));
|
||||||
|
expect(byName.profile_id?.type).toBe('INTEGER');
|
||||||
|
expect(byName.profile_id?.notnull).toBe(1);
|
||||||
|
expect(byName.profile_id?.pk).toBe(1);
|
||||||
|
expect(byName.recipe_id?.type).toBe('INTEGER');
|
||||||
|
expect(byName.recipe_id?.notnull).toBe(1);
|
||||||
|
expect(byName.recipe_id?.pk).toBe(2);
|
||||||
|
expect(byName.last_viewed_at?.type).toBe('TIMESTAMP');
|
||||||
|
expect(byName.last_viewed_at?.notnull).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('has index on (profile_id, last_viewed_at DESC)', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const idxList = db
|
||||||
|
.prepare("PRAGMA index_list(recipe_view)")
|
||||||
|
.all() as Array<{ name: string }>;
|
||||||
|
expect(idxList.some((i) => i.name === 'idx_recipe_view_recent')).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('recordView', () => {
|
||||||
|
it('inserts a view row with default timestamp', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const profile = createProfile(db, 'Test');
|
||||||
|
const recipeId = seedRecipe(db, 'Pasta');
|
||||||
|
|
||||||
|
recordView(db, profile.id, recipeId);
|
||||||
|
|
||||||
|
const rows = listViews(db, profile.id);
|
||||||
|
expect(rows.length).toBe(1);
|
||||||
|
expect(rows[0].recipe_id).toBe(recipeId);
|
||||||
|
expect(rows[0].last_viewed_at).toMatch(/^\d{4}-\d{2}-\d{2}/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates timestamp on subsequent view of same recipe', async () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const profile = createProfile(db, 'Test');
|
||||||
|
const recipeId = seedRecipe(db, 'Pasta');
|
||||||
|
|
||||||
|
recordView(db, profile.id, recipeId);
|
||||||
|
const first = listViews(db, profile.id)[0].last_viewed_at;
|
||||||
|
|
||||||
|
// tiny delay so the second timestamp differs
|
||||||
|
await new Promise((r) => setTimeout(r, 1100));
|
||||||
|
recordView(db, profile.id, recipeId);
|
||||||
|
|
||||||
|
const rows = listViews(db, profile.id);
|
||||||
|
expect(rows.length).toBe(1);
|
||||||
|
expect(rows[0].last_viewed_at >= first).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws on unknown profile_id (FK)', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const recipeId = seedRecipe(db, 'Pasta');
|
||||||
|
expect(() => recordView(db, 999, recipeId)).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('throws on unknown recipe_id (FK)', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const profile = createProfile(db, 'Test');
|
||||||
|
expect(() => recordView(db, profile.id, 999)).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("listAllRecipesPaginated sort='viewed'", () => {
|
||||||
|
it('puts recently-viewed recipes first, NULLs alphabetically last', async () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const profile = createProfile(db, 'Test');
|
||||||
|
const recipeA = seedRecipe(db, 'Apfelkuchen');
|
||||||
|
const recipeB = seedRecipe(db, 'Brokkoli');
|
||||||
|
// Inserted in reverse-alphabetical order (Z before D) to prove the
|
||||||
|
// tiebreaker sorts by title, not insertion order.
|
||||||
|
const recipeC = seedRecipe(db, 'Zwiebelkuchen');
|
||||||
|
const recipeD = seedRecipe(db, 'Donauwelle');
|
||||||
|
|
||||||
|
// View order: B then A. C and D never viewed.
|
||||||
|
recordView(db, profile.id, recipeB);
|
||||||
|
await new Promise((r) => setTimeout(r, 1100));
|
||||||
|
recordView(db, profile.id, recipeA);
|
||||||
|
|
||||||
|
const hits = listAllRecipesPaginated(db, 'viewed', 50, 0, profile.id);
|
||||||
|
// Viewed: A (most recent), B — then unviewed alphabetically: D before C.
|
||||||
|
expect(hits.map((h) => h.id)).toEqual([recipeA, recipeB, recipeD, recipeC]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to alphabetical when profileId is null', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
seedRecipe(db, 'Couscous');
|
||||||
|
seedRecipe(db, 'Apfelkuchen');
|
||||||
|
seedRecipe(db, 'Brokkoli');
|
||||||
|
|
||||||
|
const hits = listAllRecipesPaginated(db, 'viewed', 50, 0, null);
|
||||||
|
expect(hits.map((h) => h.title)).toEqual(['Apfelkuchen', 'Brokkoli', 'Couscous']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps existing sorts working unchanged', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
seedRecipe(db, 'Couscous');
|
||||||
|
seedRecipe(db, 'Apfelkuchen');
|
||||||
|
seedRecipe(db, 'Brokkoli');
|
||||||
|
|
||||||
|
const hits = listAllRecipesPaginated(db, 'name', 50, 0);
|
||||||
|
expect(hits.map((h) => h.title)).toEqual(['Apfelkuchen', 'Brokkoli', 'Couscous']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// POST /api/recipes/[id]/view — endpoint integration tests
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
testDb.current = openInMemoryForTest();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('POST /api/recipes/[id]/view', () => {
|
||||||
|
it('204 + view row written on success', async () => {
|
||||||
|
const db = testDb.current!;
|
||||||
|
const profile = createProfile(db, 'Tester');
|
||||||
|
const recipeId = seedRecipe(db, 'Pasta');
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
const res = await POST({ params: { id: String(recipeId) }, request: mkReq({ profile_id: profile.id }) } as any);
|
||||||
|
|
||||||
|
expect(res.status).toBe(204);
|
||||||
|
const rows = listViews(db, profile.id);
|
||||||
|
expect(rows.length).toBe(1);
|
||||||
|
expect(rows[0].recipe_id).toBe(recipeId);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('400 on recipe id = 0', async () => {
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: '0' }, request: mkReq({ profile_id: 1 }) } as any)
|
||||||
|
).rejects.toMatchObject({ status: 400 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('400 on non-numeric recipe id', async () => {
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: 'abc' }, request: mkReq({ profile_id: 1 }) } as any)
|
||||||
|
).rejects.toMatchObject({ status: 400 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('400 on missing profile_id in body', async () => {
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: '1' }, request: mkReq({}) } as any)
|
||||||
|
).rejects.toMatchObject({ status: 400 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('400 on non-positive profile_id', async () => {
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: '1' }, request: mkReq({ profile_id: 0 }) } as any)
|
||||||
|
).rejects.toMatchObject({ status: 400 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('400 on malformed JSON body', async () => {
|
||||||
|
const badReq = new Request('http://test/api/recipes/1/view', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: 'not-json'
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: '1' }, request: badReq } as any)
|
||||||
|
).rejects.toMatchObject({ status: 400 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('404 on unknown profile_id (FK violation)', async () => {
|
||||||
|
const recipeId = seedRecipe(testDb.current!, 'Pasta');
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: String(recipeId) }, request: mkReq({ profile_id: 999 }) } as any)
|
||||||
|
).rejects.toMatchObject({ status: 404 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('404 on unknown recipe_id (FK violation)', async () => {
|
||||||
|
const profile = createProfile(testDb.current!, 'Tester');
|
||||||
|
await expect(
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
POST({ params: { id: '99999' }, request: mkReq({ profile_id: profile.id }) } as any)
|
||||||
|
).rejects.toMatchObject({ status: 404 });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// GET /api/recipes/all — sort=viewed + profile_id
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import { GET as allGet } from '../../src/routes/api/recipes/all/+server';
|
||||||
|
|
||||||
|
describe('GET /api/recipes/all sort=viewed', () => {
|
||||||
|
it('passes profile_id through and returns viewed-order hits', async () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
testDb.current = db;
|
||||||
|
const profile = createProfile(db, 'Test');
|
||||||
|
const a = seedRecipe(db, 'Apfel');
|
||||||
|
const b = seedRecipe(db, 'Birne');
|
||||||
|
recordView(db, profile.id, b);
|
||||||
|
await new Promise((r) => setTimeout(r, 1100));
|
||||||
|
recordView(db, profile.id, a);
|
||||||
|
|
||||||
|
const url = new URL(`http://localhost/api/recipes/all?sort=viewed&profile_id=${profile.id}&limit=10`);
|
||||||
|
const res = await allGet({ url } as never);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const body = await res.json();
|
||||||
|
expect(body.sort).toBe('viewed');
|
||||||
|
expect(body.hits.map((h: { id: number }) => h.id)).toEqual([a, b]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('400 on invalid sort', async () => {
|
||||||
|
const url = new URL('http://localhost/api/recipes/all?sort=invalid');
|
||||||
|
await expect(allGet({ url } as never)).rejects.toMatchObject({ status: 400 });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -123,7 +123,7 @@ describe('listShoppingList aggregation', () => {
|
|||||||
const rows = listShoppingList(db).rows;
|
const rows = listShoppingList(db).rows;
|
||||||
expect(rows).toHaveLength(1);
|
expect(rows).toHaveLength(1);
|
||||||
expect(rows[0].name_key).toBe('mehl');
|
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].total_quantity).toBe(400);
|
||||||
expect(rows[0].from_recipes).toContain('Carbonara');
|
expect(rows[0].from_recipes).toContain('Carbonara');
|
||||||
expect(rows[0].from_recipes).toContain('Lasagne');
|
expect(rows[0].from_recipes).toContain('Lasagne');
|
||||||
@@ -201,15 +201,15 @@ describe('toggleCheck', () => {
|
|||||||
|
|
||||||
it('marks a row as checked', () => {
|
it('marks a row as checked', () => {
|
||||||
const { db } = setupOneRowCart();
|
const { db } = setupOneRowCart();
|
||||||
toggleCheck(db, 'mehl', 'g', true);
|
toggleCheck(db, 'mehl', 'weight', true);
|
||||||
const rows = listShoppingList(db).rows;
|
const rows = listShoppingList(db).rows;
|
||||||
expect(rows[0].checked).toBe(1);
|
expect(rows[0].checked).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('unchecks a row when passed false', () => {
|
it('unchecks a row when passed false', () => {
|
||||||
const { db } = setupOneRowCart();
|
const { db } = setupOneRowCart();
|
||||||
toggleCheck(db, 'mehl', 'g', true);
|
toggleCheck(db, 'mehl', 'weight', true);
|
||||||
toggleCheck(db, 'mehl', 'g', false);
|
toggleCheck(db, 'mehl', 'weight', false);
|
||||||
expect(listShoppingList(db).rows[0].checked).toBe(0);
|
expect(listShoppingList(db).rows[0].checked).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -225,7 +225,7 @@ describe('toggleCheck', () => {
|
|||||||
}));
|
}));
|
||||||
addRecipeToCart(db, a, null);
|
addRecipeToCart(db, a, null);
|
||||||
addRecipeToCart(db, b, 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
|
// Rezept A weg, Mehl kommt noch aus B — check bleibt, mit neuer Menge
|
||||||
removeRecipeFromCart(db, a);
|
removeRecipeFromCart(db, a);
|
||||||
const rows = listShoppingList(db).rows;
|
const rows = listShoppingList(db).rows;
|
||||||
@@ -304,3 +304,178 @@ describe('clearCart', () => {
|
|||||||
expect(anyCheck).toBeUndefined();
|
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', () => {
|
it('renders fractional with up to 2 decimals, trailing zeros trimmed', () => {
|
||||||
expect(formatQuantity(0.5)).toBe('0.5');
|
expect(formatQuantity(0.5)).toBe('0,5');
|
||||||
expect(formatQuantity(0.333333)).toBe('0.33');
|
expect(formatQuantity(0.333333)).toBe('0,33');
|
||||||
expect(formatQuantity(1.1)).toBe('1.1');
|
expect(formatQuantity(1.1)).toBe('1,1');
|
||||||
expect(formatQuantity(1.1)).toBe('1.1');
|
expect(formatQuantity(1.1)).toBe('1,1');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('handles zero', () => {
|
it('handles zero', () => {
|
||||||
|
|||||||
107
tests/unit/unit-consolidation.test.ts
Normal file
107
tests/unit/unit-consolidation.test.ts
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { unitFamily, consolidate } from '../../src/lib/server/unit-consolidation';
|
||||||
|
|
||||||
|
describe('unitFamily', () => {
|
||||||
|
it('maps g and kg to weight', () => {
|
||||||
|
expect(unitFamily('g')).toBe('weight');
|
||||||
|
expect(unitFamily('kg')).toBe('weight');
|
||||||
|
});
|
||||||
|
it('maps ml and l to volume', () => {
|
||||||
|
expect(unitFamily('ml')).toBe('volume');
|
||||||
|
expect(unitFamily('l')).toBe('volume');
|
||||||
|
});
|
||||||
|
it('lowercases and trims unknown units', () => {
|
||||||
|
expect(unitFamily(' Bund ')).toBe('bund');
|
||||||
|
expect(unitFamily('TL')).toBe('tl');
|
||||||
|
expect(unitFamily('Stück')).toBe('stück');
|
||||||
|
});
|
||||||
|
it('is case-insensitive for weight/volume', () => {
|
||||||
|
expect(unitFamily('Kg')).toBe('weight');
|
||||||
|
expect(unitFamily('ML')).toBe('volume');
|
||||||
|
});
|
||||||
|
it('returns empty string for null/undefined/empty', () => {
|
||||||
|
expect(unitFamily(null)).toBe('');
|
||||||
|
expect(unitFamily(undefined)).toBe('');
|
||||||
|
expect(unitFamily('')).toBe('');
|
||||||
|
expect(unitFamily(' ')).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('consolidate', () => {
|
||||||
|
it('kombiniert 500 g + 1 kg zu 1,5 kg', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 500, unit: 'g' },
|
||||||
|
{ quantity: 1, unit: 'kg' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1.5, unit: 'kg' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('bleibt bei g wenn Summe < 1 kg', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 200, unit: 'g' },
|
||||||
|
{ quantity: 300, unit: 'g' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 500, unit: 'g' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('promoted bei exakt 1000 g (Boundary)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 1000, unit: 'g' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1, unit: 'kg' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('kombiniert ml + l analog (400 ml + 0,5 l → 900 ml)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 400, unit: 'ml' },
|
||||||
|
{ quantity: 0.5, unit: 'l' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 900, unit: 'ml' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('promoted zu l ab 1000 ml (0,5 l + 0,8 l → 1,3 l)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 0.5, unit: 'l' },
|
||||||
|
{ quantity: 0.8, unit: 'l' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1.3, unit: 'l' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('summiert gleiche nicht-family-units (2 Bund + 1 Bund → 3 Bund)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 2, unit: 'Bund' },
|
||||||
|
{ quantity: 1, unit: 'Bund' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 3, unit: 'Bund' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('behandelt quantity=null als 0', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: null, unit: 'TL' },
|
||||||
|
{ quantity: 1, unit: 'TL' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: 1, unit: 'TL' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gibt null zurueck wenn alle quantities null sind', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: null, unit: 'Prise' },
|
||||||
|
{ quantity: null, unit: 'Prise' }
|
||||||
|
]);
|
||||||
|
expect(out).toEqual({ quantity: null, unit: 'Prise' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rundet Float-Artefakte auf 2 Dezimalen (0,1 + 0,2 kg → 0,3 kg)', () => {
|
||||||
|
const out = consolidate([
|
||||||
|
{ quantity: 0.1, unit: 'kg' },
|
||||||
|
{ quantity: 0.2, unit: 'kg' }
|
||||||
|
]);
|
||||||
|
// 0.1 + 0.2 in kg = 0.3 kg, in g = 300 → promoted? 300 < 1000 → 300 g
|
||||||
|
expect(out).toEqual({ quantity: 300, unit: 'g' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('nimmt unit vom ersten Eintrag bei unbekannter family', () => {
|
||||||
|
const out = consolidate([{ quantity: 5, unit: 'Stück' }]);
|
||||||
|
expect(out).toEqual({ quantity: 5, unit: 'Stück' });
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user