54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getDb } from "@/lib/db";
|
|
|
|
export async function GET(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ productId: string }> }
|
|
) {
|
|
const sql = getDb();
|
|
const { productId } = await params;
|
|
|
|
const rows = await sql(
|
|
`SELECT bi.*,
|
|
c.name as component_name,
|
|
c.part_number,
|
|
c.unit_price,
|
|
c.stock_quantity,
|
|
cat.name as category_name,
|
|
(bi.quantity * c.unit_price) as line_cost
|
|
FROM bom_items bi
|
|
JOIN components c ON bi.component_id = c.id
|
|
LEFT JOIN categories cat ON c.category_id = cat.id
|
|
WHERE bi.product_id = $1
|
|
ORDER BY bi.reference_designator ASC, c.name ASC`,
|
|
[Number(productId)]
|
|
);
|
|
|
|
return NextResponse.json(rows);
|
|
}
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ productId: string }> }
|
|
) {
|
|
const sql = getDb();
|
|
const { productId } = await params;
|
|
const body = await request.json();
|
|
const { component_id, quantity, reference_designator, notes } = body;
|
|
|
|
const rows = await sql(
|
|
`INSERT INTO bom_items (product_id, component_id, quantity, reference_designator, notes)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING *`,
|
|
[
|
|
Number(productId),
|
|
component_id,
|
|
quantity || 1,
|
|
reference_designator || null,
|
|
notes || null,
|
|
]
|
|
);
|
|
|
|
return NextResponse.json(rows[0], { status: 201 });
|
|
}
|