52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import pool from '@/lib/db'
|
|
|
|
// GET /api/sales/drop/[dropId] - Get all sales for a specific drop
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { dropId: string } }
|
|
) {
|
|
try {
|
|
const dropId = parseInt(params.dropId, 10)
|
|
if (isNaN(dropId)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid drop ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
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
|
|
WHERE s.drop_id = ?
|
|
ORDER BY s.created_at DESC`,
|
|
[dropId]
|
|
)
|
|
return NextResponse.json(rows)
|
|
} catch (error) {
|
|
console.error('Error fetching sales for drop:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch sales' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|