feat: initial NFC Attendance Hub implementation

- Multi-tenant client management with unique API keys
- Semaphore SMS integration (per-client key + sender name + credit balance)
- DB-based SMS queue with 3-attempt retry and exponential backoff
- Failed SMS dashboard with manual retry button
- Reports page with date/client filter and CSV export
- Admin auth via NextAuth (email/password)
- Docker Compose setup (app + PostgreSQL 16)
- Prisma 5 schema with SmsQueue, SmsLog, Client, User models

Tech: Next.js 14 App Router + TypeScript + Tailwind CSS + Prisma + PostgreSQL
This commit is contained in:
Nemo
2026-03-12 08:49:13 +00:00
parent bb3f8d0ef2
commit b273f1a573
53 changed files with 6000 additions and 2 deletions

61
app/api/reports/route.ts Normal file
View File

@@ -0,0 +1,61 @@
import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
export async function GET(req: NextRequest) {
const session = await getServerSession(authOptions)
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
const { searchParams } = new URL(req.url)
const clientId = searchParams.get('clientId')
const from = searchParams.get('from')
const to = searchParams.get('to')
const format = searchParams.get('format')
const where: any = {}
if (clientId) where.clientId = clientId
if (from || to) {
where.sentAt = {}
if (from) where.sentAt.gte = new Date(from)
if (to) where.sentAt.lte = new Date(to + 'T23:59:59Z')
}
const logs = await prisma.smsLog.findMany({
where,
include: { client: { select: { name: true } }, queue: true },
orderBy: { sentAt: 'desc' },
take: format === 'csv' ? 10000 : 500,
})
if (format === 'csv') {
const headers = ['Date', 'Client', 'Student Name', 'Student ID', 'Phone', 'Event', 'Status', 'Error']
const rows = logs.map(l => [
new Date(l.sentAt).toISOString(),
l.client.name,
l.queue.studentName,
l.queue.studentId,
l.phone,
l.queue.event,
l.status,
l.errorReason || '',
])
const csv = [headers, ...rows].map(r => r.map(v => `"${String(v).replace(/"/g, '""')}"`).join(',')).join('\n')
return new NextResponse(csv, {
headers: {
'Content-Type': 'text/csv',
'Content-Disposition': `attachment; filename="sms-report-${Date.now()}.csv"`,
},
})
}
// Stats summary
const stats = {
total: logs.length,
success: logs.filter(l => l.status === 'SUCCESS').length,
failed: logs.filter(l => l.status === 'FAILED').length,
logs,
}
return NextResponse.json(stats)
}