Files
website-secure/app/api/supplier-packing-lists/[id]/route.ts
T
pm 9c14a19a87
DevSecOps Enterprise Pipeline / security-gate-and-deploy (push) Failing after 41s
chore: upload parte 1 - logica
2026-06-01 15:35:53 +01:00

87 lines
2.7 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getDb } from "@/lib/db";
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const sql = getDb();
const { id } = await params;
const plRows = await sql(
`SELECT spl.*,
s.name AS supplier_name,
s.code AS supplier_code,
s.email AS supplier_email,
s.phone AS supplier_phone,
s.address AS supplier_address,
s.contact_person AS supplier_contact
FROM supplier_packing_lists spl
JOIN suppliers s ON s.id = spl.supplier_id
WHERE spl.id = $1`,
[Number(id)]
);
if (plRows.length === 0) {
return NextResponse.json({ error: "Supplier packing list not found" }, { status: 404 });
}
// Fetch items with component details and products where each component is used
const items = await sql(
`SELECT spli.*,
c.part_number,
c.name AS component_name,
(
SELECT STRING_AGG(DISTINCT p.name, ', ' ORDER BY p.name)
FROM bom_items bi
JOIN products p ON p.id = bi.product_id
WHERE bi.component_id = spli.component_id
) AS used_in_products
FROM supplier_packing_list_items spli
JOIN components c ON c.id = spli.component_id
WHERE spli.supplier_packing_list_id = $1
ORDER BY spli.id ASC`,
[Number(id)]
);
return NextResponse.json({ ...plRows[0], items });
} catch (error) {
console.error("[v0] Supplier packing list detail GET error:", error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Failed to fetch supplier packing list" },
{ status: 500 }
);
}
}
export async function DELETE(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const sql = getDb();
const { id } = await params;
const plRows = await sql(
`SELECT * FROM supplier_packing_lists WHERE id = $1`,
[Number(id)]
);
if (plRows.length === 0) {
return NextResponse.json({ error: "Supplier packing list not found" }, { status: 404 });
}
// Delete items then packing list
await sql(`DELETE FROM supplier_packing_list_items WHERE supplier_packing_list_id = $1`, [Number(id)]);
await sql(`DELETE FROM supplier_packing_lists WHERE id = $1`, [Number(id)]);
return NextResponse.json({ success: true });
} catch (error) {
console.error("[v0] Supplier packing list DELETE error:", error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Failed to delete supplier packing list" },
{ status: 500 }
);
}
}