220 lines
5.1 KiB
TypeScript
220 lines
5.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import pool from '@/lib/db'
|
|
|
|
// GET /api/sales/[id] - Get a specific sale
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const id = parseInt(params.id, 10)
|
|
if (isNaN(id)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid sale 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
|
|
FROM sales s
|
|
LEFT JOIN drops d ON s.drop_id = d.id
|
|
LEFT JOIN buyers b ON s.buyer_id = b.id
|
|
WHERE s.id = ?`,
|
|
[id]
|
|
)
|
|
|
|
const sales = rows as any[]
|
|
if (sales.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Sale not found' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
return NextResponse.json(sales[0])
|
|
} catch (error) {
|
|
console.error('Error fetching sale:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch sale' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// PUT /api/sales/[id] - Update a sale
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const id = parseInt(params.id, 10)
|
|
if (isNaN(id)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid sale ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { drop_id, buyer_id, size, payment_id } = body
|
|
|
|
// Check if sale exists
|
|
const [existingRows] = await pool.execute(
|
|
'SELECT id FROM sales WHERE id = ?',
|
|
[id]
|
|
)
|
|
const existing = existingRows as any[]
|
|
if (existing.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Sale not found' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// Build update query dynamically
|
|
const updates: string[] = []
|
|
const values: any[] = []
|
|
|
|
if (drop_id !== undefined) {
|
|
// Verify drop exists
|
|
const [dropCheck] = await pool.execute(
|
|
'SELECT id FROM drops WHERE id = ?',
|
|
[drop_id]
|
|
)
|
|
if ((dropCheck as any[]).length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Drop not found' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
updates.push('drop_id = ?')
|
|
values.push(drop_id)
|
|
}
|
|
|
|
if (buyer_id !== undefined) {
|
|
// Verify buyer exists
|
|
const [buyerCheck] = await pool.execute(
|
|
'SELECT id FROM buyers WHERE id = ?',
|
|
[buyer_id]
|
|
)
|
|
if ((buyerCheck as any[]).length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Buyer not found' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
updates.push('buyer_id = ?')
|
|
values.push(buyer_id)
|
|
}
|
|
|
|
if (size !== undefined) {
|
|
if (size <= 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Size must be greater than 0' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
updates.push('size = ?')
|
|
values.push(size)
|
|
}
|
|
|
|
if (payment_id !== undefined) {
|
|
updates.push('payment_id = ?')
|
|
values.push(payment_id)
|
|
}
|
|
|
|
if (updates.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'No fields to update' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
values.push(id)
|
|
const query = `UPDATE sales SET ${updates.join(', ')} WHERE id = ?`
|
|
await pool.execute(query, values)
|
|
|
|
// Fetch updated sale
|
|
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
|
|
FROM sales s
|
|
LEFT JOIN drops d ON s.drop_id = d.id
|
|
LEFT JOIN buyers b ON s.buyer_id = b.id
|
|
WHERE s.id = ?`,
|
|
[id]
|
|
)
|
|
|
|
return NextResponse.json((rows as any[])[0])
|
|
} catch (error) {
|
|
console.error('Error updating sale:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to update sale' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// DELETE /api/sales/[id] - Delete a sale
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const id = parseInt(params.id, 10)
|
|
if (isNaN(id)) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid sale ID' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Check if sale exists
|
|
const [existingRows] = await pool.execute(
|
|
'SELECT id FROM sales WHERE id = ?',
|
|
[id]
|
|
)
|
|
const existing = existingRows as any[]
|
|
if (existing.length === 0) {
|
|
return NextResponse.json(
|
|
{ error: 'Sale not found' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
// Delete sale
|
|
await pool.execute('DELETE FROM sales WHERE id = ?', [id])
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error deleting sale:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to delete sale' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|