Pantry now runs on the v1 API like Inventory/Animals: service layer in src/lib/services/pantry.ts, pure logic + tests in src/lib/pantry/core.ts, thin routes at /api/v1/pantry/* riding the items:* scopes. Pulls write ItemLog type USED with a structured amount (queryable consumption). Bulk-use runs its reads inside the transaction, accumulates repeated pulls, and reports skipped items instead of silently dropping them — the Pull dialog now surfaces those by name. Old /api/pantry routes deleted. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
18 lines
729 B
TypeScript
18 lines
729 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { authenticateRequest, handleApiError } from "@/lib/api/authenticate";
|
|
import { pantryBulkUseInput } from "@/lib/api/schemas";
|
|
import { bulkUsePantry } from "@/lib/services/pantry";
|
|
|
|
// POST /api/v1/pantry/bulk-use — pull several items at once.
|
|
// Responds with `skipped` for any pull whose item no longer exists.
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const ctx = await authenticateRequest(req, "items:write");
|
|
const input = pantryBulkUseInput.parse(await req.json());
|
|
const result = await bulkUsePantry(ctx, input);
|
|
return NextResponse.json({ ok: result.skipped.length === 0, ...result });
|
|
} catch (err) {
|
|
return handleApiError(err);
|
|
}
|
|
}
|