28 lines
928 B
TypeScript
28 lines
928 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { getDb } from "@/lib/db";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const sql = getDb();
|
|
const rows = await sql(
|
|
`SELECT ph.*,
|
|
COUNT(phi.id) AS item_count,
|
|
COALESCE(SUM(phi.quantity_needed), 0) AS total_quantity,
|
|
COUNT(CASE WHEN phi.shortage > 0 THEN 1 END) AS shortage_count,
|
|
(SELECT string_agg(DISTINCT sp.po_number, ', ' ORDER BY sp.po_number)
|
|
FROM supplier_pos sp WHERE sp.planning_id = ph.id) AS linked_po_numbers
|
|
FROM planning_history ph
|
|
LEFT JOIN planning_history_items phi ON phi.planning_id = ph.id
|
|
GROUP BY ph.id
|
|
ORDER BY ph.created_at DESC`
|
|
);
|
|
return NextResponse.json(rows);
|
|
} catch (error) {
|
|
console.error("Error fetching planning history:", error);
|
|
return NextResponse.json(
|
|
{ error: "Failed to fetch planning history" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|