feat(context): GET /api/v1/order-items/[id]/context route

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
SJCoetz 2026-08-07 02:46:18 +02:00
parent 351c1b2b96
commit 7ad7375dd2

View file

@ -0,0 +1,35 @@
import { NextRequest, NextResponse } from "next/server";
import { getOrderItemContext } from "@/lib/server/order-item-context-service";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
/**
* GET /api/v1/order-items/[id]/context
*
* Deterministic SKU-based context for an order_item: last quote,
* last order, last invoice checked in, stock state, supplier
* history, cheapest. Used by the sprint process modal (L1 inline)
* and the orders detail page (L2 popover).
*
* No Bearer required — matches the existing item-history route's
* read-only SKU aggregates pattern. Auth happens upstream at the
* page level.
*/
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
if (!id || typeof id !== "string") {
return NextResponse.json({ error: "Missing id" }, { status: 400 });
}
try {
return NextResponse.json(await getOrderItemContext(id));
} catch (err) {
return NextResponse.json(
{ error: err instanceof Error ? err.message : "Lookup failed" },
{ status: 500 },
);
}
}