93936eae10
Pantry: notes + category fields (collapsible grouping like the shopping list), a "Merge duplicates" cleanup action, and fixed quantity display precision (was showing raw decimal(10,4) strings like "0.3333 kg" everywhere — pantry, shopping list, print views, Markdown exports). Ingredient-alias matching: the ingredients table (canonical name + aliases) existed but was never populated or used. Seeded ~10 bilingual EN/FR staples and wired resolution into pantry add/edit, can-cook scoring, auto-deduct-on-cook, and shopping-list pantry-awareness, so "sel"/"sel fin"/"table salt" are recognized as the same ingredient. Cook log: entries from "Mark cooked" can now be edited and deleted (previously log-only, no fix-a-mistake path). The "Cooked N times" text is a hover tooltip listing every date and opens a full manage sheet on click. Also: the "Forked by N others" backlink is now a click-to-open popover instead of an always-inline list. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
53 lines
2.5 KiB
TypeScript
53 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { db, pantryItems, eq, and } from "@epicure/db";
|
|
import { requireSessionOrApiKey } from "@/lib/api-auth";
|
|
import { findIngredientIdByName } from "@/lib/ingredient-match";
|
|
|
|
type Params = { params: Promise<{ id: string }> };
|
|
|
|
export async function PUT(req: NextRequest, { params }: Params) {
|
|
const { session, response } = await requireSessionOrApiKey(req);
|
|
if (response) return response;
|
|
const { id } = await params;
|
|
|
|
const item = await db.query.pantryItems.findFirst({ where: and(eq(pantryItems.id, id), eq(pantryItems.userId, session!.user.id)) });
|
|
if (!item) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
|
|
|
const body = await req.json() as unknown;
|
|
const parsed = z.object({
|
|
rawName: z.string().min(1).max(200).optional(),
|
|
quantity: z.string().nullable().optional(),
|
|
unit: z.string().nullable().optional(),
|
|
notes: z.string().max(500).nullable().optional(),
|
|
aisle: z.string().max(50).nullable().optional(),
|
|
expiresAt: z.string().datetime().nullable().optional(),
|
|
}).safeParse(body);
|
|
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
|
|
|
const data = parsed.data;
|
|
// Renaming can change which canonical ingredient this item resolves to
|
|
// (e.g. "sel" -> "sel de table") — re-resolve whenever rawName changes,
|
|
// rather than leaving a stale link from the item's original name.
|
|
const ingredientId = data.rawName ? await findIngredientIdByName(data.rawName) : undefined;
|
|
await db.update(pantryItems).set({
|
|
...(data.rawName && { rawName: data.rawName, ingredientId }),
|
|
...(data.quantity !== undefined && { quantity: data.quantity ?? undefined }),
|
|
...(data.unit !== undefined && { unit: data.unit ?? undefined }),
|
|
...(data.notes !== undefined && { notes: data.notes ?? undefined }),
|
|
...(data.aisle !== undefined && { aisle: data.aisle ?? undefined }),
|
|
...(data.expiresAt !== undefined && { expiresAt: data.expiresAt ? new Date(data.expiresAt) : undefined }),
|
|
}).where(eq(pantryItems.id, id));
|
|
|
|
return NextResponse.json({ updated: true });
|
|
}
|
|
|
|
export async function DELETE(req: NextRequest, { params }: Params) {
|
|
const { session, response } = await requireSessionOrApiKey(req);
|
|
if (response) return response;
|
|
const { id } = await params;
|
|
|
|
await db.delete(pantryItems).where(and(eq(pantryItems.id, id), eq(pantryItems.userId, session!.user.id)));
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|