chore: upload parte 1 - logica
DevSecOps Enterprise Pipeline / security-gate-and-deploy (push) Failing after 41s
DevSecOps Enterprise Pipeline / security-gate-and-deploy (push) Failing after 41s
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
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 rows = await sql(
|
||||
`SELECT poi.*,
|
||||
p.name AS product_name,
|
||||
p.sku AS product_sku,
|
||||
(poi.quantity * poi.unit_price) AS line_total,
|
||||
COALESCE(poi.shipped_quantity, 0) AS shipped_quantity,
|
||||
(poi.quantity - COALESCE(poi.shipped_quantity, 0)) AS remaining_quantity
|
||||
FROM purchase_order_items poi
|
||||
JOIN products p ON p.id = poi.product_id
|
||||
WHERE poi.purchase_order_id = $1
|
||||
ORDER BY poi.id ASC`,
|
||||
[Number(id)]
|
||||
);
|
||||
|
||||
return NextResponse.json(rows);
|
||||
} catch (error) {
|
||||
console.error("[v0] PO items GET error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to fetch PO items" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
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 orderRows = await sql(
|
||||
`SELECT po.*, c.name AS customer_name, c.code AS customer_code,
|
||||
c.email AS customer_email, c.phone AS customer_phone,
|
||||
c.address AS customer_address, c.contact_person AS customer_contact
|
||||
FROM purchase_orders po
|
||||
JOIN customers c ON c.id = po.customer_id
|
||||
WHERE po.id = $1`,
|
||||
[Number(id)]
|
||||
);
|
||||
|
||||
if (orderRows.length === 0) {
|
||||
return NextResponse.json({ error: "Order not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
const items = await sql(
|
||||
`SELECT poi.*, p.name AS product_name, p.sku AS product_sku,
|
||||
(poi.quantity * poi.unit_price) AS line_total,
|
||||
COALESCE(poi.shipped_quantity, 0) AS shipped_quantity,
|
||||
(poi.quantity - COALESCE(poi.shipped_quantity, 0)) AS remaining_quantity
|
||||
FROM purchase_order_items poi
|
||||
JOIN products p ON p.id = poi.product_id
|
||||
WHERE poi.purchase_order_id = $1
|
||||
ORDER BY poi.created_at ASC`,
|
||||
[Number(id)]
|
||||
);
|
||||
|
||||
return NextResponse.json({ ...orderRows[0], items });
|
||||
} catch (error) {
|
||||
console.error("[v0] Order GET error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to fetch order" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const sql = getDb();
|
||||
const { id } = await params;
|
||||
const body = await request.json();
|
||||
const { customer_id, po_number, customer_ref, status, order_date, due_date, notes, items } = body;
|
||||
|
||||
const orderRows = await sql(
|
||||
`UPDATE purchase_orders SET
|
||||
customer_id = $1, po_number = $2, customer_ref = $3, status = $4,
|
||||
order_date = $5, due_date = $6, notes = $7, updated_at = NOW()
|
||||
WHERE id = $8
|
||||
RETURNING *`,
|
||||
[customer_id, po_number, customer_ref || null, status, order_date, due_date || null, notes || null, Number(id)]
|
||||
);
|
||||
|
||||
if (orderRows.length === 0) {
|
||||
return NextResponse.json({ error: "Order not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Replace items
|
||||
if (items && Array.isArray(items)) {
|
||||
await sql(`DELETE FROM purchase_order_items WHERE purchase_order_id = $1`, [Number(id)]);
|
||||
for (const item of items) {
|
||||
await sql(
|
||||
`INSERT INTO purchase_order_items (purchase_order_id, product_id, quantity, unit_price, notes)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
[Number(id), item.product_id, item.quantity, item.unit_price || 0, item.notes || null]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(orderRows[0]);
|
||||
} catch (error) {
|
||||
console.error("[v0] Order PUT error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to update order" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
_request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const sql = getDb();
|
||||
const { id } = await params;
|
||||
|
||||
await sql(`DELETE FROM purchase_order_items WHERE purchase_order_id = $1`, [Number(id)]);
|
||||
await sql(`DELETE FROM purchase_orders WHERE id = $1`, [Number(id)]);
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error("[v0] Order DELETE error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to delete order" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getDb } from "@/lib/db";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const sql = getDb();
|
||||
const { searchParams } = new URL(request.url);
|
||||
const customerId = searchParams.get("customerId");
|
||||
const status = searchParams.get("status");
|
||||
const search = searchParams.get("search") || "";
|
||||
|
||||
let query = `
|
||||
SELECT po.*,
|
||||
c.name AS customer_name,
|
||||
c.code AS customer_code,
|
||||
COUNT(poi.id) AS item_count,
|
||||
COALESCE(SUM(poi.quantity * poi.unit_price), 0) AS total_value,
|
||||
COALESCE(SUM(poi.quantity), 0) AS total_ordered,
|
||||
COALESCE(SUM(poi.shipped_quantity), 0) AS total_shipped
|
||||
FROM purchase_orders po
|
||||
JOIN customers c ON c.id = po.customer_id
|
||||
LEFT JOIN purchase_order_items poi ON poi.purchase_order_id = po.id
|
||||
`;
|
||||
|
||||
const conditions: string[] = [];
|
||||
const params: (string | number)[] = [];
|
||||
let idx = 1;
|
||||
|
||||
if (customerId) {
|
||||
conditions.push(`po.customer_id = $${idx++}`);
|
||||
params.push(Number(customerId));
|
||||
}
|
||||
if (status) {
|
||||
const statuses = status.split(",").map((s) => s.trim());
|
||||
const placeholders = statuses.map(() => `$${idx++}`).join(", ");
|
||||
conditions.push(`po.status IN (${placeholders})`);
|
||||
params.push(...statuses);
|
||||
}
|
||||
if (search) {
|
||||
conditions.push(
|
||||
`(po.po_number ILIKE $${idx} OR c.name ILIKE $${idx} OR po.customer_ref ILIKE $${idx})`
|
||||
);
|
||||
params.push(`%${search}%`);
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (conditions.length > 0) {
|
||||
query += ` WHERE ${conditions.join(" AND ")}`;
|
||||
}
|
||||
|
||||
query += ` GROUP BY po.id, c.name, c.code ORDER BY po.created_at DESC`;
|
||||
|
||||
const rows = await sql(query, params);
|
||||
return NextResponse.json(rows);
|
||||
} catch (error) {
|
||||
console.error("[v0] Orders GET error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to fetch orders" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const sql = getDb();
|
||||
const body = await request.json();
|
||||
const {
|
||||
customer_id,
|
||||
po_number,
|
||||
customer_ref,
|
||||
status,
|
||||
order_date,
|
||||
due_date,
|
||||
notes,
|
||||
items,
|
||||
} = body;
|
||||
|
||||
if (!customer_id || !po_number) {
|
||||
return NextResponse.json(
|
||||
{ error: "Customer and PO number are required" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create the order
|
||||
const orderRows = await sql(
|
||||
`INSERT INTO purchase_orders (customer_id, po_number, customer_ref, status, order_date, due_date, notes)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING *`,
|
||||
[
|
||||
customer_id,
|
||||
po_number,
|
||||
customer_ref || null,
|
||||
status || "draft",
|
||||
order_date || new Date().toISOString().slice(0, 10),
|
||||
due_date || null,
|
||||
notes || null,
|
||||
]
|
||||
);
|
||||
|
||||
const order = orderRows[0];
|
||||
|
||||
// Create items if provided
|
||||
if (items && Array.isArray(items) && items.length > 0) {
|
||||
for (const item of items) {
|
||||
await sql(
|
||||
`INSERT INTO purchase_order_items (purchase_order_id, product_id, quantity, unit_price, notes)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
[
|
||||
order.id,
|
||||
item.product_id,
|
||||
item.quantity,
|
||||
item.unit_price || 0,
|
||||
item.notes || null,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(order, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("[v0] Orders POST error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to create order" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user