Files
cbd420/app/api/sales/list/route.ts
2025-12-21 11:39:41 +01:00

39 lines
1.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import pool from '@/lib/db'
// GET /api/sales/list - Get all sales with related data
export async function GET(request: NextRequest) {
try {
const [rows] = await pool.execute(
`SELECT
s.id,
s.drop_id,
s.buyer_id,
s.size,
s.payment_id,
s.created_at,
d.item as drop_item,
d.unit as drop_unit,
d.ppu as drop_ppu,
b.username as buyer_username,
b.email as buyer_email,
bd.fullname as buyer_fullname,
bd.address as buyer_address,
bd.phone as buyer_phone
FROM sales s
LEFT JOIN drops d ON s.drop_id = d.id
LEFT JOIN buyers b ON s.buyer_id = b.id
LEFT JOIN buyer_data bd ON s.buyer_data_id = bd.id
ORDER BY s.created_at DESC`
)
return NextResponse.json(rows)
} catch (error) {
console.error('Error fetching sales:', error)
return NextResponse.json(
{ error: 'Failed to fetch sales' },
{ status: 500 }
)
}
}