buyer data
This commit is contained in:
74
app/api/buyer-data/get-or-create/route.ts
Normal file
74
app/api/buyer-data/get-or-create/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { cookies } from 'next/headers'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// POST /api/buyer-data/get-or-create - Get existing buyer_data or create new one
|
||||
export async function POST(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)
|
||||
const body = await request.json()
|
||||
const { fullname, address, phone } = body
|
||||
|
||||
// Validate required fields
|
||||
if (!fullname || !address || !phone) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Full name, address, and phone are required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Validate phone format (basic validation - 10-15 digits)
|
||||
const phoneRegex = /^[+]?[\d\s\-()]{10,15}$/
|
||||
if (!phoneRegex.test(phone)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid phone number format' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Check if buyer_data with same values already exists for this buyer
|
||||
const [existingRows] = await pool.execute(
|
||||
'SELECT id FROM buyer_data WHERE buyer_id = ? AND fullname = ? AND address = ? AND phone = ?',
|
||||
[buyer_id, fullname.trim(), address.trim(), phone.trim()]
|
||||
)
|
||||
|
||||
const existing = existingRows as any[]
|
||||
if (existing.length > 0) {
|
||||
// Return existing buyer_data_id
|
||||
return NextResponse.json({
|
||||
buyer_data_id: existing[0].id,
|
||||
created: false,
|
||||
})
|
||||
}
|
||||
|
||||
// Create new buyer_data record
|
||||
const [result] = await pool.execute(
|
||||
'INSERT INTO buyer_data (buyer_id, fullname, address, phone) VALUES (?, ?, ?, ?)',
|
||||
[buyer_id, fullname.trim(), address.trim(), phone.trim()]
|
||||
)
|
||||
|
||||
const buyer_data_id = (result as any).insertId
|
||||
|
||||
return NextResponse.json({
|
||||
buyer_data_id,
|
||||
created: true,
|
||||
}, { status: 201 })
|
||||
} catch (error) {
|
||||
console.error('Error getting or creating buyer_data:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to process buyer data' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
47
app/api/buyer-data/route.ts
Normal file
47
app/api/buyer-data/route.ts
Normal 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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user