notification + admin panel
This commit is contained in:
12
app/api/admin/check/route.ts
Normal file
12
app/api/admin/check/route.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { isAdminAuthenticated } from '@/lib/admin-auth'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const authenticated = await isAdminAuthenticated()
|
||||
return NextResponse.json({ authenticated })
|
||||
} catch (error) {
|
||||
return NextResponse.json({ authenticated: false })
|
||||
}
|
||||
}
|
||||
|
||||
33
app/api/admin/login/route.ts
Normal file
33
app/api/admin/login/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { verifyAdminPassword, setAdminSession } from '@/lib/admin-auth'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { password } = body
|
||||
|
||||
if (!password) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Password is required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
if (verifyAdminPassword(password)) {
|
||||
await setAdminSession()
|
||||
return NextResponse.json({ success: true })
|
||||
} else {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid password' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error during admin login:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to process login' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
16
app/api/admin/logout/route.ts
Normal file
16
app/api/admin/logout/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { clearAdminSession } from '@/lib/admin-auth'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await clearAdminSession()
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error('Error during admin logout:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to process logout' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
215
app/api/drops/[id]/route.ts
Normal file
215
app/api/drops/[id]/route.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// GET /api/drops/[id] - Get a specific drop
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = parseInt(params.id, 10)
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid drop ID' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const [rows] = await pool.execute(
|
||||
'SELECT * FROM drops WHERE id = ?',
|
||||
[id]
|
||||
)
|
||||
|
||||
const drops = rows as any[]
|
||||
if (drops.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Drop not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
const drop = drops[0]
|
||||
|
||||
// Calculate fill from sales
|
||||
const [salesRows] = await pool.execute(
|
||||
'SELECT COALESCE(SUM(size), 0) as total_fill FROM sales WHERE drop_id = ?',
|
||||
[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,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching drop:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch drop' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// PUT /api/drops/[id] - Update a drop
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = parseInt(params.id, 10)
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid drop ID' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { item, size, unit, ppu, imageUrl, startTime } = body
|
||||
|
||||
// Check if drop exists
|
||||
const [existingRows] = await pool.execute(
|
||||
'SELECT id FROM drops WHERE id = ?',
|
||||
[id]
|
||||
)
|
||||
const existing = existingRows as any[]
|
||||
if (existing.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Drop not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// Build update query dynamically
|
||||
const updates: string[] = []
|
||||
const values: any[] = []
|
||||
|
||||
if (item !== undefined) {
|
||||
updates.push('item = ?')
|
||||
values.push(item)
|
||||
}
|
||||
|
||||
if (size !== undefined) {
|
||||
if (size <= 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Size must be greater than 0' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
updates.push('size = ?')
|
||||
values.push(size)
|
||||
}
|
||||
|
||||
if (unit !== undefined) {
|
||||
updates.push('unit = ?')
|
||||
values.push(unit)
|
||||
}
|
||||
|
||||
if (ppu !== undefined) {
|
||||
if (ppu <= 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Price per unit must be greater than 0' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
updates.push('ppu = ?')
|
||||
values.push(ppu)
|
||||
}
|
||||
|
||||
if (imageUrl !== undefined) {
|
||||
updates.push('image_url = ?')
|
||||
values.push(imageUrl || null)
|
||||
}
|
||||
|
||||
if (startTime !== undefined) {
|
||||
updates.push('start_time = ?')
|
||||
values.push(startTime || null)
|
||||
}
|
||||
|
||||
if (updates.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No fields to update' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
values.push(id)
|
||||
const query = `UPDATE drops SET ${updates.join(', ')} WHERE id = ?`
|
||||
await pool.execute(query, values)
|
||||
|
||||
// Fetch updated drop
|
||||
const [rows] = await pool.execute('SELECT * FROM drops WHERE id = ?', [id])
|
||||
const drop = rows[0] as any
|
||||
|
||||
// Calculate fill
|
||||
const [salesRows] = await pool.execute(
|
||||
'SELECT COALESCE(SUM(size), 0) as total_fill FROM sales WHERE drop_id = ?',
|
||||
[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,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error updating drop:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update drop' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/drops/[id] - Delete a drop
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const id = parseInt(params.id, 10)
|
||||
if (isNaN(id)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid drop ID' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Check if drop exists
|
||||
const [existingRows] = await pool.execute(
|
||||
'SELECT id FROM drops WHERE id = ?',
|
||||
[id]
|
||||
)
|
||||
const existing = existingRows as any[]
|
||||
if (existing.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Drop not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
// Delete drop (cascade will handle related sales)
|
||||
await pool.execute('DELETE FROM drops WHERE id = ?', [id])
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error('Error deleting drop:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete drop' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
76
app/api/notifications/subscribe/route.ts
Normal file
76
app/api/notifications/subscribe/route.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { cookies } from 'next/headers'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// POST /api/notifications/subscribe - Subscribe to notifications
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Get buyer_id from session cookie if logged in
|
||||
const cookieStore = await cookies()
|
||||
const buyerIdCookie = cookieStore.get('buyer_id')?.value
|
||||
const buyer_id = buyerIdCookie ? parseInt(buyerIdCookie, 10) : null
|
||||
|
||||
const body = await request.json()
|
||||
const { email, phone } = body
|
||||
|
||||
// Validate that at least one field is provided
|
||||
if (!email && !phone) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Email or phone number is required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Validate email format if provided
|
||||
if (email) {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
if (!emailRegex.test(email)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid email format' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate phone format if provided (basic validation)
|
||||
if (phone) {
|
||||
const phoneRegex = /^[+]?[\d\s\-()]{10,15}$/
|
||||
if (!phoneRegex.test(phone)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid phone number format' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Insert email subscription if provided
|
||||
// Using INSERT IGNORE to handle duplicate addresses (address is now primary key)
|
||||
if (email) {
|
||||
await pool.execute(
|
||||
'INSERT IGNORE INTO notification_subscribers (buyer_id, type, address) VALUES (?, ?, ?)',
|
||||
[buyer_id, 'email', email.trim()]
|
||||
)
|
||||
}
|
||||
|
||||
// Insert phone subscription if provided
|
||||
// Using INSERT IGNORE to handle duplicate addresses (address is now primary key)
|
||||
if (phone) {
|
||||
await pool.execute(
|
||||
'INSERT IGNORE INTO notification_subscribers (buyer_id, type, address) VALUES (?, ?, ?)',
|
||||
[buyer_id, 'phone', phone.trim()]
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: true, message: 'Successfully subscribed to notifications' },
|
||||
{ status: 200 }
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('Error subscribing to notifications:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to subscribe to notifications' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
51
app/api/sales/drop/[dropId]/route.ts
Normal file
51
app/api/sales/drop/[dropId]/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// GET /api/sales/drop/[dropId] - Get all sales for a specific drop
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { dropId: string } }
|
||||
) {
|
||||
try {
|
||||
const dropId = parseInt(params.dropId, 10)
|
||||
if (isNaN(dropId)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid drop ID' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const [rows] = await pool.execute(
|
||||
`SELECT
|
||||
s.id,
|
||||
s.drop_id,
|
||||
s.buyer_id,
|
||||
s.size,
|
||||
s.payment_id,
|
||||
s.created_at,
|
||||
d.item as drop_item,
|
||||
d.unit as drop_unit,
|
||||
d.ppu as drop_ppu,
|
||||
b.username as buyer_username,
|
||||
b.email as buyer_email,
|
||||
bd.fullname as buyer_fullname,
|
||||
bd.address as buyer_address,
|
||||
bd.phone as buyer_phone
|
||||
FROM sales s
|
||||
LEFT JOIN drops d ON s.drop_id = d.id
|
||||
LEFT JOIN buyers b ON s.buyer_id = b.id
|
||||
LEFT JOIN buyer_data bd ON s.buyer_data_id = bd.id
|
||||
WHERE s.drop_id = ?
|
||||
ORDER BY s.created_at DESC`,
|
||||
[dropId]
|
||||
)
|
||||
return NextResponse.json(rows)
|
||||
} catch (error) {
|
||||
console.error('Error fetching sales for drop:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch sales' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,14 @@ export async function GET(request: NextRequest) {
|
||||
d.unit as drop_unit,
|
||||
d.ppu as drop_ppu,
|
||||
b.username as buyer_username,
|
||||
b.email as buyer_email
|
||||
b.email as buyer_email,
|
||||
bd.fullname as buyer_fullname,
|
||||
bd.address as buyer_address,
|
||||
bd.phone as buyer_phone
|
||||
FROM sales s
|
||||
LEFT JOIN drops d ON s.drop_id = d.id
|
||||
LEFT JOIN buyers b ON s.buyer_id = b.id
|
||||
LEFT JOIN buyer_data bd ON s.buyer_data_id = bd.id
|
||||
ORDER BY s.created_at DESC`
|
||||
)
|
||||
return NextResponse.json(rows)
|
||||
|
||||
Reference in New Issue
Block a user