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

@@ -0,0 +1,65 @@
import { NextResponse } from 'next/server'
import pool from '@/lib/db'
// GET /api/drops/past - Get all sold-out drops
export async function GET() {
try {
// Get all drops
const [rows] = await pool.execute(
'SELECT * FROM drops ORDER BY created_at DESC'
)
const drops = rows as any[]
// Calculate fill from sales for each drop and filter sold-out ones
const soldOutDrops = []
for (const drop of drops) {
// Calculate fill from sales records
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 sold out (fill >= size)
if (fill >= drop.size) {
// Get the timestamp of the last sale to calculate "sold out in X hours"
const [lastSaleRows] = await pool.execute(
'SELECT created_at FROM sales WHERE drop_id = ? ORDER BY created_at DESC LIMIT 1',
[drop.id]
)
const lastSaleData = lastSaleRows as any[]
const lastSaleDate = lastSaleData[0]?.created_at || drop.created_at
// Calculate hours between drop creation and last sale
const dropDate = new Date(drop.created_at)
const soldOutDate = new Date(lastSaleDate)
const hoursDiff = Math.round(
(soldOutDate.getTime() - dropDate.getTime()) / (1000 * 60 * 60)
)
soldOutDrops.push({
...drop,
fill: fill,
soldOutInHours: hoursDiff,
})
}
}
return NextResponse.json(soldOutDrops)
} catch (error) {
console.error('Error fetching past drops:', error)
return NextResponse.json(
{ error: 'Failed to fetch past drops' },
{ status: 500 }
)
}
}