sync
This commit is contained in:
@@ -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(
|
||||
|
||||
65
app/api/drops/past/route.ts
Normal file
65
app/api/drops/past/route.ts
Normal 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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user