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>
63 lines
2.4 KiB
TypeScript
63 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { db, pantryItems, eq } from "@epicure/db";
|
|
import { requireSessionOrApiKey } from "@/lib/api-auth";
|
|
import { loadIngredientAliasIndex, resolveIngredientKey, findIngredientIdByName } from "@/lib/ingredient-match";
|
|
|
|
const Schema = z.object({
|
|
items: z.array(z.object({
|
|
rawName: z.string().min(1).max(200),
|
|
quantity: z.string().optional(),
|
|
unit: z.string().max(50).optional(),
|
|
})).min(1).max(100),
|
|
});
|
|
|
|
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 userId = session!.user.id;
|
|
const [existing, aliasIndex] = await Promise.all([
|
|
db.query.pantryItems.findMany({ where: eq(pantryItems.userId, userId) }),
|
|
loadIngredientAliasIndex(),
|
|
]);
|
|
|
|
for (const incoming of parsed.data.items) {
|
|
const key = resolveIngredientKey(incoming.rawName, aliasIndex);
|
|
const match = existing.find(
|
|
(e) => resolveIngredientKey(e.rawName, aliasIndex) === key && (e.unit ?? "") === (incoming.unit ?? "")
|
|
);
|
|
|
|
if (match) {
|
|
const existingQty = match.quantity ? parseFloat(match.quantity) : null;
|
|
const incomingQty = incoming.quantity ? parseFloat(incoming.quantity) : null;
|
|
if (existingQty !== null && incomingQty !== null && !isNaN(existingQty) && !isNaN(incomingQty)) {
|
|
const merged = existingQty + incomingQty;
|
|
await db.update(pantryItems)
|
|
.set({ quantity: String(merged) })
|
|
.where(eq(pantryItems.id, match.id));
|
|
}
|
|
// if quantities aren't numeric, leave as-is (item already exists)
|
|
} else {
|
|
const ingredientId = await findIngredientIdByName(incoming.rawName);
|
|
const created = {
|
|
id: crypto.randomUUID(),
|
|
userId,
|
|
ingredientId,
|
|
rawName: incoming.rawName,
|
|
quantity: incoming.quantity,
|
|
unit: incoming.unit,
|
|
};
|
|
await db.insert(pantryItems).values(created);
|
|
// Later items in this same batch can now also match this one.
|
|
existing.push({ ...created, notes: null, aisle: null, expiresAt: null, quantity: created.quantity ?? null, unit: created.unit ?? null, createdAt: new Date() });
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ ok: true });
|
|
}
|