buyer data

This commit is contained in:
root
2025-12-21 11:12:02 +01:00
parent 67646c75a4
commit 514e04f43d
6 changed files with 359 additions and 23 deletions

View File

@@ -0,0 +1,47 @@
import { NextRequest, NextResponse } from 'next/server'
import { cookies } from 'next/headers'
import pool from '@/lib/db'
// GET /api/buyer-data - Get the most recent buyer_data for the current buyer
export async function GET(request: NextRequest) {
try {
const cookieStore = 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 the most recent buyer_data for this buyer
// Note: buyer_data table doesn't have created_at, so we'll get the one with highest ID (most recent)
const [rows] = await pool.execute(
'SELECT id, fullname, address, phone FROM buyer_data WHERE buyer_id = ? ORDER BY id DESC LIMIT 1',
[buyer_id]
)
const buyerData = rows as any[]
if (buyerData.length === 0) {
return NextResponse.json(
{ buyer_data: null },
{ status: 200 }
)
}
return NextResponse.json({
buyer_data: buyerData[0],
})
} catch (error) {
console.error('Error fetching buyer_data:', error)
return NextResponse.json(
{ error: 'Failed to fetch buyer data' },
{ status: 500 }
)
}
}