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:
58
app/api/v1/sms/send/route.ts
Normal file
58
app/api/v1/sms/send/route.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const apiKey = req.headers.get('x-api-key')
|
||||
if (!apiKey) {
|
||||
return NextResponse.json({ error: 'Missing X-API-Key header' }, { status: 401 })
|
||||
}
|
||||
|
||||
const client = await prisma.client.findUnique({ where: { apiKey } })
|
||||
if (!client || !client.isActive) {
|
||||
return NextResponse.json({ error: 'Invalid or inactive API key' }, { status: 403 })
|
||||
}
|
||||
|
||||
const body = await req.json()
|
||||
const { student_name, student_id, parent_phone, event, timestamp, message } = body
|
||||
|
||||
if (!student_name || !student_id || !parent_phone || !event || !timestamp) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing required fields: student_name, student_id, parent_phone, event, timestamp' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
if (!['time_in', 'time_out'].includes(event)) {
|
||||
return NextResponse.json({ error: 'event must be "time_in" or "time_out"' }, { status: 400 })
|
||||
}
|
||||
|
||||
const queueEntry = await prisma.smsQueue.create({
|
||||
data: {
|
||||
clientId: client.id,
|
||||
studentName: student_name,
|
||||
studentId: student_id,
|
||||
parentPhone: parent_phone,
|
||||
event,
|
||||
timestamp: new Date(timestamp),
|
||||
message: message || null,
|
||||
status: 'PENDING',
|
||||
},
|
||||
})
|
||||
|
||||
// Trigger queue processing in background (fire and forget)
|
||||
fetch(`${process.env.NEXTAUTH_URL}/api/queue/process`, {
|
||||
method: 'POST',
|
||||
headers: { 'x-internal-key': process.env.INTERNAL_API_KEY || '' },
|
||||
}).catch(() => {})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
queueId: queueEntry.id,
|
||||
message: 'SMS queued for delivery',
|
||||
}, { status: 202 })
|
||||
} catch (err: any) {
|
||||
console.error('[SMS Send]', err)
|
||||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user