This commit is contained in:
root
2025-12-20 19:00:42 +01:00
parent 9871289bfb
commit e1a0966dee
23 changed files with 1878 additions and 48 deletions

View File

@@ -4,12 +4,42 @@ import pool from '@/lib/db'
// GET /api/drops/active - Get the earliest unfilled drop (not sold out)
export async function GET() {
try {
// Get all drops ordered by creation date
const [rows] = await pool.execute(
'SELECT * FROM drops WHERE fill < size ORDER BY created_at ASC LIMIT 1'
'SELECT * FROM drops ORDER BY created_at ASC'
)
const drops = rows as any[]
return NextResponse.json(drops[0] || null)
// Find the first drop that's not fully sold out
for (const drop of drops) {
// Calculate fill from sales records
// Sales are stored in grams, so we need to convert based on drop unit
const [salesRows] = await pool.execute(
'SELECT COALESCE(SUM(size), 0) as total_fill FROM sales WHERE drop_id = ?',
[drop.id]
)
const salesData = salesRows as any[]
const totalFillInGrams = salesData[0]?.total_fill || 0
// Convert fill to drop's unit for comparison
let fill = totalFillInGrams
if (drop.unit === 'kg') {
fill = totalFillInGrams / 1000
}
// Check if drop is not fully sold out
if (fill < drop.size) {
// Return drop with calculated fill
return NextResponse.json({
...drop,
fill: fill,
})
}
}
// No active drops found
return NextResponse.json(null)
} catch (error) {
console.error('Error fetching active drop:', error)
return NextResponse.json(