diff --git a/src/routes/api/recipes/[id]/cooked/+server.ts b/src/routes/api/recipes/[id]/cooked/+server.ts index fc933cc..e1965c9 100644 --- a/src/routes/api/recipes/[id]/cooked/+server.ts +++ b/src/routes/api/recipes/[id]/cooked/+server.ts @@ -3,6 +3,7 @@ import { json, error } from '@sveltejs/kit'; import { z } from 'zod'; import { getDb } from '$lib/server/db'; import { logCooked } from '$lib/server/recipes/actions'; +import { removeFromWishlistForAll } from '$lib/server/wishlist/repository'; const Schema = z.object({ profile_id: z.number().int().positive() }); @@ -17,6 +18,11 @@ export const POST: RequestHandler = async ({ params, request }) => { const body = await request.json().catch(() => null); const parsed = Schema.safeParse(body); if (!parsed.success) error(400, { message: 'Invalid body' }); - const entry = logCooked(getDb(), id, parsed.data.profile_id); - return json(entry, { status: 201 }); + const db = getDb(); + const entry = logCooked(db, id, parsed.data.profile_id); + // Wenn das Rezept heute gekocht wurde, ist der Wunsch erfüllt — für alle + // Profile raus aus der Wunschliste. Client nutzt den removed_from_wishlist- + // Flag, um den lokalen State (Badge, Button) ohne Reload zu aktualisieren. + removeFromWishlistForAll(db, id); + return json({ ...entry, removed_from_wishlist: true }, { status: 201 }); }; diff --git a/src/routes/recipes/[id]/+page.svelte b/src/routes/recipes/[id]/+page.svelte index 9391c55..793a447 100644 --- a/src/routes/recipes/[id]/+page.svelte +++ b/src/routes/recipes/[id]/+page.svelte @@ -111,6 +111,10 @@ }); const entry = await res.json(); cookingLog = [entry, ...cookingLog]; + if (entry.removed_from_wishlist) { + wishlistProfileIds = []; + void wishlistStore.refresh(); + } } async function addComment() { diff --git a/tests/integration/wishlist.test.ts b/tests/integration/wishlist.test.ts index 472a499..c008579 100644 --- a/tests/integration/wishlist.test.ts +++ b/tests/integration/wishlist.test.ts @@ -6,6 +6,7 @@ import { insertRecipe } from '../../src/lib/server/recipes/repository'; import { addToWishlist, removeFromWishlist, + removeFromWishlistForAll, listWishlist, listWishlistProfileIds, isOnMyWishlist, @@ -129,4 +130,14 @@ describe('per-user wishlist', () => { addToWishlist(db, r2, a.id); expect(countWishlistRecipes(db)).toBe(2); }); + + it('removeFromWishlistForAll drops every profile', () => { + const r1 = insertRecipe(db, recipe('R1')); + const a = createProfile(db, 'A'); + const b = createProfile(db, 'B'); + addToWishlist(db, r1, a.id); + addToWishlist(db, r1, b.id); + removeFromWishlistForAll(db, r1); + expect(listWishlistProfileIds(db, r1)).toEqual([]); + }); });