Files
Epicure/apps/web/app/api/v1/pantry/route.ts
T
Arnaud 93936eae10 feat: pantry notes/categories, ingredient-alias matching, cook-log edit/delete, fork-list popover (v0.83.0)
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>
2026-07-24 15:13:33 +02:00

68 lines
2.3 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { db, pantryItems, eq, desc, sql } from "@epicure/db";
import { requireSessionOrApiKey } from "@/lib/api-auth";
import { findIngredientIdByName } from "@/lib/ingredient-match";
const Schema = z.object({
rawName: z.string().min(1).max(200),
quantity: z.string().optional(),
unit: z.string().optional(),
notes: z.string().max(500).optional(),
aisle: z.string().max(50).optional(),
expiresAt: z.string().datetime().optional(),
});
export async function GET(req: NextRequest) {
const { session, response } = await requireSessionOrApiKey(req);
if (response) return response;
const { searchParams } = req.nextUrl;
const limitRaw = parseInt(searchParams.get("limit") ?? "50");
const limit = Math.min(Number.isNaN(limitRaw) ? 50 : Math.max(1, limitRaw), 100);
const offsetRaw = parseInt(searchParams.get("offset") ?? "0");
const offset = Number.isNaN(offsetRaw) ? 0 : Math.max(0, offsetRaw);
const [items, totalRow] = await Promise.all([
db.query.pantryItems.findMany({
where: eq(pantryItems.userId, session!.user.id),
orderBy: desc(pantryItems.createdAt),
limit,
offset,
}),
db
.select({ total: sql<number>`count(*)::int` })
.from(pantryItems)
.where(eq(pantryItems.userId, session!.user.id)),
]);
const total = totalRow[0]?.total ?? 0;
return NextResponse.json({ data: items, total, limit, offset });
}
export async function POST(req: NextRequest) {
const { session, response } = await requireSessionOrApiKey(req);
if (response) return response;
const body = await req.json() as unknown;
const parsed = Schema.safeParse(body);
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
const id = crypto.randomUUID();
const ingredientId = await findIngredientIdByName(parsed.data.rawName);
await db.insert(pantryItems).values({
id,
userId: session!.user.id,
ingredientId,
rawName: parsed.data.rawName,
quantity: parsed.data.quantity,
unit: parsed.data.unit,
notes: parsed.data.notes,
aisle: parsed.data.aisle,
expiresAt: parsed.data.expiresAt ? new Date(parsed.data.expiresAt) : undefined,
});
return NextResponse.json({ id }, { status: 201 });
}