final
This commit is contained in:
54
app/api/orders/route.ts
Normal file
54
app/api/orders/route.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { cookies } from 'next/headers'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// GET /api/orders - Get all orders (sales) for the current user
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// Get buyer_id from session cookie
|
||||
const cookieStore = await cookies()
|
||||
const buyerIdCookie = cookieStore.get('buyer_id')?.value
|
||||
|
||||
if (!buyerIdCookie) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Authentication required' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const buyer_id = parseInt(buyerIdCookie, 10)
|
||||
|
||||
// Get all sales for this buyer with drop and buyer_data information
|
||||
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,
|
||||
d.image_url as drop_image_url,
|
||||
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 buyer_data bd ON s.buyer_data_id = bd.id
|
||||
WHERE s.buyer_id = ?
|
||||
ORDER BY s.created_at DESC`,
|
||||
[buyer_id]
|
||||
)
|
||||
|
||||
return NextResponse.json(rows)
|
||||
} catch (error) {
|
||||
console.error('Error fetching orders:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch orders' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user