feat: shopping list rename/delete, item reorder+categories+search, ingredient-quantity parsing fix
- Fixed a real i18n bug: the checked-count line called the wrong translation namespace and rendered the literal key on screen - Shopping lists can now be renamed and deleted from both the list index and detail pages (API already supported delete; rename was net new) - Root-caused "long list UI is off": meal-plan-generated lists never set an aisle, so every item fell into one undifferentiated "Other" bucket despite the grouping UI existing. Added a keyword-based aisle guesser wired into list generation (fallback only, never overrides an explicit aisle) plus a one-click "auto-categorize" for existing lists - Items can now be reordered by drag-and-drop within a category (dnd-kit), recategorized via a dropdown, deleted, searched, and sorted (category / alphabetical / unchecked-first); searching flattens the grouped view - Fixed a separate bug: AI-generated ingredients sometimes embedded the quantity/unit in the name itself (e.g. "2 cups flour" as one string). Added extractIngredientQuantity() as a Zod transform at both recipe create/update routes (the choke point every creation path funnels through) to split it back out, plus schema descriptions on the AI ingredient schemas as a prevention layer New migration 0028 (shopping_list_items.sort_order), left unapplied like the others. Verified with typecheck, lint, and a clean --no-cache docker build. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import { requireSessionOrApiKey } from "@/lib/api-auth";
|
||||
import { deleteObject } from "@/lib/storage";
|
||||
import { dispatchWebhook } from "@/lib/webhooks";
|
||||
import { parseQuantity } from "@/lib/parse-quantity";
|
||||
import { extractIngredientQuantity } from "@/lib/extract-ingredient-quantity";
|
||||
|
||||
const UpdateRecipeSchema = z.object({
|
||||
title: z.string().min(1).max(200).optional(),
|
||||
@@ -31,6 +32,9 @@ const UpdateRecipeSchema = z.object({
|
||||
unit: z.string().max(50).optional(),
|
||||
note: z.string().max(500).optional(),
|
||||
order: z.number().int().default(0),
|
||||
}).transform((ing) => {
|
||||
const { rawName, quantity, unit } = extractIngredientQuantity(ing.rawName, ing.quantity, ing.unit);
|
||||
return { ...ing, rawName, quantity, unit };
|
||||
})).max(100).optional(),
|
||||
steps: z.array(z.object({
|
||||
instruction: z.string().min(1).max(2000),
|
||||
|
||||
@@ -6,6 +6,7 @@ import { requireSessionOrApiKey } from "@/lib/api-auth";
|
||||
import { checkAndIncrementTierLimit, TierLimitError } from "@/lib/tiers";
|
||||
import { dispatchWebhook } from "@/lib/webhooks";
|
||||
import { parseQuantity } from "@/lib/parse-quantity";
|
||||
import { extractIngredientQuantity } from "@/lib/extract-ingredient-quantity";
|
||||
|
||||
const CreateRecipeSchema = z.object({
|
||||
title: z.string().min(1).max(200),
|
||||
@@ -33,6 +34,9 @@ const CreateRecipeSchema = z.object({
|
||||
unit: z.string().max(50).optional(),
|
||||
note: z.string().max(500).optional(),
|
||||
order: z.number().int().default(0),
|
||||
}).transform((ing) => {
|
||||
const { rawName, quantity, unit } = extractIngredientQuantity(ing.rawName, ing.quantity, ing.unit);
|
||||
return { ...ing, rawName, quantity, unit };
|
||||
})).max(100).default([]),
|
||||
steps: z.array(z.object({
|
||||
instruction: z.string().min(1).max(2000),
|
||||
|
||||
@@ -8,6 +8,11 @@ type Params = { params: Promise<{ id: string; itemId: string }> };
|
||||
|
||||
const UpdateItemSchema = z.object({
|
||||
checked: z.boolean().optional(),
|
||||
rawName: z.string().min(1).optional(),
|
||||
quantity: z.string().nullable().optional(),
|
||||
unit: z.string().nullable().optional(),
|
||||
aisle: z.string().nullable().optional(),
|
||||
sortOrder: z.number().int().optional(),
|
||||
});
|
||||
|
||||
export async function PUT(req: NextRequest, { params }: Params) {
|
||||
@@ -24,9 +29,35 @@ export async function PUT(req: NextRequest, { params }: Params) {
|
||||
return NextResponse.json({ error: "Validation error", issues: parsed.error.issues }, { status: 400 });
|
||||
}
|
||||
|
||||
await db.update(shoppingListItems)
|
||||
.set({ checked: parsed.data.checked ?? false })
|
||||
.where(and(eq(shoppingListItems.id, itemId), eq(shoppingListItems.listId, id)));
|
||||
const data = parsed.data;
|
||||
const patch: Partial<typeof shoppingListItems.$inferInsert> = {};
|
||||
if (data.checked !== undefined) patch.checked = data.checked;
|
||||
if (data.rawName !== undefined) patch.rawName = data.rawName;
|
||||
if (data.quantity !== undefined) patch.quantity = data.quantity;
|
||||
if (data.unit !== undefined) patch.unit = data.unit;
|
||||
if (data.aisle !== undefined) patch.aisle = data.aisle;
|
||||
if (data.sortOrder !== undefined) patch.sortOrder = data.sortOrder;
|
||||
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await db.update(shoppingListItems)
|
||||
.set(patch)
|
||||
.where(and(eq(shoppingListItems.id, itemId), eq(shoppingListItems.listId, id)));
|
||||
}
|
||||
|
||||
return NextResponse.json({ updated: true });
|
||||
}
|
||||
|
||||
export async function DELETE(_req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
const { id, itemId } = await params;
|
||||
|
||||
const access = await getShoppingListAccess(id, session!.user.id);
|
||||
if (!access) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
if (!canWriteShoppingList(access.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
|
||||
await db.delete(shoppingListItems)
|
||||
.where(and(eq(shoppingListItems.id, itemId), eq(shoppingListItems.listId, id)));
|
||||
|
||||
return NextResponse.json({ deleted: true });
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, shoppingListItems } from "@epicure/db";
|
||||
import { db, shoppingListItems, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { getShoppingListAccess, canWriteShoppingList } from "@/lib/shopping-list-access";
|
||||
import { guessAisle } from "@/lib/grocery-categories";
|
||||
|
||||
const AddItemsSchema = z.object({
|
||||
items: z.array(z.object({
|
||||
@@ -13,6 +14,17 @@ const AddItemsSchema = z.object({
|
||||
})).min(1),
|
||||
});
|
||||
|
||||
// Bulk-update a set of existing items (sortOrder and/or aisle) in one request —
|
||||
// used for drag-and-drop reordering (avoids one PUT per item on every drag) and
|
||||
// for the "auto-categorize" action on lists whose items predate aisle-guessing.
|
||||
const BulkUpdateSchema = z.object({
|
||||
items: z.array(z.object({
|
||||
id: z.string().min(1),
|
||||
sortOrder: z.number().int().optional(),
|
||||
aisle: z.string().nullable().optional(),
|
||||
})).min(1),
|
||||
});
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(req: NextRequest, { params }: Params) {
|
||||
@@ -35,10 +47,39 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
rawName: item.rawName,
|
||||
quantity: item.quantity,
|
||||
unit: item.unit,
|
||||
aisle: item.aisle,
|
||||
// Only guess when the caller didn't provide an explicit aisle — never override.
|
||||
aisle: item.aisle ?? guessAisle(item.rawName) ?? undefined,
|
||||
checked: false,
|
||||
}))
|
||||
);
|
||||
|
||||
return NextResponse.json({ ok: true }, { status: 201 });
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
const { id } = await params;
|
||||
|
||||
const access = await getShoppingListAccess(id, session!.user.id);
|
||||
if (!access) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
if (!canWriteShoppingList(access.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
|
||||
const body = await req.json() as unknown;
|
||||
const parsed = BulkUpdateSchema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
await Promise.all(
|
||||
parsed.data.items.map((item) => {
|
||||
const patch: Partial<typeof shoppingListItems.$inferInsert> = {};
|
||||
if (item.sortOrder !== undefined) patch.sortOrder = item.sortOrder;
|
||||
if (item.aisle !== undefined) patch.aisle = item.aisle;
|
||||
if (Object.keys(patch).length === 0) return Promise.resolve();
|
||||
return db.update(shoppingListItems)
|
||||
.set(patch)
|
||||
.where(and(eq(shoppingListItems.id, item.id), eq(shoppingListItems.listId, id)));
|
||||
})
|
||||
);
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
@@ -23,7 +23,10 @@ export async function GET(_req: NextRequest, { params }: Params) {
|
||||
return NextResponse.json(list);
|
||||
}
|
||||
|
||||
const PatchSchema = z.object({ completed: z.boolean() });
|
||||
const PatchSchema = z.object({
|
||||
completed: z.boolean().optional(),
|
||||
name: z.string().min(1).max(100).optional(),
|
||||
});
|
||||
|
||||
export async function PATCH(req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
@@ -32,12 +35,18 @@ export async function PATCH(req: NextRequest, { params }: Params) {
|
||||
|
||||
const access = await getShoppingListAccess(id, session!.user.id);
|
||||
if (!access) return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
if (!canWriteShoppingList(access.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
|
||||
const body = PatchSchema.safeParse(await req.json());
|
||||
if (!body.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
if (body.data.name !== undefined) {
|
||||
// Renaming is owner-only, same as delete
|
||||
if (access.role !== "owner") return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
await db.update(shoppingLists).set({ name: body.data.name }).where(eq(shoppingLists.id, id));
|
||||
}
|
||||
|
||||
if (body.data.completed) {
|
||||
if (!canWriteShoppingList(access.role)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||
// Mark all items as checked
|
||||
await db.update(shoppingListItems).set({ checked: true }).where(eq(shoppingListItems.listId, id));
|
||||
void dispatchWebhook(session!.user.id, "shopping_list.completed", { id, name: access.list.name });
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
import { db, shoppingLists, shoppingListItems, mealPlans, mealPlanEntries, recipeIngredients, pantryItems, eq, and, desc, inArray } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyPantryToItems, mergeIngredients } from "@/lib/pantry-shopping-match";
|
||||
import { guessAisle } from "@/lib/grocery-categories";
|
||||
|
||||
const CreateSchema = z.object({
|
||||
name: z.string().min(1).max(100),
|
||||
@@ -77,15 +78,18 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
if (items.length > 0) {
|
||||
await db.insert(shoppingListItems).values(
|
||||
items.map((item) => ({
|
||||
items.map((item, index) => ({
|
||||
id: crypto.randomUUID(),
|
||||
listId,
|
||||
rawName: item.rawName,
|
||||
quantity: item.quantity,
|
||||
unit: item.unit,
|
||||
aisle: item.aisle,
|
||||
// Never override an explicit aisle — only guess one when the item doesn't have one
|
||||
// (which today is every item coming from the meal-plan generation path).
|
||||
aisle: item.aisle ?? guessAisle(item.rawName) ?? undefined,
|
||||
checked: item.inPantry === true && !item.quantity,
|
||||
inPantry: item.inPantry ?? false,
|
||||
sortOrder: index,
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user