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:
51
app/api/clients/[id]/route.ts
Normal file
51
app/api/clients/[id]/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { getBalance } from '@/lib/semaphore'
|
||||
|
||||
export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const client = await prisma.client.findUnique({
|
||||
where: { id: params.id },
|
||||
include: { _count: { select: { smsQueue: true, smsLogs: true } } },
|
||||
})
|
||||
if (!client) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
let balance: number | null = null
|
||||
if (client.semaphoreKey) {
|
||||
balance = await getBalance(client.semaphoreKey)
|
||||
}
|
||||
|
||||
return NextResponse.json({ ...client, balance })
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest, { params }: { params: { id: string } }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const body = await req.json()
|
||||
const { name, semaphoreKey, senderName, isActive } = body
|
||||
|
||||
const client = await prisma.client.update({
|
||||
where: { id: params.id },
|
||||
data: {
|
||||
...(name !== undefined && { name }),
|
||||
...(semaphoreKey !== undefined && { semaphoreKey }),
|
||||
...(senderName !== undefined && { senderName }),
|
||||
...(isActive !== undefined && { isActive }),
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json(client)
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest, { params }: { params: { id: string } }) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
await prisma.client.delete({ where: { id: params.id } })
|
||||
return NextResponse.json({ success: true })
|
||||
}
|
||||
37
app/api/clients/route.ts
Normal file
37
app/api/clients/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getServerSession } from 'next-auth'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { generateApiKey, slugify } from '@/lib/utils'
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const clients = await prisma.client.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
_count: { select: { smsQueue: true, smsLogs: true } },
|
||||
},
|
||||
})
|
||||
return NextResponse.json(clients)
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession(authOptions)
|
||||
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const body = await req.json()
|
||||
const { name, semaphoreKey, senderName } = body
|
||||
|
||||
if (!name) return NextResponse.json({ error: 'Name is required' }, { status: 400 })
|
||||
|
||||
const slug = slugify(name)
|
||||
const apiKey = generateApiKey()
|
||||
|
||||
const client = await prisma.client.create({
|
||||
data: { name, slug, apiKey, semaphoreKey, senderName: senderName || 'NFC-HUB' },
|
||||
})
|
||||
|
||||
return NextResponse.json(client, { status: 201 })
|
||||
}
|
||||
Reference in New Issue
Block a user