This commit is contained in:
root
2025-12-20 22:05:21 +01:00
parent e1a0966dee
commit f2c3b04a88
10 changed files with 1134 additions and 28 deletions

View File

@@ -1,18 +1,45 @@
import { NextResponse } from 'next/server'
import pool from '@/lib/db'
// GET /api/drops/active - Get the earliest unfilled drop (not sold out)
// GET /api/drops/active - Get the earliest unfilled drop (not sold out) that has started
export async function GET() {
try {
// Get all drops ordered by creation date
const now = new Date()
// Get all drops ordered by start_time (or created_at if start_time is null)
const [rows] = await pool.execute(
'SELECT * FROM drops ORDER BY created_at ASC'
'SELECT * FROM drops ORDER BY COALESCE(start_time, created_at) ASC'
)
const drops = rows as any[]
// Find the first drop that's not fully sold out
// Find the first drop that's not fully sold out and has started
for (const drop of drops) {
// Check if drop has started (start_time is in the past or null)
const startTime = drop.start_time ? new Date(drop.start_time) : new Date(drop.created_at)
if (startTime > now) {
// Drop hasn't started yet - return it with a flag indicating it's upcoming
// Calculate fill (will be 0 for upcoming drops)
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
let fill = totalFillInGrams
if (drop.unit === 'kg') {
fill = totalFillInGrams / 1000
}
return NextResponse.json({
...drop,
fill: fill,
is_upcoming: true,
start_time: drop.start_time || drop.created_at,
})
}
// Calculate fill from sales records
// Sales are stored in grams, so we need to convert based on drop unit
const [salesRows] = await pool.execute(
@@ -34,6 +61,8 @@ export async function GET() {
return NextResponse.json({
...drop,
fill: fill,
is_upcoming: false,
start_time: drop.start_time || drop.created_at,
})
}
}

View File

@@ -48,7 +48,7 @@ export async function GET(request: NextRequest) {
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { item, size, unit = 'g', ppu, imageUrl } = body
const { item, size, unit = 'g', ppu, imageUrl, startTime } = body
// Validate required fields
if (!item || !size || !ppu) {
@@ -63,8 +63,8 @@ export async function POST(request: NextRequest) {
// ALTER TABLE drops ADD COLUMN image_url VARCHAR(255) DEFAULT NULL AFTER unit;
// Note: fill is no longer stored, it's calculated from sales
const [result] = await pool.execute(
'INSERT INTO drops (item, size, unit, ppu, image_url) VALUES (?, ?, ?, ?, ?)',
[item, size, unit, ppu, imageUrl || null]
'INSERT INTO drops (item, size, unit, ppu, image_url, start_time) VALUES (?, ?, ?, ?, ?, ?)',
[item, size, unit, ppu, imageUrl || null, startTime || null]
)
const insertId = (result as any).insertId