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

@@ -7,7 +7,34 @@ export async function GET(request: NextRequest) {
const [rows] = await pool.execute(
'SELECT * FROM drops ORDER BY created_at DESC'
)
return NextResponse.json(rows)
const drops = rows as any[]
// Calculate fill from sales for each drop
const dropsWithFill = await Promise.all(
drops.map(async (drop) => {
// 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
}
return {
...drop,
fill: fill,
}
})
)
return NextResponse.json(dropsWithFill)
} catch (error) {
console.error('Error fetching drops:', error)
return NextResponse.json(
@@ -34,8 +61,9 @@ export async function POST(request: NextRequest) {
// Insert new drop
// Note: If imageUrl column doesn't exist in database, add it with:
// 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, fill, image_url) VALUES (?, ?, ?, ?, 0, ?)',
'INSERT INTO drops (item, size, unit, ppu, image_url) VALUES (?, ?, ?, ?, ?)',
[item, size, unit, ppu, imageUrl || null]
)
@@ -46,7 +74,13 @@ export async function POST(request: NextRequest) {
insertId,
])
return NextResponse.json(rows[0], { status: 201 })
const drop = rows[0] as any
// Return drop with calculated fill (will be 0 for new drop)
return NextResponse.json({
...drop,
fill: 0,
}, { status: 201 })
} catch (error) {
console.error('Error creating drop:', error)
return NextResponse.json(